joist-migration-utils 1.1.3 → 1.2.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.
- package/build/index.js +13 -1
- package/build/index.js.map +1 -1
- package/build/migrate.d.ts +2 -4
- package/build/migrate.js +11 -67
- package/build/migrate.js.map +1 -1
- package/package.json +2 -2
package/build/index.js
CHANGED
|
@@ -10,15 +10,27 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
10
10
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
11
|
};
|
|
12
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
const joist_utils_1 = require("joist-utils");
|
|
14
|
+
const pg_1 = require("pg");
|
|
13
15
|
const migrate_1 = require("./migrate");
|
|
14
16
|
__exportStar(require("./migrate"), exports);
|
|
15
17
|
__exportStar(require("./utils"), exports);
|
|
18
|
+
async function main() {
|
|
19
|
+
const client = new pg_1.Client((0, joist_utils_1.newPgConnectionConfig)());
|
|
20
|
+
await client.connect();
|
|
21
|
+
try {
|
|
22
|
+
await (0, migrate_1.runMigrationsIfNeeded)(client, "./migrations");
|
|
23
|
+
}
|
|
24
|
+
finally {
|
|
25
|
+
await client.end();
|
|
26
|
+
}
|
|
27
|
+
}
|
|
16
28
|
// If we're being run locally.
|
|
17
29
|
if (require.main === module) {
|
|
18
30
|
if (Object.fromEntries === undefined) {
|
|
19
31
|
throw new Error("Joist requires Node v12.4.0+");
|
|
20
32
|
}
|
|
21
|
-
(
|
|
33
|
+
main().catch((err) => {
|
|
22
34
|
console.error(err);
|
|
23
35
|
process.exit(1);
|
|
24
36
|
});
|
package/build/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,uCAAkD;AAElD,4CAA0B;AAC1B,0CAAwB;AAExB,8BAA8B;AAC9B,IAAI,OAAO,CAAC,IAAI,KAAK,MAAM,EAAE;IAC3B,IAAI,MAAM,CAAC,WAAW,KAAK,SAAS,EAAE;QACpC,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;KACjD;IACD,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,6CAAoD;AACpD,2BAA4B;AAC5B,uCAAkD;AAElD,4CAA0B;AAC1B,0CAAwB;AAExB,KAAK,UAAU,IAAI;IACjB,MAAM,MAAM,GAAG,IAAI,WAAM,CAAC,IAAA,mCAAqB,GAAE,CAAC,CAAC;IACnD,MAAM,MAAM,CAAC,OAAO,EAAE,CAAC;IACvB,IAAI;QACF,MAAM,IAAA,+BAAqB,EAAC,MAAM,EAAE,cAAc,CAAC,CAAC;KACrD;YAAS;QACR,MAAM,MAAM,CAAC,GAAG,EAAE,CAAC;KACpB;AACH,CAAC;AAED,8BAA8B;AAC9B,IAAI,OAAO,CAAC,IAAI,KAAK,MAAM,EAAE;IAC3B,IAAI,MAAM,CAAC,WAAW,KAAK,SAAS,EAAE;QACpC,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;KACjD;IACD,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;QACnB,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QACnB,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC,CAAC,CAAC;CACJ"}
|
package/build/migrate.d.ts
CHANGED
|
@@ -1,4 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare function runMigrationsIfNeeded(dir?: string): Promise<void>;
|
|
3
|
-
export declare function isEntityTable(t: Table): boolean;
|
|
4
|
-
export declare function isJoinTable(t: Table): boolean;
|
|
1
|
+
import { Client } from "pg";
|
|
2
|
+
export declare function runMigrationsIfNeeded(client: Client, dir?: string): Promise<void>;
|
package/build/migrate.js
CHANGED
|
@@ -3,75 +3,19 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
7
|
-
const joist_utils_1 = require("joist-utils");
|
|
6
|
+
exports.runMigrationsIfNeeded = void 0;
|
|
8
7
|
const node_pg_migrate_1 = __importDefault(require("node-pg-migrate"));
|
|
9
|
-
const pg_1 = require("pg");
|
|
10
|
-
const pg_structure_1 = __importDefault(require("pg-structure"));
|
|
11
8
|
const productionDirectory = "/home/node/app/migrations";
|
|
12
|
-
async function runMigrationsIfNeeded(dir = productionDirectory) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
ignorePattern: "(.*.d.ts)|(.*utils.[jt]s)|(migrate.[jt]s)|(migrate.test.[jt]s)",
|
|
23
|
-
decamelize: true,
|
|
24
|
-
});
|
|
25
|
-
const db = await (0, pg_structure_1.default)((0, joist_utils_1.newPgConnectionConfig)());
|
|
26
|
-
if (process.env.ADD_FLUSH_DATABASE === "true") {
|
|
27
|
-
console.log("Creating flush_database() function");
|
|
28
|
-
await createFlushDbFunction(db, client);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
finally {
|
|
32
|
-
await client.end();
|
|
33
|
-
}
|
|
9
|
+
async function runMigrationsIfNeeded(client, dir = productionDirectory) {
|
|
10
|
+
await (0, node_pg_migrate_1.default)({
|
|
11
|
+
dbClient: client,
|
|
12
|
+
migrationsTable: "migrations",
|
|
13
|
+
dir,
|
|
14
|
+
count: undefined,
|
|
15
|
+
direction: "up",
|
|
16
|
+
ignorePattern: "(.*.d.ts)|(.*utils.[jt]s)|(migrate.[jt]s)|(migrate.test.[jt]s)",
|
|
17
|
+
decamelize: true,
|
|
18
|
+
});
|
|
34
19
|
}
|
|
35
20
|
exports.runMigrationsIfNeeded = runMigrationsIfNeeded;
|
|
36
|
-
/** Creates a `flush_database` stored procedure to truncate all of the tables between tests. */
|
|
37
|
-
async function createFlushDbFunction(db, client) {
|
|
38
|
-
await client.query(generateFlushFunction(db));
|
|
39
|
-
}
|
|
40
|
-
function generateFlushFunction(db) {
|
|
41
|
-
const tables = db.tables.filter((t) => isEntityTable(t) || isJoinTable(t)).map((t) => t.name);
|
|
42
|
-
// Note that, for whatever reason, doing DELETEs + ALTER SEQUENCEs is dramatically faster than TRUNCATEs.
|
|
43
|
-
// On even a 40-table schema, TRUNCATEs (either 1 per table or even a single TRUNCATE with all tables) takes
|
|
44
|
-
// 100s of milliseconds, where as DELETEs takes single-digit milliseconds and DELETEs + ALTER SEQUENCEs is
|
|
45
|
-
// 10s of milliseconds.
|
|
46
|
-
//
|
|
47
|
-
// One cute idea would be to use a single sequence for all tables when running locally. That would
|
|
48
|
-
// mean our flush_database function could reset a single sequence. Plus it would reduce bugs where
|
|
49
|
-
// something "works" but only b/c in the test database, all entities have id = 1.
|
|
50
|
-
const statements = tables
|
|
51
|
-
.map((t) => `DELETE FROM "${t}"; ALTER SEQUENCE IF EXISTS "${t}_id_seq" RESTART WITH 1 INCREMENT BY 1;`)
|
|
52
|
-
.join("\n");
|
|
53
|
-
return `CREATE OR REPLACE FUNCTION flush_database() RETURNS void AS $$
|
|
54
|
-
BEGIN
|
|
55
|
-
${statements}
|
|
56
|
-
END;
|
|
57
|
-
$$ LANGUAGE
|
|
58
|
-
'plpgsql'`;
|
|
59
|
-
}
|
|
60
|
-
function isEntityTable(t) {
|
|
61
|
-
const columnNames = t.columns.map((c) => c.name);
|
|
62
|
-
return includesAllOf(columnNames, ["id", "created_at", "updated_at"]);
|
|
63
|
-
}
|
|
64
|
-
exports.isEntityTable = isEntityTable;
|
|
65
|
-
function isJoinTable(t) {
|
|
66
|
-
const { columns } = t;
|
|
67
|
-
const hasOnePk = columns.filter((c) => c.isPrimaryKey).length === 1;
|
|
68
|
-
const hasTwoFks = columns.filter((c) => c.isForeignKey).length === 2;
|
|
69
|
-
const hasThreeColumns = columns.length === 3;
|
|
70
|
-
const hasFourColumnsOneIsCreatedAt = columns.length === 4 && columns.filter((c) => c.name === "created_at").length === 1;
|
|
71
|
-
return hasOnePk && hasTwoFks && (hasThreeColumns || hasFourColumnsOneIsCreatedAt);
|
|
72
|
-
}
|
|
73
|
-
exports.isJoinTable = isJoinTable;
|
|
74
|
-
function includesAllOf(set, subset) {
|
|
75
|
-
return subset.find((e) => !set.includes(e)) === undefined;
|
|
76
|
-
}
|
|
77
21
|
//# sourceMappingURL=migrate.js.map
|
package/build/migrate.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"migrate.js","sourceRoot":"","sources":["../src/migrate.ts"],"names":[],"mappings":";;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"migrate.js","sourceRoot":"","sources":["../src/migrate.ts"],"names":[],"mappings":";;;;;;AAAA,sEAAwC;AAGxC,MAAM,mBAAmB,GAAG,2BAA2B,CAAC;AAEjD,KAAK,UAAU,qBAAqB,CAAC,MAAc,EAAE,MAAc,mBAAmB;IAC3F,MAAM,IAAA,yBAAS,EAAC;QACd,QAAQ,EAAE,MAAM;QAChB,eAAe,EAAE,YAAY;QAC7B,GAAG;QACH,KAAK,EAAE,SAA0B;QACjC,SAAS,EAAE,IAAI;QACf,aAAa,EAAE,gEAAgE;QAC/E,UAAU,EAAE,IAAI;KACjB,CAAC,CAAC;AACL,CAAC;AAVD,sDAUC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "joist-migration-utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
],
|
|
18
18
|
"dependencies": {
|
|
19
19
|
"@types/pluralize": "0.0.29",
|
|
20
|
-
"joist-utils": "1.
|
|
20
|
+
"joist-utils": "1.2.0",
|
|
21
21
|
"node-pg-migrate": "^5.0.0",
|
|
22
22
|
"pg": "^8.7.1",
|
|
23
23
|
"pg-structure": "^7.13.0",
|