@forge/sql 2.3.0-next.0 → 2.3.0-next.1
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/out/__test__/migration.test.js +81 -0
- package/out/errors.d.ts +4 -2
- package/out/errors.d.ts.map +1 -1
- package/out/errors.js +12 -2
- package/out/migration.d.ts.map +1 -1
- package/out/migration.js +2 -2
- package/package.json +1 -1
|
@@ -5,6 +5,7 @@ const sql_1 = require("../sql");
|
|
|
5
5
|
const errorCodes_1 = require("../errorCodes");
|
|
6
6
|
const jest_when_1 = require("jest-when");
|
|
7
7
|
const sql_statement_1 = require("../sql-statement");
|
|
8
|
+
const errors_1 = require("../errors");
|
|
8
9
|
jest.mock('../sql');
|
|
9
10
|
describe('Migration', () => {
|
|
10
11
|
let migrationRunner;
|
|
@@ -92,6 +93,86 @@ describe('Migration', () => {
|
|
|
92
93
|
expect(mockSqlClient.prepare).toHaveBeenCalledTimes(1);
|
|
93
94
|
expect(mockSqlClient.executeDDL).toHaveBeenCalledTimes(3);
|
|
94
95
|
});
|
|
96
|
+
it('should throw forge sql api error during check point', async () => {
|
|
97
|
+
migrationRunner.enqueue('v_001_create_table_foo', CREATE_TABLE_FOO_QUERY);
|
|
98
|
+
migrationRunner.enqueue('v_002_create_table_bar', CREATE_TABLE_BAR_QUERY);
|
|
99
|
+
(0, jest_when_1.when)(mockSqlClient.executeDDL)
|
|
100
|
+
.calledWith((0, jest_when_1.when)((arg) => arg.startsWith('CREATE TABLE IF NOT EXISTS __migrations')))
|
|
101
|
+
.mockResolvedValue({ rows: [] });
|
|
102
|
+
(0, jest_when_1.when)(mockSqlClient.executeDDL)
|
|
103
|
+
.calledWith('SELECT id, name, migratedAt FROM __migrations;')
|
|
104
|
+
.mockResolvedValue({
|
|
105
|
+
rows: [{ name: 'v_001_create_table_foo', statement: CREATE_TABLE_FOO_QUERY, migratedAt: new Date() }]
|
|
106
|
+
});
|
|
107
|
+
(0, jest_when_1.when)(mockSqlClient.executeDDL)
|
|
108
|
+
.calledWith(CREATE_TABLE_BAR_QUERY)
|
|
109
|
+
.mockRejectedValue(new errors_1.ForgeSQLAPIError({ status: 400, statusText: 'Invalid query' }, { code: 'SQL_EXECUTION_ERROR', message: '', suggestion: 'Check your SQL query' }));
|
|
110
|
+
await expect(migrationRunner.run()).rejects.toMatchObject({
|
|
111
|
+
migrationName: 'v_002_create_table_bar',
|
|
112
|
+
migrationsYetToRun: ['v_002_create_table_bar'],
|
|
113
|
+
cause: new errors_1.ForgeSQLAPIError({ status: 400, statusText: 'Invalid query' }, { code: 'SQL_EXECUTION_ERROR', message: '', suggestion: 'Check your SQL query' })
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
it('should throw an error when a migration fails', async () => {
|
|
117
|
+
migrationRunner.enqueue('v_001_create_table_foo', CREATE_TABLE_FOO_QUERY);
|
|
118
|
+
migrationRunner.enqueue('v_002_create_table_bar', CREATE_TABLE_BAR_QUERY);
|
|
119
|
+
(0, jest_when_1.when)(mockSqlClient.executeDDL)
|
|
120
|
+
.calledWith((0, jest_when_1.when)((arg) => arg.startsWith('CREATE TABLE IF NOT EXISTS __migrations')))
|
|
121
|
+
.mockResolvedValue({ rows: [] });
|
|
122
|
+
(0, jest_when_1.when)(mockSqlClient.executeDDL)
|
|
123
|
+
.calledWith('SELECT id, name, migratedAt FROM __migrations;')
|
|
124
|
+
.mockResolvedValue({
|
|
125
|
+
rows: [{ name: 'v_001_create_table_foo', statement: CREATE_TABLE_FOO_QUERY, migratedAt: new Date() }]
|
|
126
|
+
});
|
|
127
|
+
(0, jest_when_1.when)(mockSqlClient.executeDDL)
|
|
128
|
+
.calledWith(CREATE_TABLE_BAR_QUERY)
|
|
129
|
+
.mockRejectedValue(new errors_1.ForgeSQLAPIError({ status: 400, statusText: 'Invalid query' }, { code: 'SQL_EXECUTION_ERROR', message: '', suggestion: 'Check your SQL query' }));
|
|
130
|
+
await expect(migrationRunner.run()).rejects.toMatchObject({
|
|
131
|
+
migrationName: 'v_002_create_table_bar',
|
|
132
|
+
migrationsYetToRun: ['v_002_create_table_bar'],
|
|
133
|
+
cause: new errors_1.ForgeSQLAPIError({ status: 400, statusText: 'Invalid query' }, { code: 'SQL_EXECUTION_ERROR', message: '', suggestion: 'Check your SQL query' })
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
it('should throw generic error when a migration fails', async () => {
|
|
137
|
+
migrationRunner.enqueue('v_001_create_table_foo', CREATE_TABLE_FOO_QUERY);
|
|
138
|
+
migrationRunner.enqueue('v_002_create_table_bar', CREATE_TABLE_BAR_QUERY);
|
|
139
|
+
(0, jest_when_1.when)(mockSqlClient.executeDDL)
|
|
140
|
+
.calledWith((0, jest_when_1.when)((arg) => arg.startsWith('CREATE TABLE IF NOT EXISTS __migrations')))
|
|
141
|
+
.mockResolvedValue({ rows: [] });
|
|
142
|
+
(0, jest_when_1.when)(mockSqlClient.executeDDL)
|
|
143
|
+
.calledWith('SELECT id, name, migratedAt FROM __migrations;')
|
|
144
|
+
.mockResolvedValue({
|
|
145
|
+
rows: [{ name: 'v_001_create_table_foo', statement: CREATE_TABLE_FOO_QUERY, migratedAt: new Date() }]
|
|
146
|
+
});
|
|
147
|
+
(0, jest_when_1.when)(mockSqlClient.executeDDL)
|
|
148
|
+
.calledWith(CREATE_TABLE_BAR_QUERY)
|
|
149
|
+
.mockRejectedValue(new Error('Failed to execute migration'));
|
|
150
|
+
await expect(migrationRunner.run()).rejects.toMatchObject({
|
|
151
|
+
migrationName: 'v_002_create_table_bar',
|
|
152
|
+
migrationsYetToRun: ['v_002_create_table_bar'],
|
|
153
|
+
cause: new Error('Failed to execute migration')
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
it('should throw generic error when check point fails', async () => {
|
|
157
|
+
migrationRunner.enqueue('v_001_create_table_foo', CREATE_TABLE_FOO_QUERY);
|
|
158
|
+
migrationRunner.enqueue('v_002_create_table_bar', CREATE_TABLE_BAR_QUERY);
|
|
159
|
+
(0, jest_when_1.when)(mockSqlClient.executeDDL)
|
|
160
|
+
.calledWith((0, jest_when_1.when)((arg) => arg.startsWith('CREATE TABLE IF NOT EXISTS __migrations')))
|
|
161
|
+
.mockResolvedValue({ rows: [] });
|
|
162
|
+
(0, jest_when_1.when)(mockSqlClient.executeDDL)
|
|
163
|
+
.calledWith('SELECT id, name, migratedAt FROM __migrations;')
|
|
164
|
+
.mockResolvedValue({
|
|
165
|
+
rows: [{ name: 'v_001_create_table_foo', statement: CREATE_TABLE_FOO_QUERY, migratedAt: new Date() }]
|
|
166
|
+
});
|
|
167
|
+
(0, jest_when_1.when)(mockSqlClient.executeDDL)
|
|
168
|
+
.calledWith(CREATE_TABLE_BAR_QUERY)
|
|
169
|
+
.mockRejectedValue(new Error('Failed to execute migration'));
|
|
170
|
+
await expect(migrationRunner.run()).rejects.toMatchObject({
|
|
171
|
+
migrationName: 'v_002_create_table_bar',
|
|
172
|
+
migrationsYetToRun: ['v_002_create_table_bar'],
|
|
173
|
+
cause: new Error('Failed to execute migration')
|
|
174
|
+
});
|
|
175
|
+
});
|
|
95
176
|
});
|
|
96
177
|
describe('when no migrations have been run in the past', () => {
|
|
97
178
|
it('should execute migrations that have not been previously run', async () => {
|
package/out/errors.d.ts
CHANGED
|
@@ -23,11 +23,13 @@ export declare class ForgeSQLAPIError extends ForgeSQLError {
|
|
|
23
23
|
export declare class MigrationExecutionError extends ForgeSQLError {
|
|
24
24
|
readonly migrationName: string;
|
|
25
25
|
readonly migrationsYetToRun: string[];
|
|
26
|
-
|
|
26
|
+
readonly cause?: Error | undefined;
|
|
27
|
+
constructor(migrationName: string, migrationsYetToRun: string[], cause?: Error | undefined);
|
|
27
28
|
}
|
|
28
29
|
export declare class MigrationCheckPointError extends ForgeSQLError {
|
|
29
30
|
readonly migrationName: string;
|
|
30
31
|
readonly migrationsYetToRun: string[];
|
|
31
|
-
|
|
32
|
+
readonly cause?: Error | undefined;
|
|
33
|
+
constructor(migrationName: string, migrationsYetToRun: string[], cause?: Error | undefined);
|
|
32
34
|
}
|
|
33
35
|
//# sourceMappingURL=errors.d.ts.map
|
package/out/errors.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AACA,MAAM,WAAW,UAAU;IAKzB,IAAI,EAAE,MAAM,CAAC;IAGb,OAAO,EAAE,MAAM,CAAC;IAGhB,UAAU,CAAC,EAAE,MAAM,CAAC;IAGpB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAGnC;AAGD,qBAAa,aAAc,SAAQ,KAAK;gBAC1B,OAAO,EAAE,MAAM;CAI5B;AASD,MAAM,WAAW,uBAAuB;IAEtC,MAAM,EAAE,MAAM,CAAC;IAGf,UAAU,EAAE,MAAM,CAAC;IAGnB,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CACzB;AAMD,qBAAa,gBAAiB,SAAQ,aAAa;IAMjD,eAAe,EAAE,uBAAuB,CAAC;IAMzC,IAAI,EAAE,MAAM,CAAC;IAGb,OAAO,EAAE,MAAM,CAAC;IAGhB,UAAU,CAAC,EAAE,MAAM,CAAC;IAGpB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;gBAEtB,eAAe,EAAE,uBAAuB,EAAE,UAAU,EAAE,UAAU;CAgB7E;AAED,qBAAa,uBAAwB,SAAQ,aAAa;IAEtD,QAAQ,CAAC,aAAa,EAAE,MAAM;IAC9B,QAAQ,CAAC,kBAAkB,EAAE,MAAM,EAAE;
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AACA,MAAM,WAAW,UAAU;IAKzB,IAAI,EAAE,MAAM,CAAC;IAGb,OAAO,EAAE,MAAM,CAAC;IAGhB,UAAU,CAAC,EAAE,MAAM,CAAC;IAGpB,OAAO,CAAC,EAAE,M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
33
|
+
constructor(migrationName, migrationsYetToRun, cause) {
|
|
33
34
|
super(`Failed to execute migration with name ${migrationName}`);
|
|
34
35
|
this.migrationName = migrationName;
|
|
35
36
|
this.migrationsYetToRun = migrationsYetToRun;
|
|
37
|
+
this.cause = cause;
|
|
38
|
+
this.migrationName = migrationName;
|
|
39
|
+
this.migrationsYetToRun = migrationsYetToRun;
|
|
40
|
+
this.cause = cause;
|
|
36
41
|
}
|
|
37
42
|
}
|
|
38
43
|
exports.MigrationExecutionError = MigrationExecutionError;
|
|
39
44
|
class MigrationCheckPointError extends ForgeSQLError {
|
|
40
45
|
migrationName;
|
|
41
46
|
migrationsYetToRun;
|
|
42
|
-
|
|
47
|
+
cause;
|
|
48
|
+
constructor(migrationName, migrationsYetToRun, cause) {
|
|
43
49
|
super(`Failed to checkpoint after running migration with name ${migrationName}`);
|
|
44
50
|
this.migrationName = migrationName;
|
|
45
51
|
this.migrationsYetToRun = migrationsYetToRun;
|
|
52
|
+
this.cause = cause;
|
|
53
|
+
this.migrationName = migrationName;
|
|
54
|
+
this.migrationsYetToRun = migrationsYetToRun;
|
|
55
|
+
this.cause = cause;
|
|
46
56
|
}
|
|
47
57
|
}
|
|
48
58
|
exports.MigrationCheckPointError = MigrationCheckPointError;
|
package/out/migration.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"migration.d.ts","sourceRoot":"","sources":["../src/migration.ts"],"names":[],"mappings":"AAAA,OAAO,EAAO,SAAS,EAAE,MAAM,OAAO,CAAC;AAGvC,aAAK,eAAe,GAAG;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,aAAK,gBAAgB,GAAG;IACtB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,IAAI,CAAC;CAClB,CAAC;AAcF,qBAAa,eAAe;IAC1B,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAY;IACtC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAoB;gBAEnC,SAAS,EAAE,SAAS;IAK1B,UAAU;IAIT,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,eAAe;IAQzD,WAAW,IAAI,eAAe,EAAE;IAIjC,IAAI,IAAI,OAAO,CAAC,gBAAgB,EAAE,CAAC;IAYnC,GAAG,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;
|
|
1
|
+
{"version":3,"file":"migration.d.ts","sourceRoot":"","sources":["../src/migration.ts"],"names":[],"mappings":"AAAA,OAAO,EAAO,SAAS,EAAE,MAAM,OAAO,CAAC;AAGvC,aAAK,eAAe,GAAG;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,aAAK,gBAAgB,GAAG;IACtB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,IAAI,CAAC;CAClB,CAAC;AAcF,qBAAa,eAAe;IAC1B,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAY;IACtC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAoB;gBAEnC,SAAS,EAAE,SAAS;IAK1B,UAAU;IAIT,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,eAAe;IAQzD,WAAW,IAAI,eAAe,EAAE;IAIjC,IAAI,IAAI,OAAO,CAAC,gBAAgB,EAAE,CAAC;IAYnC,GAAG,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;CAmC/B;AAED,eAAO,MAAM,eAAe,iBAA2B,CAAC"}
|
package/out/migration.js
CHANGED
|
@@ -47,13 +47,13 @@ class MigrationRunner {
|
|
|
47
47
|
await this.sqlClient.executeDDL(migration.statement);
|
|
48
48
|
}
|
|
49
49
|
catch (error) {
|
|
50
|
-
throw new errors_1.MigrationExecutionError(migration.name, migrationsToRun.map((m) => m.name));
|
|
50
|
+
throw new errors_1.MigrationExecutionError(migration.name, migrationsToRun.map((m) => m.name), error);
|
|
51
51
|
}
|
|
52
52
|
try {
|
|
53
53
|
await this.sqlClient.prepare(INSERT_SCHEMA_VERSION_QUERY).bindParams(migration.name).execute();
|
|
54
54
|
}
|
|
55
55
|
catch (error) {
|
|
56
|
-
throw new errors_1.MigrationCheckPointError(migration.name, migrationsToRun.map((m) => m.name));
|
|
56
|
+
throw new errors_1.MigrationCheckPointError(migration.name, migrationsToRun.map((m) => m.name), error);
|
|
57
57
|
}
|
|
58
58
|
migrationsSuccessfullyRun.push(migration.name);
|
|
59
59
|
}
|