mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #23359 from kamipo/make_to_primary_key
Make to primary key instead of an unique index for internal tables
This commit is contained in:
commit
2f8ba24ec6
4 changed files with 5 additions and 40 deletions
|
@ -18,10 +18,6 @@ module ActiveRecord
|
|||
"#{table_name_prefix}#{ActiveRecord::Base.internal_metadata_table_name}#{table_name_suffix}"
|
||||
end
|
||||
|
||||
def index_name
|
||||
"#{table_name_prefix}unique_#{ActiveRecord::Base.internal_metadata_table_name}#{table_name_suffix}"
|
||||
end
|
||||
|
||||
def []=(key, value)
|
||||
first_or_initialize(key: key).update_attributes!(value: value)
|
||||
end
|
||||
|
@ -38,10 +34,8 @@ module ActiveRecord
|
|||
def create_table
|
||||
unless table_exists?
|
||||
connection.create_table(table_name, id: false) do |t|
|
||||
t.column :key, :string, null: false, limit: KEY_LIMIT
|
||||
t.column :value, :string
|
||||
t.index :key, unique: true, name: index_name
|
||||
|
||||
t.string :key, primary_key: true, limit: KEY_LIMIT
|
||||
t.string :value
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
|
|
|
@ -16,22 +16,17 @@ module ActiveRecord
|
|||
"#{table_name_prefix}#{ActiveRecord::Base.schema_migrations_table_name}#{table_name_suffix}"
|
||||
end
|
||||
|
||||
def index_name
|
||||
"#{table_name_prefix}unique_#{ActiveRecord::Base.schema_migrations_table_name}#{table_name_suffix}"
|
||||
end
|
||||
|
||||
def table_exists?
|
||||
ActiveSupport::Deprecation.silence { connection.table_exists?(table_name) }
|
||||
end
|
||||
|
||||
def create_table(limit=nil)
|
||||
unless table_exists?
|
||||
version_options = {null: false}
|
||||
version_options = { primary_key: true }
|
||||
version_options[:limit] = limit if limit
|
||||
|
||||
connection.create_table(table_name, id: false) do |t|
|
||||
t.column :version, :string, version_options
|
||||
t.index :version, unique: true, name: index_name
|
||||
t.string :version, version_options
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -21,7 +21,7 @@ if ActiveRecord::Base.connection.supports_migrations?
|
|||
ActiveRecord::Migration.verbose = @original_verbose
|
||||
end
|
||||
|
||||
def test_has_has_primary_key
|
||||
def test_has_primary_key
|
||||
old_primary_key_prefix_type = ActiveRecord::Base.primary_key_prefix_type
|
||||
ActiveRecord::Base.primary_key_prefix_type = :table_name_with_underscore
|
||||
assert_equal "version", ActiveRecord::SchemaMigration.primary_key
|
||||
|
|
|
@ -1,24 +0,0 @@
|
|||
require "cases/helper"
|
||||
|
||||
module ActiveRecord
|
||||
class Migration
|
||||
class TableAndIndexTest < ActiveRecord::TestCase
|
||||
def test_add_schema_info_respects_prefix_and_suffix
|
||||
conn = ActiveRecord::Base.connection
|
||||
|
||||
conn.drop_table(ActiveRecord::Migrator.schema_migrations_table_name, if_exists: true)
|
||||
# Use shorter prefix and suffix as in Oracle database identifier cannot be larger than 30 characters
|
||||
ActiveRecord::Base.table_name_prefix = 'p_'
|
||||
ActiveRecord::Base.table_name_suffix = '_s'
|
||||
conn.drop_table(ActiveRecord::Migrator.schema_migrations_table_name, if_exists: true)
|
||||
|
||||
conn.initialize_schema_migrations_table
|
||||
|
||||
assert_equal "p_unique_schema_migrations_s", conn.indexes(ActiveRecord::Migrator.schema_migrations_table_name)[0][:name]
|
||||
ensure
|
||||
ActiveRecord::Base.table_name_prefix = ""
|
||||
ActiveRecord::Base.table_name_suffix = ""
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue