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
# File 'lib/typed_uuid/psql_schema_dumper.rb', line 3

def extensions(stream)
  super(stream)
  stream.puts <<-RB
## These are functions that must be enabled in order to support typed_uuids
## in this database
execute <<-SQL
  CREATE OR REPLACE FUNCTION typed_uuid(t bytea) RETURNS uuid AS $$
    DECLARE
      bytes bytea := gen_random_bytes(16);
      uuid bytea;
    BEGIN
      bytes := set_byte(bytes, 6, (get_byte(bytes, 4) # get_byte(bytes, 8)) # get_byte(t, 0));
      bytes := set_byte(bytes, 7, (get_byte(bytes, 5) # get_byte(bytes, 9)) # get_byte(t, 1));
      RETURN encode( bytes, 'hex') :: uuid;
    END;
  $$ LANGUAGE plpgsql;
SQL

  RB
  stream
end