@marinoscar/platform-db 0.0.0-stage → 0.1.0-next.4

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.
Files changed (244) hide show
  1. package/CHANGELOG.md +74 -0
  2. package/LICENSE +21 -0
  3. package/README.md +525 -2
  4. package/bin/platform.js +28 -0
  5. package/dist/baseline/deps.d.ts +32 -0
  6. package/dist/baseline/deps.d.ts.map +1 -0
  7. package/dist/baseline/deps.js +66 -0
  8. package/dist/baseline/deps.js.map +1 -0
  9. package/dist/baseline/errors.d.ts +36 -0
  10. package/dist/baseline/errors.d.ts.map +1 -0
  11. package/dist/baseline/errors.js +23 -0
  12. package/dist/baseline/errors.js.map +1 -0
  13. package/dist/baseline/fs.d.ts +9 -0
  14. package/dist/baseline/fs.d.ts.map +1 -0
  15. package/dist/baseline/fs.js +39 -0
  16. package/dist/baseline/fs.js.map +1 -0
  17. package/dist/baseline/index.d.ts +8 -0
  18. package/dist/baseline/index.d.ts.map +1 -0
  19. package/dist/baseline/index.js +24 -0
  20. package/dist/baseline/index.js.map +1 -0
  21. package/dist/baseline/mapping.d.ts +43 -0
  22. package/dist/baseline/mapping.d.ts.map +1 -0
  23. package/dist/baseline/mapping.js +80 -0
  24. package/dist/baseline/mapping.js.map +1 -0
  25. package/dist/baseline/normalise.d.ts +42 -0
  26. package/dist/baseline/normalise.d.ts.map +1 -0
  27. package/dist/baseline/normalise.js +133 -0
  28. package/dist/baseline/normalise.js.map +1 -0
  29. package/dist/baseline/plan.d.ts +66 -0
  30. package/dist/baseline/plan.d.ts.map +1 -0
  31. package/dist/baseline/plan.js +105 -0
  32. package/dist/baseline/plan.js.map +1 -0
  33. package/dist/baseline/report.d.ts +127 -0
  34. package/dist/baseline/report.d.ts.map +1 -0
  35. package/dist/baseline/report.js +89 -0
  36. package/dist/baseline/report.js.map +1 -0
  37. package/dist/baseline/run.d.ts +117 -0
  38. package/dist/baseline/run.d.ts.map +1 -0
  39. package/dist/baseline/run.js +358 -0
  40. package/dist/baseline/run.js.map +1 -0
  41. package/dist/bin/db-commands.d.ts +31 -0
  42. package/dist/bin/db-commands.d.ts.map +1 -0
  43. package/dist/bin/db-commands.js +311 -0
  44. package/dist/bin/db-commands.js.map +1 -0
  45. package/dist/bin/fs-io.d.ts +7 -0
  46. package/dist/bin/fs-io.d.ts.map +1 -0
  47. package/dist/bin/fs-io.js +45 -0
  48. package/dist/bin/fs-io.js.map +1 -0
  49. package/dist/bin/platform.d.ts +21 -0
  50. package/dist/bin/platform.d.ts.map +1 -0
  51. package/dist/bin/platform.js +99 -0
  52. package/dist/bin/platform.js.map +1 -0
  53. package/dist/compose/api.d.ts +84 -0
  54. package/dist/compose/api.d.ts.map +1 -0
  55. package/dist/compose/api.js +101 -0
  56. package/dist/compose/api.js.map +1 -0
  57. package/dist/compose/compose.d.ts +51 -0
  58. package/dist/compose/compose.d.ts.map +1 -0
  59. package/dist/compose/compose.js +177 -0
  60. package/dist/compose/compose.js.map +1 -0
  61. package/dist/compose/diff.d.ts +7 -0
  62. package/dist/compose/diff.d.ts.map +1 -0
  63. package/dist/compose/diff.js +68 -0
  64. package/dist/compose/diff.js.map +1 -0
  65. package/dist/compose/errors.d.ts +31 -0
  66. package/dist/compose/errors.d.ts.map +1 -0
  67. package/dist/compose/errors.js +35 -0
  68. package/dist/compose/errors.js.map +1 -0
  69. package/dist/compose/index.d.ts +3 -0
  70. package/dist/compose/index.d.ts.map +1 -0
  71. package/dist/compose/index.js +11 -0
  72. package/dist/compose/index.js.map +1 -0
  73. package/dist/compose/parse.d.ts +65 -0
  74. package/dist/compose/parse.d.ts.map +1 -0
  75. package/dist/compose/parse.js +122 -0
  76. package/dist/compose/parse.js.map +1 -0
  77. package/dist/drift/app-objects.d.ts +107 -0
  78. package/dist/drift/app-objects.d.ts.map +1 -0
  79. package/dist/drift/app-objects.js +193 -0
  80. package/dist/drift/app-objects.js.map +1 -0
  81. package/dist/drift/db-conformance.d.ts +52 -0
  82. package/dist/drift/db-conformance.d.ts.map +1 -0
  83. package/dist/drift/db-conformance.js +89 -0
  84. package/dist/drift/db-conformance.js.map +1 -0
  85. package/dist/drift/index.d.ts +8 -0
  86. package/dist/drift/index.d.ts.map +1 -0
  87. package/dist/drift/index.js +43 -0
  88. package/dist/drift/index.js.map +1 -0
  89. package/dist/drift/raw-sql-indexes.d.ts +106 -0
  90. package/dist/drift/raw-sql-indexes.d.ts.map +1 -0
  91. package/dist/drift/raw-sql-indexes.js +113 -0
  92. package/dist/drift/raw-sql-indexes.js.map +1 -0
  93. package/dist/drift/raw-sql-tripwire.d.ts +191 -0
  94. package/dist/drift/raw-sql-tripwire.d.ts.map +1 -0
  95. package/dist/drift/raw-sql-tripwire.js +422 -0
  96. package/dist/drift/raw-sql-tripwire.js.map +1 -0
  97. package/dist/drift/rls-policies.d.ts +226 -0
  98. package/dist/drift/rls-policies.d.ts.map +1 -0
  99. package/dist/drift/rls-policies.js +317 -0
  100. package/dist/drift/rls-policies.js.map +1 -0
  101. package/dist/drift/run.d.ts +152 -0
  102. package/dist/drift/run.d.ts.map +1 -0
  103. package/dist/drift/run.js +252 -0
  104. package/dist/drift/run.js.map +1 -0
  105. package/dist/drift/sql-expression.d.ts +27 -0
  106. package/dist/drift/sql-expression.d.ts.map +1 -0
  107. package/dist/drift/sql-expression.js +64 -0
  108. package/dist/drift/sql-expression.js.map +1 -0
  109. package/dist/index.d.ts +16 -0
  110. package/dist/index.d.ts.map +1 -0
  111. package/dist/index.js +33 -0
  112. package/dist/index.js.map +1 -0
  113. package/dist/lock/hash.d.ts +13 -0
  114. package/dist/lock/hash.d.ts.map +1 -0
  115. package/dist/lock/hash.js +19 -0
  116. package/dist/lock/hash.js.map +1 -0
  117. package/dist/lock/index.d.ts +5 -0
  118. package/dist/lock/index.d.ts.map +1 -0
  119. package/dist/lock/index.js +20 -0
  120. package/dist/lock/index.js.map +1 -0
  121. package/dist/lock/lock.d.ts +154 -0
  122. package/dist/lock/lock.d.ts.map +1 -0
  123. package/dist/lock/lock.js +151 -0
  124. package/dist/lock/lock.js.map +1 -0
  125. package/dist/lock/manifest.d.ts +68 -0
  126. package/dist/lock/manifest.d.ts.map +1 -0
  127. package/dist/lock/manifest.js +96 -0
  128. package/dist/lock/manifest.js.map +1 -0
  129. package/dist/lock/semver.d.ts +10 -0
  130. package/dist/lock/semver.d.ts.map +1 -0
  131. package/dist/lock/semver.js +13 -0
  132. package/dist/lock/semver.js.map +1 -0
  133. package/dist/seed/index.d.ts +5 -0
  134. package/dist/seed/index.d.ts.map +1 -0
  135. package/dist/seed/index.js +16 -0
  136. package/dist/seed/index.js.map +1 -0
  137. package/dist/seed/input.d.ts +52 -0
  138. package/dist/seed/input.d.ts.map +1 -0
  139. package/dist/seed/input.js +77 -0
  140. package/dist/seed/input.js.map +1 -0
  141. package/dist/seed/seed-platform.d.ts +41 -0
  142. package/dist/seed/seed-platform.d.ts.map +1 -0
  143. package/dist/seed/seed-platform.js +165 -0
  144. package/dist/seed/seed-platform.js.map +1 -0
  145. package/dist/seed/steps.d.ts +172 -0
  146. package/dist/seed/steps.d.ts.map +1 -0
  147. package/dist/seed/steps.js +136 -0
  148. package/dist/seed/steps.js.map +1 -0
  149. package/dist/seed/types.d.ts +287 -0
  150. package/dist/seed/types.d.ts.map +1 -0
  151. package/dist/seed/types.js +5 -0
  152. package/dist/seed/types.js.map +1 -0
  153. package/dist/sync/app-migrations.d.ts +52 -0
  154. package/dist/sync/app-migrations.d.ts.map +1 -0
  155. package/dist/sync/app-migrations.js +62 -0
  156. package/dist/sync/app-migrations.js.map +1 -0
  157. package/dist/sync/apply.d.ts +29 -0
  158. package/dist/sync/apply.d.ts.map +1 -0
  159. package/dist/sync/apply.js +38 -0
  160. package/dist/sync/apply.js.map +1 -0
  161. package/dist/sync/check.d.ts +59 -0
  162. package/dist/sync/check.d.ts.map +1 -0
  163. package/dist/sync/check.js +115 -0
  164. package/dist/sync/check.js.map +1 -0
  165. package/dist/sync/errors.d.ts +32 -0
  166. package/dist/sync/errors.d.ts.map +1 -0
  167. package/dist/sync/errors.js +23 -0
  168. package/dist/sync/errors.js.map +1 -0
  169. package/dist/sync/index.d.ts +10 -0
  170. package/dist/sync/index.d.ts.map +1 -0
  171. package/dist/sync/index.js +27 -0
  172. package/dist/sync/index.js.map +1 -0
  173. package/dist/sync/ledger.d.ts +65 -0
  174. package/dist/sync/ledger.d.ts.map +1 -0
  175. package/dist/sync/ledger.js +49 -0
  176. package/dist/sync/ledger.js.map +1 -0
  177. package/dist/sync/plan.d.ts +63 -0
  178. package/dist/sync/plan.d.ts.map +1 -0
  179. package/dist/sync/plan.js +86 -0
  180. package/dist/sync/plan.js.map +1 -0
  181. package/dist/sync/promote.d.ts +62 -0
  182. package/dist/sync/promote.d.ts.map +1 -0
  183. package/dist/sync/promote.js +95 -0
  184. package/dist/sync/promote.js.map +1 -0
  185. package/dist/sync/timestamps.d.ts +41 -0
  186. package/dist/sync/timestamps.d.ts.map +1 -0
  187. package/dist/sync/timestamps.js +68 -0
  188. package/dist/sync/timestamps.js.map +1 -0
  189. package/dist/sync/version.d.ts +28 -0
  190. package/dist/sync/version.d.ts.map +1 -0
  191. package/dist/sync/version.js +66 -0
  192. package/dist/sync/version.js.map +1 -0
  193. package/migrations/0001_initial/migration.sql +297 -0
  194. package/migrations/0002_add_personal_access_tokens/migration.sql +31 -0
  195. package/migrations/0003_add_credentials/migration.sql +20 -0
  196. package/migrations/0004_add_notification_deliveries/migration.sql +27 -0
  197. package/migrations/0005_drop_stale_uuid_defaults/migration.sql +35 -0
  198. package/migrations/0006_add_notifications/migration.sql +22 -0
  199. package/migrations/0007_add_push_subscriptions/migration.sql +25 -0
  200. package/migrations/0008_add_jobs/migration.sql +110 -0
  201. package/migrations/0009_add_worker_nodes/migration.sql +90 -0
  202. package/migrations/0010_add_database_backup_runs/migration.sql +104 -0
  203. package/migrations/0011_add_notification_broadcasts/migration.sql +57 -0
  204. package/migrations/0012_add_backup_run_job_link/migration.sql +87 -0
  205. package/migrations/0013_add_job_node_secrets/migration.sql +61 -0
  206. package/migrations/0014_add_backup_run_pg_dump_version/migration.sql +26 -0
  207. package/migrations/0015_add_job_claim_token/migration.sql +92 -0
  208. package/migrations/0016_add_ai_platform/migration.sql +108 -0
  209. package/migrations/0017_revoke_viewer_ai_use/migration.sql +32 -0
  210. package/migrations/0018_add_user_credentials/migration.sql +29 -0
  211. package/migrations/0019_add_device_session_credential_link/migration.sql +52 -0
  212. package/migrations/0020_add_worker_node_vitals/migration.sql +3 -0
  213. package/migrations/0021_add_job_trace_context/migration.sql +6 -0
  214. package/migrations/0022_add_retention_created_at_indexes/migration.sql +13 -0
  215. package/migrations/0023_add_organizations/migration.sql +142 -0
  216. package/migrations/0024_split_system_org_roles/migration.sql +131 -0
  217. package/migrations/0025_org_scoped_rls/migration.sql +215 -0
  218. package/migrations/0026_add_groups/migration.sql +165 -0
  219. package/migrations/0027_add_grants/migration.sql +117 -0
  220. package/migrations/0028_add_org_settings/migration.sql +49 -0
  221. package/migrations/0029_add_org_credentials/migration.sql +54 -0
  222. package/migrations/0030_add_jobs_org_id/migration.sql +18 -0
  223. package/migrations/0031_add_jobs_org_id_status_index/migration.sql +6 -0
  224. package/migrations/0032_add_broadcast_target_org/migration.sql +21 -0
  225. package/migrations/0033_add_android_app/migration.sql +60 -0
  226. package/migrations/0034_open_job_reason/migration.sql +16 -0
  227. package/migrations/0035_ai_usage_feature/migration.sql +37 -0
  228. package/migrations/0036_add_app_metadata/migration.sql +49 -0
  229. package/migrations/0037_open_sharing_vocabularies/migration.sql +97 -0
  230. package/migrations/manifest.json +379 -0
  231. package/package.json +78 -4
  232. package/raw-sql-indexes.json +94 -0
  233. package/rls-policies.json +74 -0
  234. package/schema/ai.prisma +444 -0
  235. package/schema/android-app.prisma +47 -0
  236. package/schema/base.prisma +11 -0
  237. package/schema/credentials.prisma +170 -0
  238. package/schema/db-backup.prisma +376 -0
  239. package/schema/identity.prisma +439 -0
  240. package/schema/jobs.prisma +760 -0
  241. package/schema/notifications.prisma +649 -0
  242. package/schema/settings.prisma +60 -0
  243. package/schema/sharing.prisma +223 -0
  244. package/schema/storage.prisma +84 -0
@@ -0,0 +1,252 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.loadPg = loadPg;
4
+ exports.prismaCli = prismaCli;
5
+ exports.schemaDiff = schemaDiff;
6
+ exports.withShadowDatabase = withShadowDatabase;
7
+ exports.stripBanner = stripBanner;
8
+ exports.readIndexRows = readIndexRows;
9
+ exports.readLedgerRows = readLedgerRows;
10
+ exports.runDrift = runDrift;
11
+ exports.readPolicyRows = readPolicyRows;
12
+ exports.readTableRlsRows = readTableRlsRows;
13
+ const node_child_process_1 = require("node:child_process");
14
+ const node_fs_1 = require("node:fs");
15
+ const node_module_1 = require("node:module");
16
+ const node_path_1 = require("node:path");
17
+ const raw_sql_indexes_js_1 = require("./raw-sql-indexes.js");
18
+ const rls_policies_js_1 = require("./rls-policies.js");
19
+ /**
20
+ * Resolves the `pg` client constructor from the app, falling back to this package's own copy.
21
+ *
22
+ * @internal
23
+ */
24
+ function loadPg(cwd) {
25
+ const req = (0, node_module_1.createRequire)((0, node_path_1.join)((0, node_path_1.resolve)(cwd), 'noop.js'));
26
+ try {
27
+ return req('pg').Client;
28
+ }
29
+ catch {
30
+ // Fall back to this package's own resolution (a workspace hoists it).
31
+ return (0, node_module_1.createRequire)(__filename)('pg').Client;
32
+ }
33
+ }
34
+ function withDatabase(url, database) {
35
+ const parsed = new URL(url);
36
+ parsed.pathname = `/${database}`;
37
+ return parsed.toString();
38
+ }
39
+ const databaseOf = (url) => decodeURIComponent(new URL(url).pathname.replace(/^\//, ''));
40
+ const quoteIdent = (name) => `"${name.replace(/"/g, '""')}"`;
41
+ /**
42
+ * The path of the `prisma` CLI entry point resolved from the app.
43
+ *
44
+ * @internal
45
+ */
46
+ function prismaCli(cwd) {
47
+ const req = (0, node_module_1.createRequire)((0, node_path_1.join)((0, node_path_1.resolve)(cwd), 'noop.js'));
48
+ const found = req.resolve('prisma/build/index.js');
49
+ if (!(0, node_fs_1.existsSync)(found))
50
+ throw new Error('the prisma CLI was not found; install `prisma` in the app');
51
+ return found;
52
+ }
53
+ /**
54
+ * Runs `prisma migrate diff` from the migration history to the composed
55
+ * schema, with `--exit-code` and `--script`. The diff replays the history in a
56
+ * shadow database, so it needs one: a supplied URL, or a throwaway database next to the live
57
+ * one that is dropped afterwards.
58
+ *
59
+ * @param options - See {@link DriftOptions}.
60
+ * @returns The SQL of the difference; empty when the history equals the schema.
61
+ * @throws Error when Prisma fails for a reason other than a difference.
62
+ * @stability experimental
63
+ */
64
+ async function schemaDiff(options) {
65
+ return withShadowDatabase(options, async (shadowUrl) => {
66
+ const result = (0, node_child_process_1.spawnSync)(process.execPath, [
67
+ prismaCli(options.cwd),
68
+ 'migrate',
69
+ 'diff',
70
+ '--from-migrations',
71
+ options.migrations,
72
+ '--to-schema',
73
+ options.schema,
74
+ '--exit-code',
75
+ '--script',
76
+ ], {
77
+ cwd: options.cwd,
78
+ env: { ...process.env, DATABASE_URL: options.databaseUrl, SHADOW_DATABASE_URL: shadowUrl },
79
+ encoding: 'utf8',
80
+ maxBuffer: 64 * 1024 * 1024,
81
+ });
82
+ if (result.error)
83
+ throw result.error;
84
+ if (result.status === 0)
85
+ return '';
86
+ if (result.status === 2)
87
+ return stripBanner(result.stdout);
88
+ throw new Error(`prisma migrate diff failed (exit ${result.status}): ${(result.stderr || result.stdout).trim()}`);
89
+ });
90
+ }
91
+ /**
92
+ * Runs `body` with a shadow database URL: the supplied one, or a throwaway
93
+ * database created next to the live one (through the `postgres` maintenance
94
+ * database) and dropped afterwards. The live database is never touched.
95
+ *
96
+ * @param options - `cwd` (where `pg` resolves from), the live `databaseUrl` and an optional `shadowDatabaseUrl`.
97
+ * @param body - Receives the shadow database URL.
98
+ * @returns What `body` returns.
99
+ * @internal
100
+ */
101
+ async function withShadowDatabase(options, body) {
102
+ const Client = loadPg(options.cwd);
103
+ let shadowUrl = options.shadowDatabaseUrl;
104
+ let created;
105
+ if (!shadowUrl) {
106
+ const name = `${databaseOf(options.databaseUrl)}_drift_shadow_${process.pid}`;
107
+ const admin = new Client({ connectionString: withDatabase(options.databaseUrl, 'postgres') });
108
+ await admin.connect();
109
+ await admin.query(`DROP DATABASE IF EXISTS ${quoteIdent(name)} WITH (FORCE)`);
110
+ await admin.query(`CREATE DATABASE ${quoteIdent(name)}`);
111
+ created = { admin, name };
112
+ shadowUrl = withDatabase(options.databaseUrl, name);
113
+ }
114
+ try {
115
+ return await body(shadowUrl);
116
+ }
117
+ finally {
118
+ if (created) {
119
+ await created.admin.query(`DROP DATABASE IF EXISTS ${quoteIdent(created.name)} WITH (FORCE)`);
120
+ await created.admin.end();
121
+ }
122
+ }
123
+ }
124
+ /**
125
+ * Removes the Prisma CLI's banner and environment-loader lines from its output.
126
+ *
127
+ * @internal
128
+ */
129
+ function stripBanner(stdout) {
130
+ return stdout
131
+ .split('\n')
132
+ .filter((line) => !/^(Loaded Prisma config|Prisma schema loaded|Datasource |◇ injected env|\[dotenv)/.test(line))
133
+ .join('\n')
134
+ .trim();
135
+ }
136
+ /**
137
+ * Reads every index of the `public` schema as `name` and `indexdef`.
138
+ *
139
+ * @param databaseUrl - Connection string.
140
+ * @param cwd - Where to resolve `pg` from.
141
+ * @returns The rows of `pg_indexes`.
142
+ * @stability experimental
143
+ */
144
+ async function readIndexRows(databaseUrl, cwd) {
145
+ const Client = loadPg(cwd);
146
+ const client = new Client({ connectionString: databaseUrl });
147
+ await client.connect();
148
+ try {
149
+ const { rows } = await client.query("SELECT indexname AS name, indexdef AS definition FROM pg_indexes WHERE schemaname = 'public' ORDER BY indexname");
150
+ return rows.map((row) => ({ name: String(row.name), definition: String(row.definition) }));
151
+ }
152
+ finally {
153
+ await client.end();
154
+ }
155
+ }
156
+ /**
157
+ * Reads Prisma's `_prisma_migrations` ledger, oldest first. A database Prisma
158
+ * never migrated has no such table and yields no rows.
159
+ *
160
+ * @param databaseUrl - Connection string.
161
+ * @param cwd - Where to resolve `pg` from.
162
+ * @returns The ledger rows.
163
+ * @stability experimental
164
+ */
165
+ async function readLedgerRows(databaseUrl, cwd) {
166
+ const Client = loadPg(cwd);
167
+ const client = new Client({ connectionString: databaseUrl });
168
+ await client.connect();
169
+ try {
170
+ const { rows } = await client.query('SELECT migration_name, checksum, finished_at, rolled_back_at FROM _prisma_migrations ORDER BY started_at, migration_name');
171
+ return rows.map((row) => ({
172
+ migrationName: String(row.migration_name),
173
+ checksum: String(row.checksum),
174
+ finishedAt: row.finished_at ? new Date(row.finished_at) : null,
175
+ rolledBackAt: row.rolled_back_at ? new Date(row.rolled_back_at) : null,
176
+ }));
177
+ }
178
+ catch (error) {
179
+ if (error.code === '42P01')
180
+ return [];
181
+ throw error;
182
+ }
183
+ finally {
184
+ await client.end();
185
+ }
186
+ }
187
+ /**
188
+ * The CI drift test: the migration history replayed in a shadow database must
189
+ * equal the composed schema (`prisma migrate diff --exit-code`), and every
190
+ * raw-SQL index must exist with its recorded definition. There is no
191
+ * allow-list over the diff output: Prisma ignores partial and expression
192
+ * indexes in both directions, so the indexes are asserted positively instead.
193
+ *
194
+ * @param options - See {@link DriftOptions}.
195
+ * @returns The diff SQL and the index problems; `ok` when both are empty.
196
+ * @stability experimental
197
+ */
198
+ async function runDrift(options) {
199
+ const diff = await schemaDiff(options);
200
+ const live = await readIndexRows(options.databaseUrl, options.cwd);
201
+ const indexProblems = (0, raw_sql_indexes_js_1.checkRawSqlIndexes)(options.rawSqlIndexes, live);
202
+ let policyProblems = [];
203
+ if (options.rlsPolicies) {
204
+ const policies = await readPolicyRows(options.databaseUrl, options.cwd);
205
+ const tables = await readTableRlsRows(options.databaseUrl, options.cwd);
206
+ policyProblems = (0, rls_policies_js_1.checkRlsPolicies)(options.rlsPolicies, policies, tables);
207
+ }
208
+ return { ok: diff === '' && indexProblems.length === 0 && policyProblems.length === 0, schemaDiff: diff, indexProblems, policyProblems };
209
+ }
210
+ const isTrue = (value) => value === true || value === 't' || value === 'true';
211
+ /**
212
+ * Reads every policy of the `public` schema (`pg_policies`).
213
+ *
214
+ * @param databaseUrl - Connection string.
215
+ * @param cwd - Where to resolve `pg` from.
216
+ * @returns The table and name of each policy.
217
+ * @stability experimental
218
+ */
219
+ async function readPolicyRows(databaseUrl, cwd) {
220
+ const Client = loadPg(cwd);
221
+ const client = new Client({ connectionString: databaseUrl });
222
+ await client.connect();
223
+ try {
224
+ const { rows } = await client.query("SELECT tablename AS tbl, policyname AS name FROM pg_policies WHERE schemaname = 'public' ORDER BY tablename, policyname");
225
+ return rows.map((row) => ({ table: String(row.tbl), name: String(row.name) }));
226
+ }
227
+ finally {
228
+ await client.end();
229
+ }
230
+ }
231
+ /**
232
+ * Reads the row-level-security flags of every ordinary table of the `public`
233
+ * schema (`pg_class.relrowsecurity`, `relforcerowsecurity`).
234
+ *
235
+ * @param databaseUrl - Connection string.
236
+ * @param cwd - Where to resolve `pg` from.
237
+ * @returns One row per table.
238
+ * @stability experimental
239
+ */
240
+ async function readTableRlsRows(databaseUrl, cwd) {
241
+ const Client = loadPg(cwd);
242
+ const client = new Client({ connectionString: databaseUrl });
243
+ await client.connect();
244
+ try {
245
+ const { rows } = await client.query("SELECT c.relname AS tbl, c.relrowsecurity AS enabled, c.relforcerowsecurity AS forced FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace WHERE n.nspname = 'public' AND c.relkind IN ('r', 'p') ORDER BY c.relname");
246
+ return rows.map((row) => ({ table: String(row.tbl), enabled: isTrue(row.enabled), forced: isTrue(row.forced) }));
247
+ }
248
+ finally {
249
+ await client.end();
250
+ }
251
+ }
252
+ //# sourceMappingURL=run.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"run.js","sourceRoot":"","sources":["../../src/drift/run.ts"],"names":[],"mappings":";;AAgEA,wBAQC;AAgBD,8BAKC;AAaD,gCA2BC;AAYD,gDAwBC;AAOD,kCAMC;AAUD,sCAUC;AAWD,wCAoBC;AAaD,4BAWC;AA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AAO,EAAE,CAAC;IACvB,IAAI,CAAC;QACH,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,MAAM,CAAC,KAAK,CACjC,+NAA+N,CAChO,CAAC;QACF,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,OAAO,EAAE,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,CAAC;IACnH,CAAC;YAAS,CAAC;QACT,MAAM,MAAM,CAAC,GAAG,EAAE,CAAC;IACrB,CAAC;AACH,CAAC"}
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Normalises a SQL expression so two spellings of the same policy compare
3
+ * equal: runs of whitespace collapse to one space, the space inside `(` and
4
+ * `)` and around `,` goes, and everything outside a string literal or a quoted
5
+ * identifier is lower-cased. The text inside quotes is kept byte for byte.
6
+ * Comments are not removed here: {@link scanRlsPolicies} reads statements that
7
+ * already lost theirs.
8
+ *
9
+ * It is a drift marker, not a SQL parser: it never reorders or simplifies, so
10
+ * `a AND b` and `b AND a` stay different, and so do `(x)` and `((x))`.
11
+ *
12
+ * @param expression - The text of a `USING (...)` or `WITH CHECK (...)` expression, without the outer parentheses.
13
+ * @returns The normalised text.
14
+ * @stability experimental
15
+ */
16
+ export declare function normaliseSqlExpression(expression: string): string;
17
+ /**
18
+ * The SHA-256 (lower-case hex) of {@link normaliseSqlExpression} of an
19
+ * expression: the value a `platform.lock` `rlsPolicies` entry records as
20
+ * `usingSha256` or `checkSha256`.
21
+ *
22
+ * @param expression - The text of a `USING (...)` or `WITH CHECK (...)` expression, without the outer parentheses.
23
+ * @returns 64 lower-case hex characters.
24
+ * @stability experimental
25
+ */
26
+ export declare function sqlExpressionSha256(expression: string): string;
27
+ //# sourceMappingURL=sql-expression.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sql-expression.d.ts","sourceRoot":"","sources":["../../src/drift/sql-expression.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,sBAAsB,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,CA2BjE;AAED;;;;;;;;GAQG;AACH,wBAAgB,mBAAmB,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,CAE9D"}
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.normaliseSqlExpression = normaliseSqlExpression;
4
+ exports.sqlExpressionSha256 = sqlExpressionSha256;
5
+ const index_js_1 = require("../lock/index.js");
6
+ /**
7
+ * Normalises a SQL expression so two spellings of the same policy compare
8
+ * equal: runs of whitespace collapse to one space, the space inside `(` and
9
+ * `)` and around `,` goes, and everything outside a string literal or a quoted
10
+ * identifier is lower-cased. The text inside quotes is kept byte for byte.
11
+ * Comments are not removed here: {@link scanRlsPolicies} reads statements that
12
+ * already lost theirs.
13
+ *
14
+ * It is a drift marker, not a SQL parser: it never reorders or simplifies, so
15
+ * `a AND b` and `b AND a` stay different, and so do `(x)` and `((x))`.
16
+ *
17
+ * @param expression - The text of a `USING (...)` or `WITH CHECK (...)` expression, without the outer parentheses.
18
+ * @returns The normalised text.
19
+ * @stability experimental
20
+ */
21
+ function normaliseSqlExpression(expression) {
22
+ let out = '';
23
+ let quote = '';
24
+ let pendingSpace = false;
25
+ for (const c of expression) {
26
+ if (quote) {
27
+ out += c;
28
+ if (c === quote)
29
+ quote = '';
30
+ continue;
31
+ }
32
+ if (/\s/.test(c)) {
33
+ pendingSpace = out !== '';
34
+ continue;
35
+ }
36
+ if (pendingSpace) {
37
+ const last = out[out.length - 1];
38
+ if (last !== '(' && last !== ',' && c !== ')' && c !== ',')
39
+ out += ' ';
40
+ pendingSpace = false;
41
+ }
42
+ if (c === "'" || c === '"') {
43
+ quote = c;
44
+ out += c;
45
+ }
46
+ else {
47
+ out += c.toLowerCase();
48
+ }
49
+ }
50
+ return out;
51
+ }
52
+ /**
53
+ * The SHA-256 (lower-case hex) of {@link normaliseSqlExpression} of an
54
+ * expression: the value a `platform.lock` `rlsPolicies` entry records as
55
+ * `usingSha256` or `checkSha256`.
56
+ *
57
+ * @param expression - The text of a `USING (...)` or `WITH CHECK (...)` expression, without the outer parentheses.
58
+ * @returns 64 lower-case hex characters.
59
+ * @stability experimental
60
+ */
61
+ function sqlExpressionSha256(expression) {
62
+ return (0, index_js_1.sha256Hex)(normaliseSqlExpression(expression));
63
+ }
64
+ //# sourceMappingURL=sql-expression.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sql-expression.js","sourceRoot":"","sources":["../../src/drift/sql-expression.ts"],"names":[],"mappings":";;AAiBA,wDA2BC;AAWD,kDAEC;AAzDD,+CAA6C;AAE7C;;;;;;;;;;;;;;GAcG;AACH,SAAgB,sBAAsB,CAAC,UAAkB;IACvD,IAAI,GAAG,GAAG,EAAE,CAAC;IACb,IAAI,KAAK,GAAG,EAAE,CAAC;IACf,IAAI,YAAY,GAAG,KAAK,CAAC;IACzB,KAAK,MAAM,CAAC,IAAI,UAAU,EAAE,CAAC;QAC3B,IAAI,KAAK,EAAE,CAAC;YACV,GAAG,IAAI,CAAC,CAAC;YACT,IAAI,CAAC,KAAK,KAAK;gBAAE,KAAK,GAAG,EAAE,CAAC;YAC5B,SAAS;QACX,CAAC;QACD,IAAI,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC;YACjB,YAAY,GAAG,GAAG,KAAK,EAAE,CAAC;YAC1B,SAAS;QACX,CAAC;QACD,IAAI,YAAY,EAAE,CAAC;YACjB,MAAM,IAAI,GAAG,GAAG,CAAC,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;YACjC,IAAI,IAAI,KAAK,GAAG,IAAI,IAAI,KAAK,GAAG,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,GAAG;gBAAE,GAAG,IAAI,GAAG,CAAC;YACvE,YAAY,GAAG,KAAK,CAAC;QACvB,CAAC;QACD,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,GAAG,EAAE,CAAC;YAC3B,KAAK,GAAG,CAAC,CAAC;YACV,GAAG,IAAI,CAAC,CAAC;QACX,CAAC;aAAM,CAAC;YACN,GAAG,IAAI,CAAC,CAAC,WAAW,EAAE,CAAC;QACzB,CAAC;IACH,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC;AAED;;;;;;;;GAQG;AACH,SAAgB,mBAAmB,CAAC,UAAkB;IACpD,OAAO,IAAA,oBAAS,EAAC,sBAAsB,CAAC,UAAU,CAAC,CAAC,CAAC;AACvD,CAAC"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * The npm name of this package, which holds Prisma schema fragments, SQL migrations and seed functions of the platform.
3
+ *
4
+ * A placeholder export so the build, the pack check and the smoke imports in
5
+ * CI have something to load end to end. Real slices arrive as subpath
6
+ * exports (`@marinoscar/platform-db/<slice>`).
7
+ *
8
+ * @stability experimental
9
+ */
10
+ export declare const PLATFORM_PACKAGE: "@marinoscar/platform-db";
11
+ export * from './compose/index.js';
12
+ export * from './lock/index.js';
13
+ export * from './sync/index.js';
14
+ export * from './drift/index.js';
15
+ export * from './baseline/index.js';
16
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,eAAO,MAAM,gBAAgB,EAAG,yBAAkC,CAAC;AAEnE,cAAc,oBAAoB,CAAC;AACnC,cAAc,iBAAiB,CAAC;AAChC,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,qBAAqB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.PLATFORM_PACKAGE = void 0;
18
+ /**
19
+ * The npm name of this package, which holds Prisma schema fragments, SQL migrations and seed functions of the platform.
20
+ *
21
+ * A placeholder export so the build, the pack check and the smoke imports in
22
+ * CI have something to load end to end. Real slices arrive as subpath
23
+ * exports (`@marinoscar/platform-db/<slice>`).
24
+ *
25
+ * @stability experimental
26
+ */
27
+ exports.PLATFORM_PACKAGE = '@marinoscar/platform-db';
28
+ __exportStar(require("./compose/index.js"), exports);
29
+ __exportStar(require("./lock/index.js"), exports);
30
+ __exportStar(require("./sync/index.js"), exports);
31
+ __exportStar(require("./drift/index.js"), exports);
32
+ __exportStar(require("./baseline/index.js"), exports);
33
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA;;;;;;;;GAQG;AACU,QAAA,gBAAgB,GAAG,yBAAkC,CAAC;AAEnE,qDAAmC;AACnC,kDAAgC;AAChC,kDAAgC;AAChC,mDAAiC;AACjC,sDAAoC"}
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Lower-case hex SHA-256 of raw bytes.
3
+ *
4
+ * Migration files are hashed as bytes and never normalised: Prisma checksums
5
+ * the raw file, so a changed comment, a trailing newline or a line ending is a
6
+ * different migration.
7
+ *
8
+ * @param bytes - The file contents, exactly as stored.
9
+ * @returns 64 lower-case hex characters.
10
+ * @stability experimental
11
+ */
12
+ export declare function sha256Hex(bytes: Uint8Array | string): string;
13
+ //# sourceMappingURL=hash.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hash.d.ts","sourceRoot":"","sources":["../../src/lock/hash.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;GAUG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,GAAG,MAAM,CAE5D"}
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sha256Hex = sha256Hex;
4
+ const node_crypto_1 = require("node:crypto");
5
+ /**
6
+ * Lower-case hex SHA-256 of raw bytes.
7
+ *
8
+ * Migration files are hashed as bytes and never normalised: Prisma checksums
9
+ * the raw file, so a changed comment, a trailing newline or a line ending is a
10
+ * different migration.
11
+ *
12
+ * @param bytes - The file contents, exactly as stored.
13
+ * @returns 64 lower-case hex characters.
14
+ * @stability experimental
15
+ */
16
+ function sha256Hex(bytes) {
17
+ return (0, node_crypto_1.createHash)('sha256').update(bytes).digest('hex');
18
+ }
19
+ //# sourceMappingURL=hash.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hash.js","sourceRoot":"","sources":["../../src/lock/hash.ts"],"names":[],"mappings":";;AAaA,8BAEC;AAfD,6CAAyC;AAEzC;;;;;;;;;;GAUG;AACH,SAAgB,SAAS,CAAC,KAA0B;IAClD,OAAO,IAAA,wBAAU,EAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAC1D,CAAC"}
@@ -0,0 +1,5 @@
1
+ export { sha256Hex } from './hash.js';
2
+ export { SEMVER_PATTERN } from './semver.js';
3
+ export { LockFormatError, emptyLock, parseLock, readLock, serializeJson, serializeLock, type LockDeviation, type LockEntry, type LockRlsPolicy, type PlatformLock, type RawSqlIndex, } from './lock.js';
4
+ export { ManifestFormatError, originIdOf, parseManifest, readManifest, type ManifestEntry } from './manifest.js';
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/lock/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,EACL,eAAe,EACf,SAAS,EACT,SAAS,EACT,QAAQ,EACR,aAAa,EACb,aAAa,EACb,KAAK,aAAa,EAClB,KAAK,SAAS,EACd,KAAK,aAAa,EAClB,KAAK,YAAY,EACjB,KAAK,WAAW,GACjB,MAAM,WAAW,CAAC;AACnB,OAAO,EAAE,mBAAmB,EAAE,UAAU,EAAE,aAAa,EAAE,YAAY,EAAE,KAAK,aAAa,EAAE,MAAM,eAAe,CAAC"}
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.readManifest = exports.parseManifest = exports.originIdOf = exports.ManifestFormatError = exports.serializeLock = exports.serializeJson = exports.readLock = exports.parseLock = exports.emptyLock = exports.LockFormatError = exports.SEMVER_PATTERN = exports.sha256Hex = void 0;
4
+ var hash_js_1 = require("./hash.js");
5
+ Object.defineProperty(exports, "sha256Hex", { enumerable: true, get: function () { return hash_js_1.sha256Hex; } });
6
+ var semver_js_1 = require("./semver.js");
7
+ Object.defineProperty(exports, "SEMVER_PATTERN", { enumerable: true, get: function () { return semver_js_1.SEMVER_PATTERN; } });
8
+ var lock_js_1 = require("./lock.js");
9
+ Object.defineProperty(exports, "LockFormatError", { enumerable: true, get: function () { return lock_js_1.LockFormatError; } });
10
+ Object.defineProperty(exports, "emptyLock", { enumerable: true, get: function () { return lock_js_1.emptyLock; } });
11
+ Object.defineProperty(exports, "parseLock", { enumerable: true, get: function () { return lock_js_1.parseLock; } });
12
+ Object.defineProperty(exports, "readLock", { enumerable: true, get: function () { return lock_js_1.readLock; } });
13
+ Object.defineProperty(exports, "serializeJson", { enumerable: true, get: function () { return lock_js_1.serializeJson; } });
14
+ Object.defineProperty(exports, "serializeLock", { enumerable: true, get: function () { return lock_js_1.serializeLock; } });
15
+ var manifest_js_1 = require("./manifest.js");
16
+ Object.defineProperty(exports, "ManifestFormatError", { enumerable: true, get: function () { return manifest_js_1.ManifestFormatError; } });
17
+ Object.defineProperty(exports, "originIdOf", { enumerable: true, get: function () { return manifest_js_1.originIdOf; } });
18
+ Object.defineProperty(exports, "parseManifest", { enumerable: true, get: function () { return manifest_js_1.parseManifest; } });
19
+ Object.defineProperty(exports, "readManifest", { enumerable: true, get: function () { return manifest_js_1.readManifest; } });
20
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/lock/index.ts"],"names":[],"mappings":";;;AAAA,qCAAsC;AAA7B,oGAAA,SAAS,OAAA;AAClB,yCAA6C;AAApC,2GAAA,cAAc,OAAA;AACvB,qCAYmB;AAXjB,0GAAA,eAAe,OAAA;AACf,oGAAA,SAAS,OAAA;AACT,oGAAA,SAAS,OAAA;AACT,mGAAA,QAAQ,OAAA;AACR,wGAAA,aAAa,OAAA;AACb,wGAAA,aAAa,OAAA;AAOf,6CAAiH;AAAxG,kHAAA,mBAAmB,OAAA;AAAE,yGAAA,UAAU,OAAA;AAAE,4GAAA,aAAa,OAAA;AAAE,2GAAA,YAAY,OAAA"}
@@ -0,0 +1,154 @@
1
+ /**
2
+ * One installed (or mapped) platform migration in the app's `platform.lock`.
3
+ *
4
+ * @stability experimental
5
+ */
6
+ export interface LockEntry {
7
+ /** `platform:` plus the package directory, for example `platform:0001_initial`. */
8
+ originId: string;
9
+ /** The app's directory name under `prisma/migrations`; it may differ from the package slug. */
10
+ localDir: string;
11
+ /** SHA-256 of the package file's bytes; equals Prisma's `_prisma_migrations.checksum` for a byte copy. */
12
+ sha256: string;
13
+ /** The platform version that introduced the migration for this app. */
14
+ since: string;
15
+ /** SHA-256 of the app's file, present only for a recorded comment-only divergence. */
16
+ localSha256?: string;
17
+ /** Why the app's file differs from the package's; present with `localSha256`. */
18
+ note?: string;
19
+ }
20
+ /**
21
+ * The app's declared alteration of a package-owned table (read by the baseline tooling).
22
+ *
23
+ * @stability experimental
24
+ */
25
+ export interface LockDeviation {
26
+ /** Stable id, `<app>:<name>`. */
27
+ id: string;
28
+ /** Why the app alters a package-owned table. */
29
+ reason: string;
30
+ /** The normalised statements `prisma migrate diff` is expected to print for it. */
31
+ expectDiff: string[];
32
+ }
33
+ /**
34
+ * A raw-SQL index the app owns, asserted by the drift test against `pg_indexes`.
35
+ *
36
+ * @stability experimental
37
+ */
38
+ export interface RawSqlIndex {
39
+ /** The index name. */
40
+ name: string;
41
+ /** `pg_indexes.indexdef` of the index. */
42
+ definition: string;
43
+ }
44
+ /**
45
+ * The contents of `prisma/platform.lock`: proof that the app's installed
46
+ * migrations are byte-identical to the package's.
47
+ *
48
+ * @stability experimental
49
+ */
50
+ export interface PlatformLock {
51
+ /** The lock format version; a reader refuses a higher value. */
52
+ lockVersion: 1;
53
+ /** The package version the history was last synced to. */
54
+ platformVersion: string;
55
+ /** One entry per installed or mapped package migration, in package order. */
56
+ migrations: LockEntry[];
57
+ /** Declared alterations of package-owned tables. */
58
+ deviations?: LockDeviation[];
59
+ /** The app's own raw-SQL indexes. */
60
+ rawSqlIndexes?: RawSqlIndex[];
61
+ /**
62
+ * The app's own row-level-security policies, asserted against `pg_policies`
63
+ * by `platform db drift` and against the migration SQL by the offline
64
+ * `runDbConformance` check, like the package's.
65
+ */
66
+ rlsPolicies?: LockRlsPolicy[];
67
+ /**
68
+ * The app-authored migration directories that existed when `platform db sync`
69
+ * (or `promote`) last wrote this lock. The offline check reads it to tell an
70
+ * app migration that predates an installed platform migration (it was there
71
+ * when the platform migration was installed, so it sorts before it by design)
72
+ * from one that was added afterwards under a stale timestamp, which would
73
+ * reorder history. Absent in a lock written before the field existed: the
74
+ * ordering check is then skipped until the next sync.
75
+ */
76
+ appMigrations?: string[];
77
+ }
78
+ /**
79
+ * A row-level-security policy the app owns, listed in `platform.lock`.
80
+ *
81
+ * @stability experimental
82
+ */
83
+ export interface LockRlsPolicy {
84
+ /** The policy name, as `pg_policies.policyname` reports it. */
85
+ name: string;
86
+ /** The table the policy is defined on, as `pg_policies.tablename` reports it. */
87
+ table: string;
88
+ /**
89
+ * SHA-256 of the policy's `USING` expression, normalised by
90
+ * {@link normaliseSqlExpression} (whitespace collapsed, case folded outside
91
+ * quotes). Optional: when present, the offline check fails if the migration
92
+ * SQL leaves a different expression, so a weakened policy is caught without
93
+ * a database.
94
+ */
95
+ usingSha256?: string;
96
+ /** SHA-256 of the policy's `WITH CHECK` expression, normalised the same way; optional. */
97
+ checkSha256?: string;
98
+ }
99
+ /**
100
+ * A lock file that failed validation, with the offending path.
101
+ *
102
+ * @stability experimental
103
+ */
104
+ export declare class LockFormatError extends Error {
105
+ /** @param message - What is wrong with the file. */
106
+ constructor(message: string);
107
+ }
108
+ /**
109
+ * Parses and validates the text of a `platform.lock`.
110
+ *
111
+ * @param text - The file contents.
112
+ * @param source - A label for error messages (usually the path).
113
+ * @returns The validated lock.
114
+ * @throws LockFormatError when the JSON is invalid or does not match the schema, or the lock version is newer than this release reads.
115
+ * @stability experimental
116
+ */
117
+ export declare function parseLock(text: string, source?: string): PlatformLock;
118
+ /**
119
+ * Reads and validates a `platform.lock`.
120
+ *
121
+ * @param file - Path of the lock file.
122
+ * @returns The validated lock.
123
+ * @throws LockFormatError when the file is invalid (see {@link parseLock}).
124
+ * @stability experimental
125
+ */
126
+ export declare function readLock(file: string): PlatformLock;
127
+ /**
128
+ * An empty lock for an app that has installed nothing.
129
+ *
130
+ * @param platformVersion - The package version, default `0.0.0`.
131
+ * @returns A lock with no migrations.
132
+ * @stability experimental
133
+ */
134
+ export declare function emptyLock(platformVersion?: string): PlatformLock;
135
+ /**
136
+ * Serialises a lock (or any manifest-shaped JSON) deterministically: keys
137
+ * sorted at every level, arrays in their given order, two-space indent, one
138
+ * trailing newline. The same input always produces the same bytes, so diffs
139
+ * stay stable.
140
+ *
141
+ * @param value - The lock, or any JSON value.
142
+ * @returns The file contents.
143
+ * @stability experimental
144
+ */
145
+ export declare function serializeJson(value: unknown): string;
146
+ /**
147
+ * Serialises a lock for writing to `platform.lock`.
148
+ *
149
+ * @param lock - The lock.
150
+ * @returns The file contents (see {@link serializeJson}).
151
+ * @stability experimental
152
+ */
153
+ export declare function serializeLock(lock: PlatformLock): string;
154
+ //# sourceMappingURL=lock.d.ts.map