diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2017-12-26 18:22:18 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2017-12-26 18:22:18 +0900 |
commit | 6783bcab7ab11f2ced4b711d3518422e35e3dc01 (patch) | |
tree | 9fb99caae21443d2150db03901038088ae3a168d /activerecord | |
parent | 9507b4f02d332cccf76ca803b967dab69ab6bfdd (diff) | |
download | rails-6783bcab7ab11f2ced4b711d3518422e35e3dc01.tar.gz rails-6783bcab7ab11f2ced4b711d3518422e35e3dc01.tar.bz2 rails-6783bcab7ab11f2ced4b711d3518422e35e3dc01.zip |
SQLite: Add more test cases for adding primary key
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb | 52 | ||||
-rw-r--r-- | activerecord/test/cases/migration/compatibility_test.rb | 106 |
2 files changed, 100 insertions, 58 deletions
diff --git a/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb b/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb index c67c2d6ede..6f72df4412 100644 --- a/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb +++ b/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb @@ -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 diff --git a/activerecord/test/cases/migration/compatibility_test.rb b/activerecord/test/cases/migration/compatibility_test.rb index eb6d65d1b1..26d3b3e29d 100644 --- a/activerecord/test/cases/migration/compatibility_test.rb +++ b/activerecord/test/cases/migration/compatibility_test.rb @@ -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,74 +214,50 @@ 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 - create_table :legacy_primary_keys, id: false do |t| - t.primary_key :id - end + def test_legacy_primary_key_in_create_table_should_be_integer + @migration = Class.new(migration_class) { + def change + create_table :legacy_primary_keys, id: false do |t| + t.primary_key :id end - }.new + end + }.new - @migration.migrate(:up) + @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 + assert_legacy_primary_key + end - def test_legacy_primary_key_in_change_table_should_be_integer - @migration = Class.new(migration_class) { - def change - create_table :legacy_primary_keys, id: false do |t| - t.integer :dummy - end - change_table :legacy_primary_keys do |t| - t.primary_key :id - end + def test_legacy_primary_key_in_change_table_should_be_integer + @migration = Class.new(migration_class) { + def change + create_table :legacy_primary_keys, id: false do |t| + t.integer :dummy end - }.new - - @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 - - def test_add_column_with_legacy_primary_key_should_be_integer - @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 + change_table :legacy_primary_keys do |t| + t.primary_key :id end - }.new + end + }.new - @migration.migrate(:up) + @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 + assert_legacy_primary_key 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 + def test_add_column_with_legacy_primary_key_should_be_integer + @migration = Class.new(migration_class) { + def change + create_table :legacy_primary_keys, id: false do |t| + t.integer :dummy end - }.new + add_column :legacy_primary_keys, :id, :primary_key + end + }.new - @migration.migrate(:up) + @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 |