From f414acb2d58d42a16d1cabdb46733be47c84ac00 Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Sat, 25 Jan 2025 09:43:43 +0000 Subject: [PATCH] Test reversibility of migrations (#220) We want to make sure that: 1. Migrations are reversible 2. Reapplying migrations added to a PR leads to the same schema.rb Reviewed-on: https://gitea.bustikiller.com/bustikiller/wedding-planner/pulls/220 Co-authored-by: Manuel Bustillo Co-committed-by: Manuel Bustillo --- .gitea/workflows/build.yml | 46 ------- .gitea/workflows/copyright_notice.yml | 39 ------ .gitea/workflows/license_finder.yml | 21 --- .gitea/workflows/tests.yml | 123 +++++++++++++++++- app/views/layouts/mailer.html.erb | 4 +- app/views/layouts/mailer.text.erb | 4 +- .../mailer/confirmation_instructions.html.erb | 4 +- app/views/users/mailer/email_changed.html.erb | 4 +- .../users/mailer/password_change.html.erb | 4 +- .../reset_password_instructions.html.erb | 4 +- .../users/mailer/unlock_instructions.html.erb | 4 +- db/schema.rb | 4 +- 12 files changed, 134 insertions(+), 127 deletions(-) delete mode 100644 .gitea/workflows/build.yml delete mode 100644 .gitea/workflows/copyright_notice.yml delete mode 100644 .gitea/workflows/license_finder.yml diff --git a/.gitea/workflows/build.yml b/.gitea/workflows/build.yml deleted file mode 100644 index 5748059..0000000 --- a/.gitea/workflows/build.yml +++ /dev/null @@ -1,46 +0,0 @@ -name: Build Nginx-based docker image -on: - push: - branches: - - main - pull_request: -concurrency: - group: ${{ github.ref }} - cancel-in-progress: true -jobs: - build-static-assets: - runs-on: ubuntu-latest - timeout-minutes: 30 - steps: - - uses: actions/checkout@v4 - with: - token: ${{ secrets.GITHUB_TOKEN }} - - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v3 - - - name: Login to the private Docker registry - uses: docker/login-action@v3 - with: - registry: ${{ secrets.PRIVATE_REGISTRY_HOST }} - username: ${{ secrets.PRIVATE_REGISTRY_USERNAME }} - password: ${{ secrets.PRIVATE_REGISTRY_TOKEN }} - - - name: Build and push intermediate stages (build) - uses: docker/build-push-action@v6 - with: - context: . - target: build - push: ${{ github.ref == 'refs/heads/main' }} - tags: ${{ secrets.PRIVATE_REGISTRY_HOST }}/${{ env.GITHUB_REPOSITORY }}:build - cache-from: type=registry,ref=${{ secrets.PRIVATE_REGISTRY_HOST }}/${{ env.GITHUB_REPOSITORY }}:build - cache-to: type=inline - - - name: Build and push (final) - uses: docker/build-push-action@v6 - with: - context: . - push: ${{ github.ref == 'refs/heads/main' }} - tags: ${{ secrets.PRIVATE_REGISTRY_HOST }}/${{ env.GITHUB_REPOSITORY }}:latest - cache-from: type=registry,ref=${{ secrets.PRIVATE_REGISTRY_HOST }}/${{ env.GITHUB_REPOSITORY }}:latest - cache-to: type=inline \ No newline at end of file diff --git a/.gitea/workflows/copyright_notice.yml b/.gitea/workflows/copyright_notice.yml deleted file mode 100644 index c362a23..0000000 --- a/.gitea/workflows/copyright_notice.yml +++ /dev/null @@ -1,39 +0,0 @@ -name: Add copyright notice -on: - pull_request: -permissions: - contents: write -concurrency: - group: ${{ github.ref }} - cancel-in-progress: true -jobs: - copyright_notice: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - with: - token: ${{ secrets.ACTIONS_TOKEN }} - ref: ${{ github.head_ref }} - - uses: VinnyBabuManjaly/copyright-action@v1.0.0 - with: - CopyrightString: '# Copyright (C) 2024-2025 LibreWeddingPlanner contributors\n\n' - FileType: '.rb' - Path: 'app/, config/, db/, spec/' - - uses: VinnyBabuManjaly/copyright-action@v1.0.0 - with: - CopyrightString: '<%# Copyright (C) 22024-2025 LibreWeddingPlanner contributors %>\n\n' - FileType: '.erb' - Path: 'app/' - - name: Commit changes - run: | - git config --local user.email "bustikiller@bustikiller.com" - git config --local user.name "Manuel Bustillo" - git add . - - if [ -n "$(git status --porcelain)" ]; then - echo "there are changes"; - git commit -m "Add copyright notice" - git push - else - echo "no changes"; - fi diff --git a/.gitea/workflows/license_finder.yml b/.gitea/workflows/license_finder.yml deleted file mode 100644 index 6e21f6e..0000000 --- a/.gitea/workflows/license_finder.yml +++ /dev/null @@ -1,21 +0,0 @@ -name: Check usage of free licenses -on: - push: - branches: - - main - pull_request: -concurrency: - group: ${{ github.ref }} - cancel-in-progress: true -jobs: - check-licenses: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - with: - token: ${{ secrets.GITHUB_TOKEN }} - - uses: ruby/setup-ruby@v1.207.0 - - name: Install project dependencies - run: bundle install --jobs `getconf _NPROCESSORS_ONLN` - - name: Run license finder - run: license_finder diff --git a/.gitea/workflows/tests.yml b/.gitea/workflows/tests.yml index 886fce3..9c53589 100644 --- a/.gitea/workflows/tests.yml +++ b/.gitea/workflows/tests.yml @@ -10,7 +10,7 @@ concurrency: jobs: unit_tests: runs-on: ubuntu-latest - services: + services: &services postgres: image: postgres env: @@ -22,10 +22,11 @@ jobs: - uses: actions/checkout@v4 with: token: ${{ secrets.GITHUB_TOKEN }} + ref: ${{ github.head_ref }} # Checkout the actual branch, not the result if merged into the base - uses: ruby/setup-ruby@v1.207.0 - run: bundle install - - run: bundle exec rubocop --force-exclusion --parallel - - name: Wait until Postgres is ready to accept connections + - &postgres_wait + name: Wait until Postgres is ready to accept connections run: | apt-get update && apt-get install -f -y postgresql-client until pg_isready -h postgres -U postgres -d postgres @@ -33,12 +34,126 @@ jobs: sleep 1 echo "Trying again" done - - run: | + - name: Load schema and run unit tests + run: | bundle exec rake db:schema:load bundle exec rspec env: RAILS_ENV: test DATABASE_URL: postgres://postgres:postgres@postgres:5432/postgres + - name: Get all migrations added + id: changed-migration-files + uses: tj-actions/changed-files@v45 + with: + files: | + db/migrate/**.rb + - name: Redo all migrations and check there are no schema changes + if: steps.changed-migration-files.outputs.any_changed == 'true' + env: + ALL_CHANGED_FILES: ${{ steps.changed-migration-files.outputs.all_changed_files }} + RAILS_ENV: test + DATABASE_URL: postgres://postgres:postgres@postgres:5432/postgres + run: | + echo ${#ALL_CHANGED_FILES[@]} migrations changed: + for file in ${ALL_CHANGED_FILES}; do + echo "$file" + done + + bundle exec rake db:migrate:redo STEP=${#ALL_CHANGED_FILES[@]} + git diff --exit-code db/schema.rb - name: Clean up containers generated by this flow if: failure() run: docker ps --filter network=$JOB_CONTAINER_NAME-$GITHUB_JOB-network --filter name=$JOB_CONTAINER_NAME-* --format "{{.ID}}" | xargs docker rm -f + rubocop: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + token: ${{ secrets.GITHUB_TOKEN }} + - uses: ruby/setup-ruby@v1.207.0 + - run: bundle install + - run: bundle exec rubocop --force-exclusion --parallel + check-licenses: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + token: ${{ secrets.GITHUB_TOKEN }} + - uses: ruby/setup-ruby@v1.207.0 + - name: Install project dependencies + run: bundle install --jobs `getconf _NPROCESSORS_ONLN` + - name: Run license finder + run: license_finder + copyright_notice: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + token: ${{ secrets.ACTIONS_TOKEN }} + ref: ${{ github.head_ref }} + - uses: VinnyBabuManjaly/copyright-action@v1.0.0 + with: + CopyrightString: '# Copyright (C) 2024-2025 LibreWeddingPlanner contributors\n\n' + FileType: '.rb' + Path: 'app/, config/, db/, spec/' + IgnorePath: 'db' + - uses: VinnyBabuManjaly/copyright-action@v1.0.0 + with: + CopyrightString: '<%# Copyright (C) 2024-2025 LibreWeddingPlanner contributors %>\n\n' + FileType: '.erb' + Path: 'app/' + - name: Commit changes + run: | + git config --local user.email "bustikiller@bustikiller.com" + git config --local user.name "Manuel Bustillo" + git add . + + if [ -n "$(git status --porcelain)" ]; then + echo "there are changes"; + git commit -m "Add copyright notice" + git push + else + echo "no changes"; + fi + + build-static-assets: + runs-on: ubuntu-latest + timeout-minutes: 30 + needs: + - unit_tests + - rubocop + - check-licenses + - copyright_notice + steps: + - uses: actions/checkout@v4 + with: + token: ${{ secrets.GITHUB_TOKEN }} + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Login to the private Docker registry + uses: docker/login-action@v3 + with: + registry: ${{ secrets.PRIVATE_REGISTRY_HOST }} + username: ${{ secrets.PRIVATE_REGISTRY_USERNAME }} + password: ${{ secrets.PRIVATE_REGISTRY_TOKEN }} + + - name: Build and push intermediate stages (build) + uses: docker/build-push-action@v6 + with: + context: . + target: build + push: ${{ github.ref == 'refs/heads/main' }} + tags: ${{ secrets.PRIVATE_REGISTRY_HOST }}/${{ env.GITHUB_REPOSITORY }}:build + cache-from: type=registry,ref=${{ secrets.PRIVATE_REGISTRY_HOST }}/${{ env.GITHUB_REPOSITORY }}:build + cache-to: type=inline + + - name: Build and push (final) + uses: docker/build-push-action@v6 + with: + context: . + push: ${{ github.ref == 'refs/heads/main' }} + tags: ${{ secrets.PRIVATE_REGISTRY_HOST }}/${{ env.GITHUB_REPOSITORY }}:latest + cache-from: type=registry,ref=${{ secrets.PRIVATE_REGISTRY_HOST }}/${{ env.GITHUB_REPOSITORY }}:latest + cache-to: type=inline \ No newline at end of file diff --git a/app/views/layouts/mailer.html.erb b/app/views/layouts/mailer.html.erb index a8eafc7..32b0c16 100644 --- a/app/views/layouts/mailer.html.erb +++ b/app/views/layouts/mailer.html.erb @@ -1,6 +1,6 @@ -<%# Copyright (C) 22024-2025 LibreWeddingPlanner contributors %> +<%# Copyright (C) 2024-2025 LibreWeddingPlanner contributors %> -<%<%<% + diff --git a/app/views/layouts/mailer.text.erb b/app/views/layouts/mailer.text.erb index 0769757..9bdce1a 100644 --- a/app/views/layouts/mailer.text.erb +++ b/app/views/layouts/mailer.text.erb @@ -1,3 +1,3 @@ -<%# Copyright (C) 22024-2025 LibreWeddingPlanner contributors %> +<%# Copyright (C) 2024-2025 LibreWeddingPlanner contributors %> -<%<%<%<%= yield %> +<%= yield %> diff --git a/app/views/users/mailer/confirmation_instructions.html.erb b/app/views/users/mailer/confirmation_instructions.html.erb index f477218..cd3b2f1 100644 --- a/app/views/users/mailer/confirmation_instructions.html.erb +++ b/app/views/users/mailer/confirmation_instructions.html.erb @@ -1,6 +1,6 @@ -<%# Copyright (C) 22024-2025 LibreWeddingPlanner contributors %> +<%# Copyright (C) 2024-2025 LibreWeddingPlanner contributors %> -<%<%<%

Welcome <%= @email %>!

+

Welcome <%= @email %>!

You can confirm your account email through the link below:

diff --git a/app/views/users/mailer/email_changed.html.erb b/app/views/users/mailer/email_changed.html.erb index 0e68a0a..f5f2998 100644 --- a/app/views/users/mailer/email_changed.html.erb +++ b/app/views/users/mailer/email_changed.html.erb @@ -1,6 +1,6 @@ -<%# Copyright (C) 22024-2025 LibreWeddingPlanner contributors %> +<%# Copyright (C) 2024-2025 LibreWeddingPlanner contributors %> -<%<%<%

Hello <%= @email %>!

+

Hello <%= @email %>!

<% if @resource.try(:unconfirmed_email?) %>

We're contacting you to notify you that your email is being changed to <%= @resource.unconfirmed_email %>.

diff --git a/app/views/users/mailer/password_change.html.erb b/app/views/users/mailer/password_change.html.erb index bf4de87..1181727 100644 --- a/app/views/users/mailer/password_change.html.erb +++ b/app/views/users/mailer/password_change.html.erb @@ -1,5 +1,5 @@ -<%# Copyright (C) 22024-2025 LibreWeddingPlanner contributors %> +<%# Copyright (C) 2024-2025 LibreWeddingPlanner contributors %> -<%<%<%

Hello <%= @resource.email %>!

+

Hello <%= @resource.email %>!

We're contacting you to notify you that your password has been changed.

diff --git a/app/views/users/mailer/reset_password_instructions.html.erb b/app/views/users/mailer/reset_password_instructions.html.erb index 0499866..95bc588 100644 --- a/app/views/users/mailer/reset_password_instructions.html.erb +++ b/app/views/users/mailer/reset_password_instructions.html.erb @@ -1,6 +1,6 @@ -<%# Copyright (C) 22024-2025 LibreWeddingPlanner contributors %> +<%# Copyright (C) 2024-2025 LibreWeddingPlanner contributors %> -<%<%<%

Hello <%= @resource.email %>!

+

Hello <%= @resource.email %>!

Someone has requested a link to change your password. You can do this through the link below.

diff --git a/app/views/users/mailer/unlock_instructions.html.erb b/app/views/users/mailer/unlock_instructions.html.erb index b59cb33..3efb341 100644 --- a/app/views/users/mailer/unlock_instructions.html.erb +++ b/app/views/users/mailer/unlock_instructions.html.erb @@ -1,6 +1,6 @@ -<%# Copyright (C) 22024-2025 LibreWeddingPlanner contributors %> +<%# Copyright (C) 2024-2025 LibreWeddingPlanner contributors %> -<%<%<%

Hello <%= @resource.email %>!

+

Hello <%= @resource.email %>!

Your account has been locked due to an excessive number of unsuccessful sign in attempts.

diff --git a/db/schema.rb b/db/schema.rb index 11aa68c..10ab0cd 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1,5 +1,3 @@ -# Copyright (C) 2024-2025 LibreWeddingPlanner contributors - # This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. @@ -39,7 +37,7 @@ ActiveRecord::Schema[8.0].define(version: 2024_12_16_231415) do t.index "LEAST(group_a_id, group_b_id), GREATEST(group_a_id, group_b_id)", name: "uindex_group_pair", unique: true t.index ["group_a_id"], name: "index_group_affinities_on_group_a_id" t.index ["group_b_id"], name: "index_group_affinities_on_group_b_id" - t.check_constraint "discomfort >= 0::double precision AND discomfort <= 2::double precision" + t.check_constraint "discomfort >= 0::double precision AND discomfort <= 2::double precision", name: "check_valid_discomfort" t.check_constraint "group_a_id <> group_b_id", name: "check_distinct_groups" end