Commit ec785083 authored by Lin Jen-Shin's avatar Lin Jen-Shin

Make sure db:check-schema can catch corrupted structure.sql

This is done by migrating to the previous major version in
`db:check-schema` and then run migrations, it'll only be basing on
`db/structure.sql` from last major version rather than blindly using the
currently committed `db/structure.sql`.
parent f6ddd1b3
......@@ -345,10 +345,13 @@ db:migrate:reset:
db:check-schema:
extends:
- .db-job-base
- db:migrate-from-previous-major-version
- .rails:rules:ee-mr-and-default-branch-only
variables:
TAG_TO_CHECKOUT: "v14.4.0"
script:
- source scripts/schema_changed.sh
- run_timed_command "bundle exec rake db:migrate"
- scripts/schema_changed.sh
- scripts/validate_migration_timestamps
db:check-migrations:
......@@ -367,7 +370,8 @@ db:migrate-from-previous-major-version:
SETUP_DB: "false"
PROJECT_TO_CHECKOUT: "gitlab-foss"
TAG_TO_CHECKOUT: "v13.12.9"
script:
before_script:
- !reference [.default-before_script, before_script]
- '[[ -d "ee/" ]] || export PROJECT_TO_CHECKOUT="gitlab"'
- '[[ -d "ee/" ]] || export TAG_TO_CHECKOUT="${TAG_TO_CHECKOUT}-ee"'
- retry 'git fetch https://gitlab.com/gitlab-org/$PROJECT_TO_CHECKOUT.git $TAG_TO_CHECKOUT'
......@@ -376,6 +380,7 @@ db:migrate-from-previous-major-version:
- run_timed_command "bundle exec rake db:drop db:create db:structure:load db:migrate db:seed_fu"
- git checkout -f $CI_COMMIT_SHA
- SETUP_DB=false USE_BUNDLE_INSTALL=true bash scripts/prepare_build.sh
script:
- run_timed_command "bundle exec rake db:migrate"
db:rollback:
......
#!/bin/sh
schema_changed() {
if [ ! -z "$(git diff --name-only -- db/structure.sql)" ]; then
printf "Schema changes are not cleanly committed to db/structure.sql\n"
printf "The diff is as follows:\n"
diff=$(git diff -p --binary -- db/structure.sql)
printf "%s" "$diff"
exit 1
else
printf "Schema changes are correctly applied to db/structure.sql\n"
fi
if [ ! -z "$(git diff --name-only -- db/structure.sql)" ]; then
printf "Schema changes are not cleanly committed to db/structure.sql\n"
printf "The diff is as follows:\n"
diff=$(git diff -p --binary -- db/structure.sql)
printf "%s" "$diff"
exit 1
else
printf "Schema changes are correctly applied to db/structure.sql\n"
fi
if [ ! -z "$(git add -A -n db/schema_migrations)" ]; then
printf "Schema version files have not been committed to the repository:\n"
printf "The following files should be committed:\n"
diff=$(git add -A -n db/schema_migrations)
printf "%s" "$diff"
exit 2
else
printf "Schema changes are correctly applied to db/structure.sql and db/schema_migrations/\n"
fi
}
schema_changed
if [ ! -z "$(git add -A -n db/schema_migrations)" ]; then
printf "Schema version files have not been committed to the repository:\n"
printf "The following files should be committed:\n"
diff=$(git add -A -n db/schema_migrations)
printf "%s" "$diff"
exit 2
else
printf "Schema changes are correctly applied to db/structure.sql and db/schema_migrations/\n"
fi
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment