rake-db 2.23.20 → 2.23.22
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 +14 -6
- package/dist/index.js +54 -24
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +53 -25
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -59,7 +59,7 @@ declare namespace RakeDbAst {
|
|
|
59
59
|
comment?: string | null;
|
|
60
60
|
compression?: string;
|
|
61
61
|
primaryKey?: boolean;
|
|
62
|
-
|
|
62
|
+
checks?: ColumnDataCheckBase[];
|
|
63
63
|
foreignKeys?: TableData.ColumnReferences[];
|
|
64
64
|
indexes?: TableData.ColumnIndex[];
|
|
65
65
|
excludes?: TableData.ColumnExclude[];
|
|
@@ -198,7 +198,7 @@ declare namespace RakeDbAst {
|
|
|
198
198
|
|
|
199
199
|
declare function add(item: ColumnType, options?: {
|
|
200
200
|
dropMode?: DropMode;
|
|
201
|
-
}):
|
|
201
|
+
}): EmptyObject;
|
|
202
202
|
declare function add(emptyObject: EmptyObject): EmptyObject;
|
|
203
203
|
declare function add(items: Record<string, ColumnType>, options?: {
|
|
204
204
|
dropMode?: DropMode;
|
|
@@ -1641,7 +1641,7 @@ declare namespace DbStructure {
|
|
|
1641
1641
|
dateTimePrecision?: number;
|
|
1642
1642
|
collate?: string;
|
|
1643
1643
|
default?: string;
|
|
1644
|
-
|
|
1644
|
+
checks?: string[];
|
|
1645
1645
|
}
|
|
1646
1646
|
interface Collation {
|
|
1647
1647
|
schemaName: string;
|
|
@@ -1692,8 +1692,11 @@ declare const instantiateDbColumn: (ctx: StructureToAstCtx, data: IntrospectedSt
|
|
|
1692
1692
|
declare const tableToAst: (ctx: StructureToAstCtx, data: IntrospectedStructure, table: DbStructure.Table, action: 'create' | 'drop', domains: DbStructureDomainsMap) => RakeDbAst.Table;
|
|
1693
1693
|
declare const getDbStructureTableData: (data: IntrospectedStructure, { name, schemaName }: DbStructure.Table) => StructureToAstTableData;
|
|
1694
1694
|
declare const makeDbStructureColumnsShape: (ctx: StructureToAstCtx, data: IntrospectedStructure, domains: DbStructureDomainsMap, table: DbStructure.Table | DbStructure.View, tableData?: StructureToAstTableData) => ColumnsShape;
|
|
1695
|
-
|
|
1696
|
-
|
|
1695
|
+
interface ColumnChecks {
|
|
1696
|
+
[K: string]: string[];
|
|
1697
|
+
}
|
|
1698
|
+
declare const getDbTableColumnsChecks: (tableData: StructureToAstTableData) => ColumnChecks;
|
|
1699
|
+
declare const dbColumnToAst: (ctx: StructureToAstCtx, data: IntrospectedStructure, domains: DbStructureDomainsMap, tableName: string, item: DbStructure.Column, table?: DbStructure.Table, tableData?: StructureToAstTableData, checks?: ColumnChecks) => [key: string, column: ColumnType];
|
|
1697
1700
|
|
|
1698
1701
|
declare const astToMigration: (currentSchema: string, config: AnyRakeDbConfig, asts: RakeDbAst[]) => string | undefined;
|
|
1699
1702
|
|
|
@@ -1746,6 +1749,11 @@ declare const commentsToQuery: (schemaTable: {
|
|
|
1746
1749
|
}, comments: ColumnComment[]) => SingleSql[];
|
|
1747
1750
|
declare const primaryKeyToSql: (primaryKey: Exclude<TableData['primaryKey'], undefined>) => string;
|
|
1748
1751
|
declare const interpolateSqlValues: ({ text, values }: TableQuery) => string;
|
|
1752
|
+
interface ColumnNamedCheck extends ColumnDataCheckBase {
|
|
1753
|
+
name: string;
|
|
1754
|
+
}
|
|
1755
|
+
declare const nameColumnChecks: (table: string, column: string, checks: ColumnDataCheckBase[]) => ColumnNamedCheck[];
|
|
1756
|
+
declare const cmpRawSql: (a: RawSQLBase, b: RawSQLBase) => boolean;
|
|
1749
1757
|
|
|
1750
1758
|
/**
|
|
1751
1759
|
* Type of {@link rakeDb} function
|
|
@@ -1831,4 +1839,4 @@ declare const promptText: ({ message, default: def, password, min, }: {
|
|
|
1831
1839
|
min?: number;
|
|
1832
1840
|
}) => Promise<string>;
|
|
1833
1841
|
|
|
1834
|
-
export { type AnyRakeDbConfig, type ChangeCallback, type ChangeTableCallback, type ChangeTableOptions, type ColumnComment, type ColumnsShapeCallback, type CommandFn, type DbMigration, DbStructure, type DbStructureDomainsMap, type DropMode, type GetIndexOrExcludeName, type InputRakeDbConfig, type InputRakeDbConfigBase, type IntrospectedStructure, Migration, type MigrationAdapter, type MigrationColumnTypes, type ModuleExportsRecord, NoMigrationsTableError, RAKE_DB_LOCK_KEY, type RakeDbAppliedVersions, RakeDbAst, type RakeDbBaseTable, type RakeDbChangeFn, type RakeDbConfig, type RakeDbCtx, type RakeDbFn, type RakeDbFnReturns, type RakeDbLazyFn, type RakeDbMigrationId, type RakeDbRenameMigrations, type RakeDbRenameMigrationsInput, type RakeDbRenameMigrationsMap, type RakeDbResult, type SilentQueries, type StructureToAstCtx, type StructureToAstTableData, type TableOptions, addColumnComment, addColumnExclude, addColumnIndex, addOrDropEnumValues, astToMigration, changeCache, changeEnumValues, clearChanges, colors, columnToSql, columnTypeToSql, commentsToQuery, concatSchemaAndName, constraintToSql, createDb, createMigrationInterface, dbColumnToAst, deleteMigratedVersion, dropDb, encodeColumnDefault, excludesToQuery, exhaustive, generateTimeStamp, getColumnName, getConstraintName, getCurrentChanges, getDatabaseAndUserFromOptions, getDbStructureTableData, getDbTableColumnsChecks, getExcludeName, getFirstWordAndRest, getForeignKeyTable, getIndexName, getMigratedVersionsMap, getSchemaAndTableFromName, getTextAfterFrom, getTextAfterTo, identityToSql, indexesToQuery, instantiateDbColumn, interpolateSqlValues, introspectDbSchema, joinColumns, joinWords, makeDbStructureColumnsShape, makeDomainsMap, makeFileVersion, makePopulateEnumQuery, makeStructureToAstCtx, migrate, migrateOrRollback, migrationConfigDefaults, newMigration, pluralize, primaryKeyToSql, processRakeDbConfig, promptConfirm, promptSelect, promptText, pushChange, queryLock, quoteCustomType, quoteNameFromString, quoteSchemaTable, quoteTable, quoteWithSchema, rakeDb, rakeDbAliases, rakeDbCommands, redo, referencesToSql, renameType, resetDb, rollback, saveMigratedVersion, structureToAst, tableToAst, transaction, versionToString, writeMigrationFile };
|
|
1842
|
+
export { type AnyRakeDbConfig, type ChangeCallback, type ChangeTableCallback, type ChangeTableOptions, type ColumnChecks, type ColumnComment, type ColumnNamedCheck, type ColumnsShapeCallback, type CommandFn, type DbMigration, DbStructure, type DbStructureDomainsMap, type DropMode, type GetIndexOrExcludeName, type InputRakeDbConfig, type InputRakeDbConfigBase, type IntrospectedStructure, Migration, type MigrationAdapter, type MigrationColumnTypes, type ModuleExportsRecord, NoMigrationsTableError, RAKE_DB_LOCK_KEY, type RakeDbAppliedVersions, RakeDbAst, type RakeDbBaseTable, type RakeDbChangeFn, type RakeDbConfig, type RakeDbCtx, type RakeDbFn, type RakeDbFnReturns, type RakeDbLazyFn, type RakeDbMigrationId, type RakeDbRenameMigrations, type RakeDbRenameMigrationsInput, type RakeDbRenameMigrationsMap, type RakeDbResult, type SilentQueries, type StructureToAstCtx, type StructureToAstTableData, type TableOptions, addColumnComment, addColumnExclude, addColumnIndex, addOrDropEnumValues, astToMigration, changeCache, changeEnumValues, clearChanges, cmpRawSql, colors, columnToSql, columnTypeToSql, commentsToQuery, concatSchemaAndName, constraintToSql, createDb, createMigrationInterface, dbColumnToAst, deleteMigratedVersion, dropDb, encodeColumnDefault, excludesToQuery, exhaustive, generateTimeStamp, getColumnName, getConstraintName, getCurrentChanges, getDatabaseAndUserFromOptions, getDbStructureTableData, getDbTableColumnsChecks, getExcludeName, getFirstWordAndRest, getForeignKeyTable, getIndexName, getMigratedVersionsMap, getSchemaAndTableFromName, getTextAfterFrom, getTextAfterTo, identityToSql, indexesToQuery, instantiateDbColumn, interpolateSqlValues, introspectDbSchema, joinColumns, joinWords, makeDbStructureColumnsShape, makeDomainsMap, makeFileVersion, makePopulateEnumQuery, makeStructureToAstCtx, migrate, migrateOrRollback, migrationConfigDefaults, nameColumnChecks, newMigration, pluralize, primaryKeyToSql, processRakeDbConfig, promptConfirm, promptSelect, promptText, pushChange, queryLock, quoteCustomType, quoteNameFromString, quoteSchemaTable, quoteTable, quoteWithSchema, rakeDb, rakeDbAliases, rakeDbCommands, redo, referencesToSql, renameType, resetDb, rollback, saveMigratedVersion, structureToAst, tableToAst, transaction, versionToString, writeMigrationFile };
|
package/dist/index.js
CHANGED
|
@@ -128,8 +128,12 @@ const columnToSql = (name, item, values, hasMultiplePrimaryKeys, snakeCase) => {
|
|
|
128
128
|
} else if (!item.data.isNullable) {
|
|
129
129
|
line.push("NOT NULL");
|
|
130
130
|
}
|
|
131
|
-
if (item.data.
|
|
132
|
-
line.push(
|
|
131
|
+
if (item.data.checks) {
|
|
132
|
+
line.push(
|
|
133
|
+
item.data.checks.map(
|
|
134
|
+
(check) => (check.name ? `CONSTRAINT "${check.name}" ` : "") + checkToSql(check.sql, values)
|
|
135
|
+
).join(", ")
|
|
136
|
+
);
|
|
133
137
|
}
|
|
134
138
|
const def = encodeColumnDefault(item.data.default, values, item);
|
|
135
139
|
if (def !== null) line.push(`DEFAULT ${def}`);
|
|
@@ -449,6 +453,19 @@ const interpolateSqlValues = ({ text, values }) => {
|
|
|
449
453
|
return pqb.escapeForMigration(values[i]);
|
|
450
454
|
}) : text;
|
|
451
455
|
};
|
|
456
|
+
const nameColumnChecks = (table, column, checks) => checks.map((check, i) => ({
|
|
457
|
+
...check,
|
|
458
|
+
name: check.name || `${table}_${column}_check${i === 0 ? "" : i}`
|
|
459
|
+
}));
|
|
460
|
+
const cmpRawSql = (a, b) => {
|
|
461
|
+
const values = [];
|
|
462
|
+
const aSql = a.makeSQL({ values });
|
|
463
|
+
const aValues = JSON.stringify(values);
|
|
464
|
+
values.length = 0;
|
|
465
|
+
const bSql = b.makeSQL({ values });
|
|
466
|
+
const bValues = JSON.stringify(values);
|
|
467
|
+
return aSql === bSql && aValues === bValues;
|
|
468
|
+
};
|
|
452
469
|
|
|
453
470
|
const tableMethods = {
|
|
454
471
|
enum(name) {
|
|
@@ -496,6 +513,10 @@ const createTable = async (migration, up, tableName, first, second, third) => {
|
|
|
496
513
|
language
|
|
497
514
|
);
|
|
498
515
|
tableData = pqb.parseTableData(dataFn);
|
|
516
|
+
tableData.constraints?.forEach((x, i) => {
|
|
517
|
+
if (x.name || !x.check) return;
|
|
518
|
+
x.name = `${tableName}_check${i === 0 ? "" : i}`;
|
|
519
|
+
});
|
|
499
520
|
} else {
|
|
500
521
|
shape = tableData = orchidCore.emptyObject;
|
|
501
522
|
}
|
|
@@ -745,7 +766,7 @@ const addOrDrop = (type, item, options) => {
|
|
|
745
766
|
type: "change",
|
|
746
767
|
from: {},
|
|
747
768
|
to: {
|
|
748
|
-
|
|
769
|
+
checks: item.data.checks
|
|
749
770
|
}
|
|
750
771
|
});
|
|
751
772
|
return {
|
|
@@ -1194,20 +1215,23 @@ const handleTableItemChange = (key, item, ast, alterTable, renameItems, values,
|
|
|
1194
1215
|
`ALTER COLUMN "${name}" SET COMPRESSION ${to.compression || "DEFAULT"}`
|
|
1195
1216
|
);
|
|
1196
1217
|
}
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1218
|
+
const fromChecks = from.checks && nameColumnChecks(ast.name, fromName, from.checks);
|
|
1219
|
+
const toChecks = to.checks && nameColumnChecks(ast.name, name, to.checks);
|
|
1220
|
+
fromChecks?.forEach((fromCheck) => {
|
|
1221
|
+
if (!toChecks?.some((toCheck) => cmpRawSql(fromCheck.sql, toCheck.sql))) {
|
|
1222
|
+
alterTable.push(`DROP CONSTRAINT "${fromCheck.name}"`);
|
|
1201
1223
|
}
|
|
1202
|
-
|
|
1224
|
+
});
|
|
1225
|
+
toChecks?.forEach((toCheck) => {
|
|
1226
|
+
if (!fromChecks?.some((fromCheck) => cmpRawSql(fromCheck.sql, toCheck.sql))) {
|
|
1203
1227
|
alterTable.push(
|
|
1204
|
-
`ADD CONSTRAINT "${
|
|
1205
|
-
CHECK (${
|
|
1228
|
+
`ADD CONSTRAINT "${toCheck.name}"
|
|
1229
|
+
CHECK (${toCheck.sql.toSQL({
|
|
1206
1230
|
values
|
|
1207
1231
|
})})`
|
|
1208
1232
|
);
|
|
1209
1233
|
}
|
|
1210
|
-
}
|
|
1234
|
+
});
|
|
1211
1235
|
const foreignKeysLen = Math.max(
|
|
1212
1236
|
from.foreignKeys?.length || 0,
|
|
1213
1237
|
to.foreignKeys?.length || 0
|
|
@@ -2315,9 +2339,9 @@ const createDomain = async (migration, up, name, fn) => {
|
|
|
2315
2339
|
const column = ast.baseType;
|
|
2316
2340
|
query = `CREATE DOMAIN ${quotedName} AS ${columnTypeToSql(column)}${column.data.collate ? `
|
|
2317
2341
|
COLLATE "${column.data.collate}"` : ""}${column.data.default !== void 0 ? `
|
|
2318
|
-
DEFAULT ${encodeColumnDefault(column.data.default, values)}` : ""}${!column.data.isNullable || column.data.
|
|
2342
|
+
DEFAULT ${encodeColumnDefault(column.data.default, values)}` : ""}${!column.data.isNullable || column.data.checks ? "\n" : ""}${[
|
|
2319
2343
|
!column.data.isNullable && "NOT NULL",
|
|
2320
|
-
column.data.check
|
|
2344
|
+
column.data.checks?.map((check) => `CHECK (${check.sql.toSQL({ values })})`).join(" ")
|
|
2321
2345
|
].filter(Boolean).join(" ")}`;
|
|
2322
2346
|
} else {
|
|
2323
2347
|
query = `DROP DOMAIN ${quotedName}`;
|
|
@@ -3938,7 +3962,11 @@ const domainsSql = `SELECT
|
|
|
3938
3962
|
datetime_precision AS "dateTimePrecision",
|
|
3939
3963
|
collation_name AS "collate",
|
|
3940
3964
|
domain_default AS "default",
|
|
3941
|
-
|
|
3965
|
+
(
|
|
3966
|
+
SELECT json_agg(pg_get_expr(conbin, conrelid))
|
|
3967
|
+
FROM pg_catalog.pg_constraint c
|
|
3968
|
+
WHERE c.contypid = d.oid
|
|
3969
|
+
) AS "checks"
|
|
3942
3970
|
FROM pg_catalog.pg_type d
|
|
3943
3971
|
JOIN pg_catalog.pg_namespace n ON n.oid = d.typnamespace
|
|
3944
3972
|
JOIN information_schema.domains i
|
|
@@ -3952,7 +3980,6 @@ JOIN pg_catalog.pg_type t
|
|
|
3952
3980
|
END
|
|
3953
3981
|
) = d.typbasetype
|
|
3954
3982
|
JOIN pg_catalog.pg_namespace s ON s.oid = t.typnamespace
|
|
3955
|
-
LEFT JOIN pg_catalog.pg_constraint c ON c.contypid = d.oid
|
|
3956
3983
|
WHERE d.typtype = 'd' AND ${filterSchema("n.nspname")}`;
|
|
3957
3984
|
const collationsSql = `SELECT
|
|
3958
3985
|
nspname "schemaName",
|
|
@@ -4171,10 +4198,10 @@ const makeDomainsMap = (ctx, data) => {
|
|
|
4171
4198
|
default: it.default,
|
|
4172
4199
|
typmod: -1
|
|
4173
4200
|
});
|
|
4174
|
-
if (it.
|
|
4175
|
-
column.data.
|
|
4176
|
-
sql: new pqb.RawSQL([[
|
|
4177
|
-
};
|
|
4201
|
+
if (it.checks) {
|
|
4202
|
+
column.data.checks = it.checks.map((check) => ({
|
|
4203
|
+
sql: new pqb.RawSQL([[check]])
|
|
4204
|
+
}));
|
|
4178
4205
|
}
|
|
4179
4206
|
domains[`${it.schemaName}.${it.name}`] = column;
|
|
4180
4207
|
}
|
|
@@ -4403,8 +4430,9 @@ const makeDbStructureColumnsShape = (ctx, data, domains, table, tableData) => {
|
|
|
4403
4430
|
return shape;
|
|
4404
4431
|
};
|
|
4405
4432
|
const getDbTableColumnsChecks = (tableData) => tableData.constraints.reduce((acc, item) => {
|
|
4433
|
+
var _a;
|
|
4406
4434
|
if (isColumnCheck(item)) {
|
|
4407
|
-
acc[item.check.columns[0]] = item.check.expression;
|
|
4435
|
+
(acc[_a = item.check.columns[0]] ?? (acc[_a] = [])).push(item.check.expression);
|
|
4408
4436
|
}
|
|
4409
4437
|
return acc;
|
|
4410
4438
|
}, {});
|
|
@@ -4439,11 +4467,11 @@ const dbColumnToAst = (ctx, data, domains, tableName, item, table, tableData, ch
|
|
|
4439
4467
|
);
|
|
4440
4468
|
}
|
|
4441
4469
|
}
|
|
4442
|
-
const
|
|
4443
|
-
if (
|
|
4444
|
-
column.data.
|
|
4470
|
+
const columnChecks = checks?.[item.name];
|
|
4471
|
+
if (columnChecks) {
|
|
4472
|
+
column.data.checks = columnChecks.map((check) => ({
|
|
4445
4473
|
sql: new pqb.RawSQL([[check]])
|
|
4446
|
-
};
|
|
4474
|
+
}));
|
|
4447
4475
|
}
|
|
4448
4476
|
const camelCaseName = orchidCore.toCamelCase(item.name);
|
|
4449
4477
|
if (ctx.snakeCase) {
|
|
@@ -5792,6 +5820,7 @@ exports.astToMigration = astToMigration;
|
|
|
5792
5820
|
exports.changeCache = changeCache;
|
|
5793
5821
|
exports.changeEnumValues = changeEnumValues;
|
|
5794
5822
|
exports.clearChanges = clearChanges;
|
|
5823
|
+
exports.cmpRawSql = cmpRawSql;
|
|
5795
5824
|
exports.colors = colors;
|
|
5796
5825
|
exports.columnToSql = columnToSql;
|
|
5797
5826
|
exports.columnTypeToSql = columnTypeToSql;
|
|
@@ -5836,6 +5865,7 @@ exports.makeStructureToAstCtx = makeStructureToAstCtx;
|
|
|
5836
5865
|
exports.migrate = migrate;
|
|
5837
5866
|
exports.migrateOrRollback = migrateOrRollback;
|
|
5838
5867
|
exports.migrationConfigDefaults = migrationConfigDefaults;
|
|
5868
|
+
exports.nameColumnChecks = nameColumnChecks;
|
|
5839
5869
|
exports.newMigration = newMigration;
|
|
5840
5870
|
exports.pluralize = pluralize;
|
|
5841
5871
|
exports.primaryKeyToSql = primaryKeyToSql;
|