@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/cli.js CHANGED
@@ -192,6 +192,16 @@ var DatabaseConnectionError = class _DatabaseConnectionError extends MigrationEr
192
192
  return new _DatabaseConnectionError(`Authentication failed for ${dbType} database`);
193
193
  }
194
194
  };
195
+ var MigrationStampError = class _MigrationStampError extends MigrationError {
196
+ constructor(migrationKey) {
197
+ super(
198
+ `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.`
199
+ );
200
+ this.migrationKey = migrationKey;
201
+ this.name = "MigrationStampError";
202
+ Object.setPrototypeOf(this, _MigrationStampError.prototype);
203
+ }
204
+ };
195
205
  var MigrationExecutionError = class _MigrationExecutionError extends MigrationError {
196
206
  constructor(message, migrationName, sql, originalError) {
197
207
  let fullMessage = `Migration Execution Error${migrationName ? ` in '${migrationName}'` : ""}: ${message}`;
@@ -461,6 +471,13 @@ function isMigrationFile(fileName) {
461
471
  function canonicalMigrationKey(value) {
462
472
  return value.replace(MIGRATION_FILE_REGEX, "").toLowerCase();
463
473
  }
474
+ function mintMigrationStamp(now = Date.now()) {
475
+ return now % 10 === 0 ? now + 1 : now;
476
+ }
477
+ function isHandStampedKey(key) {
478
+ const stamp = key.split("_")[0];
479
+ return /^\d+$/.test(stamp) && /000$/.test(stamp);
480
+ }
464
481
  function resolveMigrationFile(directory, baseName, direction, dialect) {
465
482
  const dialectExt = dialect === "sql" ? "mysql" : dialect;
466
483
  const dialectFile = import_path.default.join(directory, `${baseName}.${dialectExt}.${direction}.sql`);
@@ -1357,7 +1374,7 @@ var PatchCreator = class _PatchCreator {
1357
1374
  if (!import_fs5.default.existsSync(this.patchFolder)) {
1358
1375
  import_fs5.default.mkdirSync(this.patchFolder, { recursive: true });
1359
1376
  }
1360
- let stamp = Date.now();
1377
+ let stamp = mintMigrationStamp();
1361
1378
  for (let attempt = 0; attempt < 1e3; attempt++) {
1362
1379
  const filePath = import_path4.default.join(this.patchFolder, `${stamp}_${normalized}.yaml`);
1363
1380
  try {
@@ -1365,7 +1382,7 @@ var PatchCreator = class _PatchCreator {
1365
1382
  return filePath;
1366
1383
  } catch (error) {
1367
1384
  if (error && error.code === "EEXIST") {
1368
- stamp += 1;
1385
+ stamp = mintMigrationStamp(stamp + 1);
1369
1386
  continue;
1370
1387
  }
1371
1388
  throw error;
@@ -1956,6 +1973,14 @@ var MySQLMigrationRunner = class {
1956
1973
  migrate(migrationNodes, forward) {
1957
1974
  return __async(this, null, function* () {
1958
1975
  yield this.setup();
1976
+ if (forward) {
1977
+ for (const node of migrationNodes) {
1978
+ const key = node.get_key();
1979
+ if (isHandStampedKey(key) && !(yield node.status()).completed) {
1980
+ throw new MigrationStampError(key);
1981
+ }
1982
+ }
1983
+ }
1959
1984
  for (let node of migrationNodes) {
1960
1985
  try {
1961
1986
  if (forward) {
@@ -2093,7 +2118,7 @@ var MigrationCreator = class {
2093
2118
  if (!import_fs6.default.existsSync(this.config.migration_folder)) {
2094
2119
  import_fs6.default.mkdirSync(this.config.migration_folder, { recursive: true });
2095
2120
  }
2096
- const now_timestamp = Date.now();
2121
+ const now_timestamp = mintMigrationStamp();
2097
2122
  const filename_up = `${now_timestamp}_${name}.up.sql`;
2098
2123
  const filename_down = `${now_timestamp}_${name}.down.sql`;
2099
2124
  import_fs6.default.writeFileSync(`${this.config.migration_folder}/${filename_up}`, `