summaryrefslogtreecommitdiff
path: root/scripts/regenerate-schema
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/regenerate-schema')
-rwxr-xr-xscripts/regenerate-schema38
1 files changed, 38 insertions, 0 deletions
diff --git a/scripts/regenerate-schema b/scripts/regenerate-schema
index cedd612f766..e2d46d14218 100755
--- a/scripts/regenerate-schema
+++ b/scripts/regenerate-schema
@@ -3,6 +3,7 @@
# frozen_string_literal: true
require 'open3'
+require 'fileutils'
require 'uri'
class SchemaRegenerator
@@ -19,11 +20,19 @@ class SchemaRegenerator
# these to disable/enable migrations.
MIGRATION_DIRS = %w[db/migrate db/post_migrate].freeze
+ ##
+ # Directory where we store schema versions
+ #
+ # The remove_schema_migration_files removes files added in this
+ # directory when it runs.
+ SCHEMA_MIGRATIONS_DIR = 'db/schema_migrations/'
+
def execute
Dir.chdir(File.expand_path('..', __dir__)) do
checkout_ref
checkout_clean_schema
hide_migrations
+ remove_schema_migration_files
reset_db
unhide_migrations
migrate
@@ -111,6 +120,35 @@ class SchemaRegenerator
end
##
+ # Remove files added to db/schema_migrations
+ #
+ # In order to properly reset the database and re-run migrations
+ # the schema migrations for new migrations must be removed.
+ def remove_schema_migration_files
+ (untracked_schema_migrations + commited_schema_migrations).each do |schema_migration|
+ FileUtils.rm(schema_migration)
+ end
+ end
+
+ ##
+ # List of untracked schema migrations
+ #
+ # Get a list of schema migrations that are not tracked so we can remove them
+ def untracked_schema_migrations
+ git_command = "git ls-files --others --exclude-standard -- #{SCHEMA_MIGRATIONS_DIR}"
+ run(git_command).chomp.split("\n")
+ end
+
+ ##
+ # List of untracked schema migrations
+ #
+ # Get a list of schema migrations that have been committed since the last
+ def commited_schema_migrations
+ git_command = "git diff --name-only --diff-filter=A #{merge_base} -- #{SCHEMA_MIGRATIONS_DIR}"
+ run(git_command).chomp.split("\n")
+ end
+
+ ##
# Run rake task to reset the database.
def reset_db
run %q[bin/rails db:reset RAILS_ENV=test]