-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate.ts
64 lines (53 loc) · 1.58 KB
/
migrate.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { drizzle } from "drizzle-orm/postgres-js";
import { migrate } from "drizzle-orm/postgres-js/migrator";
import postgres from "postgres";
import { db } from "~/lib/db";
import { vectorDatabases } from "~/lib/db/schema";
import { neonApiClient } from "~/lib/vector-db";
const main = async () => {
try {
const databases = await db.select().from(vectorDatabases);
console.log("Databases to migrate:", databases.length);
for (const database of databases) {
const { vectorDbId } = database;
try {
const getProject = await neonApiClient.GET(
"/projects/{project_id}/connection_uri",
{
params: {
path: {
project_id: vectorDbId,
},
query: {
role_name: "neondb_owner",
database_name: "neondb",
},
},
},
);
const connectionString = getProject.data?.uri;
if (!connectionString) {
throw new Error(
`Connection string not found for database ${vectorDbId}`,
);
}
const client = postgres(connectionString, { max: 1 });
const drizzleDb = drizzle(client);
await migrate(drizzleDb, {
migrationsFolder: "app/lib/vector-db/migrations",
});
await client.end();
console.log(`Successfully migrated database ${vectorDbId}`);
} catch (error) {
console.error(`Error migrating database ${vectorDbId}:`, error);
// Continue with the next database instead of stopping the entire process
}
}
console.log("Migration process completed");
} catch (error) {
console.error("Error in main process:", error);
} finally {
process.exit(0);
}
};
main();