@fragno-dev/db 0.1.3 → 0.1.6
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/.turbo/turbo-build.log +43 -30
- package/CHANGELOG.md +19 -0
- package/dist/adapters/adapters.js +1 -0
- package/dist/adapters/drizzle/drizzle-adapter.d.ts +1 -1
- package/dist/adapters/drizzle/drizzle-adapter.d.ts.map +1 -1
- package/dist/adapters/drizzle/drizzle-adapter.js +9 -2
- package/dist/adapters/drizzle/drizzle-adapter.js.map +1 -1
- package/dist/adapters/drizzle/generate.js +1 -1
- package/dist/adapters/drizzle/generate.js.map +1 -1
- package/dist/adapters/kysely/kysely-adapter.d.ts +1 -1
- package/dist/adapters/kysely/kysely-adapter.d.ts.map +1 -1
- package/dist/adapters/kysely/kysely-adapter.js +47 -27
- package/dist/adapters/kysely/kysely-adapter.js.map +1 -1
- package/dist/adapters/kysely/kysely-query-compiler.js +2 -2
- package/dist/adapters/kysely/kysely-query-compiler.js.map +1 -1
- package/dist/adapters/kysely/kysely-query.js +2 -1
- package/dist/adapters/kysely/kysely-query.js.map +1 -1
- package/dist/adapters/kysely/kysely-uow-compiler.js +2 -2
- package/dist/adapters/kysely/kysely-uow-compiler.js.map +1 -1
- package/dist/adapters/kysely/migration/execute-base.js +128 -0
- package/dist/adapters/kysely/migration/execute-base.js.map +1 -0
- package/dist/adapters/kysely/migration/execute-factory.js +28 -0
- package/dist/adapters/kysely/migration/execute-factory.js.map +1 -0
- package/dist/adapters/kysely/migration/execute-mssql.js +112 -0
- package/dist/adapters/kysely/migration/execute-mssql.js.map +1 -0
- package/dist/adapters/kysely/migration/execute-mysql.js +93 -0
- package/dist/adapters/kysely/migration/execute-mysql.js.map +1 -0
- package/dist/adapters/kysely/migration/execute-postgres.js +104 -0
- package/dist/adapters/kysely/migration/execute-postgres.js.map +1 -0
- package/dist/adapters/kysely/migration/execute-sqlite.js +123 -0
- package/dist/adapters/kysely/migration/execute-sqlite.js.map +1 -0
- package/dist/adapters/kysely/migration/execute.js +23 -168
- package/dist/adapters/kysely/migration/execute.js.map +1 -1
- package/dist/migration-engine/shared.d.ts +24 -5
- package/dist/migration-engine/shared.d.ts.map +1 -1
- package/dist/migration-engine/shared.js.map +1 -1
- package/dist/query/query.d.ts +4 -4
- package/dist/query/query.d.ts.map +1 -1
- package/dist/query/unit-of-work.d.ts +22 -22
- package/dist/query/unit-of-work.d.ts.map +1 -1
- package/dist/schema/create.d.ts +41 -41
- package/dist/schema/create.d.ts.map +1 -1
- package/package.json +7 -2
- package/src/adapters/drizzle/drizzle-adapter-pglite.test.ts +1 -1
- package/src/adapters/drizzle/drizzle-adapter.ts +13 -3
- package/src/adapters/drizzle/generate.test.ts +97 -0
- package/src/adapters/drizzle/generate.ts +3 -2
- package/src/adapters/kysely/kysely-adapter.ts +64 -35
- package/src/adapters/kysely/kysely-query-compiler.ts +3 -1
- package/src/adapters/kysely/kysely-query.ts +3 -1
- package/src/adapters/kysely/kysely-uow-compiler.ts +4 -2
- package/src/adapters/kysely/migration/execute-base.ts +256 -0
- package/src/adapters/kysely/migration/execute-factory.ts +32 -0
- package/src/adapters/kysely/migration/execute-mssql.ts +250 -0
- package/src/adapters/kysely/migration/execute-mysql.ts +211 -0
- package/src/adapters/kysely/migration/execute-postgres.ts +234 -0
- package/src/adapters/kysely/migration/execute-sqlite.test.ts +1363 -0
- package/src/adapters/kysely/migration/execute-sqlite.ts +247 -0
- package/src/adapters/kysely/migration/execute.ts +33 -396
- package/src/adapters/kysely/migration/kysely-migrator.test.ts +84 -2
- package/src/migration-engine/shared.ts +29 -11
- package/tsdown.config.ts +1 -0
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { sql } from "kysely";
|
|
2
|
+
import {
|
|
3
|
+
type ColumnOperation,
|
|
4
|
+
isUpdated,
|
|
5
|
+
type MigrationOperation,
|
|
6
|
+
} from "../../../migration-engine/shared";
|
|
7
|
+
import type { TableNameMapper } from "../kysely-shared";
|
|
8
|
+
import {
|
|
9
|
+
BaseMigrationExecutor,
|
|
10
|
+
createUniqueIndex,
|
|
11
|
+
dropUniqueIndex,
|
|
12
|
+
type ExecuteNode,
|
|
13
|
+
getForeignKeyAction,
|
|
14
|
+
} from "./execute-base";
|
|
15
|
+
|
|
16
|
+
const errors = {
|
|
17
|
+
IdColumnUpdate:
|
|
18
|
+
"ID columns cannot be updated. Not every database supports updating primary keys and often requires workarounds.",
|
|
19
|
+
} as const;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* MySQL-specific migration executor.
|
|
23
|
+
* Uses modifyColumn for updates and "no action" for FK actions (RESTRICT not supported).
|
|
24
|
+
*/
|
|
25
|
+
export class MysqlMigrationExecutor extends BaseMigrationExecutor {
|
|
26
|
+
executeOperation(
|
|
27
|
+
operation: MigrationOperation,
|
|
28
|
+
mapper?: TableNameMapper,
|
|
29
|
+
): ExecuteNode | ExecuteNode[] {
|
|
30
|
+
switch (operation.type) {
|
|
31
|
+
case "create-table":
|
|
32
|
+
return this.createTable(operation, mapper);
|
|
33
|
+
case "rename-table":
|
|
34
|
+
return this.renameTable(operation, mapper);
|
|
35
|
+
case "alter-table":
|
|
36
|
+
return this.alterTable(operation, mapper);
|
|
37
|
+
case "drop-table":
|
|
38
|
+
return this.dropTable(operation, mapper);
|
|
39
|
+
case "add-foreign-key":
|
|
40
|
+
return this.addForeignKey(operation, mapper);
|
|
41
|
+
case "drop-foreign-key":
|
|
42
|
+
return this.dropForeignKey(operation, mapper);
|
|
43
|
+
case "add-index":
|
|
44
|
+
return this.addIndex(operation, mapper);
|
|
45
|
+
case "drop-index":
|
|
46
|
+
return this.dropIndex(operation, mapper);
|
|
47
|
+
case "custom":
|
|
48
|
+
return this.handleCustomOperation(operation);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
private createTable(
|
|
53
|
+
operation: Extract<MigrationOperation, { type: "create-table" }>,
|
|
54
|
+
mapper?: TableNameMapper,
|
|
55
|
+
): ExecuteNode {
|
|
56
|
+
const tableName = this.getTableName(operation.name, mapper);
|
|
57
|
+
let builder = this.db.schema.createTable(tableName);
|
|
58
|
+
|
|
59
|
+
// Add columns
|
|
60
|
+
for (const columnInfo of operation.columns) {
|
|
61
|
+
builder = builder.addColumn(
|
|
62
|
+
columnInfo.name,
|
|
63
|
+
sql.raw(this.getDBType(columnInfo)),
|
|
64
|
+
this.getColumnBuilderCallback(columnInfo),
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return builder;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
private renameTable(
|
|
72
|
+
operation: Extract<MigrationOperation, { type: "rename-table" }>,
|
|
73
|
+
mapper?: TableNameMapper,
|
|
74
|
+
): ExecuteNode {
|
|
75
|
+
return this.db.schema
|
|
76
|
+
.alterTable(this.getTableName(operation.from, mapper))
|
|
77
|
+
.renameTo(this.getTableName(operation.to, mapper));
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
private alterTable(
|
|
81
|
+
operation: Extract<MigrationOperation, { type: "alter-table" }>,
|
|
82
|
+
mapper?: TableNameMapper,
|
|
83
|
+
): ExecuteNode[] {
|
|
84
|
+
const results: ExecuteNode[] = [];
|
|
85
|
+
const tableName = this.getTableName(operation.name, mapper);
|
|
86
|
+
|
|
87
|
+
for (const columnOp of operation.value) {
|
|
88
|
+
results.push(...this.executeColumnOperation(tableName, columnOp));
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return results;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
private executeColumnOperation(tableName: string, operation: ColumnOperation): ExecuteNode[] {
|
|
95
|
+
const next = () => this.db.schema.alterTable(tableName);
|
|
96
|
+
const results: ExecuteNode[] = [];
|
|
97
|
+
|
|
98
|
+
switch (operation.type) {
|
|
99
|
+
case "rename-column":
|
|
100
|
+
results.push(next().renameColumn(operation.from, operation.to));
|
|
101
|
+
return results;
|
|
102
|
+
|
|
103
|
+
case "drop-column":
|
|
104
|
+
results.push(next().dropColumn(operation.name));
|
|
105
|
+
return results;
|
|
106
|
+
|
|
107
|
+
case "create-column": {
|
|
108
|
+
const col = operation.value;
|
|
109
|
+
results.push(
|
|
110
|
+
next().addColumn(
|
|
111
|
+
col.name,
|
|
112
|
+
sql.raw(this.getDBType(col)),
|
|
113
|
+
this.getColumnBuilderCallback(col),
|
|
114
|
+
),
|
|
115
|
+
);
|
|
116
|
+
return results;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
case "update-column": {
|
|
120
|
+
const col = operation.value;
|
|
121
|
+
|
|
122
|
+
if (col.role === "external-id" || col.role === "internal-id") {
|
|
123
|
+
throw new Error(errors.IdColumnUpdate);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (!isUpdated(operation)) {
|
|
127
|
+
return results;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// MySQL: Use modifyColumn which requires the full column definition
|
|
131
|
+
results.push(
|
|
132
|
+
next().modifyColumn(
|
|
133
|
+
operation.name,
|
|
134
|
+
sql.raw(this.getDBType(col)),
|
|
135
|
+
this.getColumnBuilderCallback(col),
|
|
136
|
+
),
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
return results;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
private dropTable(
|
|
145
|
+
operation: Extract<MigrationOperation, { type: "drop-table" }>,
|
|
146
|
+
mapper?: TableNameMapper,
|
|
147
|
+
): ExecuteNode {
|
|
148
|
+
return this.db.schema.dropTable(this.getTableName(operation.name, mapper));
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
private addForeignKey(
|
|
152
|
+
operation: Extract<MigrationOperation, { type: "add-foreign-key" }>,
|
|
153
|
+
mapper?: TableNameMapper,
|
|
154
|
+
): ExecuteNode {
|
|
155
|
+
const { table, value } = operation;
|
|
156
|
+
const action = getForeignKeyAction(this.provider);
|
|
157
|
+
|
|
158
|
+
return this.db.schema
|
|
159
|
+
.alterTable(this.getTableName(table, mapper))
|
|
160
|
+
.addForeignKeyConstraint(
|
|
161
|
+
value.name,
|
|
162
|
+
value.columns,
|
|
163
|
+
this.getTableName(value.referencedTable, mapper),
|
|
164
|
+
value.referencedColumns,
|
|
165
|
+
(b) => b.onUpdate(action).onDelete(action),
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
private dropForeignKey(
|
|
170
|
+
operation: Extract<MigrationOperation, { type: "drop-foreign-key" }>,
|
|
171
|
+
mapper?: TableNameMapper,
|
|
172
|
+
): ExecuteNode {
|
|
173
|
+
const { table, name } = operation;
|
|
174
|
+
// MySQL doesn't support IF EXISTS for dropping constraints
|
|
175
|
+
return this.db.schema.alterTable(this.getTableName(table, mapper)).dropConstraint(name);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
private addIndex(
|
|
179
|
+
operation: Extract<MigrationOperation, { type: "add-index" }>,
|
|
180
|
+
mapper?: TableNameMapper,
|
|
181
|
+
): ExecuteNode {
|
|
182
|
+
const tableName = this.getTableName(operation.table, mapper);
|
|
183
|
+
|
|
184
|
+
if (operation.unique) {
|
|
185
|
+
return createUniqueIndex(
|
|
186
|
+
this.db,
|
|
187
|
+
operation.name,
|
|
188
|
+
tableName,
|
|
189
|
+
operation.columns,
|
|
190
|
+
this.provider,
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
return this.db.schema.createIndex(operation.name).on(tableName).columns(operation.columns);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
private dropIndex(
|
|
198
|
+
operation: Extract<MigrationOperation, { type: "drop-index" }>,
|
|
199
|
+
mapper?: TableNameMapper,
|
|
200
|
+
): ExecuteNode {
|
|
201
|
+
const tableName = this.getTableName(operation.table, mapper);
|
|
202
|
+
return dropUniqueIndex(this.db, operation.name, tableName, this.provider);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
private handleCustomOperation(
|
|
206
|
+
operation: Extract<MigrationOperation, { type: "custom" }>,
|
|
207
|
+
): ExecuteNode {
|
|
208
|
+
const statement = sql.raw(operation["sql"] as string);
|
|
209
|
+
return this.rawToNode(statement);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import { sql } from "kysely";
|
|
2
|
+
import {
|
|
3
|
+
type ColumnOperation,
|
|
4
|
+
isUpdated,
|
|
5
|
+
type MigrationOperation,
|
|
6
|
+
} from "../../../migration-engine/shared";
|
|
7
|
+
import type { TableNameMapper } from "../kysely-shared";
|
|
8
|
+
import {
|
|
9
|
+
BaseMigrationExecutor,
|
|
10
|
+
createUniqueIndex,
|
|
11
|
+
dropUniqueIndex,
|
|
12
|
+
type ExecuteNode,
|
|
13
|
+
getForeignKeyAction,
|
|
14
|
+
} from "./execute-base";
|
|
15
|
+
|
|
16
|
+
const errors = {
|
|
17
|
+
IdColumnUpdate:
|
|
18
|
+
"ID columns cannot be updated. Not every database supports updating primary keys and often requires workarounds.",
|
|
19
|
+
} as const;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* PostgreSQL/CockroachDB-specific migration executor.
|
|
23
|
+
* Uses explicit USING clauses for type conversion.
|
|
24
|
+
*/
|
|
25
|
+
export class PostgresMigrationExecutor extends BaseMigrationExecutor {
|
|
26
|
+
executeOperation(
|
|
27
|
+
operation: MigrationOperation,
|
|
28
|
+
mapper?: TableNameMapper,
|
|
29
|
+
): ExecuteNode | ExecuteNode[] {
|
|
30
|
+
switch (operation.type) {
|
|
31
|
+
case "create-table":
|
|
32
|
+
return this.createTable(operation, mapper);
|
|
33
|
+
case "rename-table":
|
|
34
|
+
return this.renameTable(operation, mapper);
|
|
35
|
+
case "alter-table":
|
|
36
|
+
return this.alterTable(operation, mapper);
|
|
37
|
+
case "drop-table":
|
|
38
|
+
return this.dropTable(operation, mapper);
|
|
39
|
+
case "add-foreign-key":
|
|
40
|
+
return this.addForeignKey(operation, mapper);
|
|
41
|
+
case "drop-foreign-key":
|
|
42
|
+
return this.dropForeignKey(operation, mapper);
|
|
43
|
+
case "add-index":
|
|
44
|
+
return this.addIndex(operation, mapper);
|
|
45
|
+
case "drop-index":
|
|
46
|
+
return this.dropIndex(operation, mapper);
|
|
47
|
+
case "custom":
|
|
48
|
+
return this.handleCustomOperation(operation);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
private createTable(
|
|
53
|
+
operation: Extract<MigrationOperation, { type: "create-table" }>,
|
|
54
|
+
mapper?: TableNameMapper,
|
|
55
|
+
): ExecuteNode {
|
|
56
|
+
const tableName = this.getTableName(operation.name, mapper);
|
|
57
|
+
let builder = this.db.schema.createTable(tableName);
|
|
58
|
+
|
|
59
|
+
// Add columns
|
|
60
|
+
for (const columnInfo of operation.columns) {
|
|
61
|
+
builder = builder.addColumn(
|
|
62
|
+
columnInfo.name,
|
|
63
|
+
sql.raw(this.getDBType(columnInfo)),
|
|
64
|
+
this.getColumnBuilderCallback(columnInfo),
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return builder;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
private renameTable(
|
|
72
|
+
operation: Extract<MigrationOperation, { type: "rename-table" }>,
|
|
73
|
+
mapper?: TableNameMapper,
|
|
74
|
+
): ExecuteNode {
|
|
75
|
+
return this.db.schema
|
|
76
|
+
.alterTable(this.getTableName(operation.from, mapper))
|
|
77
|
+
.renameTo(this.getTableName(operation.to, mapper));
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
private alterTable(
|
|
81
|
+
operation: Extract<MigrationOperation, { type: "alter-table" }>,
|
|
82
|
+
mapper?: TableNameMapper,
|
|
83
|
+
): ExecuteNode[] {
|
|
84
|
+
const results: ExecuteNode[] = [];
|
|
85
|
+
const tableName = this.getTableName(operation.name, mapper);
|
|
86
|
+
|
|
87
|
+
for (const columnOp of operation.value) {
|
|
88
|
+
results.push(...this.executeColumnOperation(tableName, columnOp));
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return results;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
private executeColumnOperation(tableName: string, operation: ColumnOperation): ExecuteNode[] {
|
|
95
|
+
const next = () => this.db.schema.alterTable(tableName);
|
|
96
|
+
const results: ExecuteNode[] = [];
|
|
97
|
+
|
|
98
|
+
switch (operation.type) {
|
|
99
|
+
case "rename-column":
|
|
100
|
+
results.push(next().renameColumn(operation.from, operation.to));
|
|
101
|
+
return results;
|
|
102
|
+
|
|
103
|
+
case "drop-column":
|
|
104
|
+
results.push(next().dropColumn(operation.name));
|
|
105
|
+
return results;
|
|
106
|
+
|
|
107
|
+
case "create-column": {
|
|
108
|
+
const col = operation.value;
|
|
109
|
+
results.push(
|
|
110
|
+
next().addColumn(
|
|
111
|
+
col.name,
|
|
112
|
+
sql.raw(this.getDBType(col)),
|
|
113
|
+
this.getColumnBuilderCallback(col),
|
|
114
|
+
),
|
|
115
|
+
);
|
|
116
|
+
return results;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
case "update-column": {
|
|
120
|
+
const col = operation.value;
|
|
121
|
+
|
|
122
|
+
if (col.role === "external-id" || col.role === "internal-id") {
|
|
123
|
+
throw new Error(errors.IdColumnUpdate);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (!isUpdated(operation)) {
|
|
127
|
+
return results;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// PostgreSQL/CockroachDB: Use explicit USING clause for type conversion
|
|
131
|
+
if (operation.updateDataType) {
|
|
132
|
+
const dbType = sql.raw(this.getDBType(col));
|
|
133
|
+
results.push(
|
|
134
|
+
this.rawToNode(
|
|
135
|
+
sql`ALTER TABLE ${sql.ref(tableName)} ALTER COLUMN ${sql.ref(operation.name)} TYPE ${dbType} USING (${sql.ref(operation.name)}::${dbType})`,
|
|
136
|
+
),
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (operation.updateNullable) {
|
|
141
|
+
results.push(
|
|
142
|
+
next().alterColumn(operation.name, (build) =>
|
|
143
|
+
col.isNullable ? build.dropNotNull() : build.setNotNull(),
|
|
144
|
+
),
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (operation.updateDefault) {
|
|
149
|
+
const defaultValue = this.defaultValueToDB(col);
|
|
150
|
+
results.push(
|
|
151
|
+
next().alterColumn(operation.name, (build) => {
|
|
152
|
+
if (!defaultValue) {
|
|
153
|
+
return build.dropDefault();
|
|
154
|
+
}
|
|
155
|
+
return build.setDefault(defaultValue);
|
|
156
|
+
}),
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return results;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
private dropTable(
|
|
166
|
+
operation: Extract<MigrationOperation, { type: "drop-table" }>,
|
|
167
|
+
mapper?: TableNameMapper,
|
|
168
|
+
): ExecuteNode {
|
|
169
|
+
return this.db.schema.dropTable(this.getTableName(operation.name, mapper));
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
private addForeignKey(
|
|
173
|
+
operation: Extract<MigrationOperation, { type: "add-foreign-key" }>,
|
|
174
|
+
mapper?: TableNameMapper,
|
|
175
|
+
): ExecuteNode {
|
|
176
|
+
const { table, value } = operation;
|
|
177
|
+
const action = getForeignKeyAction(this.provider);
|
|
178
|
+
|
|
179
|
+
return this.db.schema
|
|
180
|
+
.alterTable(this.getTableName(table, mapper))
|
|
181
|
+
.addForeignKeyConstraint(
|
|
182
|
+
value.name,
|
|
183
|
+
value.columns,
|
|
184
|
+
this.getTableName(value.referencedTable, mapper),
|
|
185
|
+
value.referencedColumns,
|
|
186
|
+
(b) => b.onUpdate(action).onDelete(action),
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
private dropForeignKey(
|
|
191
|
+
operation: Extract<MigrationOperation, { type: "drop-foreign-key" }>,
|
|
192
|
+
mapper?: TableNameMapper,
|
|
193
|
+
): ExecuteNode {
|
|
194
|
+
const { table, name } = operation;
|
|
195
|
+
return this.db.schema
|
|
196
|
+
.alterTable(this.getTableName(table, mapper))
|
|
197
|
+
.dropConstraint(name)
|
|
198
|
+
.ifExists();
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
private addIndex(
|
|
202
|
+
operation: Extract<MigrationOperation, { type: "add-index" }>,
|
|
203
|
+
mapper?: TableNameMapper,
|
|
204
|
+
): ExecuteNode {
|
|
205
|
+
const tableName = this.getTableName(operation.table, mapper);
|
|
206
|
+
|
|
207
|
+
if (operation.unique) {
|
|
208
|
+
return createUniqueIndex(
|
|
209
|
+
this.db,
|
|
210
|
+
operation.name,
|
|
211
|
+
tableName,
|
|
212
|
+
operation.columns,
|
|
213
|
+
this.provider,
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
return this.db.schema.createIndex(operation.name).on(tableName).columns(operation.columns);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
private dropIndex(
|
|
221
|
+
operation: Extract<MigrationOperation, { type: "drop-index" }>,
|
|
222
|
+
mapper?: TableNameMapper,
|
|
223
|
+
): ExecuteNode {
|
|
224
|
+
const tableName = this.getTableName(operation.table, mapper);
|
|
225
|
+
return dropUniqueIndex(this.db, operation.name, tableName, this.provider);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
private handleCustomOperation(
|
|
229
|
+
operation: Extract<MigrationOperation, { type: "custom" }>,
|
|
230
|
+
): ExecuteNode {
|
|
231
|
+
const statement = sql.raw(operation["sql"] as string);
|
|
232
|
+
return this.rawToNode(statement);
|
|
233
|
+
}
|
|
234
|
+
}
|