-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathenums.sql
63 lines (51 loc) · 1.69 KB
/
enums.sql
1
2
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/** Functions for creating enums from queries */
/**
Adds a list of strings to an enum type
Deprecated, only needed for PostgreSQL < 12
*/
CREATE OR REPLACE FUNCTION util.add_enum_values(type REGTYPE, values_ TEXT [])
RETURNS VOID AS $$
DECLARE sortorder INTEGER;
value TEXT;
BEGIN
SELECT coalesce(max(enumsortorder), 0) + 1
FROM pg_enum
WHERE enumtypid = type :: OID
INTO sortorder;
FOR value IN SELECT unnest(values_) AS value
LOOP
IF char_length(value) > 63
THEN
RAISE WARNING 'WARNING: Enum value should not be more than 63 characters length. Values are trimmed automatically. (%)', value;
END IF;
INSERT INTO pg_enum
VALUES (type :: OID, sortorder, value);
sortorder := sortorder + 1;
END LOOP;
END;
$$
LANGUAGE plpgsql;
/** Creates an enum type with a list of values */
CREATE OR REPLACE FUNCTION util.create_enum(enum_ TEXT, values_ TEXT [])
RETURNS VOID AS $$
DECLARE value TEXT;
pg_version INTEGER;
BEGIN
EXECUTE 'DROP TYPE IF EXISTS ' || enum_ || ' CASCADE';
EXECUTE 'CREATE TYPE ' || enum_ || ' AS ENUM ();';
SELECT current_setting('server_version_num') INTO pg_version;
-- Since PostgreSQL 12, it is possible to add enum values via ALTER TYPE .. ADD VALUE inside a transaction
-- Before version 12, direct inserts into pg_enum were needed
IF (pg_version >= 120000) THEN
FOR value IN SELECT unnest(values_) AS value
LOOP
EXECUTE 'ALTER TYPE ' || enum_ || ' ADD VALUE ''' || value || '''';
END LOOP;
ELSE
PERFORM util.add_enum_values(enum_ :: REGTYPE, values_);
END IF;
END;
$$
LANGUAGE plpgsql;
--SELECT util.create_enum('util.enum_test', array['a', 'b', 'c']);
--SELECT * from pg_enum;