rake-db 2.17.2 → 2.17.3

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/dist/index.d.ts CHANGED
@@ -653,6 +653,31 @@ declare class Migration<CT extends RakeDbColumnTypes> {
653
653
  * @param values - object where keys are for old names, values are for new names
654
654
  */
655
655
  renameEnumValues(enumName: string, values: RecordString): Promise<void>;
656
+ /**
657
+ * Drops the enum and re-creates it with a new set of values.
658
+ * Before dropping, changes all related column types to text, and after creating changes types back to the enum,
659
+ * in the same way as [dropEnumValues](/guide/migration-writing.html#addenumvalues,-dropenumvalues) works.
660
+ *
661
+ * ```ts
662
+ * import { change } from '../dbScript';
663
+ *
664
+ * change(async (db) => {
665
+ * await db.changeEnumValues(
666
+ * // can be prefixed with schema: 'public.numbers'
667
+ * 'numbers',
668
+ * // change from:
669
+ * ['one', 'two'],
670
+ * // change to:
671
+ * ['three', 'four'],
672
+ * );
673
+ * });
674
+ * ```
675
+ *
676
+ * @param enumName - target enum name, can be prefixed with schema
677
+ * @param fromValues - array of values before the change
678
+ * @param toValues - array of values to set
679
+ */
680
+ changeEnumValues(enumName: string, fromValues: string[], toValues: string[]): Promise<void>;
656
681
  /**
657
682
  * Rename a type (such as enum):
658
683
  *
@@ -950,8 +975,9 @@ interface AddEnumValueOptions {
950
975
  after?: string;
951
976
  }
952
977
  declare const addOrDropEnumValues: (migration: Migration<RakeDbColumnTypes>, up: boolean, enumName: string, values: string[], options?: AddEnumValueOptions) => Promise<void>;
978
+ declare const changeEnumValues: (migration: Migration<RakeDbColumnTypes>, enumName: string, fromValues: string[], toValues: string[]) => Promise<void>;
953
979
 
954
- type RakeDbAst = RakeDbAst.Table | RakeDbAst.ChangeTable | RakeDbAst.RenameType | RakeDbAst.Schema | RakeDbAst.Extension | RakeDbAst.Enum | RakeDbAst.EnumValues | RakeDbAst.RenameEnumValues | RakeDbAst.Domain | RakeDbAst.Collation | RakeDbAst.Constraint | RakeDbAst.View;
980
+ type RakeDbAst = RakeDbAst.Table | RakeDbAst.ChangeTable | RakeDbAst.RenameType | RakeDbAst.Schema | RakeDbAst.Extension | RakeDbAst.Enum | RakeDbAst.EnumValues | RakeDbAst.RenameEnumValues | RakeDbAst.ChangeEnumValues | RakeDbAst.Domain | RakeDbAst.Collation | RakeDbAst.Constraint | RakeDbAst.View;
955
981
  declare namespace RakeDbAst {
956
982
  interface Table extends TableData {
957
983
  type: 'table';
@@ -1058,6 +1084,13 @@ declare namespace RakeDbAst {
1058
1084
  name: string;
1059
1085
  values: RecordString;
1060
1086
  }
1087
+ interface ChangeEnumValues {
1088
+ type: 'changeEnumValues';
1089
+ schema?: string;
1090
+ name: string;
1091
+ fromValues: string[];
1092
+ toValues: string[];
1093
+ }
1061
1094
  interface Domain {
1062
1095
  type: 'domain';
1063
1096
  action: 'create' | 'drop';
@@ -1402,4 +1435,4 @@ type RakeDbChangeFn<CT extends RakeDbColumnTypes> = (fn: ChangeCallback<CT>) =>
1402
1435
  declare const rakeDb: RakeDbFn;
1403
1436
  declare const rakeDbAliases: RecordOptionalString;
1404
1437
 
1405
- export { AnyRakeDbConfig, AppCodeUpdater, AppCodeUpdaterParams, ChangeTableCallback, ChangeTableOptions, ColumnComment, ColumnsShapeCallback, ConstraintArg, DbMigration, DropMode, InputRakeDbConfig, Migration, MigrationAdapter, MigrationColumnTypes, ModuleExportsRecord, NoMigrationsTableError, RAKE_DB_LOCK_KEY, RakeDbAppliedVersions, RakeDbAst, RakeDbBaseTable, RakeDbChangeFn, RakeDbColumnTypes, RakeDbConfig, RakeDbFn, RakeDbFnReturns, RakeDbLazyFn, RakeDbMigrationId, RakeDbResult, SilentQueries, TableOptions, addOrDropEnumValues, changeCache, createDb, createMigrationInterface, deleteMigratedVersion, dropDb, generate, generateTimeStamp, getDatabaseAndUserFromOptions, getMigratedVersionsMap, makeFileVersion, migrate, migrateOrRollback, migrationConfigDefaults, processRakeDbConfig, rakeDb, rakeDbAliases, redo, renameType, resetDb, rollback, saveMigratedVersion, writeMigrationFile };
1438
+ export { AnyRakeDbConfig, AppCodeUpdater, AppCodeUpdaterParams, ChangeTableCallback, ChangeTableOptions, ColumnComment, ColumnsShapeCallback, ConstraintArg, DbMigration, DropMode, InputRakeDbConfig, Migration, MigrationAdapter, MigrationColumnTypes, ModuleExportsRecord, NoMigrationsTableError, RAKE_DB_LOCK_KEY, RakeDbAppliedVersions, RakeDbAst, RakeDbBaseTable, RakeDbChangeFn, RakeDbColumnTypes, RakeDbConfig, RakeDbFn, RakeDbFnReturns, RakeDbLazyFn, RakeDbMigrationId, RakeDbResult, SilentQueries, TableOptions, addOrDropEnumValues, changeCache, changeEnumValues, createDb, createMigrationInterface, deleteMigratedVersion, dropDb, generate, generateTimeStamp, getDatabaseAndUserFromOptions, getMigratedVersionsMap, makeFileVersion, migrate, migrateOrRollback, migrationConfigDefaults, processRakeDbConfig, rakeDb, rakeDbAliases, redo, renameType, resetDb, rollback, saveMigratedVersion, writeMigrationFile };
package/dist/index.js CHANGED
@@ -1679,6 +1679,33 @@ class Migration {
1679
1679
  );
1680
1680
  }
1681
1681
  }
1682
+ /**
1683
+ * Drops the enum and re-creates it with a new set of values.
1684
+ * Before dropping, changes all related column types to text, and after creating changes types back to the enum,
1685
+ * in the same way as [dropEnumValues](/guide/migration-writing.html#addenumvalues,-dropenumvalues) works.
1686
+ *
1687
+ * ```ts
1688
+ * import { change } from '../dbScript';
1689
+ *
1690
+ * change(async (db) => {
1691
+ * await db.changeEnumValues(
1692
+ * // can be prefixed with schema: 'public.numbers'
1693
+ * 'numbers',
1694
+ * // change from:
1695
+ * ['one', 'two'],
1696
+ * // change to:
1697
+ * ['three', 'four'],
1698
+ * );
1699
+ * });
1700
+ * ```
1701
+ *
1702
+ * @param enumName - target enum name, can be prefixed with schema
1703
+ * @param fromValues - array of values before the change
1704
+ * @param toValues - array of values to set
1705
+ */
1706
+ changeEnumValues(enumName, fromValues, toValues) {
1707
+ return changeEnumValues(this, enumName, fromValues, toValues);
1708
+ }
1682
1709
  /**
1683
1710
  * Rename a type (such as enum):
1684
1711
  *
@@ -2110,7 +2137,6 @@ const renameType = async (migration, from, to, table) => {
2110
2137
  };
2111
2138
  const addOrDropEnumValues = async (migration, up, enumName, values, options) => {
2112
2139
  var _a;
2113
- const defaultSchema = migration.adapter.schema;
2114
2140
  const [schema, name] = getSchemaAndTableFromName(enumName);
2115
2141
  const quotedName = quoteTable(schema, name);
2116
2142
  const ast = {
@@ -2137,6 +2163,41 @@ const addOrDropEnumValues = async (migration, up, enumName, values, options) =>
2137
2163
  `SELECT unnest(enum_range(NULL::${quotedName}))::text value`
2138
2164
  );
2139
2165
  const existingValues = valuesRows.map((r) => r.value);
2166
+ await recreateEnum(
2167
+ migration,
2168
+ ast,
2169
+ existingValues.filter((v) => !ast.values.includes(v)),
2170
+ (quotedName2, table, column) => `Cannot drop ${quotedName2} enum values [${ast.values.map(orchidCore.singleQuote).join(
2171
+ ", "
2172
+ )}]: table ${table} has a row with such value in the column "${column}"`
2173
+ );
2174
+ };
2175
+ const changeEnumValues = async (migration, enumName, fromValues, toValues) => {
2176
+ const [schema, name] = getSchemaAndTableFromName(enumName);
2177
+ if (!migration.up) {
2178
+ const values = fromValues;
2179
+ fromValues = toValues;
2180
+ toValues = values;
2181
+ }
2182
+ const ast = {
2183
+ type: "changeEnumValues",
2184
+ schema,
2185
+ name,
2186
+ fromValues,
2187
+ toValues
2188
+ };
2189
+ await recreateEnum(
2190
+ migration,
2191
+ ast,
2192
+ ast.toValues,
2193
+ (quotedName, table, column) => `Cannot change ${quotedName} enum values from [${fromValues.map(orchidCore.singleQuote).join(", ")}] to [${toValues.map(orchidCore.singleQuote).join(
2194
+ ", "
2195
+ )}]: table ${table} has a row with removed value in the column "${column}"`
2196
+ );
2197
+ };
2198
+ const recreateEnum = async (migration, { schema, name }, values, errorMessage) => {
2199
+ const defaultSchema = migration.adapter.schema;
2200
+ const quotedName = quoteTable(schema, name);
2140
2201
  const { rows: tables } = await migration.adapter.query(
2141
2202
  `SELECT n.nspname AS "schema",
2142
2203
  c.relname AS "table",
@@ -2156,7 +2217,7 @@ GROUP BY n.nspname, c.relname`
2156
2217
  );
2157
2218
  sql.push(
2158
2219
  `DROP TYPE ${quotedName}`,
2159
- `CREATE TYPE ${quotedName} AS ENUM (${existingValues.filter((v) => !ast.values.includes(v)).map(orchidCore.singleQuote).join(", ")})`
2220
+ `CREATE TYPE ${quotedName} AS ENUM (${values.map(orchidCore.singleQuote).join(", ")})`
2160
2221
  );
2161
2222
  await migration.adapter.query(sql.join(";\n"));
2162
2223
  for (const t of tables) {
@@ -2169,12 +2230,7 @@ GROUP BY n.nspname, c.relname`
2169
2230
  );
2170
2231
  } catch (err) {
2171
2232
  if (err.code === "22P02") {
2172
- throw new Error(
2173
- `Cannot drop ${quotedName} enum values [${ast.values.map(orchidCore.singleQuote).join(
2174
- ", "
2175
- )}]: table ${table} has a row with such value in the column "${c}"`,
2176
- { cause: err }
2177
- );
2233
+ throw new Error(errorMessage(quotedName, table, c), { cause: err });
2178
2234
  }
2179
2235
  throw err;
2180
2236
  }
@@ -4813,6 +4869,7 @@ exports.NoMigrationsTableError = NoMigrationsTableError;
4813
4869
  exports.RAKE_DB_LOCK_KEY = RAKE_DB_LOCK_KEY;
4814
4870
  exports.addOrDropEnumValues = addOrDropEnumValues;
4815
4871
  exports.changeCache = changeCache;
4872
+ exports.changeEnumValues = changeEnumValues;
4816
4873
  exports.createDb = createDb;
4817
4874
  exports.createMigrationInterface = createMigrationInterface;
4818
4875
  exports.deleteMigratedVersion = deleteMigratedVersion;