From 2d29c02d943f4ad01851a8e1cf6e3533aa0dcdde Mon Sep 17 00:00:00 2001 From: Jordan Byron Date: Thu, 29 Aug 2024 16:23:00 -0400 Subject: [PATCH] feat: Allow multiple data_migrations_path values --- lib/data_migrate/data_migrator.rb | 2 +- lib/data_migrate/data_schema.rb | 12 ++++++------ lib/data_migrate/tasks/data_migrate_tasks.rb | 14 ++++++++------ .../data_migration/data_migration_generator.rb | 3 ++- spec/data_migrate/data_migrator_spec.rb | 3 ++- spec/db/data_two/20241231235959_data_two_update.rb | 9 +++++++++ spec/spec_helper.rb | 2 +- 7 files changed, 29 insertions(+), 16 deletions(-) create mode 100644 spec/db/data_two/20241231235959_data_two_update.rb diff --git a/lib/data_migrate/data_migrator.rb b/lib/data_migrate/data_migrator.rb index 26a08dca..9c173891 100644 --- a/lib/data_migrate/data_migrator.rb +++ b/lib/data_migrate/data_migrator.rb @@ -12,7 +12,7 @@ def load_migrated class << self def migrations_paths - [DataMigrate.config.data_migrations_path] + Array.wrap(DataMigrate.config.data_migrations_path) end def create_data_schema_table diff --git a/lib/data_migrate/data_schema.rb b/lib/data_migrate/data_schema.rb index a77bd56a..1c7297fc 100644 --- a/lib/data_migrate/data_schema.rb +++ b/lib/data_migrate/data_schema.rb @@ -29,13 +29,13 @@ def migrated end def versions - @versions ||= begin - versions = [] - Dir.foreach(DataMigrate::DataMigrator.full_migrations_path) do |file| - match_data = DataMigrate::DataMigrator.match(file) - versions << match_data[1].to_i if match_data + @versions ||= Set.new.tap do |versions| + DataMigrate::DataMigrator.migrations_paths.each do |path| + Dir.foreach(path) do |file| + match_data = DataMigrate::DataMigrator.match(file) + versions << match_data[1].to_i if match_data + end end - versions end end diff --git a/lib/data_migrate/tasks/data_migrate_tasks.rb b/lib/data_migrate/tasks/data_migrate_tasks.rb index 9143610e..6e35acaf 100644 --- a/lib/data_migrate/tasks/data_migrate_tasks.rb +++ b/lib/data_migrate/tasks/data_migrate_tasks.rb @@ -6,7 +6,7 @@ module DataMigrateTasks extend self def migrations_paths - @migrations_paths ||= DataMigrate.config.data_migrations_path + @migrations_paths ||= Array.wrap(DataMigrate.config.data_migrations_path) end def dump @@ -55,11 +55,13 @@ def status_with_schema db_list_schema = DataMigrate::RailsHelper.schema_migration_versions file_list = [] - Dir.foreach(File.join(Rails.root, migrations_paths)) do |file| - # only files matching "20091231235959_some_name.rb" pattern - if match_data = /(\d{14})_(.+)\.rb/.match(file) - status = db_list_data.delete(match_data[1]) ? 'up' : 'down' - file_list << [status, match_data[1], match_data[2], 'data'] + migrations_paths.each do |path| + Dir.foreach(File.join(Rails.root, path)) do |file| + # only files matching "20091231235959_some_name.rb" pattern + if match_data = /(\d{14})_(.+)\.rb/.match(file) + status = db_list_data.delete(match_data[1]) ? 'up' : 'down' + file_list << [status, match_data[1], match_data[2], 'data'] + end end end diff --git a/lib/generators/data_migration/data_migration_generator.rb b/lib/generators/data_migration/data_migration_generator.rb index b488ec83..1bd23a17 100644 --- a/lib/generators/data_migration/data_migration_generator.rb +++ b/lib/generators/data_migration/data_migration_generator.rb @@ -38,8 +38,9 @@ def data_migrations_file_path File.join(data_migrations_path, "#{file_name}.rb") end + # Use the first path in the data_migrations_path as the target directory def data_migrations_path - DataMigrate.config.data_migrations_path + Array.wrap(DataMigrate.config.data_migrations_path).first end end end diff --git a/spec/data_migrate/data_migrator_spec.rb b/spec/data_migrate/data_migrator_spec.rb index 649e8d6f..6fa7b61e 100644 --- a/spec/data_migrate/data_migrator_spec.rb +++ b/spec/data_migrate/data_migrator_spec.rb @@ -51,9 +51,10 @@ describe "#migrations_status" do it "returns all migrations statuses" do status = described_class.migrations_status - expect(status.length).to eq 2 + expect(status.length).to eq 3 expect(status.first).to eq ["down", "20091231235959", "Some name"] expect(status.second).to eq ["down", "20171231235959", "Super update"] + expect(status.third).to eq ["down", "20241231235959", "Data two update"] end end diff --git a/spec/db/data_two/20241231235959_data_two_update.rb b/spec/db/data_two/20241231235959_data_two_update.rb new file mode 100644 index 00000000..d29dd105 --- /dev/null +++ b/spec/db/data_two/20241231235959_data_two_update.rb @@ -0,0 +1,9 @@ +class DataTwoUpdate < ActiveRecord::Migration[6.1] + def up + puts "Doing DataTwoUpdate" + end + + def down + puts "Undoing DataTwoUpdate" + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index f9da55e4..ebf239c0 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -21,7 +21,7 @@ else @prev_data_migrations_path = DataMigrate.config.data_migrations_path DataMigrate.configure do |config| - config.data_migrations_path = "spec/db/data" + config.data_migrations_path = ["spec/db/data", "spec/db/data_two"] end end end