@event-driven-io/dumbo 0.12.5 → 0.12.6

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,6 +724,7 @@ 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 () => {
@@ -732,14 +733,21 @@ var runSQLMigrations = (pool, migrations, options) => pool.withTransaction(async
732
733
  await execute.command(rawSql(sql2));
733
734
  }
734
735
  for (const migration of migrations) {
735
- await runSQLMigration(execute, migration);
736
+ const wasApplied = await runSQLMigration(execute, migration, {
737
+ ignoreMigrationHashMismatch: _nullishCoalesce(options.ignoreMigrationHashMismatch, () => ( false))
738
+ });
739
+ if (wasApplied) {
740
+ result.applied.push(migration);
741
+ } else {
742
+ result.skipped.push(migration);
743
+ }
736
744
  }
737
745
  },
738
746
  lockOptions
739
747
  );
740
- return { success: options.dryRun ? false : true, result: void 0 };
748
+ return { success: options.dryRun ? false : true, result };
741
749
  });
742
- var runSQLMigration = async (execute, migration) => {
750
+ var runSQLMigration = async (execute, migration, options) => {
743
751
  const sql2 = combineMigrations(migration);
744
752
  const sqlHash = await getMigrationHash(sql2);
745
753
  try {
@@ -747,13 +755,32 @@ var runSQLMigration = async (execute, migration) => {
747
755
  name: migration.name,
748
756
  sqlHash
749
757
  };
750
- const wasMigrationApplied = await ensureMigrationWasNotAppliedYet(
758
+ const checkResult = await ensureMigrationWasNotAppliedYet(
751
759
  execute,
752
760
  newMigration
753
761
  );
754
- if (wasMigrationApplied) return;
762
+ if (checkResult.exists === true) {
763
+ if (checkResult.hashesMatch === true) {
764
+ tracer.info("migration-already-applied", {
765
+ migrationName: migration.name
766
+ });
767
+ return false;
768
+ }
769
+ if (_optionalChain([options, 'optionalAccess', _12 => _12.ignoreMigrationHashMismatch]) !== true)
770
+ throw new Error(
771
+ `Migration hash mismatch for "${migration.name}". Aborting migration.`
772
+ );
773
+ tracer.warn("migration-hash-mismatch", {
774
+ migrationName: migration.name,
775
+ expectedHash: sqlHash,
776
+ actualHash: checkResult.hashFromDB
777
+ });
778
+ await updateMigrationHash(execute, newMigration);
779
+ return false;
780
+ }
755
781
  await execute.command(rawSql(sql2));
756
782
  await recordMigration(execute, newMigration);
783
+ return true;
757
784
  } catch (error) {
758
785
  tracer.error("migration-error", {
759
786
  migationName: migration.name,
@@ -773,23 +800,25 @@ var combineMigrations = (...migration) => migration.flatMap((m) => m.sqls).join(
773
800
  var ensureMigrationWasNotAppliedYet = async (execute, migration) => {
774
801
  const result = await singleOrNull(
775
802
  execute.query(
776
- sql(`SELECT sql_hash FROM migrations WHERE name = %L`, migration.name)
803
+ sql(
804
+ `SELECT sql_hash as "sqlHash" FROM dmb_migrations WHERE name = %L`,
805
+ migration.name
806
+ )
777
807
  )
778
808
  );
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;
809
+ if (result === null) return { exists: false };
810
+ const { sqlHash } = result;
811
+ return {
812
+ exists: true,
813
+ hashesMatch: sqlHash === migration.sqlHash,
814
+ hashFromDB: sqlHash
815
+ };
787
816
  };
788
817
  var recordMigration = async (execute, migration) => {
789
818
  await execute.command(
790
819
  sql(
791
820
  `
792
- INSERT INTO migrations (name, sql_hash)
821
+ INSERT INTO dmb_migrations (name, sql_hash)
793
822
  VALUES (%L, %L)
794
823
  `,
795
824
  migration.name,
@@ -797,6 +826,19 @@ var recordMigration = async (execute, migration) => {
797
826
  )
798
827
  );
799
828
  };
829
+ var updateMigrationHash = async (execute, migration) => {
830
+ await execute.command(
831
+ sql(
832
+ `
833
+ UPDATE dmb_migrations
834
+ SET sql_hash = %L, timestamp = NOW()
835
+ WHERE name = %L
836
+ `,
837
+ migration.sqlHash,
838
+ migration.name
839
+ )
840
+ );
841
+ };
800
842
 
801
843
  // src/core/schema/schemaComponent.ts
802
844
  var schemaComponent = (type, migrationsOrComponents) => {
@@ -892,7 +934,7 @@ ${indent} `
892
934
  )}
893
935
  ${indent}${COLOR_BRACKETS("}")}`;
894
936
  };
895
- var prettyJson = (obj, options) => formatJson(obj, 0, _optionalChain([options, 'optionalAccess', _12 => _12.handleMultiline]));
937
+ var prettyJson = (obj, options) => formatJson(obj, 0, _optionalChain([options, 'optionalAccess', _13 => _13.handleMultiline]));
896
938
 
897
939
  // src/core/tracing/index.ts
898
940
  var tracer = () => {
@@ -975,7 +1017,7 @@ var _pgconnectionstring = require('pg-connection-string'); var _pgconnectionstri
975
1017
 
976
1018
  // src/postgres/core/schema/migrations.ts
977
1019
  var migrationTableSQL = rawSql(`
978
- CREATE TABLE IF NOT EXISTS migrations (
1020
+ CREATE TABLE IF NOT EXISTS dmb_migrations (
979
1021
  id SERIAL PRIMARY KEY,
980
1022
  name VARCHAR(255) NOT NULL UNIQUE,
981
1023
  application VARCHAR(255) NOT NULL DEFAULT 'default',
@@ -1002,7 +1044,8 @@ var runPostgreSQLMigrations = (pool, migrations, options) => runSQLMigrations(po
1002
1044
  lockId: MIGRATIONS_LOCK_ID
1003
1045
  }
1004
1046
  },
1005
- dryRun: _optionalChain([options, 'optionalAccess', _13 => _13.dryRun])
1047
+ dryRun: _optionalChain([options, 'optionalAccess', _14 => _14.dryRun]),
1048
+ ignoreMigrationHashMismatch: _optionalChain([options, 'optionalAccess', _15 => _15.ignoreMigrationHashMismatch])
1006
1049
  });
1007
1050
 
1008
1051
  // src/postgres/core/schema/schema.ts
@@ -1142,8 +1185,8 @@ async function batch(client, sqlOrSqls, options) {
1142
1185
  const results = Array(
1143
1186
  sqls.length
1144
1187
  );
1145
- if (_optionalChain([options, 'optionalAccess', _14 => _14.timeoutMs])) {
1146
- await client.query(`SET statement_timeout = ${_optionalChain([options, 'optionalAccess', _15 => _15.timeoutMs])}`);
1188
+ if (_optionalChain([options, 'optionalAccess', _16 => _16.timeoutMs])) {
1189
+ await client.query(`SET statement_timeout = ${_optionalChain([options, 'optionalAccess', _17 => _17.timeoutMs])}`);
1147
1190
  }
1148
1191
  for (let i = 0; i < sqls.length; i++) {
1149
1192
  tracer.info("db:sql:query", { sql: sqls[i] });
@@ -1164,12 +1207,12 @@ var nodePostgresTransaction = (connection) => (getClient, options) => ({
1164
1207
  commit: async () => {
1165
1208
  const client = await getClient;
1166
1209
  await client.query("COMMIT");
1167
- if (_optionalChain([options, 'optionalAccess', _16 => _16.close])) await _optionalChain([options, 'optionalAccess', _17 => _17.close, 'call', _18 => _18(client)]);
1210
+ if (_optionalChain([options, 'optionalAccess', _18 => _18.close])) await _optionalChain([options, 'optionalAccess', _19 => _19.close, 'call', _20 => _20(client)]);
1168
1211
  },
1169
1212
  rollback: async (error) => {
1170
1213
  const client = await getClient;
1171
1214
  await client.query("ROLLBACK");
1172
- if (_optionalChain([options, 'optionalAccess', _19 => _19.close])) await _optionalChain([options, 'optionalAccess', _20 => _20.close, 'call', _21 => _21(client, error)]);
1215
+ if (_optionalChain([options, 'optionalAccess', _21 => _21.close])) await _optionalChain([options, 'optionalAccess', _22 => _22.close, 'call', _23 => _23(client, error)]);
1173
1216
  },
1174
1217
  execute: sqlExecutor(nodePostgresSQLExecutor(), {
1175
1218
  connect: () => getClient
@@ -1228,14 +1271,14 @@ var checkConnection = async (connectionString) => {
1228
1271
 
1229
1272
  var arePgTypesSet = false;
1230
1273
  var setNodePostgresTypeParser = (options) => {
1231
- if (arePgTypesSet && !_optionalChain([options, 'optionalAccess', _22 => _22.force])) return;
1274
+ if (arePgTypesSet && !_optionalChain([options, 'optionalAccess', _24 => _24.force])) return;
1232
1275
  arePgTypesSet = true;
1233
1276
  _pg2.default.types.setTypeParser(20, (val) => BigInt(val));
1234
1277
  _pg2.default.types.setTypeParser(3802, (val) => JSONSerializer.deserialize(val));
1235
1278
  _pg2.default.types.setTypeParser(114, (val) => JSONSerializer.deserialize(val));
1236
1279
  };
1237
1280
  var setNodePostgresTypeRawParser = (options) => {
1238
- if (arePgTypesSet && !_optionalChain([options, 'optionalAccess', _23 => _23.force])) return;
1281
+ if (arePgTypesSet && !_optionalChain([options, 'optionalAccess', _25 => _25.force])) return;
1239
1282
  arePgTypesSet = true;
1240
1283
  _pg2.default.types.setTypeParser(20, (val) => BigInt(val));
1241
1284
  _pg2.default.types.setTypeParser(3802, (val) => RawJSONSerializer.deserialize(val));