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 +35 -2
- package/dist/index.js +65 -8
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +65 -9
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1677,6 +1677,33 @@ class Migration {
|
|
|
1677
1677
|
);
|
|
1678
1678
|
}
|
|
1679
1679
|
}
|
|
1680
|
+
/**
|
|
1681
|
+
* Drops the enum and re-creates it with a new set of values.
|
|
1682
|
+
* Before dropping, changes all related column types to text, and after creating changes types back to the enum,
|
|
1683
|
+
* in the same way as [dropEnumValues](/guide/migration-writing.html#addenumvalues,-dropenumvalues) works.
|
|
1684
|
+
*
|
|
1685
|
+
* ```ts
|
|
1686
|
+
* import { change } from '../dbScript';
|
|
1687
|
+
*
|
|
1688
|
+
* change(async (db) => {
|
|
1689
|
+
* await db.changeEnumValues(
|
|
1690
|
+
* // can be prefixed with schema: 'public.numbers'
|
|
1691
|
+
* 'numbers',
|
|
1692
|
+
* // change from:
|
|
1693
|
+
* ['one', 'two'],
|
|
1694
|
+
* // change to:
|
|
1695
|
+
* ['three', 'four'],
|
|
1696
|
+
* );
|
|
1697
|
+
* });
|
|
1698
|
+
* ```
|
|
1699
|
+
*
|
|
1700
|
+
* @param enumName - target enum name, can be prefixed with schema
|
|
1701
|
+
* @param fromValues - array of values before the change
|
|
1702
|
+
* @param toValues - array of values to set
|
|
1703
|
+
*/
|
|
1704
|
+
changeEnumValues(enumName, fromValues, toValues) {
|
|
1705
|
+
return changeEnumValues(this, enumName, fromValues, toValues);
|
|
1706
|
+
}
|
|
1680
1707
|
/**
|
|
1681
1708
|
* Rename a type (such as enum):
|
|
1682
1709
|
*
|
|
@@ -2108,7 +2135,6 @@ const renameType = async (migration, from, to, table) => {
|
|
|
2108
2135
|
};
|
|
2109
2136
|
const addOrDropEnumValues = async (migration, up, enumName, values, options) => {
|
|
2110
2137
|
var _a;
|
|
2111
|
-
const defaultSchema = migration.adapter.schema;
|
|
2112
2138
|
const [schema, name] = getSchemaAndTableFromName(enumName);
|
|
2113
2139
|
const quotedName = quoteTable(schema, name);
|
|
2114
2140
|
const ast = {
|
|
@@ -2135,6 +2161,41 @@ const addOrDropEnumValues = async (migration, up, enumName, values, options) =>
|
|
|
2135
2161
|
`SELECT unnest(enum_range(NULL::${quotedName}))::text value`
|
|
2136
2162
|
);
|
|
2137
2163
|
const existingValues = valuesRows.map((r) => r.value);
|
|
2164
|
+
await recreateEnum(
|
|
2165
|
+
migration,
|
|
2166
|
+
ast,
|
|
2167
|
+
existingValues.filter((v) => !ast.values.includes(v)),
|
|
2168
|
+
(quotedName2, table, column) => `Cannot drop ${quotedName2} enum values [${ast.values.map(singleQuote).join(
|
|
2169
|
+
", "
|
|
2170
|
+
)}]: table ${table} has a row with such value in the column "${column}"`
|
|
2171
|
+
);
|
|
2172
|
+
};
|
|
2173
|
+
const changeEnumValues = async (migration, enumName, fromValues, toValues) => {
|
|
2174
|
+
const [schema, name] = getSchemaAndTableFromName(enumName);
|
|
2175
|
+
if (!migration.up) {
|
|
2176
|
+
const values = fromValues;
|
|
2177
|
+
fromValues = toValues;
|
|
2178
|
+
toValues = values;
|
|
2179
|
+
}
|
|
2180
|
+
const ast = {
|
|
2181
|
+
type: "changeEnumValues",
|
|
2182
|
+
schema,
|
|
2183
|
+
name,
|
|
2184
|
+
fromValues,
|
|
2185
|
+
toValues
|
|
2186
|
+
};
|
|
2187
|
+
await recreateEnum(
|
|
2188
|
+
migration,
|
|
2189
|
+
ast,
|
|
2190
|
+
ast.toValues,
|
|
2191
|
+
(quotedName, table, column) => `Cannot change ${quotedName} enum values from [${fromValues.map(singleQuote).join(", ")}] to [${toValues.map(singleQuote).join(
|
|
2192
|
+
", "
|
|
2193
|
+
)}]: table ${table} has a row with removed value in the column "${column}"`
|
|
2194
|
+
);
|
|
2195
|
+
};
|
|
2196
|
+
const recreateEnum = async (migration, { schema, name }, values, errorMessage) => {
|
|
2197
|
+
const defaultSchema = migration.adapter.schema;
|
|
2198
|
+
const quotedName = quoteTable(schema, name);
|
|
2138
2199
|
const { rows: tables } = await migration.adapter.query(
|
|
2139
2200
|
`SELECT n.nspname AS "schema",
|
|
2140
2201
|
c.relname AS "table",
|
|
@@ -2154,7 +2215,7 @@ GROUP BY n.nspname, c.relname`
|
|
|
2154
2215
|
);
|
|
2155
2216
|
sql.push(
|
|
2156
2217
|
`DROP TYPE ${quotedName}`,
|
|
2157
|
-
`CREATE TYPE ${quotedName} AS ENUM (${
|
|
2218
|
+
`CREATE TYPE ${quotedName} AS ENUM (${values.map(singleQuote).join(", ")})`
|
|
2158
2219
|
);
|
|
2159
2220
|
await migration.adapter.query(sql.join(";\n"));
|
|
2160
2221
|
for (const t of tables) {
|
|
@@ -2167,12 +2228,7 @@ GROUP BY n.nspname, c.relname`
|
|
|
2167
2228
|
);
|
|
2168
2229
|
} catch (err) {
|
|
2169
2230
|
if (err.code === "22P02") {
|
|
2170
|
-
throw new Error(
|
|
2171
|
-
`Cannot drop ${quotedName} enum values [${ast.values.map(singleQuote).join(
|
|
2172
|
-
", "
|
|
2173
|
-
)}]: table ${table} has a row with such value in the column "${c}"`,
|
|
2174
|
-
{ cause: err }
|
|
2175
|
-
);
|
|
2231
|
+
throw new Error(errorMessage(quotedName, table, c), { cause: err });
|
|
2176
2232
|
}
|
|
2177
2233
|
throw err;
|
|
2178
2234
|
}
|
|
@@ -4806,5 +4862,5 @@ Migrate and rollback common arguments:
|
|
|
4806
4862
|
--code false do not run code updater
|
|
4807
4863
|
`;
|
|
4808
4864
|
|
|
4809
|
-
export { Migration, NoMigrationsTableError, RAKE_DB_LOCK_KEY, addOrDropEnumValues, changeCache, createDb, createMigrationInterface, deleteMigratedVersion, dropDb, generate, generateTimeStamp, getDatabaseAndUserFromOptions, getMigratedVersionsMap, makeFileVersion, migrate, migrateOrRollback, migrationConfigDefaults, processRakeDbConfig, rakeDb, rakeDbAliases, redo, renameType, resetDb, rollback, saveMigratedVersion, writeMigrationFile };
|
|
4865
|
+
export { Migration, NoMigrationsTableError, RAKE_DB_LOCK_KEY, addOrDropEnumValues, changeCache, changeEnumValues, createDb, createMigrationInterface, deleteMigratedVersion, dropDb, generate, generateTimeStamp, getDatabaseAndUserFromOptions, getMigratedVersionsMap, makeFileVersion, migrate, migrateOrRollback, migrationConfigDefaults, processRakeDbConfig, rakeDb, rakeDbAliases, redo, renameType, resetDb, rollback, saveMigratedVersion, writeMigrationFile };
|
|
4810
4866
|
//# sourceMappingURL=index.mjs.map
|