orchid-orm 1.63.5 → 1.64.0
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/migrations/index.js +32 -34
- package/dist/migrations/index.js.map +1 -1
- package/dist/migrations/index.mjs +32 -34
- package/dist/migrations/index.mjs.map +1 -1
- package/dist/migrations/node-postgres.js +32 -34
- package/dist/migrations/node-postgres.js.map +1 -1
- package/dist/migrations/node-postgres.mjs +32 -34
- package/dist/migrations/node-postgres.mjs.map +1 -1
- package/dist/migrations/postgres-js.js +32 -34
- package/dist/migrations/postgres-js.js.map +1 -1
- package/dist/migrations/postgres-js.mjs +32 -34
- package/dist/migrations/postgres-js.mjs.map +1 -1
- package/package.json +5 -5
package/dist/migrations/index.js
CHANGED
|
@@ -197,12 +197,12 @@ const renameSchemaInStructures = (items, from, to) => {
|
|
|
197
197
|
}
|
|
198
198
|
};
|
|
199
199
|
|
|
200
|
-
const processExtensions = (
|
|
200
|
+
const processExtensions = (ast, dbStructure, {
|
|
201
201
|
currentSchema,
|
|
202
202
|
internal: { extensions, generatorIgnore }
|
|
203
203
|
}) => {
|
|
204
204
|
const codeExtensions = extensions?.map((ext) => {
|
|
205
|
-
const [schema, name] = rakeDb.getSchemaAndTableFromName(
|
|
205
|
+
const [schema, name] = rakeDb.getSchemaAndTableFromName(currentSchema, ext.name);
|
|
206
206
|
return { schema, name, version: ext.version };
|
|
207
207
|
});
|
|
208
208
|
for (const dbExt of dbStructure.extensions) {
|
|
@@ -400,7 +400,6 @@ const changeColumns = async (adapter, config, structureToAstCtx, dbStructure, do
|
|
|
400
400
|
dbColumnStructure
|
|
401
401
|
);
|
|
402
402
|
const action = await compareColumns(
|
|
403
|
-
config,
|
|
404
403
|
adapter,
|
|
405
404
|
domainsMap,
|
|
406
405
|
ast,
|
|
@@ -438,13 +437,13 @@ const changeColumns = async (adapter, config, structureToAstCtx, dbStructure, do
|
|
|
438
437
|
}
|
|
439
438
|
}
|
|
440
439
|
};
|
|
441
|
-
const compareColumns = async (
|
|
440
|
+
const compareColumns = async (adapter, domainsMap, ast, currentSchema, compareSql, changeTableData, typeCastsCache, verifying, key, dbName, dbColumn, codeColumn) => {
|
|
442
441
|
if (dbColumn instanceof pqb.ArrayColumn && codeColumn instanceof pqb.ArrayColumn) {
|
|
443
442
|
dbColumn = dbColumn.data.item;
|
|
444
443
|
codeColumn = codeColumn.data.item;
|
|
445
444
|
}
|
|
446
|
-
const dbType = getColumnDbType(
|
|
447
|
-
const codeType = getColumnDbType(
|
|
445
|
+
const dbType = getColumnDbType(dbColumn, currentSchema);
|
|
446
|
+
const codeType = getColumnDbType(codeColumn, currentSchema);
|
|
448
447
|
if (dbType !== codeType) {
|
|
449
448
|
const typeCasts = await getTypeCasts(adapter, typeCastsCache);
|
|
450
449
|
const dbBaseType = pqb.getColumnBaseType(dbColumn, domainsMap, dbType);
|
|
@@ -602,10 +601,10 @@ const changeColumn = (changeTableData, key, dbName, dbColumn, codeColumn) => {
|
|
|
602
601
|
to: { column: simpleCodeColumn }
|
|
603
602
|
};
|
|
604
603
|
};
|
|
605
|
-
const getColumnDbType = (
|
|
604
|
+
const getColumnDbType = (column, currentSchema) => {
|
|
606
605
|
if (column instanceof pqb.EnumColumn) {
|
|
607
606
|
const [schema = currentSchema, name] = rakeDb.getSchemaAndTableFromName(
|
|
608
|
-
|
|
607
|
+
currentSchema,
|
|
609
608
|
column.enumName
|
|
610
609
|
);
|
|
611
610
|
return `${schema}.${name}`;
|
|
@@ -633,7 +632,7 @@ const renameColumn = (columns, from, to) => {
|
|
|
633
632
|
}
|
|
634
633
|
};
|
|
635
634
|
|
|
636
|
-
const processDomains = async (
|
|
635
|
+
const processDomains = async (ast, adapter, domainsMap, dbStructure, {
|
|
637
636
|
codeItems: { domains },
|
|
638
637
|
structureToAstCtx,
|
|
639
638
|
currentSchema,
|
|
@@ -643,7 +642,7 @@ const processDomains = async (config, ast, adapter, domainsMap, dbStructure, {
|
|
|
643
642
|
if (domains) {
|
|
644
643
|
for (const { schemaName, name, column } of domains) {
|
|
645
644
|
codeDomains.push(
|
|
646
|
-
makeComparableDomain(
|
|
645
|
+
makeComparableDomain(currentSchema, schemaName, name, column)
|
|
647
646
|
);
|
|
648
647
|
}
|
|
649
648
|
}
|
|
@@ -679,7 +678,6 @@ const processDomains = async (config, ast, adapter, domainsMap, dbStructure, {
|
|
|
679
678
|
}));
|
|
680
679
|
}
|
|
681
680
|
const dbDomain = makeComparableDomain(
|
|
682
|
-
config,
|
|
683
681
|
currentSchema,
|
|
684
682
|
domain.schemaName,
|
|
685
683
|
domain.name,
|
|
@@ -696,7 +694,6 @@ const processDomains = async (config, ast, adapter, domainsMap, dbStructure, {
|
|
|
696
694
|
pushCompareDefault(compare, domain, found);
|
|
697
695
|
pushCompareChecks(compare, domain, found);
|
|
698
696
|
const source = `(VALUES (NULL::${getColumnDbType(
|
|
699
|
-
config,
|
|
700
697
|
dbColumn,
|
|
701
698
|
currentSchema
|
|
702
699
|
)})) t(value)`;
|
|
@@ -750,7 +747,7 @@ const processDomains = async (config, ast, adapter, domainsMap, dbStructure, {
|
|
|
750
747
|
}
|
|
751
748
|
}
|
|
752
749
|
};
|
|
753
|
-
const makeComparableDomain = (
|
|
750
|
+
const makeComparableDomain = (currentSchema, schemaName, name, column) => {
|
|
754
751
|
let arrayDims = 0;
|
|
755
752
|
const isNullable = column.data.isNullable ?? false;
|
|
756
753
|
let inner = column;
|
|
@@ -758,9 +755,9 @@ const makeComparableDomain = (config, currentSchema, schemaName, name, column) =
|
|
|
758
755
|
inner = inner.data.item;
|
|
759
756
|
arrayDims++;
|
|
760
757
|
}
|
|
761
|
-
const fullType = getColumnDbType(
|
|
758
|
+
const fullType = getColumnDbType(inner, currentSchema);
|
|
762
759
|
const [typeSchema = "pg_catalog", type] = rakeDb.getSchemaAndTableFromName(
|
|
763
|
-
|
|
760
|
+
currentSchema,
|
|
764
761
|
fullType
|
|
765
762
|
);
|
|
766
763
|
return {
|
|
@@ -1668,7 +1665,7 @@ const fnOrTableToString = (fnOrTable) => {
|
|
|
1668
1665
|
};
|
|
1669
1666
|
const parseForeignKey = (config, codeConstraint, references, currentSchema) => {
|
|
1670
1667
|
const { fnOrTable, columns, foreignColumns, options } = references;
|
|
1671
|
-
const [schema, table] = rakeDb.getSchemaAndTableFromName(
|
|
1668
|
+
const [schema, table] = rakeDb.getSchemaAndTableFromName(currentSchema, fnOrTable);
|
|
1672
1669
|
return {
|
|
1673
1670
|
references: {
|
|
1674
1671
|
foreignSchema: schema ?? currentSchema,
|
|
@@ -1854,6 +1851,7 @@ const processTables = async (ast, domainsMap, adapter, dbStructure, config, {
|
|
|
1854
1851
|
const compareSql = { values: [], expressions: [] };
|
|
1855
1852
|
const tableExpressions = [];
|
|
1856
1853
|
const { changeTables, changeTableSchemas, dropTables, tableShapes } = collectChangeAndDropTables(
|
|
1854
|
+
adapter,
|
|
1857
1855
|
config,
|
|
1858
1856
|
tables,
|
|
1859
1857
|
dbStructure,
|
|
@@ -1909,19 +1907,19 @@ const collectCreateTables = (tables, dbStructure, currentSchema) => {
|
|
|
1909
1907
|
return acc;
|
|
1910
1908
|
}, []);
|
|
1911
1909
|
};
|
|
1912
|
-
const collectChangeAndDropTables = (config, tables, dbStructure, currentSchema, createTables, generatorIgnore) => {
|
|
1910
|
+
const collectChangeAndDropTables = (adapter, config, tables, dbStructure, currentSchema, createTables, generatorIgnore) => {
|
|
1913
1911
|
const changeTables = [];
|
|
1914
1912
|
const changeTableSchemas = [];
|
|
1915
1913
|
const dropTables = [];
|
|
1916
1914
|
const tableShapes = {};
|
|
1917
1915
|
const ignoreTables = generatorIgnore?.tables?.map((name) => {
|
|
1918
1916
|
const [schema = currentSchema, table] = rakeDb.getSchemaAndTableFromName(
|
|
1919
|
-
|
|
1917
|
+
currentSchema,
|
|
1920
1918
|
name
|
|
1921
1919
|
);
|
|
1922
1920
|
return { schema, table };
|
|
1923
1921
|
});
|
|
1924
|
-
const { schema: migrationsSchema = "public", table: migrationsTable } = rakeDb.getMigrationsSchemaAndTable(config);
|
|
1922
|
+
const { schema: migrationsSchema = "public", table: migrationsTable } = rakeDb.getMigrationsSchemaAndTable(adapter, config);
|
|
1925
1923
|
for (const dbTable of dbStructure.tables) {
|
|
1926
1924
|
if (dbTable.name === migrationsTable && dbTable.schemaName === migrationsSchema || generatorIgnore?.schemas?.includes(dbTable.schemaName) || ignoreTables?.some(
|
|
1927
1925
|
({ schema, table }) => table === dbTable.name && schema === dbTable.schemaName
|
|
@@ -2179,10 +2177,9 @@ const composeMigration = async (adapter, config, ast, dbStructure, params) => {
|
|
|
2179
2177
|
const { structureToAstCtx, currentSchema } = params;
|
|
2180
2178
|
const domainsMap = rakeDb.makeDomainsMap(structureToAstCtx, dbStructure);
|
|
2181
2179
|
await processSchemas(ast, dbStructure, params);
|
|
2182
|
-
processExtensions(
|
|
2180
|
+
processExtensions(ast, dbStructure, params);
|
|
2183
2181
|
const pendingDbTypes = new PendingDbTypes();
|
|
2184
2182
|
await processDomains(
|
|
2185
|
-
config,
|
|
2186
2183
|
ast,
|
|
2187
2184
|
adapter,
|
|
2188
2185
|
domainsMap,
|
|
@@ -2208,7 +2205,7 @@ const verifyMigration = async (adapter, config, migrationCode, generateMigration
|
|
|
2208
2205
|
const migrationFn = new Function("change", migrationCode);
|
|
2209
2206
|
let code;
|
|
2210
2207
|
try {
|
|
2211
|
-
await adapter.transaction(
|
|
2208
|
+
await adapter.transaction(async (trx) => {
|
|
2212
2209
|
const changeFns = [];
|
|
2213
2210
|
migrationFn((changeCb) => {
|
|
2214
2211
|
changeFns.push(changeCb);
|
|
@@ -2325,7 +2322,7 @@ const report = (ast, config, currentSchema) => {
|
|
|
2325
2322
|
const column = change.item;
|
|
2326
2323
|
const { primaryKey, indexes, excludes, foreignKeys, checks } = column.data;
|
|
2327
2324
|
inner.push(
|
|
2328
|
-
`${change.type === "add" ? green("+ add column") : red("- drop column")} ${key} ${column.data.alias ?? getColumnDbType(
|
|
2325
|
+
`${change.type === "add" ? green("+ add column") : red("- drop column")} ${key} ${column.data.alias ?? getColumnDbType(column, currentSchema)}${column.data.isNullable ? " nullable" : ""}${primaryKey ? " primary key" : ""}${foreignKeys ? ` references ${foreignKeys.map((fk) => {
|
|
2329
2326
|
return `${fnOrTableToString(
|
|
2330
2327
|
fk.fnOrTable
|
|
2331
2328
|
)}(${fk.foreignColumns.join(", ")})`;
|
|
@@ -2386,7 +2383,7 @@ const report = (ast, config, currentSchema) => {
|
|
|
2386
2383
|
for (const { references } of a.drop.constraints) {
|
|
2387
2384
|
if (!references) continue;
|
|
2388
2385
|
const [schema, name] = rakeDb.getSchemaAndTableFromName(
|
|
2389
|
-
|
|
2386
|
+
currentSchema,
|
|
2390
2387
|
references.fnOrTable
|
|
2391
2388
|
);
|
|
2392
2389
|
inner.push(
|
|
@@ -2577,11 +2574,11 @@ const generate = async (adapters, config, args, afterPull) => {
|
|
|
2577
2574
|
afterPull
|
|
2578
2575
|
);
|
|
2579
2576
|
const [adapter] = adapters;
|
|
2580
|
-
const
|
|
2577
|
+
const adapterSchema = adapter.getSchema();
|
|
2578
|
+
const currentSchema = (typeof adapterSchema === "function" ? adapterSchema() : adapterSchema) ?? "public";
|
|
2581
2579
|
const db = await getDbFromConfig(config, dbPath);
|
|
2582
2580
|
const { columnTypes, internal } = db.$qb;
|
|
2583
2581
|
const codeItems = await getActualItems(
|
|
2584
|
-
config,
|
|
2585
2582
|
db,
|
|
2586
2583
|
currentSchema,
|
|
2587
2584
|
internal,
|
|
@@ -2738,7 +2735,7 @@ const compareDbStructures = (a, b, i, path2) => {
|
|
|
2738
2735
|
throw new Error(`${path2} in the db 0 does not match db ${i}`);
|
|
2739
2736
|
}
|
|
2740
2737
|
};
|
|
2741
|
-
const getActualItems = async (
|
|
2738
|
+
const getActualItems = async (db, currentSchema, internal, columnTypes) => {
|
|
2742
2739
|
const tableNames = /* @__PURE__ */ new Set();
|
|
2743
2740
|
const habtmTables = /* @__PURE__ */ new Map();
|
|
2744
2741
|
const codeItems = {
|
|
@@ -2781,7 +2778,7 @@ const getActualItems = async (config, db, currentSchema, internal, columnTypes)
|
|
|
2781
2778
|
delete table.shape[key2];
|
|
2782
2779
|
} else if (column instanceof pqb.DomainColumn) {
|
|
2783
2780
|
const [schemaName = currentSchema, name2] = rakeDb.getSchemaAndTableFromName(
|
|
2784
|
-
|
|
2781
|
+
currentSchema,
|
|
2785
2782
|
column.dataType
|
|
2786
2783
|
);
|
|
2787
2784
|
domains.set(column.dataType, {
|
|
@@ -2792,21 +2789,21 @@ const getActualItems = async (config, db, currentSchema, internal, columnTypes)
|
|
|
2792
2789
|
} else {
|
|
2793
2790
|
const en = column.dataType === "enum" ? column : column instanceof pqb.ArrayColumn && column.data.item.dataType === "enum" ? column.data.item : void 0;
|
|
2794
2791
|
if (en) {
|
|
2795
|
-
processEnumColumn(
|
|
2792
|
+
processEnumColumn(en, currentSchema, codeItems);
|
|
2796
2793
|
}
|
|
2797
2794
|
}
|
|
2798
2795
|
}
|
|
2799
2796
|
}
|
|
2800
2797
|
if (internal.extensions) {
|
|
2801
2798
|
for (const extension of internal.extensions) {
|
|
2802
|
-
const [schema] = rakeDb.getSchemaAndTableFromName(
|
|
2799
|
+
const [schema] = rakeDb.getSchemaAndTableFromName(currentSchema, extension.name);
|
|
2803
2800
|
if (schema) codeItems.schemas.add(schema);
|
|
2804
2801
|
}
|
|
2805
2802
|
}
|
|
2806
2803
|
if (internal.domains) {
|
|
2807
2804
|
for (const key in internal.domains) {
|
|
2808
2805
|
const [schemaName = currentSchema, name] = rakeDb.getSchemaAndTableFromName(
|
|
2809
|
-
|
|
2806
|
+
currentSchema,
|
|
2810
2807
|
key
|
|
2811
2808
|
);
|
|
2812
2809
|
const column = internal.domains[key](columnTypes);
|
|
@@ -2823,9 +2820,9 @@ const getActualItems = async (config, db, currentSchema, internal, columnTypes)
|
|
|
2823
2820
|
}
|
|
2824
2821
|
return codeItems;
|
|
2825
2822
|
};
|
|
2826
|
-
const processEnumColumn = (
|
|
2823
|
+
const processEnumColumn = (column, currentSchema, codeItems) => {
|
|
2827
2824
|
const { enumName, options } = column;
|
|
2828
|
-
const [schema, name] = rakeDb.getSchemaAndTableFromName(
|
|
2825
|
+
const [schema, name] = rakeDb.getSchemaAndTableFromName(currentSchema, enumName);
|
|
2829
2826
|
const enumSchema = schema ?? currentSchema;
|
|
2830
2827
|
codeItems.enums.set(`${enumSchema}.${name}`, {
|
|
2831
2828
|
schema: enumSchema,
|
|
@@ -3169,7 +3166,8 @@ const pull = async (adapters, config) => {
|
|
|
3169
3166
|
const baseTablePath = config.baseTable.getFilePath();
|
|
3170
3167
|
const baseTableExportedAs = config.baseTable.exportAs;
|
|
3171
3168
|
const [adapter] = adapters;
|
|
3172
|
-
const
|
|
3169
|
+
const adapterSchema = adapter.getSchema();
|
|
3170
|
+
const currentSchema = (typeof adapterSchema === "function" ? adapterSchema() : adapterSchema) ?? "public";
|
|
3173
3171
|
const ctx = rakeDb.makeStructureToAstCtx(config, currentSchema);
|
|
3174
3172
|
const asts = await rakeDb.structureToAst(ctx, adapter, config);
|
|
3175
3173
|
const { tableInfos, fkeys } = getTableInfosAndFKeys(asts, config);
|