Module: TypedUUID::PsqlSchemaDumper

Defined in:
lib/typed_uuid/psql_schema_dumper.rb

Instance Method Summary collapse

Instance Method Details

#extensions(stream) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/typed_uuid/psql_schema_dumper.rb', line 3

def extensions(stream)
  super(stream)
  stream.puts "## These are functions that must be enabled in order to support typed_uuids\n## in this database\nexecute <<-SQL\n  CREATE OR REPLACE FUNCTION typed_uuid(enum int, version int default 4)\n  RETURNS uuid AS $$\n  DECLARE\n    bytes bytea;\n    type bytea;\n  BEGIN\n    IF version = 1 THEN\n      bytes := decode(concat(\n          lpad(right(to_hex((extract(epoch from clock_timestamp())*1000000)::bigint), 14), 14, '0'),\n          encode(gen_random_bytes(9), 'hex')\n      ), 'hex');\n    ELSE\n      bytes := gen_random_bytes(16);\n    END IF;\n\n    type := decode( lpad(to_hex(((enum << 3) | version)), 4, '0'), 'hex');\n    bytes := set_byte(bytes, 14, (get_byte(bytes, 4) # get_byte(bytes, 12)) # get_byte(type, 0));\n    bytes := set_byte(bytes, 15, (get_byte(bytes, 5) # get_byte(bytes, 13)) # get_byte(type, 1));\n  \n    RETURN encode( bytes, 'hex') :: uuid;\n  END;\n  $$ LANGUAGE plpgsql;\nSQL\n\n  RB\n  stream\nend\n"