Skip to content

Commit

Permalink
Merge pull request #1 from Leafly-com/jackjennings-table-name-config
Browse files Browse the repository at this point in the history
Add configuration for table name
  • Loading branch information
jackjennings authored Mar 1, 2023
2 parents 86f10f2 + 73c3f50 commit df81ba8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ You can override this setting in `config/initializers/data_migrate.rb`

```ruby
DataMigrate.configure do |config|
config.data_migrations_table_name = 'my_migrations_database_name'
config.data_migrations_path = 'db/awesomepath/'
config.data_template_path = Rails.root.join("lib", "awesomepath", "custom_data_migration.rb")
config.db_configuration = {
Expand Down
3 changes: 2 additions & 1 deletion lib/data_migrate/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ def config
end

class Config
attr_accessor :data_migrations_path, :data_template_path, :db_configuration, :spec_name
attr_accessor :data_migrations_table_name, :data_migrations_path, :data_template_path, :db_configuration, :spec_name

DEFAULT_DATA_TEMPLATE_PATH = "data_migration.rb"

def initialize
@data_migrations_table_name = "data_migrations"
@data_migrations_path = "db/data/"
@data_template_path = DEFAULT_DATA_TEMPLATE_PATH
@db_configuration = nil
Expand Down
2 changes: 1 addition & 1 deletion lib/data_migrate/data_schema_migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class << self

def instance
@instance ||= Class.new(::ActiveRecord::SchemaMigration) do
define_singleton_method(:table_name) { ActiveRecord::Base.table_name_prefix + 'data_migrations' + ActiveRecord::Base.table_name_suffix }
define_singleton_method(:table_name) { ActiveRecord::Base.table_name_prefix + DataMigrate.config.data_migrations_table_name + ActiveRecord::Base.table_name_suffix }
define_singleton_method(:primary_key) { "version" }
end
end
Expand Down
21 changes: 21 additions & 0 deletions spec/data_migrate/data_schema_migration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,27 @@
it "returns correct table name" do
expect(subject.table_name).to eq("data_migrations")
end

describe "data migrations table name configured" do
let(:data_migrations_table_name) { "my_app_data_template_migrations"}

before do
@before = DataMigrate.config.data_migrations_table_name
DataMigrate.configure do |config|
config.data_migrations_table_name = data_migrations_table_name
end
end

after do
DataMigrate.configure do |config|
config.data_migrations_table_name = @before
end
end

it "returns correct table name" do
expect(subject.table_name).to eq(data_migrations_table_name)
end
end
end

describe :index_name do
Expand Down

0 comments on commit df81ba8

Please sign in to comment.