diff options
Diffstat (limited to 'scripts/regenerate-schema')
-rwxr-xr-x | scripts/regenerate-schema | 62 |
1 files changed, 57 insertions, 5 deletions
diff --git a/scripts/regenerate-schema b/scripts/regenerate-schema index f1018403395..75ee0c33bea 100755 --- a/scripts/regenerate-schema +++ b/scripts/regenerate-schema @@ -2,6 +2,7 @@ # frozen_string_literal: true +require 'optparse' require 'open3' require 'fileutils' require 'uri' @@ -27,16 +28,37 @@ class SchemaRegenerator # directory when it runs. SCHEMA_MIGRATIONS_DIR = 'db/schema_migrations/' + def initialize(options) + @rollback_testing = options.delete(:rollback_testing) + end + def execute Dir.chdir(File.expand_path('..', __dir__)) do + # Note: `db:drop` must run prior to hiding migrations. + # + # Executing a Rails DB command e.g., `reset`, `drop`, etc. triggers running the initializers. + # During the initialization, the default values for `application_settings` need to be set. + # Depending on the presence of migrations, the default values are either faked or inserted. + # + # 1. If no migration is detected, all the necessary columns are in place from `db/structure.sql`. + # The default values can be inserted into `application_settings` table. + # + # 2. If a migration is detected, at least one column may be missing from `db/structure.sql` + # and needs to be added through the detected migration. In this case, the default values are faked. + # If not, an error would be raised e.g., "NoMethodError: undefined method `some_setting`" + # + # See https://gitlab.com/gitlab-org/gitlab/-/merge_requests/135085#note_1628210334 for more info. + # + drop_db checkout_ref checkout_clean_schema hide_migrations remove_schema_migration_files stop_spring - reset_db + setup_db unhide_migrations migrate + rollback if @rollback_testing ensure unhide_migrations end @@ -156,9 +178,15 @@ class SchemaRegenerator end ## - # Run rake task to reset the database. - def reset_db - run %q(bin/rails db:reset RAILS_ENV=test) + # Run rake task to drop the database. + def drop_db + run %q(bin/rails db:drop RAILS_ENV=test) + end + + ## + # Run rake task to setup the database. + def setup_db + run %q(bin/rails db:setup RAILS_ENV=test) end ## @@ -168,6 +196,15 @@ class SchemaRegenerator end ## + # Run rake task to rollback migrations. + def rollback + (untracked_schema_migrations + committed_schema_migrations).sort.reverse_each do |filename| + version = filename[/\d+\Z/] + run %(bin/rails db:rollback:main db:rollback:ci RAILS_ENV=test VERSION=#{version}) + end + end + + ## # Run the given +cmd+. # # The command is colored green, and the output of the command is @@ -224,4 +261,19 @@ class SchemaRegenerator end end -SchemaRegenerator.new.execute +if $PROGRAM_NAME == __FILE__ + options = {} + + OptionParser.new do |opts| + opts.on("-r", "--rollback-testing", String, "Enable rollback testing") do + options[:rollback_testing] = true + end + + opts.on("-h", "--help", "Prints this help") do + puts opts + exit + end + end.parse! + + SchemaRegenerator.new(options).execute +end |