@@ -856,198 +856,201 @@ def migration_class
856
856
end
857
857
858
858
module LegacyPrimaryKeyTestCases
859
+ extend ActiveSupport ::Concern
859
860
include SchemaDumpingHelper
860
861
861
862
class LegacyPrimaryKey < ActiveRecord ::Base
862
863
end
863
864
864
- def setup
865
- @migration = nil
866
- @verbose_was = ActiveRecord ::Migration . verbose
867
- ActiveRecord ::Migration . verbose = false
868
- end
865
+ included do
866
+ def setup
867
+ @migration = nil
868
+ @verbose_was = ActiveRecord ::Migration . verbose
869
+ ActiveRecord ::Migration . verbose = false
870
+ end
869
871
870
- def teardown
871
- @migration . migrate ( :down ) if @migration
872
- ActiveRecord ::Migration . verbose = @verbose_was
873
- ActiveRecord ::Base . connection . schema_migration . delete_all_versions rescue nil
874
- LegacyPrimaryKey . reset_column_information
875
- end
872
+ def teardown
873
+ @migration . migrate ( :down ) if @migration
874
+ ActiveRecord ::Migration . verbose = @verbose_was
875
+ ActiveRecord ::Base . connection . schema_migration . delete_all_versions rescue nil
876
+ LegacyPrimaryKey . reset_column_information
877
+ end
876
878
877
- def test_legacy_primary_key_should_be_auto_incremented
878
- @migration = Class . new ( migration_class ) {
879
- def change
880
- create_table :legacy_primary_keys do |t |
881
- t . references :legacy_ref
879
+ def test_legacy_primary_key_should_be_auto_incremented
880
+ @migration = Class . new ( migration_class ) {
881
+ def change
882
+ create_table :legacy_primary_keys do |t |
883
+ t . references :legacy_ref
884
+ end
882
885
end
883
- end
884
- } . new
885
-
886
- @migration . migrate ( :up )
887
-
888
- assert_legacy_primary_key
886
+ } . new
889
887
890
- legacy_ref = LegacyPrimaryKey . columns_hash [ "legacy_ref_id" ]
891
- assert_not_predicate legacy_ref , :bigint?
888
+ @migration . migrate ( :up )
892
889
893
- record1 = LegacyPrimaryKey . create!
894
- assert_not_nil record1 . id
890
+ assert_legacy_primary_key
895
891
896
- record1 . destroy
892
+ legacy_ref = LegacyPrimaryKey . columns_hash [ "legacy_ref_id" ]
893
+ assert_not_predicate legacy_ref , :bigint?
897
894
898
- record2 = LegacyPrimaryKey . create!
899
- assert_not_nil record2 . id
900
- assert_operator record2 . id , :> , record1 . id
901
- end
895
+ record1 = LegacyPrimaryKey . create!
896
+ assert_not_nil record1 . id
902
897
903
- def test_legacy_integer_primary_key_should_not_be_auto_incremented
904
- skip if current_adapter? ( :SQLite3Adapter )
898
+ record1 . destroy
905
899
906
- @migration = Class . new ( migration_class ) {
907
- def change
908
- create_table :legacy_primary_keys , id : :integer do |t |
909
- end
910
- end
911
- } . new
912
-
913
- @migration . migrate ( :up )
914
-
915
- assert_raises ( ActiveRecord ::NotNullViolation ) do
916
- LegacyPrimaryKey . create!
900
+ record2 = LegacyPrimaryKey . create!
901
+ assert_not_nil record2 . id
902
+ assert_operator record2 . id , :> , record1 . id
917
903
end
918
904
919
- schema = dump_table_schema "legacy_primary_keys"
920
- assert_match %r{create_table "legacy_primary_keys", id: :integer, default: nil} , schema
921
- end
905
+ def test_legacy_integer_primary_key_should_not_be_auto_incremented
906
+ skip if current_adapter? ( :SQLite3Adapter )
922
907
923
- def test_legacy_primary_key_in_create_table_should_be_integer
924
- @migration = Class . new ( migration_class ) {
925
- def change
926
- create_table :legacy_primary_keys , id : false do |t |
927
- t . primary_key :id
908
+ @migration = Class . new ( migration_class ) {
909
+ def change
910
+ create_table :legacy_primary_keys , id : :integer do |t |
911
+ end
928
912
end
929
- end
930
- } . new
931
-
932
- @migration . migrate ( :up )
913
+ } . new
933
914
934
- assert_legacy_primary_key
935
- end
915
+ @migration . migrate ( :up )
936
916
937
- def test_legacy_primary_key_in_change_table_should_be_integer
938
- @migration = Class . new ( migration_class ) {
939
- def change
940
- create_table :legacy_primary_keys , id : false do |t |
941
- t . integer :dummy
942
- end
943
- change_table :legacy_primary_keys do |t |
944
- t . primary_key :id
945
- end
917
+ assert_raises ( ActiveRecord ::NotNullViolation ) do
918
+ LegacyPrimaryKey . create!
946
919
end
947
- } . new
948
920
949
- @migration . migrate ( :up )
950
-
951
- assert_legacy_primary_key
952
- end
921
+ schema = dump_table_schema "legacy_primary_keys"
922
+ assert_match %r{create_table "legacy_primary_keys", id: :integer, default: nil} , schema
923
+ end
953
924
954
- def test_add_column_with_legacy_primary_key_should_be_integer
955
- @migration = Class . new ( migration_class ) {
956
- def change
957
- create_table :legacy_primary_keys , id : false do |t |
958
- t . integer :dummy
925
+ def test_legacy_primary_key_in_create_table_should_be_integer
926
+ @migration = Class . new ( migration_class ) {
927
+ def change
928
+ create_table :legacy_primary_keys , id : false do |t |
929
+ t . primary_key :id
930
+ end
959
931
end
960
- add_column :legacy_primary_keys , :id , :primary_key
961
- end
962
- } . new
932
+ } . new
963
933
964
- @migration . migrate ( :up )
934
+ @migration . migrate ( :up )
965
935
966
- assert_legacy_primary_key
967
- end
936
+ assert_legacy_primary_key
937
+ end
968
938
969
- def test_legacy_join_table_foreign_keys_should_be_integer
970
- @migration = Class . new ( migration_class ) {
971
- def change
972
- create_join_table :apples , :bananas do |t |
939
+ def test_legacy_primary_key_in_change_table_should_be_integer
940
+ @migration = Class . new ( migration_class ) {
941
+ def change
942
+ create_table :legacy_primary_keys , id : false do |t |
943
+ t . integer :dummy
944
+ end
945
+ change_table :legacy_primary_keys do |t |
946
+ t . primary_key :id
947
+ end
973
948
end
974
- end
975
- } . new
949
+ } . new
976
950
977
- @migration . migrate ( :up )
951
+ @migration . migrate ( :up )
978
952
979
- schema = dump_table_schema "apples_bananas"
980
- assert_match %r{integer "apple_id", null: false} , schema
981
- assert_match %r{integer "banana_id", null: false} , schema
982
- end
953
+ assert_legacy_primary_key
954
+ end
983
955
984
- def test_legacy_join_table_column_options_should_be_overwritten
985
- @migration = Class . new ( migration_class ) {
986
- def change
987
- create_join_table :apples , :bananas , column_options : { type : :bigint } do |t |
956
+ def test_add_column_with_legacy_primary_key_should_be_integer
957
+ @migration = Class . new ( migration_class ) {
958
+ def change
959
+ create_table :legacy_primary_keys , id : false do |t |
960
+ t . integer :dummy
961
+ end
962
+ add_column :legacy_primary_keys , :id , :primary_key
988
963
end
989
- end
990
- } . new
964
+ } . new
991
965
992
- @migration . migrate ( :up )
966
+ @migration . migrate ( :up )
993
967
994
- schema = dump_table_schema "apples_bananas"
995
- assert_match %r{bigint "apple_id", null: false} , schema
996
- assert_match %r{bigint "banana_id", null: false} , schema
997
- end
968
+ assert_legacy_primary_key
969
+ end
998
970
999
- if current_adapter? ( :Mysql2Adapter )
1000
- def test_legacy_bigint_primary_key_should_be_auto_incremented
971
+ def test_legacy_join_table_foreign_keys_should_be_integer
1001
972
@migration = Class . new ( migration_class ) {
1002
973
def change
1003
- create_table :legacy_primary_keys , id : :bigint
974
+ create_join_table :apples , :bananas do |t |
975
+ end
1004
976
end
1005
977
} . new
1006
978
1007
979
@migration . migrate ( :up )
1008
980
1009
- legacy_pk = LegacyPrimaryKey . columns_hash [ "id" ]
1010
- assert_predicate legacy_pk , :bigint?
1011
- assert_predicate legacy_pk , :auto_increment?
1012
-
1013
- schema = dump_table_schema "legacy_primary_keys"
1014
- assert_match %r{create_table "legacy_primary_keys", (?!id: :bigint, default: nil)} , schema
981
+ schema = dump_table_schema "apples_bananas"
982
+ assert_match %r{integer "apple_id", null: false} , schema
983
+ assert_match %r{integer "banana_id", null: false} , schema
1015
984
end
1016
- else
1017
- def test_legacy_bigint_primary_key_should_not_be_auto_incremented
985
+
986
+ def test_legacy_join_table_column_options_should_be_overwritten
1018
987
@migration = Class . new ( migration_class ) {
1019
988
def change
1020
- create_table :legacy_primary_keys , id : : bigint do |t |
989
+ create_join_table :apples , :bananas , column_options : { type : : bigint } do |t |
1021
990
end
1022
991
end
1023
992
} . new
1024
993
1025
994
@migration . migrate ( :up )
1026
995
1027
- assert_raises ( ActiveRecord ::NotNullViolation ) do
1028
- LegacyPrimaryKey . create!
1029
- end
1030
-
1031
- schema = dump_table_schema "legacy_primary_keys"
1032
- assert_match %r{create_table "legacy_primary_keys", id: :bigint, default: nil} , schema
996
+ schema = dump_table_schema "apples_bananas"
997
+ assert_match %r{bigint "apple_id", null: false} , schema
998
+ assert_match %r{bigint "banana_id", null: false} , schema
1033
999
end
1034
- end
1035
1000
1036
- private
1037
- def assert_legacy_primary_key
1038
- assert_equal "id" , LegacyPrimaryKey . primary_key
1001
+ if current_adapter? ( :Mysql2Adapter )
1002
+ def test_legacy_bigint_primary_key_should_be_auto_incremented
1003
+ @migration = Class . new ( migration_class ) {
1004
+ def change
1005
+ create_table :legacy_primary_keys , id : :bigint
1006
+ end
1007
+ } . new
1039
1008
1040
- legacy_pk = LegacyPrimaryKey . columns_hash [ "id" ]
1009
+ @migration . migrate ( :up )
1041
1010
1042
- assert_equal :integer , legacy_pk . type
1043
- assert_not_predicate legacy_pk , :bigint?
1044
- assert_not legacy_pk . null
1011
+ legacy_pk = LegacyPrimaryKey . columns_hash [ "id" ]
1012
+ assert_predicate legacy_pk , :bigint?
1013
+ assert_predicate legacy_pk , :auto_increment?
1045
1014
1046
- if current_adapter? ( :Mysql2Adapter , :PostgreSQLAdapter )
1047
1015
schema = dump_table_schema "legacy_primary_keys"
1048
- assert_match %r{create_table "legacy_primary_keys", id: :(?:integer|serial), (?!default: nil)} , schema
1016
+ assert_match %r{create_table "legacy_primary_keys", (?!id: :bigint, default: nil)} , schema
1017
+ end
1018
+ else
1019
+ def test_legacy_bigint_primary_key_should_not_be_auto_incremented
1020
+ @migration = Class . new ( migration_class ) {
1021
+ def change
1022
+ create_table :legacy_primary_keys , id : :bigint do |t |
1023
+ end
1024
+ end
1025
+ } . new
1026
+
1027
+ @migration . migrate ( :up )
1028
+
1029
+ assert_raises ( ActiveRecord ::NotNullViolation ) do
1030
+ LegacyPrimaryKey . create!
1031
+ end
1032
+
1033
+ schema = dump_table_schema "legacy_primary_keys"
1034
+ assert_match %r{create_table "legacy_primary_keys", id: :bigint, default: nil} , schema
1049
1035
end
1050
1036
end
1037
+
1038
+ private
1039
+ def assert_legacy_primary_key
1040
+ assert_equal "id" , LegacyPrimaryKey . primary_key
1041
+
1042
+ legacy_pk = LegacyPrimaryKey . columns_hash [ "id" ]
1043
+
1044
+ assert_equal :integer , legacy_pk . type
1045
+ assert_not_predicate legacy_pk , :bigint?
1046
+ assert_not legacy_pk . null
1047
+
1048
+ if current_adapter? ( :Mysql2Adapter , :PostgreSQLAdapter )
1049
+ schema = dump_table_schema "legacy_primary_keys"
1050
+ assert_match %r{create_table "legacy_primary_keys", id: :(?:integer|serial), (?!default: nil)} , schema
1051
+ end
1052
+ end
1053
+ end
1051
1054
end
1052
1055
1053
1056
module LegacyPrimaryKeyTest
0 commit comments