@event-driven-io/dumbo 0.12.5 → 0.12.7

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/index.cjs CHANGED
@@ -724,22 +724,33 @@ var runSQLMigrations = (pool, migrations, options) => pool.withTransaction(async
724
724
  connector: "PostgreSQL:pg"
725
725
  // TODO: This will need to change to support more connectors
726
726
  });
727
+ const result = { applied: [], skipped: [] };
727
728
  await databaseLock.withAcquire(
728
729
  execute,
729
730
  async () => {
730
731
  for (const migration of coreMigrations) {
731
732
  const sql2 = combineMigrations(migration);
732
- await execute.command(rawSql(sql2));
733
+ await execute.command(rawSql(sql2), {
734
+ timeoutMs: options.migrationTimeoutMs
735
+ });
733
736
  }
734
737
  for (const migration of migrations) {
735
- await runSQLMigration(execute, migration);
738
+ const wasApplied = await runSQLMigration(execute, migration, {
739
+ ignoreMigrationHashMismatch: _nullishCoalesce(options.ignoreMigrationHashMismatch, () => ( false)),
740
+ migrationTimeoutMs: options.migrationTimeoutMs
741
+ });
742
+ if (wasApplied) {
743
+ result.applied.push(migration);
744
+ } else {
745
+ result.skipped.push(migration);
746
+ }
736
747
  }
737
748
  },
738
749
  lockOptions
739
750
  );
740
- return { success: options.dryRun ? false : true, result: void 0 };
751
+ return { success: options.dryRun ? false : true, result };
741
752
  });
742
- var runSQLMigration = async (execute, migration) => {
753
+ var runSQLMigration = async (execute, migration, options) => {
743
754
  const sql2 = combineMigrations(migration);
744
755
  const sqlHash = await getMigrationHash(sql2);
745
756
  try {
@@ -747,13 +758,34 @@ var runSQLMigration = async (execute, migration) => {
747
758
  name: migration.name,
748
759
  sqlHash
749
760
  };
750
- const wasMigrationApplied = await ensureMigrationWasNotAppliedYet(
761
+ const checkResult = await ensureMigrationWasNotAppliedYet(
751
762
  execute,
752
763
  newMigration
753
764
  );
754
- if (wasMigrationApplied) return;
755
- await execute.command(rawSql(sql2));
765
+ if (checkResult.exists === true) {
766
+ if (checkResult.hashesMatch === true) {
767
+ tracer.info("migration-already-applied", {
768
+ migrationName: migration.name
769
+ });
770
+ return false;
771
+ }
772
+ if (_optionalChain([options, 'optionalAccess', _12 => _12.ignoreMigrationHashMismatch]) !== true)
773
+ throw new Error(
774
+ `Migration hash mismatch for "${migration.name}". Aborting migration.`
775
+ );
776
+ tracer.warn("migration-hash-mismatch", {
777
+ migrationName: migration.name,
778
+ expectedHash: sqlHash,
779
+ actualHash: checkResult.hashFromDB
780
+ });
781
+ await updateMigrationHash(execute, newMigration);
782
+ return false;
783
+ }
784
+ await execute.command(rawSql(sql2), {
785
+ timeoutMs: _optionalChain([options, 'optionalAccess', _13 => _13.migrationTimeoutMs])
786
+ });
756
787
  await recordMigration(execute, newMigration);
788
+ return true;
757
789
  } catch (error) {
758
790
  tracer.error("migration-error", {
759
791
  migationName: migration.name,
@@ -773,23 +805,25 @@ var combineMigrations = (...migration) => migration.flatMap((m) => m.sqls).join(
773
805
  var ensureMigrationWasNotAppliedYet = async (execute, migration) => {
774
806
  const result = await singleOrNull(
775
807
  execute.query(
776
- sql(`SELECT sql_hash FROM migrations WHERE name = %L`, migration.name)
808
+ sql(
809
+ `SELECT sql_hash as "sqlHash" FROM dmb_migrations WHERE name = %L`,
810
+ migration.name
811
+ )
777
812
  )
778
813
  );
779
- if (result === null) return false;
780
- const { sqlHash } = mapToCamelCase(result);
781
- if (sqlHash !== migration.sqlHash) {
782
- throw new Error(
783
- `Migration hash mismatch for "${migration.name}". Aborting migration.`
784
- );
785
- }
786
- return true;
814
+ if (result === null) return { exists: false };
815
+ const { sqlHash } = result;
816
+ return {
817
+ exists: true,
818
+ hashesMatch: sqlHash === migration.sqlHash,
819
+ hashFromDB: sqlHash
820
+ };
787
821
  };
788
822
  var recordMigration = async (execute, migration) => {
789
823
  await execute.command(
790
824
  sql(
791
825
  `
792
- INSERT INTO migrations (name, sql_hash)
826
+ INSERT INTO dmb_migrations (name, sql_hash)
793
827
  VALUES (%L, %L)
794
828
  `,
795
829
  migration.name,
@@ -797,6 +831,19 @@ var recordMigration = async (execute, migration) => {
797
831
  )
798
832
  );
799
833
  };
834
+ var updateMigrationHash = async (execute, migration) => {
835
+ await execute.command(
836
+ sql(
837
+ `
838
+ UPDATE dmb_migrations
839
+ SET sql_hash = %L, timestamp = NOW()
840
+ WHERE name = %L
841
+ `,
842
+ migration.sqlHash,
843
+ migration.name
844
+ )
845
+ );
846
+ };
800
847
 
801
848
  // src/core/schema/schemaComponent.ts
802
849
  var schemaComponent = (type, migrationsOrComponents) => {
@@ -892,7 +939,7 @@ ${indent} `
892
939
  )}
893
940
  ${indent}${COLOR_BRACKETS("}")}`;
894
941
  };
895
- var prettyJson = (obj, options) => formatJson(obj, 0, _optionalChain([options, 'optionalAccess', _12 => _12.handleMultiline]));
942
+ var prettyJson = (obj, options) => formatJson(obj, 0, _optionalChain([options, 'optionalAccess', _14 => _14.handleMultiline]));
896
943
 
897
944
  // src/core/tracing/index.ts
898
945
  var tracer = () => {
@@ -975,7 +1022,7 @@ var _pgconnectionstring = require('pg-connection-string'); var _pgconnectionstri
975
1022
 
976
1023
  // src/postgres/core/schema/migrations.ts
977
1024
  var migrationTableSQL = rawSql(`
978
- CREATE TABLE IF NOT EXISTS migrations (
1025
+ CREATE TABLE IF NOT EXISTS dmb_migrations (
979
1026
  id SERIAL PRIMARY KEY,
980
1027
  name VARCHAR(255) NOT NULL UNIQUE,
981
1028
  application VARCHAR(255) NOT NULL DEFAULT 'default',
@@ -1002,7 +1049,9 @@ var runPostgreSQLMigrations = (pool, migrations, options) => runSQLMigrations(po
1002
1049
  lockId: MIGRATIONS_LOCK_ID
1003
1050
  }
1004
1051
  },
1005
- dryRun: _optionalChain([options, 'optionalAccess', _13 => _13.dryRun])
1052
+ dryRun: _optionalChain([options, 'optionalAccess', _15 => _15.dryRun]),
1053
+ ignoreMigrationHashMismatch: _optionalChain([options, 'optionalAccess', _16 => _16.ignoreMigrationHashMismatch]),
1054
+ migrationTimeoutMs: _optionalChain([options, 'optionalAccess', _17 => _17.migrationTimeoutMs])
1006
1055
  });
1007
1056
 
1008
1057
  // src/postgres/core/schema/schema.ts
@@ -1142,8 +1191,8 @@ async function batch(client, sqlOrSqls, options) {
1142
1191
  const results = Array(
1143
1192
  sqls.length
1144
1193
  );
1145
- if (_optionalChain([options, 'optionalAccess', _14 => _14.timeoutMs])) {
1146
- await client.query(`SET statement_timeout = ${_optionalChain([options, 'optionalAccess', _15 => _15.timeoutMs])}`);
1194
+ if (_optionalChain([options, 'optionalAccess', _18 => _18.timeoutMs])) {
1195
+ await client.query(`SET statement_timeout = ${_optionalChain([options, 'optionalAccess', _19 => _19.timeoutMs])}`);
1147
1196
  }
1148
1197
  for (let i = 0; i < sqls.length; i++) {
1149
1198
  tracer.info("db:sql:query", { sql: sqls[i] });
@@ -1164,12 +1213,12 @@ var nodePostgresTransaction = (connection) => (getClient, options) => ({
1164
1213
  commit: async () => {
1165
1214
  const client = await getClient;
1166
1215
  await client.query("COMMIT");
1167
- if (_optionalChain([options, 'optionalAccess', _16 => _16.close])) await _optionalChain([options, 'optionalAccess', _17 => _17.close, 'call', _18 => _18(client)]);
1216
+ if (_optionalChain([options, 'optionalAccess', _20 => _20.close])) await _optionalChain([options, 'optionalAccess', _21 => _21.close, 'call', _22 => _22(client)]);
1168
1217
  },
1169
1218
  rollback: async (error) => {
1170
1219
  const client = await getClient;
1171
1220
  await client.query("ROLLBACK");
1172
- if (_optionalChain([options, 'optionalAccess', _19 => _19.close])) await _optionalChain([options, 'optionalAccess', _20 => _20.close, 'call', _21 => _21(client, error)]);
1221
+ if (_optionalChain([options, 'optionalAccess', _23 => _23.close])) await _optionalChain([options, 'optionalAccess', _24 => _24.close, 'call', _25 => _25(client, error)]);
1173
1222
  },
1174
1223
  execute: sqlExecutor(nodePostgresSQLExecutor(), {
1175
1224
  connect: () => getClient
@@ -1228,14 +1277,14 @@ var checkConnection = async (connectionString) => {
1228
1277
 
1229
1278
  var arePgTypesSet = false;
1230
1279
  var setNodePostgresTypeParser = (options) => {
1231
- if (arePgTypesSet && !_optionalChain([options, 'optionalAccess', _22 => _22.force])) return;
1280
+ if (arePgTypesSet && !_optionalChain([options, 'optionalAccess', _26 => _26.force])) return;
1232
1281
  arePgTypesSet = true;
1233
1282
  _pg2.default.types.setTypeParser(20, (val) => BigInt(val));
1234
1283
  _pg2.default.types.setTypeParser(3802, (val) => JSONSerializer.deserialize(val));
1235
1284
  _pg2.default.types.setTypeParser(114, (val) => JSONSerializer.deserialize(val));
1236
1285
  };
1237
1286
  var setNodePostgresTypeRawParser = (options) => {
1238
- if (arePgTypesSet && !_optionalChain([options, 'optionalAccess', _23 => _23.force])) return;
1287
+ if (arePgTypesSet && !_optionalChain([options, 'optionalAccess', _27 => _27.force])) return;
1239
1288
  arePgTypesSet = true;
1240
1289
  _pg2.default.types.setTypeParser(20, (val) => BigInt(val));
1241
1290
  _pg2.default.types.setTypeParser(3802, (val) => RawJSONSerializer.deserialize(val));