1
0
Fork 0
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:
Rafael França 2016-02-01 01:53:57 -02:00
commit 2f8ba24ec6
4 changed files with 5 additions and 40 deletions

View file

@ -18,10 +18,6 @@ module ActiveRecord
"#{table_name_prefix}#{ActiveRecord::Base.internal_metadata_table_name}#{table_name_suffix}" "#{table_name_prefix}#{ActiveRecord::Base.internal_metadata_table_name}#{table_name_suffix}"
end end
def index_name
"#{table_name_prefix}unique_#{ActiveRecord::Base.internal_metadata_table_name}#{table_name_suffix}"
end
def []=(key, value) def []=(key, value)
first_or_initialize(key: key).update_attributes!(value: value) first_or_initialize(key: key).update_attributes!(value: value)
end end
@ -38,10 +34,8 @@ module ActiveRecord
def create_table def create_table
unless table_exists? unless table_exists?
connection.create_table(table_name, id: false) do |t| connection.create_table(table_name, id: false) do |t|
t.column :key, :string, null: false, limit: KEY_LIMIT t.string :key, primary_key: true, limit: KEY_LIMIT
t.column :value, :string t.string :value
t.index :key, unique: true, name: index_name
t.timestamps t.timestamps
end end
end end

View file

@ -16,22 +16,17 @@ module ActiveRecord
"#{table_name_prefix}#{ActiveRecord::Base.schema_migrations_table_name}#{table_name_suffix}" "#{table_name_prefix}#{ActiveRecord::Base.schema_migrations_table_name}#{table_name_suffix}"
end end
def index_name
"#{table_name_prefix}unique_#{ActiveRecord::Base.schema_migrations_table_name}#{table_name_suffix}"
end
def table_exists? def table_exists?
ActiveSupport::Deprecation.silence { connection.table_exists?(table_name) } ActiveSupport::Deprecation.silence { connection.table_exists?(table_name) }
end end
def create_table(limit=nil) def create_table(limit=nil)
unless table_exists? unless table_exists?
version_options = {null: false} version_options = { primary_key: true }
version_options[:limit] = limit if limit version_options[:limit] = limit if limit
connection.create_table(table_name, id: false) do |t| connection.create_table(table_name, id: false) do |t|
t.column :version, :string, version_options t.string :version, version_options
t.index :version, unique: true, name: index_name
end end
end end
end end

View file

@ -21,7 +21,7 @@ if ActiveRecord::Base.connection.supports_migrations?
ActiveRecord::Migration.verbose = @original_verbose ActiveRecord::Migration.verbose = @original_verbose
end end
def test_has_has_primary_key def test_has_primary_key
old_primary_key_prefix_type = ActiveRecord::Base.primary_key_prefix_type old_primary_key_prefix_type = ActiveRecord::Base.primary_key_prefix_type
ActiveRecord::Base.primary_key_prefix_type = :table_name_with_underscore ActiveRecord::Base.primary_key_prefix_type = :table_name_with_underscore
assert_equal "version", ActiveRecord::SchemaMigration.primary_key assert_equal "version", ActiveRecord::SchemaMigration.primary_key

View file

@ -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