@fabrika/platform-node 0.9.0-dev.3 → 0.9.0-dev.30

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fabrika/platform-node",
3
- "version": "0.9.0-dev.3",
3
+ "version": "0.9.0-dev.30",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -34,10 +34,10 @@
34
34
  }
35
35
  },
36
36
  "scripts": {
37
- "typecheck": "tsc --noEmit",
37
+ "typecheck": "tsrs --build tsconfig.test.json",
38
38
  "test": "bun test"
39
39
  },
40
40
  "dependencies": {
41
- "@fabrika/platform": "0.9.0-dev.3"
41
+ "@fabrika/platform": "0.9.0-dev.30"
42
42
  }
43
43
  }
@@ -46,9 +46,10 @@ export async function normalizeDefaultPrivileges(db: SqlDatabase, schema: string
46
46
 
47
47
  /**
48
48
  * Resets the schema's privileges to what the runtime role needs: PUBLIC and the runtime role lose everything on the
49
- * schema and its objects, then the runtime role gets USAGE, DML on every table but the ledger, EXECUTE on the schema's
50
- * routines and USAGE on its types. One transaction, so a live runtime never sees a gap. Then the effective privileges
51
- * are verified, PUBLIC included, and anything beyond that stops the deploy.
49
+ * schema and its objects, then the runtime role gets USAGE, SELECT on every relation, DML on every table but the
50
+ * ledger (never on a view), EXECUTE on the schema's routines and USAGE on its types. One transaction, so a live
51
+ * runtime never sees a gap. Then the effective privileges are verified, PUBLIC included, and anything beyond that
52
+ * stops the deploy.
52
53
  */
53
54
  export async function grantRuntimeRole(db: SqlDatabase, schema: string, ledgerTable: string, role: string): Promise<void> {
54
55
  try {
@@ -69,7 +70,17 @@ export async function grantRuntimeRole(db: SqlDatabase, schema: string, ledgerTa
69
70
  END LOOP;
70
71
  END $types$`),
71
72
  db.prepare(`GRANT USAGE ON SCHEMA ${schema} TO ${role}`),
72
- db.prepare(`GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA ${schema} TO ${role}`),
73
+ db.prepare(`GRANT SELECT ON ALL TABLES IN SCHEMA ${schema} TO ${role}`),
74
+ // DML only on plain and partitioned tables: a view, a materialized view and a foreign table stay read-only.
75
+ db.prepare(`DO $tables$
76
+ DECLARE t pg_catalog.regclass;
77
+ BEGIN
78
+ FOR t IN SELECT c.oid::pg_catalog.regclass FROM pg_catalog.pg_class c
79
+ WHERE c.relnamespace = '${schema}'::pg_catalog.regnamespace AND c.relkind IN ('r', 'p')
80
+ LOOP
81
+ EXECUTE pg_catalog.format('GRANT INSERT, UPDATE, DELETE ON TABLE %s TO ${role}', t);
82
+ END LOOP;
83
+ END $tables$`),
73
84
  db.prepare(`REVOKE ALL ON TABLE ${ledgerTable} FROM ${role}`),
74
85
  db.prepare(`GRANT EXECUTE ON ALL ROUTINES IN SCHEMA ${schema} TO ${role}`),
75
86
  ])
@@ -98,6 +109,12 @@ async function verifyRuntimeRole(db: SqlDatabase, schema: string, ledgerTable: s
98
109
  WHERE c.relnamespace OPERATOR(pg_catalog.=) ?::pg_catalog.regnamespace AND c.relkind OPERATOR(pg_catalog.=) ANY (ARRAY['r', 'p', 'v', 'm', 'f']::"char"[])
99
110
  AND pg_catalog.has_table_privilege(runtime.oid, c.oid, p.privilege)
100
111
  UNION ALL
112
+ SELECT pg_catalog.format('%s may %s %s, which is not a table', runtime.rolname, p.privilege, c.oid::pg_catalog.regclass)
113
+ FROM runtime, pg_catalog.unnest(ARRAY['INSERT', 'UPDATE', 'DELETE']) AS p(privilege), pg_catalog.pg_class c
114
+ WHERE c.relnamespace OPERATOR(pg_catalog.=) ?::pg_catalog.regnamespace AND c.relkind OPERATOR(pg_catalog.=) ANY (ARRAY['v', 'm', 'f']::"char"[])
115
+ AND (pg_catalog.has_table_privilege(runtime.oid, c.oid, p.privilege)
116
+ OR (p.privilege OPERATOR(pg_catalog.<>) 'DELETE' AND pg_catalog.has_any_column_privilege(runtime.oid, c.oid, p.privilege)))
117
+ UNION ALL
101
118
  SELECT pg_catalog.format('%s may %s the migration ledger', runtime.rolname, p.privilege) FROM runtime, ledger, pg_catalog.unnest(ARRAY['SELECT', 'INSERT', 'UPDATE', 'DELETE']) AS p(privilege)
102
119
  WHERE pg_catalog.has_table_privilege(runtime.oid, ledger.oid, p.privilege)
103
120
  OR (p.privilege OPERATOR(pg_catalog.<>) 'DELETE' AND pg_catalog.has_any_column_privilege(runtime.oid, ledger.oid, p.privilege))
@@ -106,12 +123,12 @@ async function verifyRuntimeRole(db: SqlDatabase, schema: string, ledgerTable: s
106
123
  a.privilege_type, d.defaclobjtype, CASE WHEN a.grantee OPERATOR(pg_catalog.=) 0 THEN 'PUBLIC' ELSE pg_catalog.pg_get_userbyid(a.grantee) END)
107
124
  FROM pg_catalog.pg_default_acl d, pg_catalog.aclexplode(d.defaclacl) a
108
125
  WHERE d.defaclrole OPERATOR(pg_catalog.=) (SELECT oid FROM pg_catalog.pg_roles WHERE rolname OPERATOR(pg_catalog.=) current_user)
109
- AND a.grantee OPERATOR(pg_catalog.<>) d.defaclrole`).bind(role, ledgerTable, schema, schema, schema).all<Problem>()
126
+ AND a.grantee OPERATOR(pg_catalog.<>) d.defaclrole`).bind(role, ledgerTable, schema, schema, schema, schema).all<Problem>()
110
127
  if (results.length > 0) {
111
128
  throw new PostgresMigrationError(
112
129
  `${
113
130
  results.map((row) => row.problem).join('; ')
114
- }; the deploy stops, because the runtime role ${role} has DML on its tables and nothing else (ADR-0017)`,
131
+ }; the deploy stops, because the runtime role ${role} reads its relations, has DML on its tables and nothing else (ADR-0017)`,
115
132
  )
116
133
  }
117
134
  }
package/src/index.ts CHANGED
@@ -70,7 +70,6 @@ export {
70
70
  readPostgresMigrationBundle,
71
71
  type ReadPostgresMigrationBundleOptions,
72
72
  } from './postgres-migrations'
73
- export { createTestProvisioner, type TestProvisioner } from './postgres-test-provisioner'
74
73
  export {
75
74
  definePostgresServiceMigrations,
76
75
  postgresMigrationResultMessage,