Skip to content

Commit

Permalink
feat: Allow multiple data_migrations_path values
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanbyron authored and vprigent committed Sep 17, 2024
1 parent a46eb86 commit 2d29c02
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 16 deletions.
2 changes: 1 addition & 1 deletion lib/data_migrate/data_migrator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions lib/data_migrate/data_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 8 additions & 6 deletions lib/data_migrate/tasks/data_migrate_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion lib/generators/data_migration/data_migration_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion spec/data_migrate/data_migrator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 9 additions & 0 deletions spec/db/data_two/20241231235959_data_two_update.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class DataTwoUpdate < ActiveRecord::Migration[6.1]
def up
puts "Doing DataTwoUpdate"
end

def down
puts "Undoing DataTwoUpdate"
end
end
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2d29c02

Please sign in to comment.