diff options
author | Jeremy Daer <jeremydaer@gmail.com> | 2017-02-06 22:16:19 -0700 |
---|---|---|
committer | Jeremy Daer <jeremydaer@gmail.com> | 2017-02-06 22:16:19 -0700 |
commit | 968e66b5df92e5ae88c033dc552e17b4c736874b (patch) | |
tree | d3ff117eef279026e544fc12bc81aaf25fb4c411 /activerecord/test/cases/migration/compatibility_test.rb | |
parent | e0e20d62a75d2c05c7f68e0f316effd5c2f5c26f (diff) | |
parent | 29c70abc58357e7b0f6b05e9ca89ba7a95617ed5 (diff) | |
download | rails-968e66b5df92e5ae88c033dc552e17b4c736874b.tar.gz rails-968e66b5df92e5ae88c033dc552e17b4c736874b.tar.bz2 rails-968e66b5df92e5ae88c033dc552e17b4c736874b.zip |
Merge pull request #27389 from kamipo/fix_mysql_pk_dumping_correctly
Restore the behaviour of the compatibility layer for integer-like PKs
* kamipo/fix_mysql_pk_dumping_correctly:
Restore custom primary key tests lost at #26266
Restore the behaviour of the compatibility layer for integer-like PKs
Correctly dump integer-like primary key with default nil
Diffstat (limited to 'activerecord/test/cases/migration/compatibility_test.rb')
-rw-r--r-- | activerecord/test/cases/migration/compatibility_test.rb | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/activerecord/test/cases/migration/compatibility_test.rb b/activerecord/test/cases/migration/compatibility_test.rb index 9296f3da90..fc3ec47825 100644 --- a/activerecord/test/cases/migration/compatibility_test.rb +++ b/activerecord/test/cases/migration/compatibility_test.rb @@ -1,4 +1,5 @@ require "cases/helper" +require "support/schema_dumping_helper" module ActiveRecord class Migration @@ -111,3 +112,106 @@ module ActiveRecord end end end + +class LegacyPrimaryKeyTest < ActiveRecord::TestCase + include SchemaDumpingHelper + + self.use_transactional_tests = false + + class LegacyPrimaryKey < ActiveRecord::Base + end + + def setup + @migration = nil + @verbose_was = ActiveRecord::Migration.verbose + ActiveRecord::Migration.verbose = false + end + + def teardown + @migration.migrate(:down) if @migration + ActiveRecord::Migration.verbose = @verbose_was + ActiveRecord::SchemaMigration.delete_all rescue nil + LegacyPrimaryKey.reset_column_information + end + + def test_legacy_primary_key_should_be_auto_incremented + @migration = Class.new(ActiveRecord::Migration[5.0]) { + def change + create_table :legacy_primary_keys do |t| + end + end + }.new + + @migration.migrate(:up) + + legacy_pk = LegacyPrimaryKey.columns_hash["id"] + assert_not legacy_pk.bigint? + assert_not legacy_pk.null + + record1 = LegacyPrimaryKey.create! + assert_not_nil record1.id + + record1.destroy + + record2 = LegacyPrimaryKey.create! + assert_not_nil record2.id + assert_operator record2.id, :>, record1.id + end + + def test_legacy_integer_primary_key_should_not_be_auto_incremented + skip if current_adapter?(:SQLite3Adapter) + + @migration = Class.new(ActiveRecord::Migration[5.0]) { + def change + create_table :legacy_primary_keys, id: :integer do |t| + end + end + }.new + + @migration.migrate(:up) + + assert_raises(ActiveRecord::NotNullViolation) do + LegacyPrimaryKey.create! + end + + schema = dump_table_schema "legacy_primary_keys" + assert_match %r{create_table "legacy_primary_keys", id: :integer, default: nil}, schema + end + + if current_adapter?(:Mysql2Adapter) + def test_legacy_bigint_primary_key_should_be_auto_incremented + @migration = Class.new(ActiveRecord::Migration[5.0]) { + def change + create_table :legacy_primary_keys, id: :bigint + end + }.new + + @migration.migrate(:up) + + legacy_pk = LegacyPrimaryKey.columns_hash["id"] + assert legacy_pk.bigint? + assert legacy_pk.auto_increment? + + schema = dump_table_schema "legacy_primary_keys" + assert_match %r{create_table "legacy_primary_keys", (?!id: :bigint, default: nil)}, schema + end + else + def test_legacy_bigint_primary_key_should_not_be_auto_incremented + @migration = Class.new(ActiveRecord::Migration[5.0]) { + def change + create_table :legacy_primary_keys, id: :bigint do |t| + end + end + }.new + + @migration.migrate(:up) + + assert_raises(ActiveRecord::NotNullViolation) do + LegacyPrimaryKey.create! + end + + schema = dump_table_schema "legacy_primary_keys" + assert_match %r{create_table "legacy_primary_keys", id: :bigint, default: nil}, schema + end + end +end |