@event-driven-io/dumbo 0.12.4 → 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
@@ -14,8 +14,12 @@ var isFirstLetterNumericOrMinus = (str) => {
14
14
  return c >= 48 && c <= 57 || c === 45;
15
15
  };
16
16
  var bigIntReviver = (_key, value, context) => {
17
- if (typeof value === "number" && !Number.isSafeInteger(value)) {
18
- return BigInt(_nullishCoalesce(_optionalChain([context, 'optionalAccess', _2 => _2.source]), () => ( value.toString())));
17
+ if (typeof value === "number" && Number.isInteger(value) && !Number.isSafeInteger(value)) {
18
+ try {
19
+ return BigInt(_nullishCoalesce(_optionalChain([context, 'optionalAccess', _2 => _2.source]), () => ( value.toString())));
20
+ } catch (e2) {
21
+ return value;
22
+ }
19
23
  }
20
24
  if (typeof value === "string" && value.length > 15) {
21
25
  if (isFirstLetterNumericOrMinus(value)) {
@@ -23,7 +27,7 @@ var bigIntReviver = (_key, value, context) => {
23
27
  if (Number.isFinite(num) && !Number.isSafeInteger(num)) {
24
28
  try {
25
29
  return BigInt(value);
26
- } catch (e2) {
30
+ } catch (e3) {
27
31
  }
28
32
  }
29
33
  }
@@ -720,6 +724,7 @@ var runSQLMigrations = (pool, migrations, options) => pool.withTransaction(async
720
724
  connector: "PostgreSQL:pg"
721
725
  // TODO: This will need to change to support more connectors
722
726
  });
727
+ const result = { applied: [], skipped: [] };
723
728
  await databaseLock.withAcquire(
724
729
  execute,
725
730
  async () => {
@@ -728,14 +733,21 @@ var runSQLMigrations = (pool, migrations, options) => pool.withTransaction(async
728
733
  await execute.command(rawSql(sql2));
729
734
  }
730
735
  for (const migration of migrations) {
731
- 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
+ }
732
744
  }
733
745
  },
734
746
  lockOptions
735
747
  );
736
- return { success: options.dryRun ? false : true, result: void 0 };
748
+ return { success: options.dryRun ? false : true, result };
737
749
  });
738
- var runSQLMigration = async (execute, migration) => {
750
+ var runSQLMigration = async (execute, migration, options) => {
739
751
  const sql2 = combineMigrations(migration);
740
752
  const sqlHash = await getMigrationHash(sql2);
741
753
  try {
@@ -743,13 +755,32 @@ var runSQLMigration = async (execute, migration) => {
743
755
  name: migration.name,
744
756
  sqlHash
745
757
  };
746
- const wasMigrationApplied = await ensureMigrationWasNotAppliedYet(
758
+ const checkResult = await ensureMigrationWasNotAppliedYet(
747
759
  execute,
748
760
  newMigration
749
761
  );
750
- 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
+ }
751
781
  await execute.command(rawSql(sql2));
752
782
  await recordMigration(execute, newMigration);
783
+ return true;
753
784
  } catch (error) {
754
785
  tracer.error("migration-error", {
755
786
  migationName: migration.name,
@@ -769,23 +800,25 @@ var combineMigrations = (...migration) => migration.flatMap((m) => m.sqls).join(
769
800
  var ensureMigrationWasNotAppliedYet = async (execute, migration) => {
770
801
  const result = await singleOrNull(
771
802
  execute.query(
772
- 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
+ )
773
807
  )
774
808
  );
775
- if (result === null) return false;
776
- const { sqlHash } = mapToCamelCase(result);
777
- if (sqlHash !== migration.sqlHash) {
778
- throw new Error(
779
- `Migration hash mismatch for "${migration.name}". Aborting migration.`
780
- );
781
- }
782
- 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
+ };
783
816
  };
784
817
  var recordMigration = async (execute, migration) => {
785
818
  await execute.command(
786
819
  sql(
787
820
  `
788
- INSERT INTO migrations (name, sql_hash)
821
+ INSERT INTO dmb_migrations (name, sql_hash)
789
822
  VALUES (%L, %L)
790
823
  `,
791
824
  migration.name,
@@ -793,6 +826,19 @@ var recordMigration = async (execute, migration) => {
793
826
  )
794
827
  );
795
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
+ };
796
842
 
797
843
  // src/core/schema/schemaComponent.ts
798
844
  var schemaComponent = (type, migrationsOrComponents) => {
@@ -888,7 +934,7 @@ ${indent} `
888
934
  )}
889
935
  ${indent}${COLOR_BRACKETS("}")}`;
890
936
  };
891
- 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]));
892
938
 
893
939
  // src/core/tracing/index.ts
894
940
  var tracer = () => {
@@ -971,7 +1017,7 @@ var _pgconnectionstring = require('pg-connection-string'); var _pgconnectionstri
971
1017
 
972
1018
  // src/postgres/core/schema/migrations.ts
973
1019
  var migrationTableSQL = rawSql(`
974
- CREATE TABLE IF NOT EXISTS migrations (
1020
+ CREATE TABLE IF NOT EXISTS dmb_migrations (
975
1021
  id SERIAL PRIMARY KEY,
976
1022
  name VARCHAR(255) NOT NULL UNIQUE,
977
1023
  application VARCHAR(255) NOT NULL DEFAULT 'default',
@@ -998,7 +1044,8 @@ var runPostgreSQLMigrations = (pool, migrations, options) => runSQLMigrations(po
998
1044
  lockId: MIGRATIONS_LOCK_ID
999
1045
  }
1000
1046
  },
1001
- dryRun: _optionalChain([options, 'optionalAccess', _13 => _13.dryRun])
1047
+ dryRun: _optionalChain([options, 'optionalAccess', _14 => _14.dryRun]),
1048
+ ignoreMigrationHashMismatch: _optionalChain([options, 'optionalAccess', _15 => _15.ignoreMigrationHashMismatch])
1002
1049
  });
1003
1050
 
1004
1051
  // src/postgres/core/schema/schema.ts
@@ -1138,8 +1185,8 @@ async function batch(client, sqlOrSqls, options) {
1138
1185
  const results = Array(
1139
1186
  sqls.length
1140
1187
  );
1141
- if (_optionalChain([options, 'optionalAccess', _14 => _14.timeoutMs])) {
1142
- 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])}`);
1143
1190
  }
1144
1191
  for (let i = 0; i < sqls.length; i++) {
1145
1192
  tracer.info("db:sql:query", { sql: sqls[i] });
@@ -1160,12 +1207,12 @@ var nodePostgresTransaction = (connection) => (getClient, options) => ({
1160
1207
  commit: async () => {
1161
1208
  const client = await getClient;
1162
1209
  await client.query("COMMIT");
1163
- 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)]);
1164
1211
  },
1165
1212
  rollback: async (error) => {
1166
1213
  const client = await getClient;
1167
1214
  await client.query("ROLLBACK");
1168
- 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)]);
1169
1216
  },
1170
1217
  execute: sqlExecutor(nodePostgresSQLExecutor(), {
1171
1218
  connect: () => getClient
@@ -1224,14 +1271,14 @@ var checkConnection = async (connectionString) => {
1224
1271
 
1225
1272
  var arePgTypesSet = false;
1226
1273
  var setNodePostgresTypeParser = (options) => {
1227
- if (arePgTypesSet && !_optionalChain([options, 'optionalAccess', _22 => _22.force])) return;
1274
+ if (arePgTypesSet && !_optionalChain([options, 'optionalAccess', _24 => _24.force])) return;
1228
1275
  arePgTypesSet = true;
1229
1276
  _pg2.default.types.setTypeParser(20, (val) => BigInt(val));
1230
1277
  _pg2.default.types.setTypeParser(3802, (val) => JSONSerializer.deserialize(val));
1231
1278
  _pg2.default.types.setTypeParser(114, (val) => JSONSerializer.deserialize(val));
1232
1279
  };
1233
1280
  var setNodePostgresTypeRawParser = (options) => {
1234
- if (arePgTypesSet && !_optionalChain([options, 'optionalAccess', _23 => _23.force])) return;
1281
+ if (arePgTypesSet && !_optionalChain([options, 'optionalAccess', _25 => _25.force])) return;
1235
1282
  arePgTypesSet = true;
1236
1283
  _pg2.default.types.setTypeParser(20, (val) => BigInt(val));
1237
1284
  _pg2.default.types.setTypeParser(3802, (val) => RawJSONSerializer.deserialize(val));