From bb965937987dc77059615364b8e30df7b048a6a3 Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Tue, 18 Feb 2025 01:12:15 -0800 Subject: [PATCH] Update database_migrator.rb (#182) Make database migrator run migrations in order --- lib/post_deployment/database_migrator.rb | 25 +++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/post_deployment/database_migrator.rb b/lib/post_deployment/database_migrator.rb index 356544d..91d90d4 100644 --- a/lib/post_deployment/database_migrator.rb +++ b/lib/post_deployment/database_migrator.rb @@ -18,7 +18,7 @@ module PostDeployment if @test_mode simulate_migrations else - perform_migrations + perform_migrations_in_order end end @@ -83,5 +83,28 @@ module PostDeployment end end end + + def perform_migrations_in_order + schema_context = ActiveRecord::Base.connection.migration_context + schema_migrations = schema_context.migrations + + data_migrations_path = DataMigrate.config.data_migrations_path + data_context = DataMigrate::MigrationContext.new(data_migrations_path) + data_migrations = data_context.migrations + + all_migrations = (schema_migrations + data_migrations).sort_by(&:version) + + all_migrations.each do |migration| + if migration.filename.start_with?(data_migrations_path) + say "Running data migration: #{migration.name}" + # Run the data migration (you might need to call `data_context.run(migration)` or similar) + data_context.run(migration) + else + say "Running schema migration: #{migration.name}" + # Run the schema migration (Rails will handle this for you) + schema_context.run(migration) + end + end + end end end