@noego/proper 0.2.1 → 0.2.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/bin/index.mjs CHANGED
@@ -108,6 +108,16 @@ var DatabaseConnectionError = class _DatabaseConnectionError extends MigrationEr
108
108
  return new _DatabaseConnectionError(`Authentication failed for ${dbType} database`);
109
109
  }
110
110
  };
111
+ var MigrationStampError = class _MigrationStampError extends MigrationError {
112
+ constructor(migrationKey) {
113
+ super(
114
+ `Migration '${migrationKey}' cannot be migrated: its timestamp ends in "000", so it does not look like it was generated by 'proper create'. Recreate it with 'proper create <name>' and move the SQL into the generated files.`
115
+ );
116
+ this.migrationKey = migrationKey;
117
+ this.name = "MigrationStampError";
118
+ Object.setPrototypeOf(this, _MigrationStampError.prototype);
119
+ }
120
+ };
111
121
  var MigrationExecutionError = class _MigrationExecutionError extends MigrationError {
112
122
  constructor(message, migrationName, sql, originalError) {
113
123
  let fullMessage = `Migration Execution Error${migrationName ? ` in '${migrationName}'` : ""}: ${message}`;
@@ -377,6 +387,13 @@ function isMigrationFile(fileName) {
377
387
  function canonicalMigrationKey(value) {
378
388
  return value.replace(MIGRATION_FILE_REGEX, "").toLowerCase();
379
389
  }
390
+ function mintMigrationStamp(now = Date.now()) {
391
+ return now % 10 === 0 ? now + 1 : now;
392
+ }
393
+ function isHandStampedKey(key) {
394
+ const stamp = key.split("_")[0];
395
+ return /^\d+$/.test(stamp) && /000$/.test(stamp);
396
+ }
380
397
  function resolveMigrationFile(directory, baseName, direction, dialect) {
381
398
  const dialectExt = dialect === "sql" ? "mysql" : dialect;
382
399
  const dialectFile = path.join(directory, `${baseName}.${dialectExt}.${direction}.sql`);
@@ -1288,7 +1305,7 @@ var PatchCreator = class _PatchCreator {
1288
1305
  if (!fs5.existsSync(this.patchFolder)) {
1289
1306
  fs5.mkdirSync(this.patchFolder, { recursive: true });
1290
1307
  }
1291
- let stamp = Date.now();
1308
+ let stamp = mintMigrationStamp();
1292
1309
  for (let attempt = 0; attempt < 1e3; attempt++) {
1293
1310
  const filePath = path4.join(this.patchFolder, `${stamp}_${normalized}.yaml`);
1294
1311
  try {
@@ -1296,7 +1313,7 @@ var PatchCreator = class _PatchCreator {
1296
1313
  return filePath;
1297
1314
  } catch (error) {
1298
1315
  if (error && error.code === "EEXIST") {
1299
- stamp += 1;
1316
+ stamp = mintMigrationStamp(stamp + 1);
1300
1317
  continue;
1301
1318
  }
1302
1319
  throw error;
@@ -1891,6 +1908,14 @@ var MySQLMigrationRunner = class {
1891
1908
  migrate(migrationNodes, forward) {
1892
1909
  return __async(this, null, function* () {
1893
1910
  yield this.setup();
1911
+ if (forward) {
1912
+ for (const node of migrationNodes) {
1913
+ const key = node.get_key();
1914
+ if (isHandStampedKey(key) && !(yield node.status()).completed) {
1915
+ throw new MigrationStampError(key);
1916
+ }
1917
+ }
1918
+ }
1894
1919
  for (let node of migrationNodes) {
1895
1920
  try {
1896
1921
  if (forward) {
@@ -2028,7 +2053,7 @@ var MigrationCreator = class {
2028
2053
  if (!fs6.existsSync(this.config.migration_folder)) {
2029
2054
  fs6.mkdirSync(this.config.migration_folder, { recursive: true });
2030
2055
  }
2031
- const now_timestamp = Date.now();
2056
+ const now_timestamp = mintMigrationStamp();
2032
2057
  const filename_up = `${now_timestamp}_${name}.up.sql`;
2033
2058
  const filename_down = `${now_timestamp}_${name}.down.sql`;
2034
2059
  fs6.writeFileSync(`${this.config.migration_folder}/${filename_up}`, `