@axinom/mosaic-db-common 0.9.0-rc.3 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,32 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [0.9.0](https://dev.azure.com/axinom/CMS/_git/Navy/branchCompare?baseVersion=GT@axinom/mosaic-db-common@0.8.0&targetVersion=GT@axinom/mosaic-db-common@0.9.0) (2021-12-09)
7
+
8
+
9
+ ### Features
10
+
11
+ * constraintName arg and length checks added to all ax_define multitenancy functions ([7fc8d6d](https://dev.azure.com/axinom/CMS/_git/Navy/commit/7fc8d6db952bdfeee64dbdd3a44b37224115c5e8))
12
+ * extracted the define-functions.sql ([a4cd97e](https://dev.azure.com/axinom/CMS/_git/Navy/commit/a4cd97e5b0963840a1ace6c26a1297f16cb7dba8))
13
+ * function overload to ax_utils.define_multitenancy_relation to take constraint name as input ([b97e6c0](https://dev.azure.com/axinom/CMS/_git/Navy/commit/b97e6c04634232c9ec5107c6511796568466cb40)), closes [#33592](https://dev.azure.com/axinom/CMS/_workitems/edit/33592)
14
+ * improve multitenancy SQL define functions ([60dd818](https://dev.azure.com/axinom/CMS/_git/Navy/commit/60dd818461398e35097b4b9a9ca572aa925fd4c7))
15
+ * missing RLS select permission raises exception ([ff6e08d](https://dev.azure.com/axinom/CMS/_git/Navy/commit/ff6e08d09927c5bba678cf30a10bf355d2192050))
16
+ * moved define functions to own schema ([6f57b5d](https://dev.azure.com/axinom/CMS/_git/Navy/commit/6f57b5d135c8d01ba2105e12eb0f556fa1e851d4))
17
+ * moved SQL definition functions to services ([4c4272c](https://dev.azure.com/axinom/CMS/_git/Navy/commit/4c4272ca81b852a7160816900d497c1bec57cd2c))
18
+ * PR fixes ([b45fb0e](https://dev.azure.com/axinom/CMS/_git/Navy/commit/b45fb0e9e171b37a141b096f49cd5c137db29aca))
19
+ * regenerated upgrade SQL functions ([87ee64d](https://dev.azure.com/axinom/CMS/_git/Navy/commit/87ee64d6a6fa36ee2b707fcd979b8e1a5a9fe5d2))
20
+
21
+
22
+ ### Bug Fixes
23
+
24
+ * fix malformed sql and remove unnecessary drop commands ([e3e7c89](https://dev.azure.com/axinom/CMS/_git/Navy/commit/e3e7c89f952e70e7ee44fd41e37ecde6b5bec41f))
25
+ * initial define functions added after reset ([e18efc3](https://dev.azure.com/axinom/CMS/_git/Navy/commit/e18efc34f670a064157eba5a75b48746c6fb848f))
26
+ * PR comments ([9711523](https://dev.azure.com/axinom/CMS/_git/Navy/commit/9711523ceb8984d26915f141fd84ae8f2084e00c))
27
+ * PR review ([840d6df](https://dev.azure.com/axinom/CMS/_git/Navy/commit/840d6dfeb3a9f66ee4642d46b5bec4c23594760f))
28
+ * PR review ([dddd780](https://dev.azure.com/axinom/CMS/_git/Navy/commit/dddd780100fbaa5b7c24343c9b0cb3674d203d05))
29
+
30
+
31
+
6
32
  ## [0.8.0](https://dev.azure.com/axinom/CMS/_git/Navy/branchCompare?baseVersion=GT@axinom/mosaic-db-common@0.7.0&targetVersion=GT@axinom/mosaic-db-common@0.8.0) (2021-11-24)
7
33
 
8
34
 
@@ -1,14 +1,20 @@
1
1
  -- define multitenancy columns and logic
2
2
  -- this must be called on all tables in a multitenant system
3
3
  -- idColumnNames: comma separated list of column names that are part of the primary key (e.g. 'id' or 'entity_id,name' - or '' to make tenant/env already unique)
4
+ -- constraintName: unique name for the multicolumn unique constraint. If value is NULL then a name will be generated from the table name
4
5
  CREATE OR REPLACE FUNCTION ax_define.define_multitenancy_on_table(
5
6
  idColumnNames text,
6
7
  tableName text,
7
- schemaName text
8
+ schemaName text,
9
+ constraintName text default NULL
8
10
  ) RETURNS void
9
11
  LANGUAGE plpgsql
10
12
  AS $$
11
13
  BEGIN
14
+ SELECT COALESCE(constraintName, tableName || '_unique_id_tenant_environment') INTO constraintName;
15
+ IF LENGTH(constraintName) > 63 THEN
16
+ perform ax_utils.raise_error('Invalid parameters provided to "ax_define.define_multitenancy_on_table". Constraint name "%s" exceeds 63 bytes. If the auto-generated name exceeds 63 bytes then a "constraintName" argument must be provided.', 'SETUP', constraintName);
17
+ END IF;
12
18
  EXECUTE '
13
19
  DO $do$ BEGIN
14
20
  BEGIN
@@ -31,34 +37,21 @@ BEGIN
31
37
  ALTER TABLE ' || schemaName || '.' || tableName || ' DROP CONSTRAINT IF EXISTS environment_id_not_default;
32
38
  ALTER TABLE ' || schemaName || '.' || tableName || ' ADD CONSTRAINT environment_id_not_default CHECK (ax_utils.constraint_not_default_uuid(environment_id));
33
39
  SELECT ax_define.define_tenant_environment_trigger(''' || tableName || ''', ''' || schemaName || ''');
34
- ALTER TABLE ' || schemaName || '.' || tableName || ' DROP CONSTRAINT IF EXISTS ' || tableName || '_unique_id_tenant_environment;
40
+ ALTER TABLE ' || schemaName || '.' || tableName || ' DROP CONSTRAINT IF EXISTS ' || constraintName || ';
35
41
  ';
36
42
  IF idColumnNames = '' THEN
37
- EXECUTE 'ALTER TABLE ' || schemaName || '.' || tableName || ' ADD CONSTRAINT ' || tableName || '_unique_id_tenant_environment UNIQUE(tenant_id, environment_id);';
43
+ EXECUTE 'ALTER TABLE ' || schemaName || '.' || tableName || ' ADD CONSTRAINT ' || constraintName || ' UNIQUE(tenant_id, environment_id);';
38
44
  ELSE
39
- EXECUTE 'ALTER TABLE ' || schemaName || '.' || tableName || ' ADD CONSTRAINT ' || tableName || '_unique_id_tenant_environment UNIQUE(' || idColumnNames || ', tenant_id, environment_id);';
45
+ EXECUTE 'ALTER TABLE ' || schemaName || '.' || tableName || ' ADD CONSTRAINT ' || constraintName || ' UNIQUE(' || idColumnNames || ', tenant_id, environment_id);';
40
46
  END IF;
41
47
  END;
42
48
  $$;
43
49
 
44
- -- drop method to help on consistently drop timestamp triggers
45
- CREATE OR REPLACE FUNCTION ax_define.drop_multitenancy_relation(
46
- tableName text,
47
- schemaName text,
48
- foreignTableName text,
49
- foreignSchemaName text
50
- ) RETURNS void
51
- LANGUAGE plpgsql
52
- AS $$
53
- BEGIN
54
- EXECUTE 'ALTER TABLE ' || schemaName || '.' || tableName || ' DROP CONSTRAINT IF EXISTS multitenancy_relation_' || tableName || '_to_' || foreignTableName || ';';
55
- END;
56
- $$;
57
-
58
50
  -- define multitenancy relations
59
51
  -- this must be called only after 'define_multitenancy_on_table' was called on both tables.
60
52
  -- idColumnNames: comma separated list of column names from the "tableName" table that should be used as FK
61
53
  -- foreignIdColumnNames: comma separated list of column names from the "foreignTableName" that should be used for the reference
54
+ -- constraintName: unique name for this constraint. If value is NULL then a name will be generated from the table names
62
55
  CREATE OR REPLACE FUNCTION ax_define.define_multitenancy_relation(
63
56
  idColumnNames text,
64
57
  tableName text,
@@ -66,59 +59,84 @@ CREATE OR REPLACE FUNCTION ax_define.define_multitenancy_relation(
66
59
  foreignIdColumnNames text,
67
60
  foreignTableName text,
68
61
  foreignSchemaName text,
69
- cascadeOptions text default 'ON DELETE CASCADE'
62
+ cascadeOptions text default 'ON DELETE CASCADE',
63
+ constraintName text default NULL
70
64
  ) RETURNS void
71
65
  LANGUAGE plpgsql
72
66
  AS $$
73
67
  BEGIN
74
- PERFORM ax_define.drop_multitenancy_relation(tableName, schemaName, foreignTableName, foreignSchemaName);
68
+ SELECT COALESCE(constraintName, 'multitenancy_relation_' || tableName || '_to_' || foreignTableName) INTO constraintName;
69
+ IF LENGTH(constraintName) > 63 THEN
70
+ perform ax_utils.raise_error('Invalid parameters provided to "ax_define.define_multitenancy_relation". Constraint name "%s" exceeds 63 bytes. If the auto-generated name exceeds 63 bytes then a "constraintName" argument must be provided.', 'SETUP', constraintName);
71
+ END IF;
72
+ EXECUTE 'ALTER TABLE ' || schemaName || '.' || tableName || ' DROP CONSTRAINT IF EXISTS ' || constraintName || ';';
75
73
  EXECUTE '
76
- ALTER TABLE ' || schemaName || '.' || tableName || ' ADD CONSTRAINT multitenancy_relation_' || tableName || '_to_' || foreignTableName || '
74
+ ALTER TABLE ' || schemaName || '.' || tableName || ' ADD CONSTRAINT ' || constraintName || '
77
75
  FOREIGN KEY (' || idColumnNames || ', tenant_id, environment_id) REFERENCES ' || foreignSchemaName || '.' || foreignTableName || '(' || foreignIdColumnNames || ', tenant_id, environment_id) ' || cascadeOptions || ';
78
76
  ';
79
77
  END;
80
78
  $$;
81
79
 
82
80
  -- creation method to help on consistently create UNIQUE constraint in an idempotent way
83
- CREATE OR REPLACE FUNCTION ax_define.define_multitenancy_unique_constraint(fieldName text, tableName text, schemaName text) RETURNS void
81
+ -- constraintName: unique name for this constraint. If value is NULL then a name will be generated from the table name
82
+ CREATE OR REPLACE FUNCTION ax_define.define_multitenancy_unique_constraint(fieldName text, tableName text, schemaName text, constraintName text default NULL) RETURNS void
84
83
  LANGUAGE plpgsql
85
84
  AS $$
86
85
  BEGIN
87
- EXECUTE 'ALTER TABLE ' || schemaName || '.' || tableName || ' DROP CONSTRAINT IF EXISTS ' || tableName || '_' || fieldName || '_is_unique;';
88
- EXECUTE 'ALTER TABLE ' || schemaName || '.' || tableName || ' ADD CONSTRAINT ' || tableName || '_' || fieldName || '_is_unique UNIQUE (' || fieldName || ', tenant_id, environment_id);';
86
+ SELECT COALESCE(constraintName, tableName || '_' || fieldName || '_is_unique') INTO constraintName;
87
+ IF LENGTH(constraintName) > 63 THEN
88
+ perform ax_utils.raise_error('Invalid parameters provided to "ax_define.define_multitenancy_unique_constraint". Constraint name "%s" exceeds 63 bytes. If the auto-generated name exceeds 63 bytes then a "constraintName" argument must be provided.', 'SETUP', constraintName);
89
+ END IF;
90
+ EXECUTE 'ALTER TABLE ' || schemaName || '.' || tableName || ' DROP CONSTRAINT IF EXISTS ' || constraintName || ';';
91
+ EXECUTE 'ALTER TABLE ' || schemaName || '.' || tableName || ' ADD CONSTRAINT ' || constraintName || ' UNIQUE (' || fieldName || ', tenant_id, environment_id);';
89
92
  END;
90
93
  $$;
91
94
 
92
95
  -- creation method to help on consistently create UNIQUE constraint in an idempotent way
93
- CREATE OR REPLACE FUNCTION ax_define.define_multitenancy_multi_unique_constraint(fieldNames text[], tableName text, schemaName text) RETURNS void
96
+ -- constraintName: unique name for this constraint. If value is NULL then a name will be generated from the table & field names
97
+ CREATE OR REPLACE FUNCTION ax_define.define_multitenancy_multi_unique_constraint(fieldNames text[], tableName text, schemaName text, constraintName text default NULL) RETURNS void
94
98
  LANGUAGE plpgsql
95
99
  AS $$
96
100
  DECLARE
97
101
  fieldNamesConcat text = array_to_string(array_agg(fieldNames), '_');
98
102
  fieldList text = array_to_string(array_agg(fieldNames), ', ');
99
103
  BEGIN
100
- EXECUTE 'ALTER TABLE ' || schemaName || '.' || tableName || ' DROP CONSTRAINT IF EXISTS ' || tableName || '_' || fieldNamesConcat || '_is_unique;';
101
- EXECUTE 'ALTER TABLE ' || schemaName || '.' || tableName || ' ADD CONSTRAINT ' || tableName || '_' || fieldNamesConcat || '_is_unique UNIQUE (' || fieldList || ', tenant_id, environment_id);';
104
+ SELECT COALESCE(constraintName, tableName || '_' || fieldNamesConcat || '_is_unique') INTO constraintName;
105
+ IF LENGTH(constraintName) > 63 THEN
106
+ perform ax_utils.raise_error('Invalid parameters provided to "ax_define.define_multitenancy_multi_unique_constraint". Constraint name "%s" exceeds 63 bytes. If the auto-generated name exceeds 63 bytes then a "constraintName" argument must be provided.', 'SETUP', constraintName);
107
+ END IF;
108
+ EXECUTE 'ALTER TABLE ' || schemaName || '.' || tableName || ' DROP CONSTRAINT IF EXISTS ' || constraintName || ';';
109
+ EXECUTE 'ALTER TABLE ' || schemaName || '.' || tableName || ' ADD CONSTRAINT ' || constraintName || ' UNIQUE (' || fieldList || ', tenant_id, environment_id);';
102
110
  END;
103
111
  $$;
104
112
 
105
113
  -- creation method to help on consistently create UNIQUE constraint in an idempotent way for a single row per tenant/env combination
106
- CREATE OR REPLACE FUNCTION ax_define.define_multitenancy_unique_constraint(tableName text, schemaName text) RETURNS void
114
+ -- constraintName: unique name for this constraint. If value is NULL then a name will be generated from the table name
115
+ CREATE OR REPLACE FUNCTION ax_define.define_multitenancy_unique_constraint(tableName text, schemaName text, constraintName text default NULL) RETURNS void
107
116
  LANGUAGE plpgsql
108
117
  AS $$
109
118
  BEGIN
110
- EXECUTE 'ALTER TABLE ' || schemaName || '.' || tableName || ' DROP CONSTRAINT IF EXISTS ' || tableName || '_multitenancy_entry_is_unique;';
111
- EXECUTE 'ALTER TABLE ' || schemaName || '.' || tableName || ' ADD CONSTRAINT ' || tableName || '_multitenancy_entry_is_unique UNIQUE (tenant_id, environment_id);';
119
+ SELECT COALESCE(constraintName, tableName || '_multitenancy_entry_is_unique') INTO constraintName;
120
+ IF LENGTH(constraintName) > 63 THEN
121
+ perform ax_utils.raise_error('Invalid parameters provided to "ax_define.define_multitenancy_unique_constraint". Constraint name "%s" exceeds 63 bytes. If the auto-generated name exceeds 63 bytes then a "constraintName" argument must be provided.', 'SETUP', constraintName);
122
+ END IF;
123
+ EXECUTE 'ALTER TABLE ' || schemaName || '.' || tableName || ' DROP CONSTRAINT IF EXISTS ' || constraintName || ';';
124
+ EXECUTE 'ALTER TABLE ' || schemaName || '.' || tableName || ' ADD CONSTRAINT ' || constraintName || ' UNIQUE (tenant_id, environment_id);';
112
125
  END;
113
126
  $$;
114
127
 
115
128
  -- creation method to help on consistently create "normal" indexes in an idempotent way for a single field on a multitenancy table
116
- CREATE OR REPLACE FUNCTION ax_define.define_multitenancy_index(fieldName text, tableName text, schemaName text) RETURNS void
129
+ -- indexName: unique name for this index. If value is NULL then a name will be generated from the table & field names
130
+ CREATE OR REPLACE FUNCTION ax_define.define_multitenancy_index(fieldName text, tableName text, schemaName text, indexName text default NULL) RETURNS void
117
131
  LANGUAGE plpgsql
118
132
  AS $$
119
133
  BEGIN
120
- EXECUTE 'DROP INDEX IF EXISTS idx_' || tableName || '_' || fieldName || '_mt cascade;';
121
- EXECUTE 'CREATE INDEX idx_' || tableName || '_' || fieldName || '_mt ON ' || schemaName || '.' || tableName || ' (' || fieldName || ', tenant_id, environment_id);';
134
+ SELECT COALESCE(indexName, 'idx_' || tableName || '_' || fieldName || '_mt') INTO indexName;
135
+ IF LENGTH(indexName) > 63 THEN
136
+ perform ax_utils.raise_error('Invalid parameters provided to "ax_define.define_multitenancy_index". Index name "%s" exceeds 63 bytes. If the auto-generated name exceeds 63 bytes then an "indexName" argument must be provided.', 'SETUP', indexName);
137
+ END IF;
138
+ EXECUTE 'DROP INDEX IF EXISTS ' || indexName || ' cascade;';
139
+ EXECUTE 'CREATE INDEX ' || indexName || ' ON ' || schemaName || '.' || tableName || ' (' || fieldName || ', tenant_id, environment_id);';
122
140
  END;
123
141
  $$;
124
142
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@axinom/mosaic-db-common",
3
- "version": "0.9.0-rc.3",
3
+ "version": "0.9.0",
4
4
  "description": "This library encapsulates database-related functionality to develop Mosaic based services.",
5
5
  "author": "Axinom",
6
6
  "license": "PROPRIETARY",
@@ -45,5 +45,5 @@
45
45
  "publishConfig": {
46
46
  "access": "public"
47
47
  },
48
- "gitHead": "30a5cceacde15746fcf22502f57c22c2f13643ea"
48
+ "gitHead": "de356e3d88414dc569d1291d00c21069c5e3556a"
49
49
  }