Skip to content

Commit 256272c

Browse files
committed
improve mongodb migration engine
1 parent 01e3026 commit 256272c

File tree

2 files changed

+14
-34
lines changed

2 files changed

+14
-34
lines changed

packages/fumadb/src/adapters/mongodb/migration/execute.ts

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,6 @@ async function createUniqueIndex(
5555
});
5656
}
5757

58-
async function dropUniqueIndex(
59-
collection: Collection<Document>,
60-
colName: string
61-
) {
62-
if (colName === "_id") return;
63-
const indexes = await collection.indexes();
64-
65-
for (const index of indexes) {
66-
if (!index.name || !index.unique || index.key[colName] !== 1) continue;
67-
68-
await collection.dropIndex(index.name);
69-
break;
70-
}
71-
}
72-
7358
async function executeColumn(
7459
collection: Collection<Document>,
7560
operation: ColumnOperation,
@@ -86,15 +71,27 @@ async function executeColumn(
8671
);
8772
return;
8873

89-
case "drop-column":
90-
await dropUniqueIndex(collection, operation.name);
74+
case "drop-column": {
75+
if (operation.name === "_id")
76+
throw new Error("You cannot drop `_id` column");
77+
const indexes = await collection.indexes();
78+
79+
// drop unique index on it
80+
for (const index of indexes) {
81+
if (!index.name || !index.unique || index.key[operation.name] !== 1)
82+
continue;
83+
84+
await collection.dropIndex(index.name);
85+
break;
86+
}
9187

9288
await collection.updateMany(
9389
{},
9490
{ $unset: { [operation.name]: "" } },
9591
{ session }
9692
);
9793
return;
94+
}
9895
case "create-column": {
9996
const col = operation.value;
10097
const defaultValue = col.generateDefaultValue() ?? null;
@@ -106,12 +103,6 @@ async function executeColumn(
106103
{ session }
107104
);
108105
}
109-
110-
if (col.isUnique) {
111-
await createUniqueIndex(collection, col.getUniqueConstraintName(), [
112-
col.names.mongodb,
113-
]);
114-
}
115106
return;
116107
}
117108

@@ -135,16 +126,6 @@ async function executeColumn(
135126

136127
if (bulk.batches.length > 0) await bulk.execute();
137128
}
138-
139-
if (operation.updateUnique) {
140-
if (col.isUnique) {
141-
await createUniqueIndex(collection, col.getUniqueConstraintName(), [
142-
col.names.sql,
143-
]);
144-
} else {
145-
await dropUniqueIndex(collection, col.names.mongodb);
146-
}
147-
}
148129
}
149130
}
150131
}

packages/fumadb/src/adapters/prisma/query.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
Column,
1313
} from "../../schema";
1414
import { type Condition, ConditionType } from "../../query/condition-builder";
15-
import { createId } from "../../cuid";
1615
import { checkForeignKeyOnInsert } from "../../query/polyfills/foreign-key";
1716
import type { PrismaConfig } from ".";
1817

0 commit comments

Comments
 (0)