migraguard 0.10.1 → 0.10.2
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 +50 -9
- package/dist/cli.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +50 -9
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -519,6 +519,8 @@ 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 DDL_LOCK_TIMEOUT_SEC = 5;
|
|
522
524
|
var CREATE_TABLE_SQL = `
|
|
523
525
|
CREATE TABLE IF NOT EXISTS schema_migrations (
|
|
524
526
|
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
|
@@ -558,21 +560,37 @@ var MigraguardDbMysql = class {
|
|
|
558
560
|
port: this.config.connection.port,
|
|
559
561
|
database: this.config.connection.database,
|
|
560
562
|
user: this.config.connection.user,
|
|
561
|
-
password: this.config.connection.password
|
|
563
|
+
password: this.config.connection.password,
|
|
564
|
+
connectTimeout: CONNECTION_TIMEOUT_MS
|
|
562
565
|
});
|
|
563
566
|
}
|
|
564
567
|
async close() {
|
|
565
568
|
await this.connection?.end();
|
|
566
569
|
}
|
|
567
570
|
async ensureTable() {
|
|
568
|
-
await this.
|
|
571
|
+
if (await this.tableHasAllColumns()) return;
|
|
572
|
+
await this.exec(`SET SESSION lock_wait_timeout = ${DDL_LOCK_TIMEOUT_SEC}`);
|
|
573
|
+
try {
|
|
574
|
+
await this.exec(CREATE_TABLE_SQL);
|
|
575
|
+
const rows = await this.queryRows(
|
|
576
|
+
`SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
|
|
577
|
+
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'schema_migrations' AND COLUMN_NAME = 'tag'`
|
|
578
|
+
);
|
|
579
|
+
if (rows.length === 0) {
|
|
580
|
+
await this.exec(`ALTER TABLE schema_migrations ADD COLUMN tag VARCHAR(256) NULL`);
|
|
581
|
+
}
|
|
582
|
+
} finally {
|
|
583
|
+
await this.exec(`SET SESSION lock_wait_timeout = DEFAULT`);
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
async tableHasAllColumns() {
|
|
569
587
|
const rows = await this.queryRows(
|
|
570
588
|
`SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
|
|
571
|
-
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'schema_migrations'
|
|
589
|
+
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'schema_migrations'`
|
|
572
590
|
);
|
|
573
|
-
if (rows.length === 0)
|
|
574
|
-
|
|
575
|
-
|
|
591
|
+
if (rows.length === 0) return false;
|
|
592
|
+
const existing = new Set(rows.map((r) => r["COLUMN_NAME"]));
|
|
593
|
+
return ["migration_class", "phase", "group_name", "tag"].every((c) => existing.has(c));
|
|
576
594
|
}
|
|
577
595
|
async acquireAdvisoryLock() {
|
|
578
596
|
await this.exec(`SELECT GET_LOCK(?, -1)`, [ADVISORY_LOCK_KEY]);
|
|
@@ -783,6 +801,10 @@ function createDb(config) {
|
|
|
783
801
|
return new MigraguardDb(config);
|
|
784
802
|
}
|
|
785
803
|
}
|
|
804
|
+
var CONNECTION_TIMEOUT_MS2 = 1e4;
|
|
805
|
+
var DDL_LOCK_TIMEOUT = "5s";
|
|
806
|
+
var DDL_STATEMENT_TIMEOUT = "10s";
|
|
807
|
+
var REQUIRED_COLUMNS = ["migration_class", "phase", "group_name", "tag"];
|
|
786
808
|
var MigraguardDb = class {
|
|
787
809
|
client;
|
|
788
810
|
constructor(config) {
|
|
@@ -791,7 +813,8 @@ var MigraguardDb = class {
|
|
|
791
813
|
port: config.connection.port,
|
|
792
814
|
database: config.connection.database,
|
|
793
815
|
user: config.connection.user,
|
|
794
|
-
password: config.connection.password
|
|
816
|
+
password: config.connection.password,
|
|
817
|
+
connectionTimeoutMillis: CONNECTION_TIMEOUT_MS2
|
|
795
818
|
});
|
|
796
819
|
}
|
|
797
820
|
async connect() {
|
|
@@ -801,8 +824,26 @@ var MigraguardDb = class {
|
|
|
801
824
|
await this.client.end();
|
|
802
825
|
}
|
|
803
826
|
async ensureTable() {
|
|
804
|
-
await this.
|
|
805
|
-
await this.client.query(
|
|
827
|
+
if (await this.tableHasAllColumns()) return;
|
|
828
|
+
await this.client.query(`SET lock_timeout = '${DDL_LOCK_TIMEOUT}'`);
|
|
829
|
+
await this.client.query(`SET statement_timeout = '${DDL_STATEMENT_TIMEOUT}'`);
|
|
830
|
+
try {
|
|
831
|
+
await this.client.query(CREATE_TABLE_SQL3);
|
|
832
|
+
await this.client.query(ALTER_TABLE_SQL);
|
|
833
|
+
} finally {
|
|
834
|
+
await this.client.query("RESET lock_timeout");
|
|
835
|
+
await this.client.query("RESET statement_timeout");
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
async tableHasAllColumns() {
|
|
839
|
+
const result = await this.client.query(
|
|
840
|
+
`SELECT column_name FROM information_schema.columns
|
|
841
|
+
WHERE table_name = 'schema_migrations'
|
|
842
|
+
AND table_schema = current_schema()`
|
|
843
|
+
);
|
|
844
|
+
if (result.rows.length === 0) return false;
|
|
845
|
+
const existing = new Set(result.rows.map((r) => r["column_name"]));
|
|
846
|
+
return REQUIRED_COLUMNS.every((c) => existing.has(c));
|
|
806
847
|
}
|
|
807
848
|
async acquireAdvisoryLock() {
|
|
808
849
|
await this.client.query(`SELECT pg_advisory_lock(hashtext($1))`, [ADVISORY_LOCK_KEY2]);
|