@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.mjs CHANGED
@@ -121,7 +121,7 @@ var init_MigrationFilter = __esm({
121
121
  });
122
122
 
123
123
  // framework/errors.ts
124
- var MigrationError, ConfigurationError, DatabaseConnectionError, MigrationExecutionError, PatchError, PatchValidationError, PatchIntegrityError, PatchConflictError, PatchExecutionError, CLIError;
124
+ var MigrationError, ConfigurationError, DatabaseConnectionError, MigrationStampError, MigrationExecutionError, PatchError, PatchValidationError, PatchIntegrityError, PatchConflictError, PatchExecutionError, CLIError;
125
125
  var init_errors = __esm({
126
126
  "framework/errors.ts"() {
127
127
  MigrationError = class _MigrationError extends Error {
@@ -187,6 +187,16 @@ var init_errors = __esm({
187
187
  return new _DatabaseConnectionError(`Authentication failed for ${dbType} database`);
188
188
  }
189
189
  };
190
+ MigrationStampError = class _MigrationStampError extends MigrationError {
191
+ constructor(migrationKey) {
192
+ super(
193
+ `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.`
194
+ );
195
+ this.migrationKey = migrationKey;
196
+ this.name = "MigrationStampError";
197
+ Object.setPrototypeOf(this, _MigrationStampError.prototype);
198
+ }
199
+ };
190
200
  MigrationExecutionError = class _MigrationExecutionError extends MigrationError {
191
201
  constructor(message, migrationName, sql, originalError) {
192
202
  let fullMessage = `Migration Execution Error${migrationName ? ` in '${migrationName}'` : ""}: ${message}`;
@@ -469,6 +479,13 @@ function isMigrationFile(fileName) {
469
479
  function canonicalMigrationKey(value) {
470
480
  return value.replace(MIGRATION_FILE_REGEX, "").toLowerCase();
471
481
  }
482
+ function mintMigrationStamp(now = Date.now()) {
483
+ return now % 10 === 0 ? now + 1 : now;
484
+ }
485
+ function isHandStampedKey(key) {
486
+ const stamp = key.split("_")[0];
487
+ return /^\d+$/.test(stamp) && /000$/.test(stamp);
488
+ }
472
489
  function resolveMigrationFile(directory, baseName, direction, dialect) {
473
490
  const dialectExt = dialect === "sql" ? "mysql" : dialect;
474
491
  const dialectFile = path.join(directory, `${baseName}.${dialectExt}.${direction}.sql`);
@@ -1375,6 +1392,7 @@ var MAX_NAME_LENGTH, SCAFFOLD, PatchCreator;
1375
1392
  var init_PatchCreator = __esm({
1376
1393
  "framework/PatchCreator.ts"() {
1377
1394
  init_errors();
1395
+ init_MigrationManifest();
1378
1396
  MAX_NAME_LENGTH = 120;
1379
1397
  SCAFFOLD = `version: 1
1380
1398
  description: TODO
@@ -1421,7 +1439,7 @@ operations: []
1421
1439
  if (!fs5.existsSync(this.patchFolder)) {
1422
1440
  fs5.mkdirSync(this.patchFolder, { recursive: true });
1423
1441
  }
1424
- let stamp = Date.now();
1442
+ let stamp = mintMigrationStamp();
1425
1443
  for (let attempt = 0; attempt < 1e3; attempt++) {
1426
1444
  const filePath = path4.join(this.patchFolder, `${stamp}_${normalized}.yaml`);
1427
1445
  try {
@@ -1429,7 +1447,7 @@ operations: []
1429
1447
  return filePath;
1430
1448
  } catch (error) {
1431
1449
  if (error && error.code === "EEXIST") {
1432
- stamp += 1;
1450
+ stamp = mintMigrationStamp(stamp + 1);
1433
1451
  continue;
1434
1452
  }
1435
1453
  throw error;
@@ -1787,6 +1805,8 @@ var init_MigrationRunner = __esm({
1787
1805
  init_PatchRunner();
1788
1806
  init_PatchCreator();
1789
1807
  init_PatchTypes();
1808
+ init_MigrationManifest();
1809
+ init_errors();
1790
1810
  init_SQLRunner();
1791
1811
  init_errors();
1792
1812
  MigrationRunnerFactory = class _MigrationRunnerFactory {
@@ -2040,6 +2060,14 @@ var init_MigrationRunner = __esm({
2040
2060
  migrate(migrationNodes, forward) {
2041
2061
  return __async(this, null, function* () {
2042
2062
  yield this.setup();
2063
+ if (forward) {
2064
+ for (const node of migrationNodes) {
2065
+ const key = node.get_key();
2066
+ if (isHandStampedKey(key) && !(yield node.status()).completed) {
2067
+ throw new MigrationStampError(key);
2068
+ }
2069
+ }
2070
+ }
2043
2071
  for (let node of migrationNodes) {
2044
2072
  try {
2045
2073
  if (forward) {
@@ -2177,7 +2205,7 @@ var init_MigrationRunner = __esm({
2177
2205
  if (!fs6.existsSync(this.config.migration_folder)) {
2178
2206
  fs6.mkdirSync(this.config.migration_folder, { recursive: true });
2179
2207
  }
2180
- const now_timestamp = Date.now();
2208
+ const now_timestamp = mintMigrationStamp();
2181
2209
  const filename_up = `${now_timestamp}_${name}.up.sql`;
2182
2210
  const filename_down = `${now_timestamp}_${name}.down.sql`;
2183
2211
  fs6.writeFileSync(`${this.config.migration_folder}/${filename_up}`, `