Skip to content

Commit 6fb056e

Browse files
committed
Merge pull request rails#12578 from jeradphelps/configurable_schema_migrations_table_name
Configurable name for schema_migrations table Conflicts: activerecord/CHANGELOG.md
2 parents 745d8a8 + 26638f0 commit 6fb056e

File tree

5 files changed

+24
-3
lines changed

5 files changed

+24
-3
lines changed

activerecord/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Allow for the name of the schema_migrations table to be configured.
2+
3+
*Jerad Phelps*
4+
15
* Do not add to scope includes values from through associations.
26
Fixed bug when providing `includes` in through association scope, and fetching targets.
37

activerecord/lib/active_record/model_schema.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ module ModelSchema
3232
class_attribute :table_name_suffix, instance_writer: false
3333
self.table_name_suffix = ""
3434

35+
##
36+
# :singleton-method:
37+
# Accessor for the name of the schema migrations table. By default, the value is "schema_migrations"
38+
class_attribute :schema_migrations_table_name, instance_accessor: false
39+
self.schema_migrations_table_name = "schema_migrations"
40+
3541
##
3642
# :singleton-method:
3743
# Indicates whether table names should be the pluralized versions of the corresponding class names.

activerecord/lib/active_record/schema_migration.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ class SchemaMigration < ActiveRecord::Base
77
class << self
88

99
def table_name
10-
"#{table_name_prefix}schema_migrations#{table_name_suffix}"
10+
"#{table_name_prefix}#{ActiveRecord::Base.schema_migrations_table_name}#{table_name_suffix}"
1111
end
1212

1313
def index_name
14-
"#{table_name_prefix}unique_schema_migrations#{table_name_suffix}"
14+
"#{table_name_prefix}unique_#{ActiveRecord::Base.schema_migrations_table_name}#{table_name_suffix}"
1515
end
1616

1717
def table_exists?

activerecord/test/cases/migration_test.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,14 +311,23 @@ def migrate(x)
311311
end
312312

313313
def test_schema_migrations_table_name
314+
original_schema_migrations_table_name = ActiveRecord::Migrator.schema_migrations_table_name
315+
316+
assert_equal "schema_migrations", ActiveRecord::Migrator.schema_migrations_table_name
314317
ActiveRecord::Base.table_name_prefix = "prefix_"
315318
ActiveRecord::Base.table_name_suffix = "_suffix"
316319
Reminder.reset_table_name
317320
assert_equal "prefix_schema_migrations_suffix", ActiveRecord::Migrator.schema_migrations_table_name
321+
ActiveRecord::Base.schema_migrations_table_name = "changed"
322+
Reminder.reset_table_name
323+
assert_equal "prefix_changed_suffix", ActiveRecord::Migrator.schema_migrations_table_name
318324
ActiveRecord::Base.table_name_prefix = ""
319325
ActiveRecord::Base.table_name_suffix = ""
320326
Reminder.reset_table_name
321-
assert_equal "schema_migrations", ActiveRecord::Migrator.schema_migrations_table_name
327+
assert_equal "changed", ActiveRecord::Migrator.schema_migrations_table_name
328+
ensure
329+
ActiveRecord::Base.schema_migrations_table_name = original_schema_migrations_table_name
330+
Reminder.reset_table_name
322331
end
323332

324333
def test_proper_table_name_on_migrator

guides/source/configuring.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ config.middleware.delete "Rack::MethodOverride"
261261
262262
* `config.active_record.table_name_suffix` lets you set a global string to be appended to table names. If you set this to `_northwest`, then the Customer class will look for `customers_northwest` as its table. The default is an empty string.
263263
264+
* `config.active_record.schema_migrations_table_name` lets you set a string to be used as the name of the schema migrations table.
265+
264266
* `config.active_record.pluralize_table_names` specifies whether Rails will look for singular or plural table names in the database. If set to true (the default), then the Customer class will use the `customers` table. If set to false, then the Customer class will use the `customer` table.
265267
266268
* `config.active_record.default_timezone` determines whether to use `Time.local` (if set to `:local`) or `Time.utc` (if set to `:utc`) when pulling dates and times from the database. The default is `:utc` for Rails, although Active Record defaults to `:local` when used outside of Rails.

0 commit comments

Comments
 (0)