@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.js CHANGED
@@ -157,6 +157,16 @@ var DatabaseConnectionError = class _DatabaseConnectionError extends MigrationEr
157
157
  return new _DatabaseConnectionError(`Authentication failed for ${dbType} database`);
158
158
  }
159
159
  };
160
+ var MigrationStampError = class _MigrationStampError extends MigrationError {
161
+ constructor(migrationKey) {
162
+ super(
163
+ `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.`
164
+ );
165
+ this.migrationKey = migrationKey;
166
+ this.name = "MigrationStampError";
167
+ Object.setPrototypeOf(this, _MigrationStampError.prototype);
168
+ }
169
+ };
160
170
  var MigrationExecutionError = class _MigrationExecutionError extends MigrationError {
161
171
  constructor(message, migrationName, sql, originalError) {
162
172
  let fullMessage = `Migration Execution Error${migrationName ? ` in '${migrationName}'` : ""}: ${message}`;
@@ -426,6 +436,13 @@ function isMigrationFile(fileName) {
426
436
  function canonicalMigrationKey(value) {
427
437
  return value.replace(MIGRATION_FILE_REGEX, "").toLowerCase();
428
438
  }
439
+ function mintMigrationStamp(now = Date.now()) {
440
+ return now % 10 === 0 ? now + 1 : now;
441
+ }
442
+ function isHandStampedKey(key) {
443
+ const stamp = key.split("_")[0];
444
+ return /^\d+$/.test(stamp) && /000$/.test(stamp);
445
+ }
429
446
  function resolveMigrationFile(directory, baseName, direction, dialect) {
430
447
  const dialectExt = dialect === "sql" ? "mysql" : dialect;
431
448
  const dialectFile = import_path.default.join(directory, `${baseName}.${dialectExt}.${direction}.sql`);
@@ -1337,7 +1354,7 @@ var PatchCreator = class _PatchCreator {
1337
1354
  if (!import_fs5.default.existsSync(this.patchFolder)) {
1338
1355
  import_fs5.default.mkdirSync(this.patchFolder, { recursive: true });
1339
1356
  }
1340
- let stamp = Date.now();
1357
+ let stamp = mintMigrationStamp();
1341
1358
  for (let attempt = 0; attempt < 1e3; attempt++) {
1342
1359
  const filePath = import_path4.default.join(this.patchFolder, `${stamp}_${normalized}.yaml`);
1343
1360
  try {
@@ -1345,7 +1362,7 @@ var PatchCreator = class _PatchCreator {
1345
1362
  return filePath;
1346
1363
  } catch (error) {
1347
1364
  if (error && error.code === "EEXIST") {
1348
- stamp += 1;
1365
+ stamp = mintMigrationStamp(stamp + 1);
1349
1366
  continue;
1350
1367
  }
1351
1368
  throw error;
@@ -1940,6 +1957,14 @@ var MySQLMigrationRunner = class {
1940
1957
  migrate(migrationNodes, forward) {
1941
1958
  return __async(this, null, function* () {
1942
1959
  yield this.setup();
1960
+ if (forward) {
1961
+ for (const node of migrationNodes) {
1962
+ const key = node.get_key();
1963
+ if (isHandStampedKey(key) && !(yield node.status()).completed) {
1964
+ throw new MigrationStampError(key);
1965
+ }
1966
+ }
1967
+ }
1943
1968
  for (let node of migrationNodes) {
1944
1969
  try {
1945
1970
  if (forward) {
@@ -2077,7 +2102,7 @@ var MigrationCreator = class {
2077
2102
  if (!import_fs6.default.existsSync(this.config.migration_folder)) {
2078
2103
  import_fs6.default.mkdirSync(this.config.migration_folder, { recursive: true });
2079
2104
  }
2080
- const now_timestamp = Date.now();
2105
+ const now_timestamp = mintMigrationStamp();
2081
2106
  const filename_up = `${now_timestamp}_${name}.up.sql`;
2082
2107
  const filename_down = `${now_timestamp}_${name}.down.sql`;
2083
2108
  import_fs6.default.writeFileSync(`${this.config.migration_folder}/${filename_up}`, `