提交 2f8ba24e 编写于 作者: R Rafael França

Merge pull request #23359 from kamipo/make_to_primary_key

Make to primary key instead of an unique index for internal tables
......@@ -18,10 +18,6 @@ def table_name
"#{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 @@ def table_exists?
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 @@ def table_name
"#{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 @@ class ActiveRecordSchemaTest < ActiveRecord::TestCase
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
......
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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册