Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow multiple data_migrations_path values #331

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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