migraguard 0.10.1 → 0.10.3

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
@@ -519,6 +519,9 @@ var init_dsl = __esm({
519
519
 
520
520
  // src/db-mysql.ts
521
521
  var ADVISORY_LOCK_KEY = "migraguard-apply";
522
+ var CONNECTION_TIMEOUT_MS = 1e4;
523
+ var SESSION_MAX_EXECUTION_TIME_MS = 3e4;
524
+ var DDL_LOCK_TIMEOUT_SEC = 5;
522
525
  var CREATE_TABLE_SQL = `
523
526
  CREATE TABLE IF NOT EXISTS schema_migrations (
524
527
  id BIGINT AUTO_INCREMENT PRIMARY KEY,
@@ -558,21 +561,45 @@ var MigraguardDbMysql = class {
558
561
  port: this.config.connection.port,
559
562
  database: this.config.connection.database,
560
563
  user: this.config.connection.user,
561
- password: this.config.connection.password
564
+ password: this.config.connection.password,
565
+ connectTimeout: CONNECTION_TIMEOUT_MS
562
566
  });
567
+ try {
568
+ await this.exec(`SET SESSION max_execution_time = ${SESSION_MAX_EXECUTION_TIME_MS}`);
569
+ } catch {
570
+ }
563
571
  }
564
572
  async close() {
565
573
  await this.connection?.end();
566
574
  }
567
575
  async ensureTable() {
568
- await this.exec(CREATE_TABLE_SQL);
576
+ if (await this.tableHasAllColumns()) return;
577
+ await this.exec(`SET SESSION lock_wait_timeout = ${DDL_LOCK_TIMEOUT_SEC}`);
578
+ try {
579
+ await this.exec(CREATE_TABLE_SQL);
580
+ const rows = await this.queryRows(
581
+ `SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
582
+ WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'schema_migrations' AND COLUMN_NAME = 'tag'`
583
+ );
584
+ if (rows.length === 0) {
585
+ await this.exec(`ALTER TABLE schema_migrations ADD COLUMN tag VARCHAR(256) NULL`);
586
+ }
587
+ } finally {
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
+ }
593
+ }
594
+ }
595
+ async tableHasAllColumns() {
569
596
  const rows = await this.queryRows(
570
597
  `SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
571
- WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'schema_migrations' AND COLUMN_NAME = 'tag'`
598
+ WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'schema_migrations'`
572
599
  );
573
- if (rows.length === 0) {
574
- await this.exec(`ALTER TABLE schema_migrations ADD COLUMN tag VARCHAR(256) NULL`);
575
- }
600
+ if (rows.length === 0) return false;
601
+ const existing = new Set(rows.map((r) => r["COLUMN_NAME"]));
602
+ return ["migration_class", "phase", "group_name", "tag"].every((c) => existing.has(c));
576
603
  }
577
604
  async acquireAdvisoryLock() {
578
605
  await this.exec(`SELECT GET_LOCK(?, -1)`, [ADVISORY_LOCK_KEY]);
@@ -783,6 +810,11 @@ function createDb(config) {
783
810
  return new MigraguardDb(config);
784
811
  }
785
812
  }
813
+ var CONNECTION_TIMEOUT_MS2 = 1e4;
814
+ var SESSION_STATEMENT_TIMEOUT_MS = 3e4;
815
+ var DDL_LOCK_TIMEOUT = "5s";
816
+ var DDL_STATEMENT_TIMEOUT = "10s";
817
+ var REQUIRED_COLUMNS = ["migration_class", "phase", "group_name", "tag"];
786
818
  var MigraguardDb = class {
787
819
  client;
788
820
  constructor(config) {
@@ -791,18 +823,38 @@ var MigraguardDb = class {
791
823
  port: config.connection.port,
792
824
  database: config.connection.database,
793
825
  user: config.connection.user,
794
- password: config.connection.password
826
+ password: config.connection.password,
827
+ connectionTimeoutMillis: CONNECTION_TIMEOUT_MS2
795
828
  });
796
829
  }
797
830
  async connect() {
798
831
  await this.client.connect();
832
+ await this.client.query(`SET statement_timeout = ${SESSION_STATEMENT_TIMEOUT_MS}`);
799
833
  }
800
834
  async close() {
801
835
  await this.client.end();
802
836
  }
803
837
  async ensureTable() {
804
- await this.client.query(CREATE_TABLE_SQL3);
805
- await this.client.query(ALTER_TABLE_SQL);
838
+ if (await this.tableHasAllColumns()) return;
839
+ await this.client.query(`SET lock_timeout = '${DDL_LOCK_TIMEOUT}'`);
840
+ await this.client.query(`SET statement_timeout = '${DDL_STATEMENT_TIMEOUT}'`);
841
+ try {
842
+ await this.client.query(CREATE_TABLE_SQL3);
843
+ await this.client.query(ALTER_TABLE_SQL);
844
+ } finally {
845
+ await this.client.query("RESET lock_timeout");
846
+ await this.client.query(`SET statement_timeout = ${SESSION_STATEMENT_TIMEOUT_MS}`);
847
+ }
848
+ }
849
+ async tableHasAllColumns() {
850
+ const result = await this.client.query(
851
+ `SELECT column_name FROM information_schema.columns
852
+ WHERE table_name = 'schema_migrations'
853
+ AND table_schema = current_schema()`
854
+ );
855
+ if (result.rows.length === 0) return false;
856
+ const existing = new Set(result.rows.map((r) => r["column_name"]));
857
+ return REQUIRED_COLUMNS.every((c) => existing.has(c));
806
858
  }
807
859
  async acquireAdvisoryLock() {
808
860
  await this.client.query(`SELECT pg_advisory_lock(hashtext($1))`, [ADVISORY_LOCK_KEY2]);
@@ -5328,9 +5380,11 @@ async function createPgShadow(conn, dbName) {
5328
5380
  port: conn.port,
5329
5381
  database: "postgres",
5330
5382
  user: conn.user,
5331
- password: conn.password
5383
+ password: conn.password,
5384
+ connectionTimeoutMillis: 1e4
5332
5385
  });
5333
5386
  await client.connect();
5387
+ await client.query("SET statement_timeout = 30000");
5334
5388
  try {
5335
5389
  await client.query(`CREATE DATABASE "${dbName}"`);
5336
5390
  } finally {
@@ -5343,9 +5397,11 @@ async function dropPgShadow(conn, dbName) {
5343
5397
  port: conn.port,
5344
5398
  database: "postgres",
5345
5399
  user: conn.user,
5346
- password: conn.password
5400
+ password: conn.password,
5401
+ connectionTimeoutMillis: 1e4
5347
5402
  });
5348
5403
  await client.connect();
5404
+ await client.query("SET statement_timeout = 30000");
5349
5405
  try {
5350
5406
  await client.query(`DROP DATABASE IF EXISTS "${dbName}"`);
5351
5407
  } finally {
@@ -6567,6 +6623,7 @@ program.command("explain").description("Explain command output in human-readable
6567
6623
  reportFormat: opts.reportFormat
6568
6624
  });
6569
6625
  }));
6570
- program.parse();
6626
+ await program.parseAsync();
6627
+ process.exit(0);
6571
6628
  //# sourceMappingURL=cli.js.map
6572
6629
  //# sourceMappingURL=cli.js.map