@@ -9,6 +9,10 @@ def connection
99 def drop_table ( name )
1010 connection . drop_table name , if_exists : true
1111 end
12+
13+ def uuid_function
14+ connection . supports_pgcrypto_uuid? ? "gen_random_uuid()" : "uuid_generate_v4()"
15+ end
1216end
1317
1418class PostgresqlUUIDTest < ActiveRecord ::PostgreSQLTestCase
@@ -21,6 +25,7 @@ class UUIDType < ActiveRecord::Base
2125
2226 setup do
2327 enable_extension! ( "uuid-ossp" , connection )
28+ enable_extension! ( "pgcrypto" , connection ) if connection . supports_pgcrypto_uuid?
2429
2530 connection . create_table "uuid_data_type" do |t |
2631 t . uuid "guid"
@@ -31,19 +36,27 @@ class UUIDType < ActiveRecord::Base
3136 drop_table "uuid_data_type"
3237 end
3338
34- def test_change_column_default
35- @connection . add_column :uuid_data_type , :thingy , :uuid , null : false , default : "uuid_generate_v1()"
36- UUIDType . reset_column_information
37- column = UUIDType . columns_hash [ "thingy" ]
38- assert_equal "uuid_generate_v1()" , column . default_function
39-
40- @connection . change_column :uuid_data_type , :thingy , :uuid , null : false , default : "uuid_generate_v4()"
41-
42- UUIDType . reset_column_information
43- column = UUIDType . columns_hash [ "thingy" ]
44- assert_equal "uuid_generate_v4()" , column . default_function
45- ensure
46- UUIDType . reset_column_information
39+ if ActiveRecord ::Base . connection . supports_pgcrypto_uuid?
40+ def test_uuid_column_default
41+ connection . add_column :uuid_data_type , :thingy , :uuid , null : false , default : "gen_random_uuid()"
42+ UUIDType . reset_column_information
43+ column = UUIDType . columns_hash [ "thingy" ]
44+ assert_equal "gen_random_uuid()" , column . default_function
45+ end
46+ else
47+ def test_change_column_default
48+ connection . add_column :uuid_data_type , :thingy , :uuid , null : false , default : "uuid_generate_v1()"
49+ UUIDType . reset_column_information
50+ column = UUIDType . columns_hash [ "thingy" ]
51+ assert_equal "uuid_generate_v1()" , column . default_function
52+
53+ connection . change_column :uuid_data_type , :thingy , :uuid , null : false , default : "uuid_generate_v4()"
54+ UUIDType . reset_column_information
55+ column = UUIDType . columns_hash [ "thingy" ]
56+ assert_equal "uuid_generate_v4()" , column . default_function
57+ ensure
58+ UUIDType . reset_column_information
59+ end
4760 end
4861
4962 def test_data_type_of_uuid_types
@@ -155,7 +168,7 @@ class UUID < ActiveRecord::Base
155168 # to test dumping tables which columns have defaults with custom functions
156169 connection . execute <<-SQL
157170 CREATE OR REPLACE FUNCTION my_uuid_generator() RETURNS uuid
158- AS $$ SELECT * FROM uuid_generate_v4() $$
171+ AS $$ SELECT * FROM #{ uuid_function } $$
159172 LANGUAGE SQL VOLATILE;
160173 SQL
161174
@@ -164,11 +177,16 @@ class UUID < ActiveRecord::Base
164177 t . string "name"
165178 t . uuid "other_uuid_2" , default : "my_uuid_generator()"
166179 end
180+
181+ connection . create_table ( "pg_uuids_3" , id : :uuid ) do |t |
182+ t . string "name"
183+ end
167184 end
168185
169186 teardown do
170187 drop_table "pg_uuids"
171188 drop_table "pg_uuids_2"
189+ drop_table "pg_uuids_3"
172190 connection . execute "DROP FUNCTION IF EXISTS my_uuid_generator();"
173191 end
174192
@@ -206,6 +224,33 @@ def test_schema_dumper_for_uuid_primary_key_with_custom_default
206224 assert_match ( /\b create_table "pg_uuids_2", id: :uuid, default: -> { "my_uuid_generator\( \) " }/ , schema )
207225 assert_match ( /t\. uuid "other_uuid_2", default: -> { "my_uuid_generator\( \) " }/ , schema )
208226 end
227+
228+ def test_schema_dumper_for_uuid_primary_key_default
229+ schema = dump_table_schema "pg_uuids_3"
230+ if connection . supports_pgcrypto_uuid?
231+ assert_match ( /\b create_table "pg_uuids_3", id: :uuid, default: -> { "gen_random_uuid\( \) " }/ , schema )
232+ else
233+ assert_match ( /\b create_table "pg_uuids_3", id: :uuid, default: -> { "uuid_generate_v4\( \) " }/ , schema )
234+ end
235+ end
236+
237+ if ActiveRecord ::Base . connection . supports_pgcrypto_uuid?
238+ def test_schema_dumper_for_uuid_primary_key_default_in_legacy_migration
239+ migration = Class . new ( ActiveRecord ::Migration [ 4.2 ] ) do
240+ def version ; 101 end
241+ def migrate ( x )
242+ create_table ( "pg_uuids_4" , id : :uuid )
243+ end
244+ end . new
245+ ActiveRecord ::Migrator . new ( :up , [ migration ] ) . migrate
246+
247+ schema = dump_table_schema "pg_uuids_4"
248+ assert_match ( /\b create_table "pg_uuids_4", id: :uuid, default: -> { "uuid_generate_v4\( \) " }/ , schema )
249+ ensure
250+ drop_table "pg_uuids_4"
251+ end
252+ else
253+ end
209254 end
210255end
211256
0 commit comments