mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
SQLite: Add more test cases for adding primary key
This commit is contained in:
parent
9507b4f02d
commit
6783bcab7a
2 changed files with 105 additions and 63 deletions
|
@ -401,6 +401,58 @@ module ActiveRecord
|
|||
Barcode.reset_column_information
|
||||
end
|
||||
|
||||
def test_custom_primary_key_in_create_table
|
||||
connection = Barcode.connection
|
||||
connection.create_table :barcodes, id: false, force: true do |t|
|
||||
t.primary_key :id, :string
|
||||
end
|
||||
|
||||
assert_equal "id", connection.primary_key("barcodes")
|
||||
|
||||
custom_pk = Barcode.columns_hash["id"]
|
||||
|
||||
assert_equal :string, custom_pk.type
|
||||
assert_not custom_pk.null
|
||||
ensure
|
||||
Barcode.reset_column_information
|
||||
end
|
||||
|
||||
def test_custom_primary_key_in_change_table
|
||||
connection = Barcode.connection
|
||||
connection.create_table :barcodes, id: false, force: true do |t|
|
||||
t.integer :dummy
|
||||
end
|
||||
connection.change_table :barcodes do |t|
|
||||
t.primary_key :id, :string
|
||||
end
|
||||
|
||||
assert_equal "id", connection.primary_key("barcodes")
|
||||
|
||||
custom_pk = Barcode.columns_hash["id"]
|
||||
|
||||
assert_equal :string, custom_pk.type
|
||||
assert_not custom_pk.null
|
||||
ensure
|
||||
Barcode.reset_column_information
|
||||
end
|
||||
|
||||
def test_add_column_with_custom_primary_key
|
||||
connection = Barcode.connection
|
||||
connection.create_table :barcodes, id: false, force: true do |t|
|
||||
t.integer :dummy
|
||||
end
|
||||
connection.add_column :barcodes, :id, :string, primary_key: true
|
||||
|
||||
assert_equal "id", connection.primary_key("barcodes")
|
||||
|
||||
custom_pk = Barcode.columns_hash["id"]
|
||||
|
||||
assert_equal :string, custom_pk.type
|
||||
assert_not custom_pk.null
|
||||
ensure
|
||||
Barcode.reset_column_information
|
||||
end
|
||||
|
||||
def test_supports_extensions
|
||||
assert_not @conn.supports_extensions?, "does not support extensions"
|
||||
end
|
||||
|
|
|
@ -179,9 +179,7 @@ module LegacyPrimaryKeyTestCases
|
|||
|
||||
@migration.migrate(:up)
|
||||
|
||||
legacy_pk = LegacyPrimaryKey.columns_hash["id"]
|
||||
assert_not legacy_pk.bigint?
|
||||
assert_not legacy_pk.null
|
||||
assert_legacy_primary_key
|
||||
|
||||
legacy_ref = LegacyPrimaryKey.columns_hash["legacy_ref_id"]
|
||||
assert_not legacy_ref.bigint?
|
||||
|
@ -216,7 +214,6 @@ module LegacyPrimaryKeyTestCases
|
|||
assert_match %r{create_table "legacy_primary_keys", id: :integer, default: nil}, schema
|
||||
end
|
||||
|
||||
if current_adapter?(:Mysql2Adapter, :PostgreSQLAdapter)
|
||||
def test_legacy_primary_key_in_create_table_should_be_integer
|
||||
@migration = Class.new(migration_class) {
|
||||
def change
|
||||
|
@ -228,8 +225,7 @@ module LegacyPrimaryKeyTestCases
|
|||
|
||||
@migration.migrate(:up)
|
||||
|
||||
schema = dump_table_schema "legacy_primary_keys"
|
||||
assert_match %r{create_table "legacy_primary_keys", id: :(?:integer|serial), (?!default: nil)}, schema
|
||||
assert_legacy_primary_key
|
||||
end
|
||||
|
||||
def test_legacy_primary_key_in_change_table_should_be_integer
|
||||
|
@ -246,8 +242,7 @@ module LegacyPrimaryKeyTestCases
|
|||
|
||||
@migration.migrate(:up)
|
||||
|
||||
schema = dump_table_schema "legacy_primary_keys"
|
||||
assert_match %r{create_table "legacy_primary_keys", id: :(?:integer|serial), (?!default: nil)}, schema
|
||||
assert_legacy_primary_key
|
||||
end
|
||||
|
||||
def test_add_column_with_legacy_primary_key_should_be_integer
|
||||
|
@ -262,28 +257,7 @@ module LegacyPrimaryKeyTestCases
|
|||
|
||||
@migration.migrate(:up)
|
||||
|
||||
schema = dump_table_schema "legacy_primary_keys"
|
||||
assert_match %r{create_table "legacy_primary_keys", id: :(?:integer|serial), (?!default: nil)}, schema
|
||||
end
|
||||
end
|
||||
|
||||
if current_adapter?(:SQLite3Adapter)
|
||||
def test_add_column_with_legacy_primary_key_should_work
|
||||
@migration = Class.new(migration_class) {
|
||||
def change
|
||||
create_table :legacy_primary_keys, id: false do |t|
|
||||
t.integer :dummy
|
||||
end
|
||||
add_column :legacy_primary_keys, :id, :primary_key
|
||||
end
|
||||
}.new
|
||||
|
||||
@migration.migrate(:up)
|
||||
|
||||
assert_equal "id", LegacyPrimaryKey.primary_key
|
||||
legacy_pk = LegacyPrimaryKey.columns_hash["id"]
|
||||
assert_not legacy_pk.null
|
||||
end
|
||||
assert_legacy_primary_key
|
||||
end
|
||||
|
||||
def test_legacy_join_table_foreign_keys_should_be_integer
|
||||
|
@ -352,6 +326,22 @@ module LegacyPrimaryKeyTestCases
|
|||
assert_match %r{create_table "legacy_primary_keys", id: :bigint, default: nil}, schema
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def assert_legacy_primary_key
|
||||
assert_equal "id", LegacyPrimaryKey.primary_key
|
||||
|
||||
legacy_pk = LegacyPrimaryKey.columns_hash["id"]
|
||||
|
||||
assert_equal :integer, legacy_pk.type
|
||||
assert_not legacy_pk.bigint?
|
||||
assert_not legacy_pk.null
|
||||
|
||||
if current_adapter?(:Mysql2Adapter, :PostgreSQLAdapter)
|
||||
schema = dump_table_schema "legacy_primary_keys"
|
||||
assert_match %r{create_table "legacy_primary_keys", id: :(?:integer|serial), (?!default: nil)}, schema
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
module LegacyPrimaryKeyTest
|
||||
|
|
Loading…
Reference in a new issue