migraguard 0.10.2 → 0.10.4

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/dist/cli.js CHANGED
@@ -520,6 +520,7 @@ var init_dsl = __esm({
520
520
  // src/db-mysql.ts
521
521
  var ADVISORY_LOCK_KEY = "migraguard-apply";
522
522
  var CONNECTION_TIMEOUT_MS = 1e4;
523
+ var SESSION_MAX_EXECUTION_TIME_MS = 3e4;
523
524
  var DDL_LOCK_TIMEOUT_SEC = 5;
524
525
  var CREATE_TABLE_SQL = `
525
526
  CREATE TABLE IF NOT EXISTS schema_migrations (
@@ -563,6 +564,10 @@ var MigraguardDbMysql = class {
563
564
  password: this.config.connection.password,
564
565
  connectTimeout: CONNECTION_TIMEOUT_MS
565
566
  });
567
+ try {
568
+ await this.exec(`SET SESSION max_execution_time = ${SESSION_MAX_EXECUTION_TIME_MS}`);
569
+ } catch {
570
+ }
566
571
  }
567
572
  async close() {
568
573
  await this.connection?.end();
@@ -581,6 +586,10 @@ var MigraguardDbMysql = class {
581
586
  }
582
587
  } finally {
583
588
  await this.exec(`SET SESSION lock_wait_timeout = DEFAULT`);
589
+ try {
590
+ await this.exec(`SET SESSION max_execution_time = ${SESSION_MAX_EXECUTION_TIME_MS}`);
591
+ } catch {
592
+ }
584
593
  }
585
594
  }
586
595
  async tableHasAllColumns() {
@@ -801,7 +810,24 @@ function createDb(config) {
801
810
  return new MigraguardDb(config);
802
811
  }
803
812
  }
813
+ async function safeGetAllRecords(db) {
814
+ try {
815
+ return await db.getAllRecords();
816
+ } catch (err) {
817
+ if (isTableNotFound(err)) return [];
818
+ throw err;
819
+ }
820
+ }
821
+ function isTableNotFound(err) {
822
+ if (!(err instanceof Error)) return false;
823
+ const code = err["code"];
824
+ if (code === "42P01") return true;
825
+ if (code === "ER_NO_SUCH_TABLE") return true;
826
+ if (err.message.includes("no such table")) return true;
827
+ return false;
828
+ }
804
829
  var CONNECTION_TIMEOUT_MS2 = 1e4;
830
+ var SESSION_STATEMENT_TIMEOUT_MS = 3e4;
805
831
  var DDL_LOCK_TIMEOUT = "5s";
806
832
  var DDL_STATEMENT_TIMEOUT = "10s";
807
833
  var REQUIRED_COLUMNS = ["migration_class", "phase", "group_name", "tag"];
@@ -819,6 +845,7 @@ var MigraguardDb = class {
819
845
  }
820
846
  async connect() {
821
847
  await this.client.connect();
848
+ await this.client.query(`SET statement_timeout = ${SESSION_STATEMENT_TIMEOUT_MS}`);
822
849
  }
823
850
  async close() {
824
851
  await this.client.end();
@@ -832,7 +859,7 @@ var MigraguardDb = class {
832
859
  await this.client.query(ALTER_TABLE_SQL);
833
860
  } finally {
834
861
  await this.client.query("RESET lock_timeout");
835
- await this.client.query("RESET statement_timeout");
862
+ await this.client.query(`SET statement_timeout = ${SESSION_STATEMENT_TIMEOUT_MS}`);
836
863
  }
837
864
  }
838
865
  async tableHasAllColumns() {
@@ -2490,8 +2517,7 @@ async function commandGroupStatus(config, groupName) {
2490
2517
  const db = createDb(config);
2491
2518
  try {
2492
2519
  await db.connect();
2493
- await db.ensureTable();
2494
- const allRecords = await db.getAllRecords();
2520
+ const allRecords = await safeGetAllRecords(db);
2495
2521
  let groups;
2496
2522
  if (groupName) {
2497
2523
  const state = deriveGroupState(allRecords, groupName);
@@ -2607,8 +2633,7 @@ async function commandGate(config, options) {
2607
2633
  const db = createDb(config);
2608
2634
  try {
2609
2635
  await db.connect();
2610
- await db.ensureTable();
2611
- const allRecords = await db.getAllRecords();
2636
+ const allRecords = await safeGetAllRecords(db);
2612
2637
  const groupStates = deriveAllGroupStates(allRecords);
2613
2638
  const reasons = [];
2614
2639
  for (const req of contract.required) {
@@ -5102,8 +5127,7 @@ async function commandEditable(config) {
5102
5127
  await db.connect();
5103
5128
  dbConnected = true;
5104
5129
  try {
5105
- await db.ensureTable();
5106
- const allRecords = await db.getAllRecords();
5130
+ const allRecords = await safeGetAllRecords(db);
5107
5131
  const recordsByFile = /* @__PURE__ */ new Map();
5108
5132
  for (const r of allRecords) {
5109
5133
  const list = recordsByFile.get(r.fileName) ?? [];
@@ -5155,9 +5179,8 @@ async function commandStatus(config) {
5155
5179
  let groups = [];
5156
5180
  try {
5157
5181
  await db.connect();
5158
- await db.ensureTable();
5159
5182
  const files = await scanMigrations(config);
5160
- const allRecords = await db.getAllRecords();
5183
+ const allRecords = await safeGetAllRecords(db);
5161
5184
  const recordsByFile = /* @__PURE__ */ new Map();
5162
5185
  for (const r of allRecords) {
5163
5186
  const list = recordsByFile.get(r.fileName) ?? [];
@@ -5369,9 +5392,11 @@ async function createPgShadow(conn, dbName) {
5369
5392
  port: conn.port,
5370
5393
  database: "postgres",
5371
5394
  user: conn.user,
5372
- password: conn.password
5395
+ password: conn.password,
5396
+ connectionTimeoutMillis: 1e4
5373
5397
  });
5374
5398
  await client.connect();
5399
+ await client.query("SET statement_timeout = 30000");
5375
5400
  try {
5376
5401
  await client.query(`CREATE DATABASE "${dbName}"`);
5377
5402
  } finally {
@@ -5384,9 +5409,11 @@ async function dropPgShadow(conn, dbName) {
5384
5409
  port: conn.port,
5385
5410
  database: "postgres",
5386
5411
  user: conn.user,
5387
- password: conn.password
5412
+ password: conn.password,
5413
+ connectionTimeoutMillis: 1e4
5388
5414
  });
5389
5415
  await client.connect();
5416
+ await client.query("SET statement_timeout = 30000");
5390
5417
  try {
5391
5418
  await client.query(`DROP DATABASE IF EXISTS "${dbName}"`);
5392
5419
  } finally {
@@ -5541,8 +5568,7 @@ async function getAppliedFiles(config) {
5541
5568
  const db = createDb(config);
5542
5569
  try {
5543
5570
  await db.connect();
5544
- await db.ensureTable();
5545
- const records = await db.getAllRecords();
5571
+ const records = await safeGetAllRecords(db);
5546
5572
  return new Set(
5547
5573
  records.filter((r) => r.status === "applied" || r.status === "skipped").map((r) => r.fileName)
5548
5574
  );
@@ -6608,6 +6634,7 @@ program.command("explain").description("Explain command output in human-readable
6608
6634
  reportFormat: opts.reportFormat
6609
6635
  });
6610
6636
  }));
6611
- program.parse();
6637
+ await program.parseAsync();
6638
+ process.exit(0);
6612
6639
  //# sourceMappingURL=cli.js.map
6613
6640
  //# sourceMappingURL=cli.js.map