@stacksjs/database 0.70.243 → 0.70.245

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.
@@ -55,6 +55,21 @@ export declare function sqlStatementsOf(content: string): string[];
55
55
  * The guard is a `DO` block catching `duplicate_object`, which needs a
56
56
  * dollar-quoted body - hence the splitter above having to understand one.
57
57
  */
58
+ /**
59
+ * Put a `DROP DEFAULT` in front of every `ALTER COLUMN … TYPE …`.
60
+ *
61
+ * Postgres checks a column's existing default against the new type and refuses
62
+ * the whole statement when it cannot cast it - a `varchar` column defaulting to
63
+ * `'pending'` becoming an enum fails every time. The generator emits the right
64
+ * order now, but migration files are history: every corpus written before that
65
+ * still carries the old one, and re-running it fails on a database that has not
66
+ * caught up yet.
67
+ *
68
+ * Rewriting them here is the same bargain the rest of this pass makes. Adding
69
+ * the drop is safe whatever the column had, because the statement that follows
70
+ * either sets the new default or deliberately leaves none.
71
+ */
72
+ export declare function orderPostgresColumnTypeChanges(sql: string): string;
58
73
  export declare function guardPostgresEnumTypes(sql: string): string;
59
74
  export declare function preprocessSqliteMigrations(): void;
60
75
  /**
@@ -154,6 +154,21 @@ export function sqlStatementsOf(content) {
154
154
  statements.push(trailing);
155
155
  return statements;
156
156
  }
157
+ export function orderPostgresColumnTypeChanges(sql) {
158
+ const lines = sql.split(`
159
+ `), output = [];
160
+ for (const line of lines) {
161
+ const match = /^(\s*)ALTER\s+TABLE\s+("?[\w.]+"?)\s+ALTER\s+COLUMN\s+("?[\w]+"?)\s+TYPE\s/i.exec(line);
162
+ if (match) {
163
+ const [, indent, table, column] = match, drop = `${indent}ALTER TABLE ${table} ALTER COLUMN ${column} DROP DEFAULT;`;
164
+ if ((output.length > 0 ? output[output.length - 1].trim() : "") !== drop.trim())
165
+ output.push(drop);
166
+ }
167
+ output.push(line);
168
+ }
169
+ return output.join(`
170
+ `);
171
+ }
157
172
  export function guardPostgresEnumTypes(sql) {
158
173
  return sql.replace(/CREATE\s+TYPE\s+("?[\w.]+"?)\s+AS\s+ENUM\s*\(([^)]*)\)/gi, (match, name, members, offset, whole) => {
159
174
  if (/\bBEGIN\s*$/i.test(whole.slice(Math.max(0, offset - 40), offset)))
@@ -545,9 +560,9 @@ function makeMigrationsIdempotent() {
545
560
  } catch {
546
561
  continue;
547
562
  }
548
- if (!(/\bADD\s+(?:COLUMN|CONSTRAINT)\b/i.test(sql) || /\bCREATE\s+TYPE\b/i.test(sql)))
563
+ if (!(/\bADD\s+(?:COLUMN|CONSTRAINT)\b/i.test(sql) || /\bCREATE\s+TYPE\b/i.test(sql) || /\bALTER\s+COLUMN\b[^\n]*\bTYPE\b/i.test(sql)))
549
564
  continue;
550
- const next = guardPostgresEnumTypes(idempotentSql(sql));
565
+ const next = orderPostgresColumnTypeChanges(guardPostgresEnumTypes(idempotentSql(sql)));
551
566
  if (next !== sql)
552
567
  try {
553
568
  writeFileSync(p, next);
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@stacksjs/database",
3
3
  "type": "module",
4
4
  "sideEffects": false,
5
- "version": "0.70.243",
5
+ "version": "0.70.245",
6
6
  "description": "The Stacks database integration.",
7
7
  "author": "Chris Breuer",
8
8
  "contributors": [
@@ -54,19 +54,19 @@
54
54
  },
55
55
  "dependencies": {
56
56
  "@stacksjs/ts-validation": "^0.5.0",
57
- "bun-query-builder": "^0.2.3",
57
+ "bun-query-builder": "^0.2.4",
58
58
  "dynamodb-tooling": "^0.3.2"
59
59
  },
60
60
  "devDependencies": {
61
- "@stacksjs/cli": "0.70.243",
62
- "@stacksjs/config": "0.70.243",
63
- "@stacksjs/logging": "0.70.243",
64
- "@stacksjs/router": "0.70.243",
61
+ "@stacksjs/cli": "0.70.245",
62
+ "@stacksjs/config": "0.70.245",
63
+ "@stacksjs/logging": "0.70.245",
64
+ "@stacksjs/router": "0.70.245",
65
65
  "better-dx": "^0.2.17",
66
- "@stacksjs/path": "0.70.243",
67
- "@stacksjs/query-builder": "0.70.243",
68
- "@stacksjs/storage": "0.70.243",
69
- "@stacksjs/strings": "0.70.243",
70
- "@stacksjs/utils": "0.70.243"
66
+ "@stacksjs/path": "0.70.245",
67
+ "@stacksjs/query-builder": "0.70.245",
68
+ "@stacksjs/storage": "0.70.245",
69
+ "@stacksjs/strings": "0.70.245",
70
+ "@stacksjs/utils": "0.70.245"
71
71
  }
72
72
  }