@balena/pinejs 15.0.0-delete-state-default-user-permissions-ba0732a0c5d0da9d1d5be818cb08cc898e86ebe3 → 15.0.0-delete-state-default-user-permissions-3639cb4f82f3d7c9636142a31f4df182080cef3f
Sign up to get free protection for your applications and to get access to all the features.
- package/.versionbot/CHANGELOG.yml +32 -2
- package/CHANGELOG.md +11 -1
- package/out/config-loader/config-loader.d.ts +2 -4
- package/out/config-loader/config-loader.js +34 -12
- package/out/config-loader/config-loader.js.map +1 -1
- package/out/config-loader/env.d.ts +1 -1
- package/out/config-loader/env.js.map +1 -1
- package/out/migrator/sync.d.ts +9 -0
- package/out/migrator/sync.js +121 -0
- package/out/migrator/sync.js.map +1 -0
- package/out/migrator/utils.d.ts +28 -0
- package/out/migrator/utils.js +105 -0
- package/out/migrator/utils.js.map +1 -0
- package/out/sbvr-api/permissions.d.ts +5 -4
- package/out/sbvr-api/permissions.js +20 -18
- package/out/sbvr-api/permissions.js.map +1 -1
- package/out/sbvr-api/sbvr-utils.js +3 -3
- package/out/sbvr-api/sbvr-utils.js.map +1 -1
- package/out/server-glue/module.d.ts +2 -2
- package/out/server-glue/module.js +2 -1
- package/out/server-glue/module.js.map +1 -1
- package/package.json +2 -2
- package/src/config-loader/config-loader.ts +64 -18
- package/src/config-loader/env.ts +3 -1
- package/src/migrator/sync.ts +171 -0
- package/src/migrator/utils.ts +160 -0
- package/src/sbvr-api/permissions.ts +44 -20
- package/src/sbvr-api/sbvr-utils.ts +3 -3
- package/src/server-glue/module.ts +3 -2
- package/out/migrator/migrator.d.ts +0 -17
- package/out/migrator/migrator.js +0 -185
- package/out/migrator/migrator.js.map +0 -1
- package/src/migrator/migrator.ts +0 -278
@@ -1,17 +0,0 @@
|
|
1
|
-
import type { Tx } from '../database-layer/db';
|
2
|
-
import type { Resolvable } from '../sbvr-api/common-types';
|
3
|
-
import type { Config, Model } from '../config-loader/config-loader';
|
4
|
-
import { TypedError } from 'typed-error';
|
5
|
-
import * as sbvrUtils from '../sbvr-api/sbvr-utils';
|
6
|
-
declare type ApiRootModel = Model & {
|
7
|
-
apiRoot: string;
|
8
|
-
};
|
9
|
-
declare type SbvrUtils = typeof sbvrUtils;
|
10
|
-
export declare type MigrationFn = (tx: Tx, sbvrUtils: SbvrUtils) => Resolvable<void>;
|
11
|
-
export declare type Migration = string | MigrationFn;
|
12
|
-
export declare class MigrationError extends TypedError {
|
13
|
-
}
|
14
|
-
export declare const postRun: (tx: Tx, model: ApiRootModel) => Promise<void>;
|
15
|
-
export declare const run: (tx: Tx, model: ApiRootModel) => Promise<void>;
|
16
|
-
export declare const config: Config;
|
17
|
-
export {};
|
package/out/migrator/migrator.js
DELETED
@@ -1,185 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.config = exports.run = exports.postRun = exports.MigrationError = void 0;
|
4
|
-
const _ = require("lodash");
|
5
|
-
const typed_error_1 = require("typed-error");
|
6
|
-
const env_1 = require("../config-loader/env");
|
7
|
-
const sbvrUtils = require("../sbvr-api/sbvr-utils");
|
8
|
-
const control_flow_1 = require("../sbvr-api/control-flow");
|
9
|
-
const modelText = require('./migrations.sbvr');
|
10
|
-
class MigrationError extends typed_error_1.TypedError {
|
11
|
-
}
|
12
|
-
exports.MigrationError = MigrationError;
|
13
|
-
const binds = (strings, ...bindNums) => strings
|
14
|
-
.map((str, i) => {
|
15
|
-
if (i === bindNums.length) {
|
16
|
-
return str;
|
17
|
-
}
|
18
|
-
if (i + 1 !== bindNums[i]) {
|
19
|
-
throw new SyntaxError('Migration sql binds must be sequential');
|
20
|
-
}
|
21
|
-
if (sbvrUtils.db.engine === "postgres") {
|
22
|
-
return str + `$${bindNums[i]}`;
|
23
|
-
}
|
24
|
-
return str + `?`;
|
25
|
-
})
|
26
|
-
.join('');
|
27
|
-
const postRun = async (tx, model) => {
|
28
|
-
var _a, _b;
|
29
|
-
const { initSql } = model;
|
30
|
-
if (initSql == null) {
|
31
|
-
return;
|
32
|
-
}
|
33
|
-
const modelName = model.apiRoot;
|
34
|
-
const exists = await checkModelAlreadyExists(tx, modelName);
|
35
|
-
if (!exists) {
|
36
|
-
((_b = (_a = sbvrUtils.api.migrations) === null || _a === void 0 ? void 0 : _a.logger.info) !== null && _b !== void 0 ? _b : console.info)('First time executing, running init script');
|
37
|
-
await lockMigrations(tx, modelName, async () => {
|
38
|
-
await tx.executeSql(initSql);
|
39
|
-
});
|
40
|
-
}
|
41
|
-
};
|
42
|
-
exports.postRun = postRun;
|
43
|
-
const run = async (tx, model) => {
|
44
|
-
var _a, _b;
|
45
|
-
const { migrations } = model;
|
46
|
-
if (migrations == null || _.isEmpty(migrations)) {
|
47
|
-
return;
|
48
|
-
}
|
49
|
-
const modelName = model.apiRoot;
|
50
|
-
const exists = await checkModelAlreadyExists(tx, modelName);
|
51
|
-
if (!exists) {
|
52
|
-
((_b = (_a = sbvrUtils.api.migrations) === null || _a === void 0 ? void 0 : _a.logger.info) !== null && _b !== void 0 ? _b : console.info)('First time model has executed, skipping migrations');
|
53
|
-
return await setExecutedMigrations(tx, modelName, Object.keys(migrations));
|
54
|
-
}
|
55
|
-
await lockMigrations(tx, modelName, async () => {
|
56
|
-
const executedMigrations = await getExecutedMigrations(tx, modelName);
|
57
|
-
const pendingMigrations = filterAndSortPendingMigrations(migrations, executedMigrations);
|
58
|
-
if (pendingMigrations.length === 0) {
|
59
|
-
return;
|
60
|
-
}
|
61
|
-
const newlyExecutedMigrations = await executeMigrations(tx, pendingMigrations);
|
62
|
-
await setExecutedMigrations(tx, modelName, [
|
63
|
-
...executedMigrations,
|
64
|
-
...newlyExecutedMigrations,
|
65
|
-
]);
|
66
|
-
});
|
67
|
-
};
|
68
|
-
exports.run = run;
|
69
|
-
const checkModelAlreadyExists = async (tx, modelName) => {
|
70
|
-
const result = await tx.tableList("name = 'migration'");
|
71
|
-
if (result.rows.length === 0) {
|
72
|
-
return false;
|
73
|
-
}
|
74
|
-
const { rows } = await tx.executeSql(binds `
|
75
|
-
SELECT 1
|
76
|
-
FROM "model"
|
77
|
-
WHERE "model"."is of-vocabulary" = ${1}
|
78
|
-
LIMIT 1`, [modelName]);
|
79
|
-
return rows.length > 0;
|
80
|
-
};
|
81
|
-
const getExecutedMigrations = async (tx, modelName) => {
|
82
|
-
const { rows } = await tx.executeSql(binds `
|
83
|
-
SELECT "migration"."executed migrations" AS "executed_migrations"
|
84
|
-
FROM "migration"
|
85
|
-
WHERE "migration"."model name" = ${1}`, [modelName]);
|
86
|
-
const data = rows[0];
|
87
|
-
if (data == null) {
|
88
|
-
return [];
|
89
|
-
}
|
90
|
-
return JSON.parse(data.executed_migrations);
|
91
|
-
};
|
92
|
-
const setExecutedMigrations = async (tx, modelName, executedMigrations) => {
|
93
|
-
const stringifiedMigrations = JSON.stringify(executedMigrations);
|
94
|
-
const result = await tx.tableList("name = 'migration'");
|
95
|
-
if (result.rows.length === 0) {
|
96
|
-
return;
|
97
|
-
}
|
98
|
-
const { rowsAffected } = await tx.executeSql(binds `
|
99
|
-
UPDATE "migration"
|
100
|
-
SET "model name" = ${1},
|
101
|
-
"executed migrations" = ${2}
|
102
|
-
WHERE "migration"."model name" = ${3}`, [modelName, stringifiedMigrations, modelName]);
|
103
|
-
if (rowsAffected === 0) {
|
104
|
-
await tx.executeSql(binds `
|
105
|
-
INSERT INTO "migration" ("model name", "executed migrations")
|
106
|
-
VALUES (${1}, ${2})`, [modelName, stringifiedMigrations]);
|
107
|
-
}
|
108
|
-
};
|
109
|
-
const filterAndSortPendingMigrations = (migrations, executedMigrations) => _(migrations).omit(executedMigrations)
|
110
|
-
.toPairs()
|
111
|
-
.sortBy(([migrationKey]) => migrationKey)
|
112
|
-
.value();
|
113
|
-
const lockMigrations = async (tx, modelName, fn) => {
|
114
|
-
try {
|
115
|
-
await tx.executeSql(binds `
|
116
|
-
DELETE FROM "migration lock"
|
117
|
-
WHERE "model name" = ${1}
|
118
|
-
AND "created at" < ${2}`, [modelName, new Date(Date.now() - env_1.migrator.lockTimeout)]);
|
119
|
-
await tx.executeSql(binds `
|
120
|
-
INSERT INTO "migration lock" ("model name")
|
121
|
-
VALUES (${1})`, [modelName]);
|
122
|
-
}
|
123
|
-
catch (err) {
|
124
|
-
await (0, control_flow_1.delay)(env_1.migrator.lockFailDelay);
|
125
|
-
throw err;
|
126
|
-
}
|
127
|
-
try {
|
128
|
-
return await fn();
|
129
|
-
}
|
130
|
-
finally {
|
131
|
-
try {
|
132
|
-
await tx.executeSql(binds `
|
133
|
-
DELETE FROM "migration lock"
|
134
|
-
WHERE "model name" = ${1}`, [modelName]);
|
135
|
-
}
|
136
|
-
catch (_a) {
|
137
|
-
}
|
138
|
-
}
|
139
|
-
};
|
140
|
-
const executeMigrations = async (tx, migrations = []) => {
|
141
|
-
var _a, _b;
|
142
|
-
try {
|
143
|
-
for (const migration of migrations) {
|
144
|
-
await executeMigration(tx, migration);
|
145
|
-
}
|
146
|
-
}
|
147
|
-
catch (err) {
|
148
|
-
((_b = (_a = sbvrUtils.api.migrations) === null || _a === void 0 ? void 0 : _a.logger.error) !== null && _b !== void 0 ? _b : console.error)('Error while executing migrations, rolled back');
|
149
|
-
throw new MigrationError(err);
|
150
|
-
}
|
151
|
-
return migrations.map(([migrationKey]) => migrationKey);
|
152
|
-
};
|
153
|
-
const executeMigration = async (tx, [key, migration]) => {
|
154
|
-
var _a, _b;
|
155
|
-
((_b = (_a = sbvrUtils.api.migrations) === null || _a === void 0 ? void 0 : _a.logger.info) !== null && _b !== void 0 ? _b : console.info)(`Running migration ${JSON.stringify(key)}`);
|
156
|
-
if (typeof migration === 'function') {
|
157
|
-
await migration(tx, sbvrUtils);
|
158
|
-
}
|
159
|
-
else if (typeof migration === 'string') {
|
160
|
-
await tx.executeSql(migration);
|
161
|
-
}
|
162
|
-
else {
|
163
|
-
throw new MigrationError(`Invalid migration type: ${typeof migration}`);
|
164
|
-
}
|
165
|
-
};
|
166
|
-
exports.config = {
|
167
|
-
models: [
|
168
|
-
{
|
169
|
-
modelName: 'migrations',
|
170
|
-
apiRoot: 'migrations',
|
171
|
-
modelText,
|
172
|
-
migrations: {
|
173
|
-
'11.0.0-modified-at': `
|
174
|
-
ALTER TABLE "migration"
|
175
|
-
ADD COLUMN IF NOT EXISTS "modified at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL;
|
176
|
-
`,
|
177
|
-
'11.0.1-modified-at': `
|
178
|
-
ALTER TABLE "migration lock"
|
179
|
-
ADD COLUMN IF NOT EXISTS "modified at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL;
|
180
|
-
`,
|
181
|
-
},
|
182
|
-
},
|
183
|
-
],
|
184
|
-
};
|
185
|
-
//# sourceMappingURL=migrator.js.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"migrator.js","sourceRoot":"","sources":["../../src/migrator/migrator.ts"],"names":[],"mappings":";;;AAKA,4BAA4B;AAC5B,6CAAyC;AACzC,8CAA+D;AAC/D,oDAAoD;AACpD,2DAAiD;AAGjD,MAAM,SAAS,GAAW,OAAO,CAAC,mBAAmB,CAAC,CAAC;AAYvD,MAAa,cAAe,SAAQ,wBAAU;CAAG;AAAjD,wCAAiD;AAIjD,MAAM,KAAK,GAAG,CAAC,OAA6B,EAAE,GAAG,QAAkB,EAAE,EAAE,CACtE,OAAO;KACL,GAAG,CAAC,CAAC,GAAG,EAAE,CAAC,EAAE,EAAE;IACf,IAAI,CAAC,KAAK,QAAQ,CAAC,MAAM,EAAE;QAC1B,OAAO,GAAG,CAAC;KACX;IACD,IAAI,CAAC,GAAG,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAC,EAAE;QAC1B,MAAM,IAAI,WAAW,CAAC,wCAAwC,CAAC,CAAC;KAChE;IACD,IAAI,SAAS,CAAC,EAAE,CAAC,MAAM,eAAqB,EAAE;QAC7C,OAAO,GAAG,GAAG,IAAI,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC;KAC/B;IACD,OAAO,GAAG,GAAG,GAAG,CAAC;AAClB,CAAC,CAA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E,SAAS,CAAiB,EAChB,EAAE;;IAClB,CAAC,MAAA,MAAA,SAAS,CAAC,GAAG,CAAC,UAAU,0CAAE,MAAM,CAAC,IAAI,mCAAI,OAAO,CAAC,IAAI,CAAC,CACtD,qBAAqB,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,CAC1C,CAAC;IAEF,IAAI,OAAO,SAAS,KAAK,UAAU,EAAE;QACpC,MAAM,SAAS,CAAC,EAAE,EAAE,SAAS,CAAC,CAAC;KAC/B;SAAM,IAAI,OAAO,SAAS,KAAK,QAAQ,EAAE;QACzC,MAAM,EAAE,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC;KAC/B;SAAM;QACN,MAAM,IAAI,cAAc,CAAC,2BAA2B,OAAO,SAAS,EAAE,CAAC,CAAC;KACxE;AACF,CAAC,CAAC;AAEW,QAAA,MAAM,GAAW;IAC7B,MAAM,EAAE;QACP;YACC,SAAS,EAAE,YAAY;YACvB,OAAO,EAAE,YAAY;YACrB,SAAS;YACT,UAAU,EAAE;gBACX,oBAAoB,EAAE;;;KAGrB;gBACD,oBAAoB,EAAE;;;KAGrB;aACD;SACD;KACD;CACD,CAAC"}
|
package/src/migrator/migrator.ts
DELETED
@@ -1,278 +0,0 @@
|
|
1
|
-
import type { Tx } from '../database-layer/db';
|
2
|
-
import type { Resolvable } from '../sbvr-api/common-types';
|
3
|
-
import type { Config, Model } from '../config-loader/config-loader';
|
4
|
-
|
5
|
-
import { Engines } from '@balena/abstract-sql-compiler';
|
6
|
-
import * as _ from 'lodash';
|
7
|
-
import { TypedError } from 'typed-error';
|
8
|
-
import { migrator as migratorEnv } from '../config-loader/env';
|
9
|
-
import * as sbvrUtils from '../sbvr-api/sbvr-utils';
|
10
|
-
import { delay } from '../sbvr-api/control-flow';
|
11
|
-
|
12
|
-
// tslint:disable-next-line:no-var-requires
|
13
|
-
const modelText: string = require('./migrations.sbvr');
|
14
|
-
|
15
|
-
type ApiRootModel = Model & { apiRoot: string };
|
16
|
-
|
17
|
-
type SbvrUtils = typeof sbvrUtils;
|
18
|
-
|
19
|
-
type MigrationTuple = [string, Migration];
|
20
|
-
|
21
|
-
export type MigrationFn = (tx: Tx, sbvrUtils: SbvrUtils) => Resolvable<void>;
|
22
|
-
|
23
|
-
export type Migration = string | MigrationFn;
|
24
|
-
|
25
|
-
export class MigrationError extends TypedError {}
|
26
|
-
|
27
|
-
// Tagged template to convert binds from `?` format to the necessary output format,
|
28
|
-
// eg `$1`/`$2`/etc for postgres
|
29
|
-
const binds = (strings: TemplateStringsArray, ...bindNums: number[]) =>
|
30
|
-
strings
|
31
|
-
.map((str, i) => {
|
32
|
-
if (i === bindNums.length) {
|
33
|
-
return str;
|
34
|
-
}
|
35
|
-
if (i + 1 !== bindNums[i]) {
|
36
|
-
throw new SyntaxError('Migration sql binds must be sequential');
|
37
|
-
}
|
38
|
-
if (sbvrUtils.db.engine === Engines.postgres) {
|
39
|
-
return str + `$${bindNums[i]}`;
|
40
|
-
}
|
41
|
-
return str + `?`;
|
42
|
-
})
|
43
|
-
.join('');
|
44
|
-
|
45
|
-
export const postRun = async (tx: Tx, model: ApiRootModel): Promise<void> => {
|
46
|
-
const { initSql } = model;
|
47
|
-
if (initSql == null) {
|
48
|
-
return;
|
49
|
-
}
|
50
|
-
|
51
|
-
const modelName = model.apiRoot;
|
52
|
-
|
53
|
-
const exists = await checkModelAlreadyExists(tx, modelName);
|
54
|
-
if (!exists) {
|
55
|
-
(sbvrUtils.api.migrations?.logger.info ?? console.info)(
|
56
|
-
'First time executing, running init script',
|
57
|
-
);
|
58
|
-
await lockMigrations(tx, modelName, async () => {
|
59
|
-
await tx.executeSql(initSql);
|
60
|
-
});
|
61
|
-
}
|
62
|
-
};
|
63
|
-
|
64
|
-
export const run = async (tx: Tx, model: ApiRootModel): Promise<void> => {
|
65
|
-
const { migrations } = model;
|
66
|
-
if (migrations == null || _.isEmpty(migrations)) {
|
67
|
-
return;
|
68
|
-
}
|
69
|
-
|
70
|
-
const modelName = model.apiRoot;
|
71
|
-
|
72
|
-
// migrations only run if the model has been executed before,
|
73
|
-
// to make changes that can't be automatically applied
|
74
|
-
const exists = await checkModelAlreadyExists(tx, modelName);
|
75
|
-
if (!exists) {
|
76
|
-
(sbvrUtils.api.migrations?.logger.info ?? console.info)(
|
77
|
-
'First time model has executed, skipping migrations',
|
78
|
-
);
|
79
|
-
|
80
|
-
return await setExecutedMigrations(tx, modelName, Object.keys(migrations));
|
81
|
-
}
|
82
|
-
await lockMigrations(tx, modelName, async () => {
|
83
|
-
const executedMigrations = await getExecutedMigrations(tx, modelName);
|
84
|
-
const pendingMigrations = filterAndSortPendingMigrations(
|
85
|
-
migrations,
|
86
|
-
executedMigrations,
|
87
|
-
);
|
88
|
-
if (pendingMigrations.length === 0) {
|
89
|
-
return;
|
90
|
-
}
|
91
|
-
|
92
|
-
const newlyExecutedMigrations = await executeMigrations(
|
93
|
-
tx,
|
94
|
-
pendingMigrations,
|
95
|
-
);
|
96
|
-
await setExecutedMigrations(tx, modelName, [
|
97
|
-
...executedMigrations,
|
98
|
-
...newlyExecutedMigrations,
|
99
|
-
]);
|
100
|
-
});
|
101
|
-
};
|
102
|
-
|
103
|
-
const checkModelAlreadyExists = async (
|
104
|
-
tx: Tx,
|
105
|
-
modelName: string,
|
106
|
-
): Promise<boolean> => {
|
107
|
-
const result = await tx.tableList("name = 'migration'");
|
108
|
-
if (result.rows.length === 0) {
|
109
|
-
return false;
|
110
|
-
}
|
111
|
-
const { rows } = await tx.executeSql(
|
112
|
-
binds`
|
113
|
-
SELECT 1
|
114
|
-
FROM "model"
|
115
|
-
WHERE "model"."is of-vocabulary" = ${1}
|
116
|
-
LIMIT 1`,
|
117
|
-
[modelName],
|
118
|
-
);
|
119
|
-
|
120
|
-
return rows.length > 0;
|
121
|
-
};
|
122
|
-
|
123
|
-
const getExecutedMigrations = async (
|
124
|
-
tx: Tx,
|
125
|
-
modelName: string,
|
126
|
-
): Promise<string[]> => {
|
127
|
-
const { rows } = await tx.executeSql(
|
128
|
-
binds`
|
129
|
-
SELECT "migration"."executed migrations" AS "executed_migrations"
|
130
|
-
FROM "migration"
|
131
|
-
WHERE "migration"."model name" = ${1}`,
|
132
|
-
[modelName],
|
133
|
-
);
|
134
|
-
|
135
|
-
const data = rows[0];
|
136
|
-
if (data == null) {
|
137
|
-
return [];
|
138
|
-
}
|
139
|
-
|
140
|
-
return JSON.parse(data.executed_migrations) as string[];
|
141
|
-
};
|
142
|
-
|
143
|
-
const setExecutedMigrations = async (
|
144
|
-
tx: Tx,
|
145
|
-
modelName: string,
|
146
|
-
executedMigrations: string[],
|
147
|
-
): Promise<void> => {
|
148
|
-
const stringifiedMigrations = JSON.stringify(executedMigrations);
|
149
|
-
|
150
|
-
const result = await tx.tableList("name = 'migration'");
|
151
|
-
if (result.rows.length === 0) {
|
152
|
-
return;
|
153
|
-
}
|
154
|
-
|
155
|
-
const { rowsAffected } = await tx.executeSql(
|
156
|
-
binds`
|
157
|
-
UPDATE "migration"
|
158
|
-
SET "model name" = ${1},
|
159
|
-
"executed migrations" = ${2}
|
160
|
-
WHERE "migration"."model name" = ${3}`,
|
161
|
-
[modelName, stringifiedMigrations, modelName],
|
162
|
-
);
|
163
|
-
|
164
|
-
if (rowsAffected === 0) {
|
165
|
-
await tx.executeSql(
|
166
|
-
binds`
|
167
|
-
INSERT INTO "migration" ("model name", "executed migrations")
|
168
|
-
VALUES (${1}, ${2})`,
|
169
|
-
[modelName, stringifiedMigrations],
|
170
|
-
);
|
171
|
-
}
|
172
|
-
};
|
173
|
-
|
174
|
-
// turns {"key1": migration, "key3": migration, "key2": migration}
|
175
|
-
// into [["key1", migration], ["key2", migration], ["key3", migration]]
|
176
|
-
const filterAndSortPendingMigrations = (
|
177
|
-
migrations: NonNullable<Model['migrations']>,
|
178
|
-
executedMigrations: string[],
|
179
|
-
): MigrationTuple[] =>
|
180
|
-
(_(migrations).omit(executedMigrations) as _.Object<typeof migrations>)
|
181
|
-
.toPairs()
|
182
|
-
.sortBy(([migrationKey]) => migrationKey)
|
183
|
-
.value();
|
184
|
-
|
185
|
-
const lockMigrations = async <T>(
|
186
|
-
tx: Tx,
|
187
|
-
modelName: string,
|
188
|
-
fn: () => Promise<T>,
|
189
|
-
): Promise<T> => {
|
190
|
-
try {
|
191
|
-
await tx.executeSql(
|
192
|
-
binds`
|
193
|
-
DELETE FROM "migration lock"
|
194
|
-
WHERE "model name" = ${1}
|
195
|
-
AND "created at" < ${2}`,
|
196
|
-
[modelName, new Date(Date.now() - migratorEnv.lockTimeout)],
|
197
|
-
);
|
198
|
-
await tx.executeSql(
|
199
|
-
binds`
|
200
|
-
INSERT INTO "migration lock" ("model name")
|
201
|
-
VALUES (${1})`,
|
202
|
-
[modelName],
|
203
|
-
);
|
204
|
-
} catch (err) {
|
205
|
-
await delay(migratorEnv.lockFailDelay);
|
206
|
-
throw err;
|
207
|
-
}
|
208
|
-
try {
|
209
|
-
return await fn();
|
210
|
-
} finally {
|
211
|
-
try {
|
212
|
-
await tx.executeSql(
|
213
|
-
binds`
|
214
|
-
DELETE FROM "migration lock"
|
215
|
-
WHERE "model name" = ${1}`,
|
216
|
-
[modelName],
|
217
|
-
);
|
218
|
-
} catch {
|
219
|
-
// We ignore errors here as it's mostly likely caused by the migration failing and
|
220
|
-
// rolling back the transaction, and if we rethrow here we'll overwrite the real error
|
221
|
-
// making it much harder for users to see what went wrong and fix it
|
222
|
-
}
|
223
|
-
}
|
224
|
-
};
|
225
|
-
|
226
|
-
const executeMigrations = async (
|
227
|
-
tx: Tx,
|
228
|
-
migrations: MigrationTuple[] = [],
|
229
|
-
): Promise<string[]> => {
|
230
|
-
try {
|
231
|
-
for (const migration of migrations) {
|
232
|
-
await executeMigration(tx, migration);
|
233
|
-
}
|
234
|
-
} catch (err) {
|
235
|
-
(sbvrUtils.api.migrations?.logger.error ?? console.error)(
|
236
|
-
'Error while executing migrations, rolled back',
|
237
|
-
);
|
238
|
-
throw new MigrationError(err);
|
239
|
-
}
|
240
|
-
return migrations.map(([migrationKey]) => migrationKey); // return migration keys
|
241
|
-
};
|
242
|
-
|
243
|
-
const executeMigration = async (
|
244
|
-
tx: Tx,
|
245
|
-
[key, migration]: MigrationTuple,
|
246
|
-
): Promise<void> => {
|
247
|
-
(sbvrUtils.api.migrations?.logger.info ?? console.info)(
|
248
|
-
`Running migration ${JSON.stringify(key)}`,
|
249
|
-
);
|
250
|
-
|
251
|
-
if (typeof migration === 'function') {
|
252
|
-
await migration(tx, sbvrUtils);
|
253
|
-
} else if (typeof migration === 'string') {
|
254
|
-
await tx.executeSql(migration);
|
255
|
-
} else {
|
256
|
-
throw new MigrationError(`Invalid migration type: ${typeof migration}`);
|
257
|
-
}
|
258
|
-
};
|
259
|
-
|
260
|
-
export const config: Config = {
|
261
|
-
models: [
|
262
|
-
{
|
263
|
-
modelName: 'migrations',
|
264
|
-
apiRoot: 'migrations',
|
265
|
-
modelText,
|
266
|
-
migrations: {
|
267
|
-
'11.0.0-modified-at': `
|
268
|
-
ALTER TABLE "migration"
|
269
|
-
ADD COLUMN IF NOT EXISTS "modified at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL;
|
270
|
-
`,
|
271
|
-
'11.0.1-modified-at': `
|
272
|
-
ALTER TABLE "migration lock"
|
273
|
-
ADD COLUMN IF NOT EXISTS "modified at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL;
|
274
|
-
`,
|
275
|
-
},
|
276
|
-
},
|
277
|
-
],
|
278
|
-
};
|