joist-codegen 1.172.0 → 1.174.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.
|
@@ -2,5 +2,18 @@ import { Client } from "pg";
|
|
|
2
2
|
import { DbMetadata } from "./EntityDbMetadata";
|
|
3
3
|
/** Creates a `flush_database` stored procedure to truncate all the tables between tests. */
|
|
4
4
|
export declare function createFlushFunction(client: Client, db: DbMetadata): Promise<void>;
|
|
5
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Explicitly deletes every entity/m2m table in a deterministic order.
|
|
7
|
+
*
|
|
8
|
+
* We do this for schemas with non-deferred foreign keys (order matters)
|
|
9
|
+
* or schemas with UUID id columns.
|
|
10
|
+
*/
|
|
11
|
+
export declare function generateExplicitFlushFunction(db: DbMetadata): string;
|
|
12
|
+
/**
|
|
13
|
+
* A cuter/shorter flush that only DELETEs from tables that were inserted into.
|
|
14
|
+
*
|
|
15
|
+
* The difference between this and the explicit-order function should only be
|
|
16
|
+
* noticeable on 100+ table schemas.
|
|
17
|
+
*/
|
|
18
|
+
export declare function generateSequenceFlushFunction(db: DbMetadata): string;
|
|
6
19
|
//# sourceMappingURL=generateFlushFunction.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generateFlushFunction.d.ts","sourceRoot":"","sources":["../src/generateFlushFunction.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,IAAI,CAAC;AAC5B,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAEhD,4FAA4F;AAC5F,wBAAsB,mBAAmB,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,
|
|
1
|
+
{"version":3,"file":"generateFlushFunction.d.ts","sourceRoot":"","sources":["../src/generateFlushFunction.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,IAAI,CAAC;AAC5B,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAEhD,4FAA4F;AAC5F,wBAAsB,mBAAmB,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,CAQvF;AAED;;;;;GAKG;AACH,wBAAgB,6BAA6B,CAAC,EAAE,EAAE,UAAU,GAAG,MAAM,CAiDpE;AAED;;;;;GAKG;AACH,wBAAgB,6BAA6B,CAAC,EAAE,EAAE,UAAU,GAAG,MAAM,CAqBpE"}
|
|
@@ -1,12 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.generateSequenceFlushFunction = exports.generateExplicitFlushFunction = exports.createFlushFunction = void 0;
|
|
4
4
|
/** Creates a `flush_database` stored procedure to truncate all the tables between tests. */
|
|
5
5
|
async function createFlushFunction(client, db) {
|
|
6
|
-
|
|
6
|
+
const hasAnyNonDeferredFks = db.entities.some((e) => e.nonDeferredFks.length > 0);
|
|
7
|
+
const hasAnyNonSequenceIds = db.entities.some((e) => e.primaryKey.columnType === "uuid");
|
|
8
|
+
if (hasAnyNonSequenceIds || hasAnyNonDeferredFks) {
|
|
9
|
+
await client.query(generateExplicitFlushFunction(db));
|
|
10
|
+
}
|
|
11
|
+
else {
|
|
12
|
+
await client.query(generateSequenceFlushFunction(db));
|
|
13
|
+
}
|
|
7
14
|
}
|
|
8
15
|
exports.createFlushFunction = createFlushFunction;
|
|
9
|
-
|
|
16
|
+
/**
|
|
17
|
+
* Explicitly deletes every entity/m2m table in a deterministic order.
|
|
18
|
+
*
|
|
19
|
+
* We do this for schemas with non-deferred foreign keys (order matters)
|
|
20
|
+
* or schemas with UUID id columns.
|
|
21
|
+
*/
|
|
22
|
+
function generateExplicitFlushFunction(db) {
|
|
10
23
|
// Leave code/enum tables alone
|
|
11
24
|
const tables = [
|
|
12
25
|
...[...db.entities]
|
|
@@ -29,11 +42,10 @@ function generateFlushFunction(db) {
|
|
|
29
42
|
// On even a 40-table schema, TRUNCATEs (either 1 per table or even a single TRUNCATE with all tables) takes
|
|
30
43
|
// 100s of milliseconds, whereas DELETEs takes single-digit milliseconds and DELETEs + ALTER SEQUENCEs is
|
|
31
44
|
// 10s of milliseconds.
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
const deletes = tables.map((t) => `DELETE FROM "${t}"; ALTER SEQUENCE IF EXISTS "${t}_id_seq" RESTART WITH 1 INCREMENT BY 1;`);
|
|
45
|
+
const deletes = tables.flatMap((t) => [
|
|
46
|
+
`DELETE FROM "${t}";`,
|
|
47
|
+
`ALTER SEQUENCE IF EXISTS "${t}_id_seq" RESTART WITH 1 INCREMENT BY 1;`,
|
|
48
|
+
]);
|
|
37
49
|
// Create `SET NULLs` in schemas that don't have deferred FKs
|
|
38
50
|
const setFksNulls = db.entities
|
|
39
51
|
.filter((t) => t.nonDeferredFkOrder)
|
|
@@ -44,13 +56,40 @@ function generateFlushFunction(db) {
|
|
|
44
56
|
.filter((m2o) => db.entitiesByName[m2o.otherEntity.name].nonDeferredFkOrder > t.nonDeferredFkOrder)
|
|
45
57
|
.map((m2o) => `UPDATE ${t.tableName} SET ${m2o.columnName} = NULL;`));
|
|
46
58
|
const statements = [...setFksNulls, ...deletes].join("\n");
|
|
47
|
-
// console.log({ statements });
|
|
48
59
|
return `CREATE OR REPLACE FUNCTION flush_database() RETURNS void AS $$
|
|
49
60
|
BEGIN
|
|
50
|
-
|
|
61
|
+
${statements}
|
|
51
62
|
END;
|
|
52
63
|
$$ LANGUAGE
|
|
53
64
|
'plpgsql'`;
|
|
54
65
|
}
|
|
55
|
-
exports.
|
|
66
|
+
exports.generateExplicitFlushFunction = generateExplicitFlushFunction;
|
|
67
|
+
/**
|
|
68
|
+
* A cuter/shorter flush that only DELETEs from tables that were inserted into.
|
|
69
|
+
*
|
|
70
|
+
* The difference between this and the explicit-order function should only be
|
|
71
|
+
* noticeable on 100+ table schemas.
|
|
72
|
+
*/
|
|
73
|
+
function generateSequenceFlushFunction(db) {
|
|
74
|
+
const enumTables = Object.values(db.enums).map((e) => e.table);
|
|
75
|
+
// We don't currently have a way to filter out the enum sequences in a query
|
|
76
|
+
const maybeSkipEnums = enumTables.length === 0
|
|
77
|
+
? ""
|
|
78
|
+
: `AND sequencename NOT IN (${enumTables.map((t) => `'${t.name}_id_seq'`).join(", ")})`;
|
|
79
|
+
return `CREATE OR REPLACE FUNCTION flush_database() RETURNS void AS $$
|
|
80
|
+
DECLARE seq RECORD;
|
|
81
|
+
BEGIN
|
|
82
|
+
FOR seq IN
|
|
83
|
+
SELECT sequencename AS name
|
|
84
|
+
FROM pg_sequences
|
|
85
|
+
WHERE schemaname = 'public' AND last_value IS NOT NULL AND sequencename LIKE '%_id_seq' ${maybeSkipEnums}
|
|
86
|
+
LOOP
|
|
87
|
+
EXECUTE format('DELETE FROM %I', regexp_replace(seq.name, '_id_seq$', ''));
|
|
88
|
+
EXECUTE format('ALTER SEQUENCE %I RESTART WITH 1', seq.name);
|
|
89
|
+
END LOOP;
|
|
90
|
+
END;
|
|
91
|
+
$$ LANGUAGE
|
|
92
|
+
'plpgsql'`;
|
|
93
|
+
}
|
|
94
|
+
exports.generateSequenceFlushFunction = generateSequenceFlushFunction;
|
|
56
95
|
//# sourceMappingURL=generateFlushFunction.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generateFlushFunction.js","sourceRoot":"","sources":["../src/generateFlushFunction.ts"],"names":[],"mappings":";;;AAGA,4FAA4F;AACrF,KAAK,UAAU,mBAAmB,CAAC,MAAc,EAAE,EAAc;IACtE,MAAM,MAAM,CAAC,KAAK,CAAC,
|
|
1
|
+
{"version":3,"file":"generateFlushFunction.js","sourceRoot":"","sources":["../src/generateFlushFunction.ts"],"names":[],"mappings":";;;AAGA,4FAA4F;AACrF,KAAK,UAAU,mBAAmB,CAAC,MAAc,EAAE,EAAc;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CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;IAC/D,4EAA4E;IAC5E,MAAM,cAAc,GAClB,UAAU,CAAC,MAAM,KAAK,CAAC;QACrB,CAAC,CAAC,EAAE;QACJ,CAAC,CAAC,4BAA4B,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC,IAAI,UAAU,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC;IAC5F,OAAO;;;;;;kGAMyF,cAAc;;;;;;;cAOlG,CAAC;AACf,CAAC;AArBD,sEAqBC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "joist-codegen",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.174.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"change-case": "^4.1.2",
|
|
26
26
|
"fast-glob": "^3.3.2",
|
|
27
27
|
"inquirer": "^8.2.6",
|
|
28
|
-
"joist-utils": "1.
|
|
28
|
+
"joist-utils": "1.174.0",
|
|
29
29
|
"jscodeshift": "^0.15.2",
|
|
30
30
|
"knex": "^3.1.0",
|
|
31
31
|
"pg": "^8.11.5",
|