orchid-orm 1.38.0 → 1.38.1
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 +7 -2
- package/dist/index.js +109 -11
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +109 -11
- package/dist/index.mjs.map +1 -1
- package/dist/migrations.js +122 -46
- package/dist/migrations.js.map +1 -1
- package/dist/migrations.mjs +123 -47
- package/dist/migrations.mjs.map +1 -1
- package/package.json +5 -5
package/dist/migrations.js
CHANGED
|
@@ -619,10 +619,10 @@ const processDomains = async (ast, adapter, structureToAstCtx, domainsMap, dbStr
|
|
|
619
619
|
typmod: -1
|
|
620
620
|
}
|
|
621
621
|
);
|
|
622
|
-
if (domain.
|
|
623
|
-
dbColumn.data.
|
|
624
|
-
sql: new pqb.RawSQL([[
|
|
625
|
-
};
|
|
622
|
+
if (domain.checks) {
|
|
623
|
+
dbColumn.data.checks = domain.checks.map((check) => ({
|
|
624
|
+
sql: new pqb.RawSQL([[check]])
|
|
625
|
+
}));
|
|
626
626
|
}
|
|
627
627
|
const dbDomain = makeComparableDomain(
|
|
628
628
|
currentSchema,
|
|
@@ -633,13 +633,13 @@ const processDomains = async (ast, adapter, structureToAstCtx, domainsMap, dbStr
|
|
|
633
633
|
const found = codeDomains.filter(
|
|
634
634
|
(codeDomain) => orchidCore.deepCompare(dbDomain.compare, codeDomain.compare)
|
|
635
635
|
);
|
|
636
|
-
if ((domain.default || domain.
|
|
636
|
+
if ((domain.default || domain.checks?.length) && found.length) {
|
|
637
637
|
for (const codeDomain of found) {
|
|
638
638
|
holdCodeDomains.add(codeDomain);
|
|
639
639
|
}
|
|
640
640
|
const compare = [];
|
|
641
|
-
|
|
642
|
-
|
|
641
|
+
pushCompareDefault(compare, domain, found);
|
|
642
|
+
pushCompareChecks(compare, domain, found);
|
|
643
643
|
const source = `(VALUES (NULL::${getColumnDbType(
|
|
644
644
|
dbColumn,
|
|
645
645
|
currentSchema
|
|
@@ -716,17 +716,16 @@ const makeComparableDomain = (currentSchema, schemaName, name, column) => {
|
|
|
716
716
|
dateTimePrecision: inner.data.dateTimePrecision,
|
|
717
717
|
collate: column.data.collate,
|
|
718
718
|
hasDefault: column.data.default !== void 0,
|
|
719
|
-
|
|
719
|
+
hasChecks: !!column.data.checks?.length
|
|
720
720
|
}
|
|
721
721
|
};
|
|
722
722
|
};
|
|
723
|
-
const
|
|
724
|
-
|
|
725
|
-
if (inDb) {
|
|
723
|
+
const pushCompareDefault = (compare, domain, found) => {
|
|
724
|
+
if (domain.default) {
|
|
726
725
|
compare.push({
|
|
727
|
-
inDb,
|
|
726
|
+
inDb: domain.default,
|
|
728
727
|
inCode: found.map((codeDomain) => {
|
|
729
|
-
const value = codeDomain.column.data
|
|
728
|
+
const value = codeDomain.column.data.default;
|
|
730
729
|
if ("sql" in value) {
|
|
731
730
|
return value.sql;
|
|
732
731
|
}
|
|
@@ -735,6 +734,21 @@ const pushCompare = (compare, domain, found, key) => {
|
|
|
735
734
|
});
|
|
736
735
|
}
|
|
737
736
|
};
|
|
737
|
+
const pushCompareChecks = (compare, domain, found) => {
|
|
738
|
+
if (domain.checks?.length) {
|
|
739
|
+
const inCode = found.flatMap(
|
|
740
|
+
(codeDomain) => codeDomain.column.data.checks?.map(
|
|
741
|
+
(check) => typeof check === "string" ? check : check.sql
|
|
742
|
+
) || orchidCore.emptyArray
|
|
743
|
+
);
|
|
744
|
+
compare.push(
|
|
745
|
+
...domain.checks.map((check) => ({
|
|
746
|
+
inDb: check,
|
|
747
|
+
inCode
|
|
748
|
+
}))
|
|
749
|
+
);
|
|
750
|
+
}
|
|
751
|
+
};
|
|
738
752
|
const dropAst = (dbDomain) => ({
|
|
739
753
|
type: "domain",
|
|
740
754
|
action: "drop",
|
|
@@ -1620,12 +1634,12 @@ const processChecks = (ast, changeTableData, compareExpressions) => {
|
|
|
1620
1634
|
if (!hasDbChecks) {
|
|
1621
1635
|
if (codeChecks.length) {
|
|
1622
1636
|
const constraints = add.constraints ?? (add.constraints = []);
|
|
1623
|
-
for (const
|
|
1624
|
-
if (
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1637
|
+
for (const codeCheck of codeChecks) {
|
|
1638
|
+
if (!codeCheck.column || !changeTableData.changingColumns[codeCheck.column]) {
|
|
1639
|
+
constraints.push({
|
|
1640
|
+
check: codeCheck.check.sql,
|
|
1641
|
+
name: codeCheck.name
|
|
1642
|
+
});
|
|
1629
1643
|
}
|
|
1630
1644
|
}
|
|
1631
1645
|
}
|
|
@@ -1646,20 +1660,40 @@ const processChecks = (ast, changeTableData, compareExpressions) => {
|
|
|
1646
1660
|
compare: [
|
|
1647
1661
|
{
|
|
1648
1662
|
inDb: dbCheck.expression,
|
|
1649
|
-
inCode: codeChecks.map((check) => check.sql)
|
|
1663
|
+
inCode: codeChecks.map(({ check }) => check.sql)
|
|
1650
1664
|
}
|
|
1651
1665
|
],
|
|
1652
1666
|
handle(i) {
|
|
1653
|
-
if (i !== void 0)
|
|
1654
|
-
|
|
1655
|
-
|
|
1667
|
+
if (i !== void 0) {
|
|
1668
|
+
foundCodeChecks.add(i);
|
|
1669
|
+
} else {
|
|
1670
|
+
dropCheck(changeTableData, dbCheck, name);
|
|
1671
|
+
}
|
|
1672
|
+
if (--wait !== 0) return;
|
|
1673
|
+
const checksToAdd = [];
|
|
1674
|
+
codeChecks.forEach((check, i2) => {
|
|
1675
|
+
if (foundCodeChecks.has(i2)) {
|
|
1676
|
+
if (!check.column) return;
|
|
1677
|
+
const change = changeTableData.changingColumns[check.column];
|
|
1678
|
+
if (!change) return;
|
|
1679
|
+
const columnChecks = change.to.data.checks;
|
|
1680
|
+
if (!columnChecks) return;
|
|
1681
|
+
const i3 = columnChecks.indexOf(check.check);
|
|
1682
|
+
if (i3 !== -1) {
|
|
1683
|
+
columnChecks.splice(i3, 1);
|
|
1684
|
+
}
|
|
1685
|
+
return;
|
|
1686
|
+
}
|
|
1687
|
+
checksToAdd.push({
|
|
1688
|
+
name: check.name,
|
|
1689
|
+
check: check.check.sql
|
|
1690
|
+
});
|
|
1691
|
+
});
|
|
1692
|
+
if (checksToAdd.length) {
|
|
1693
|
+
(add.constraints ?? (add.constraints = [])).push(...checksToAdd);
|
|
1694
|
+
}
|
|
1695
|
+
if (!changeTableData.pushedAst && (changeTableData.changeTableAst.drop.constraints?.length || add.constraints?.length)) {
|
|
1656
1696
|
changeTableData.pushedAst = true;
|
|
1657
|
-
(add.constraints ?? (add.constraints = [])).push(
|
|
1658
|
-
...codeChecks.filter((_, i2) => !foundCodeChecks.has(i2)).map((check) => ({
|
|
1659
|
-
name: check.name,
|
|
1660
|
-
check: check.sql
|
|
1661
|
-
}))
|
|
1662
|
-
);
|
|
1663
1697
|
ast.push(changeTableData.changeTableAst);
|
|
1664
1698
|
}
|
|
1665
1699
|
}
|
|
@@ -1673,41 +1707,70 @@ const collectCodeChecks = ({
|
|
|
1673
1707
|
codeTable,
|
|
1674
1708
|
changeTableAst: { shape }
|
|
1675
1709
|
}) => {
|
|
1710
|
+
const names = /* @__PURE__ */ new Set();
|
|
1676
1711
|
const codeChecks = [];
|
|
1677
1712
|
for (const key in codeTable.shape) {
|
|
1678
1713
|
const column = codeTable.shape[key];
|
|
1679
|
-
if (!column.data.
|
|
1680
|
-
const
|
|
1681
|
-
if (checkForColumnAddOrDrop(shape,
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
column
|
|
1685
|
-
|
|
1714
|
+
if (!column.data.checks) continue;
|
|
1715
|
+
const columnName = column.data.name ?? key;
|
|
1716
|
+
if (checkForColumnAddOrDrop(shape, columnName)) continue;
|
|
1717
|
+
const baseName = `${codeTable.table}_${columnName}_check`;
|
|
1718
|
+
codeChecks.push(
|
|
1719
|
+
...column.data.checks.map((check) => {
|
|
1720
|
+
let name = check.name;
|
|
1721
|
+
if (!name) {
|
|
1722
|
+
name = baseName;
|
|
1723
|
+
let n = 0;
|
|
1724
|
+
while (names.has(name)) {
|
|
1725
|
+
name = baseName + ++n;
|
|
1726
|
+
}
|
|
1727
|
+
}
|
|
1728
|
+
names.add(name);
|
|
1729
|
+
return {
|
|
1730
|
+
check,
|
|
1731
|
+
name,
|
|
1732
|
+
column: columnName
|
|
1733
|
+
};
|
|
1734
|
+
})
|
|
1735
|
+
);
|
|
1686
1736
|
}
|
|
1687
1737
|
if (codeTable.internal.tableData.constraints) {
|
|
1688
1738
|
for (const constraint of codeTable.internal.tableData.constraints) {
|
|
1689
1739
|
const { check } = constraint;
|
|
1690
1740
|
if (check) {
|
|
1691
|
-
|
|
1741
|
+
const baseName = `${codeTable.table}_check`;
|
|
1742
|
+
let name = constraint.name;
|
|
1743
|
+
if (!name) {
|
|
1744
|
+
name = baseName;
|
|
1745
|
+
let n = 0;
|
|
1746
|
+
while (names.has(name)) {
|
|
1747
|
+
name = baseName + ++n;
|
|
1748
|
+
}
|
|
1749
|
+
}
|
|
1750
|
+
names.add(name);
|
|
1751
|
+
codeChecks.push({
|
|
1752
|
+
check: { sql: check, name: constraint.name },
|
|
1753
|
+
name
|
|
1754
|
+
});
|
|
1692
1755
|
}
|
|
1693
1756
|
}
|
|
1694
1757
|
}
|
|
1695
1758
|
return codeChecks;
|
|
1696
1759
|
};
|
|
1697
1760
|
const dropCheck = ({ changeTableAst: { drop }, changingColumns }, dbCheck, name) => {
|
|
1698
|
-
|
|
1761
|
+
var _a;
|
|
1699
1762
|
const sql = new pqb.RawSQL([
|
|
1700
1763
|
[dbCheck.expression]
|
|
1701
1764
|
]);
|
|
1702
1765
|
if (dbCheck.columns?.length === 1 && changingColumns[dbCheck.columns[0]]) {
|
|
1703
1766
|
const column = changingColumns[dbCheck.columns[0]];
|
|
1704
1767
|
column.from.data.name = "i_d";
|
|
1705
|
-
column.from.data.
|
|
1768
|
+
((_a = column.from.data).checks ?? (_a.checks = [])).push({
|
|
1706
1769
|
name,
|
|
1707
1770
|
sql
|
|
1708
|
-
};
|
|
1771
|
+
});
|
|
1709
1772
|
} else {
|
|
1710
|
-
constraints.push({
|
|
1773
|
+
(drop.constraints ?? (drop.constraints = [])).push({
|
|
1711
1774
|
name,
|
|
1712
1775
|
check: sql
|
|
1713
1776
|
});
|
|
@@ -1921,7 +1984,7 @@ const addChangeTable = (dbStructure, changeTables, tableShapes, currentSchema, d
|
|
|
1921
1984
|
const shape = {};
|
|
1922
1985
|
const schema = codeTable.q.schema ?? currentSchema;
|
|
1923
1986
|
changeTables.push({
|
|
1924
|
-
codeTable,
|
|
1987
|
+
codeTable: cloneCodeTableForChange(codeTable),
|
|
1925
1988
|
dbTable,
|
|
1926
1989
|
dbTableData: rakeDb.getDbStructureTableData(dbStructure, dbTable),
|
|
1927
1990
|
schema,
|
|
@@ -1939,6 +2002,19 @@ const addChangeTable = (dbStructure, changeTables, tableShapes, currentSchema, d
|
|
|
1939
2002
|
});
|
|
1940
2003
|
tableShapes[`${schema}.${codeTable.table}`] = shape;
|
|
1941
2004
|
};
|
|
2005
|
+
const cloneCodeTableForChange = (codeTable) => ({
|
|
2006
|
+
...codeTable,
|
|
2007
|
+
shape: Object.fromEntries(
|
|
2008
|
+
Object.entries(codeTable.shape).map(([key, column]) => {
|
|
2009
|
+
const cloned = Object.create(column);
|
|
2010
|
+
cloned.data = {
|
|
2011
|
+
...cloned.data,
|
|
2012
|
+
checks: cloned.data.checks && [...cloned.data.checks]
|
|
2013
|
+
};
|
|
2014
|
+
return [key, cloned];
|
|
2015
|
+
})
|
|
2016
|
+
)
|
|
2017
|
+
});
|
|
1942
2018
|
const createTableAst = (currentSchema, table) => {
|
|
1943
2019
|
return {
|
|
1944
2020
|
type: "table",
|
|
@@ -2107,8 +2183,8 @@ const report = (ast, config, currentSchema) => {
|
|
|
2107
2183
|
if (column.data.foreignKeys) {
|
|
2108
2184
|
counters["foreign key"] += column.data.foreignKeys.length;
|
|
2109
2185
|
}
|
|
2110
|
-
if (column.data.
|
|
2111
|
-
counters.check
|
|
2186
|
+
if (column.data.checks) {
|
|
2187
|
+
counters.check += column.data.checks.length;
|
|
2112
2188
|
}
|
|
2113
2189
|
}
|
|
2114
2190
|
const summary = [];
|
|
@@ -2141,13 +2217,13 @@ const report = (ast, config, currentSchema) => {
|
|
|
2141
2217
|
for (const change of changes) {
|
|
2142
2218
|
if (change.type === "add" || change.type === "drop") {
|
|
2143
2219
|
const column = change.item;
|
|
2144
|
-
const { primaryKey, indexes, excludes, foreignKeys,
|
|
2220
|
+
const { primaryKey, indexes, excludes, foreignKeys, checks } = column.data;
|
|
2145
2221
|
inner.push(
|
|
2146
2222
|
`${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) => {
|
|
2147
2223
|
return `${fnOrTableToString(
|
|
2148
2224
|
fk.fnOrTable
|
|
2149
2225
|
)}(${fk.foreignColumns.join(", ")})`;
|
|
2150
|
-
}).join(", ")}` : ""}${indexes?.length ? indexes.length === 1 ? ", has index" : `, has ${indexes.length} indexes` : ""}${excludes?.length ? excludes.length === 1 ? ", has exclude" : `, has ${excludes.length} excludes` : ""}${
|
|
2226
|
+
}).join(", ")}` : ""}${indexes?.length ? indexes.length === 1 ? ", has index" : `, has ${indexes.length} indexes` : ""}${excludes?.length ? excludes.length === 1 ? ", has exclude" : `, has ${excludes.length} excludes` : ""}${checks?.length ? `, checks ${checks.map((check) => check.sql.toSQL({ values: [] })).join(", ")}` : ""}`
|
|
2151
2227
|
);
|
|
2152
2228
|
} else if (change.type === "change") {
|
|
2153
2229
|
const name = change.from.column?.data.name ?? key;
|