Skip to content

Commit 584bb20

Browse files
Merge pull request #1 from time-loop/feature/ENG_OPER-10194_schema_ownership
feat(db-ops): Allows schemas to have ownership set at creation [ENG_OPER-10194]
2 parents 7dd1b99 + 2d187f5 commit 584bb20

File tree

2 files changed

+42
-22
lines changed

2 files changed

+42
-22
lines changed

sql/functions/microsharding_ensure_exist.sql

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
DROP FUNCTION IF EXISTS microsharding_ensure_exist(integer, integer);
2+
13
CREATE OR REPLACE FUNCTION microsharding_ensure_exist(
24
shard_from integer,
3-
shard_to integer = NULL
5+
shard_to integer = NULL,
6+
schema_owner varchar(63) DEFAULT NULL
47
) RETURNS SETOF regnamespace
58
LANGUAGE plpgsql
69
SET search_path FROM CURRENT
@@ -13,6 +16,16 @@ BEGIN
1316
IF shard_from < 0 OR shard_to > 9999 THEN
1417
RAISE EXCEPTION 'Invalid shard_from or shard_to';
1518
END IF;
19+
20+
IF schema_owner IS NOT NULL
21+
AND NOT EXISTS (
22+
SELECT 1 FROM pg_catalog.pg_roles WHERE rolname = schema_owner
23+
UNION
24+
SELECT 1 FROM pg_catalog.pg_user WHERE usename = schema_owner
25+
) THEN
26+
RAISE EXCEPTION 'Invalid schema_owner: %', schema_owner;
27+
END IF;
28+
1629
FOR shard IN
1730
WITH shards AS (
1831
SELECT microsharding_schema_name_(n) AS shard
@@ -22,11 +35,17 @@ BEGIN
2235
WHERE NOT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname = shards.shard)
2336
ORDER BY shards.shard
2437
LOOP
25-
EXECUTE format('CREATE SCHEMA %I', shard);
38+
39+
IF schema_owner IS NOT NULL THEN
40+
EXECUTE format('CREATE SCHEMA IF NOT EXISTS %I AUTHORIZATION %I ', shard, schema_owner);
41+
ELSE
42+
EXECUTE format('CREATE SCHEMA IF NOT EXISTS %I', shard);
43+
END IF;
44+
2645
RETURN NEXT shard::regnamespace;
2746
END LOOP;
2847
END;
2948
$$;
3049

31-
COMMENT ON FUNCTION microsharding_ensure_exist(integer, integer)
50+
COMMENT ON FUNCTION microsharding_ensure_exist(integer, integer, varchar)
3251
IS 'Creates shards (schemas) in the range shard_from..shard_to (inclusive).';

sql/pg-microsharding-down.sql

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
DROP FUNCTION microsharding_active_shards_();
2-
DROP FUNCTION microsharding_advisory_lock_();
3-
DROP FUNCTION microsharding_debug_fdw_create(text, text[]);
4-
DROP FUNCTION microsharding_debug_fdw_drop(text);
5-
DROP FUNCTION microsharding_debug_fdw_schemas_(text);
6-
DROP FUNCTION microsharding_debug_views_create(text, text);
7-
DROP FUNCTION microsharding_debug_views_drop(text);
8-
DROP FUNCTION microsharding_do_on_each(text);
9-
DROP FUNCTION microsharding_ensure_absent(integer, integer);
10-
DROP FUNCTION microsharding_ensure_active_shards_(text[]);
11-
DROP FUNCTION microsharding_ensure_active(integer, integer);
12-
DROP FUNCTION microsharding_ensure_exist(integer, integer);
13-
DROP FUNCTION microsharding_ensure_inactive(integer, integer);
14-
DROP FUNCTION microsharding_fmt_(text, integer);
15-
DROP FUNCTION microsharding_list_active_shards();
16-
DROP FUNCTION microsharding_migration_after(text, text, text);
17-
DROP FUNCTION microsharding_migration_before(text);
18-
DROP FUNCTION microsharding_notice_locks_(text, timestamptz);
19-
DROP FUNCTION microsharding_schema_name_(integer);
1+
DROP FUNCTION IF EXISTS microsharding_active_shards_();
2+
DROP FUNCTION IF EXISTS microsharding_advisory_lock_();
3+
DROP FUNCTION IF EXISTS microsharding_debug_fdw_create(text, text[]);
4+
DROP FUNCTION IF EXISTS microsharding_debug_fdw_drop(text);
5+
DROP FUNCTION IF EXISTS microsharding_debug_fdw_schemas_(text);
6+
DROP FUNCTION IF EXISTS microsharding_debug_views_create(text, text);
7+
DROP FUNCTION IF EXISTS microsharding_debug_views_drop(text);
8+
DROP FUNCTION IF EXISTS microsharding_do_on_each(text);
9+
DROP FUNCTION IF EXISTS microsharding_ensure_absent(integer, integer);
10+
DROP FUNCTION IF EXISTS microsharding_ensure_active_shards_(text[]);
11+
DROP FUNCTION IF EXISTS microsharding_ensure_active(integer, integer);
12+
DROP FUNCTION IF EXISTS microsharding_ensure_exist(integer, integer);
13+
DROP FUNCTION IF EXISTS microsharding_ensure_exist(integer, integer, varchar);
14+
DROP FUNCTION IF EXISTS microsharding_ensure_inactive(integer, integer);
15+
DROP FUNCTION IF EXISTS microsharding_fmt_(text, integer);
16+
DROP FUNCTION IF EXISTS microsharding_list_active_shards();
17+
DROP FUNCTION IF EXISTS microsharding_migration_after(text, text, text);
18+
DROP FUNCTION IF EXISTS microsharding_migration_before(text);
19+
DROP FUNCTION IF EXISTS microsharding_notice_locks_(text, timestamptz);
20+
DROP FUNCTION IF EXISTS microsharding_schema_name_(integer);

0 commit comments

Comments
 (0)