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 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
- (0, migrate_1.runMigrationsIfNeeded)("./migrations").catch((err) => {
33
+ main().catch((err) => {
22
34
  console.error(err);
23
35
  process.exit(1);
24
36
  });
@@ -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,IAAA,+BAAqB,EAAC,cAAc,CAAC,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;QAClD,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QACnB,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC,CAAC,CAAC;CACJ"}
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"}
@@ -1,4 +1,2 @@
1
- import { Table } from "pg-structure";
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.isJoinTable = exports.isEntityTable = exports.runMigrationsIfNeeded = void 0;
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
- const client = new pg_1.Client((0, joist_utils_1.newPgConnectionConfig)());
14
- await client.connect();
15
- try {
16
- await (0, node_pg_migrate_1.default)({
17
- dbClient: client,
18
- migrationsTable: "migrations",
19
- dir,
20
- count: undefined,
21
- direction: "up",
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
@@ -1 +1 @@
1
- {"version":3,"file":"migrate.js","sourceRoot":"","sources":["../src/migrate.ts"],"names":[],"mappings":";;;;;;AAAA,6CAAoD;AACpD,sEAAwC;AACxC,2BAA4B;AAC5B,gEAAsD;AAEtD,MAAM,mBAAmB,GAAG,2BAA2B,CAAC;AAEjD,KAAK,UAAU,qBAAqB,CAAC,MAAc,mBAAmB;IAC3E,MAAM,MAAM,GAAG,I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AG,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,KAAK,SAAS,CAAC;AAC5D,CAAC"}
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.1.3",
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.1.3",
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",