@an5/orm 1.0.2 → 1.0.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/push.js +0 -51
- package/package.json +1 -1
package/dist/push.js
CHANGED
|
@@ -50,10 +50,6 @@ function parseTableName(raw) {
|
|
|
50
50
|
}
|
|
51
51
|
return { name: cleaned };
|
|
52
52
|
}
|
|
53
|
-
function tableKey(raw) {
|
|
54
|
-
const table = parseTableName(raw);
|
|
55
|
-
return `${table.schema || "dbo"}.${table.name}`.toLowerCase();
|
|
56
|
-
}
|
|
57
53
|
function tableSqlName(raw) {
|
|
58
54
|
const table = parseTableName(raw);
|
|
59
55
|
return table.schema ? `[${table.schema}].[${table.name}]` : `[${table.name}]`;
|
|
@@ -65,52 +61,6 @@ function tableObjectName(raw) {
|
|
|
65
61
|
function safeIdentifierName(raw) {
|
|
66
62
|
return raw.replace(/[^A-Za-z0-9_]/g, "_").replace(/_+/g, "_").replace(/^_|_$/g, "");
|
|
67
63
|
}
|
|
68
|
-
async function dropTablesNotInSchema(models) {
|
|
69
|
-
var _a;
|
|
70
|
-
if (!((_a = config.push) === null || _a === void 0 ? void 0 : _a.dropTables)) {
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
const validTableKeys = new Set(models.map(model => tableKey(model.tableName)));
|
|
74
|
-
const dbTables = await (await getDb()).$queryRawUnsafe(`
|
|
75
|
-
SELECT s.name AS schemaName, t.name AS tableName
|
|
76
|
-
FROM sys.tables t
|
|
77
|
-
INNER JOIN sys.schemas s ON s.schema_id = t.schema_id
|
|
78
|
-
WHERE t.is_ms_shipped = 0
|
|
79
|
-
`);
|
|
80
|
-
const tablesToDrop = dbTables.filter(table => !validTableKeys.has(`${table.schemaName}.${table.tableName}`.toLowerCase()));
|
|
81
|
-
if (tablesToDrop.length === 0) {
|
|
82
|
-
console.log("✅ No extra tables to drop.");
|
|
83
|
-
return;
|
|
84
|
-
}
|
|
85
|
-
console.warn(`⚠️ Dropping ${tablesToDrop.length} table(s) not present in schema because push.dropTables=true.`);
|
|
86
|
-
const dropKeys = new Set(tablesToDrop.map(table => `${table.schemaName}.${table.tableName}`.toLowerCase()));
|
|
87
|
-
const foreignKeys = await (await getDb()).$queryRawUnsafe(`
|
|
88
|
-
SELECT
|
|
89
|
-
fk.name AS constraintName,
|
|
90
|
-
ps.name AS parentSchema,
|
|
91
|
-
pt.name AS parentTable,
|
|
92
|
-
rs.name AS referencedSchema,
|
|
93
|
-
rt.name AS referencedTable
|
|
94
|
-
FROM sys.foreign_keys fk
|
|
95
|
-
INNER JOIN sys.tables pt ON pt.object_id = fk.parent_object_id
|
|
96
|
-
INNER JOIN sys.schemas ps ON ps.schema_id = pt.schema_id
|
|
97
|
-
INNER JOIN sys.tables rt ON rt.object_id = fk.referenced_object_id
|
|
98
|
-
INNER JOIN sys.schemas rs ON rs.schema_id = rt.schema_id
|
|
99
|
-
`);
|
|
100
|
-
for (const fk of foreignKeys) {
|
|
101
|
-
const parentKey = `${fk.parentSchema}.${fk.parentTable}`.toLowerCase();
|
|
102
|
-
const referencedKey = `${fk.referencedSchema}.${fk.referencedTable}`.toLowerCase();
|
|
103
|
-
if (dropKeys.has(parentKey) || dropKeys.has(referencedKey)) {
|
|
104
|
-
console.log(`Dropping foreign key [${fk.constraintName}] on [${fk.parentSchema}].[${fk.parentTable}]...`);
|
|
105
|
-
await (await getDb()).$executeRawUnsafe(`ALTER TABLE [${fk.parentSchema}].[${fk.parentTable}] DROP CONSTRAINT [${fk.constraintName}]`);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
for (const table of tablesToDrop) {
|
|
109
|
-
console.log(`Dropping table [${table.schemaName}].[${table.tableName}]...`);
|
|
110
|
-
await (await getDb()).$executeRawUnsafe(`DROP TABLE [${table.schemaName}].[${table.tableName}]`);
|
|
111
|
-
console.log(`✅ Dropped [${table.schemaName}].[${table.tableName}]`);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
64
|
async function push() {
|
|
115
65
|
let schemaText = "";
|
|
116
66
|
if (fs_1.default.existsSync(schemaDir)) {
|
|
@@ -229,7 +179,6 @@ async function push() {
|
|
|
229
179
|
}
|
|
230
180
|
}
|
|
231
181
|
console.log(`🚀 Pushing schema to database...`);
|
|
232
|
-
await dropTablesNotInSchema(models);
|
|
233
182
|
for (const model of models) {
|
|
234
183
|
const sqlTableName = tableSqlName(model.tableName);
|
|
235
184
|
const objectName = tableObjectName(model.tableName);
|