Skip to content

Commit

Permalink
Finish local migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Douglas-Hong authored and MinhxNguyen7 committed Oct 24, 2024
1 parent aa632e6 commit dabbadb
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 25 deletions.
7 changes: 7 additions & 0 deletions apps/backend/drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from "drizzle-kit";

export default defineConfig({
dialect: "postgresql",
schema: "./src/db/schema/index.ts",
out: "./drizzle",
});
4 changes: 3 additions & 1 deletion apps/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"build": "node scripts/build.mjs",
"start": "npm run dev",
"format": "prettier --write src",
"lint": "eslint --fix src"
"lint": "eslint --fix src",
"generate": "drizzle-kit generate",
"migrate": "tsx scripts/migrate.ts"
},
"dependencies": {
"@packages/antalmanac-types": "*",
Expand Down
20 changes: 0 additions & 20 deletions apps/backend/scripts/migrate.js

This file was deleted.

45 changes: 45 additions & 0 deletions apps/backend/scripts/migrate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* To run this script, run "pnpm run migrate".
*/

import { drizzle } from 'drizzle-orm/postgres-js';
import { migrate } from 'drizzle-orm/postgres-js/migrator';

import { ddbClient } from '../src/db/ddb.ts';
import { db, client } from '../src/db/index.ts';
import { user } from '../src/db/schema/index.ts';

/**
* Migrates the current drizzle schema to the PostgreSQL database associated
* with the drizzle client. Before migrating, run "pnpm run generate" to generate
* the migration files, and make sure the PostgreSQL database exists beforehand.
*/
async function migrateToPostgres() {
await migrate(drizzle(client), {
migrationsFolder: './drizzle'
});
}

/**
* Migrates user data from DynamoDB to the PostgreSQL database associated
* with the drizzle client.
*/
async function insertUsersToPostgres() {
const userIds = await ddbClient.getUserIds();
const usersToInsert = userIds.map((userId) => ({ id: userId }));
await db.insert(user).values(usersToInsert);
console.log(await db.query.user.findMany());
}

async function main() {
try {
await migrateToPostgres();
await insertUsersToPostgres();
} catch (error) {
console.log(error);
} finally {
await client.end();
}
}

main();
5 changes: 2 additions & 3 deletions apps/backend/src/db/ddb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ class DDBClient<T extends Type<Record<string, unknown>>> {
}

async getUserData(id: string) {
this.migrate();
return (await ddbClient.get('id', id))?.userData;
}

Expand Down Expand Up @@ -175,7 +174,7 @@ class DDBClient<T extends Type<Record<string, unknown>>> {

}

async migrate () {
async getUserIds () {
const params = {
TableName: this.tableName,
}
Expand All @@ -195,7 +194,7 @@ class DDBClient<T extends Type<Record<string, unknown>>> {
}
} while (typeof items.LastEvaluatedKey !== 'undefined');

console.log(scanResults);
return scanResults;
}
}

Expand Down
3 changes: 2 additions & 1 deletion apps/backend/src/db/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { drizzle } from 'drizzle-orm/postgres-js';
import postgres from 'postgres';
import * as schema from './schema/index.js';

export const client = postgres('postgres://postgres:postgres@localhost:5432/antalmanac');

export const db = drizzle(client);
export const db = drizzle(client, { schema });

0 comments on commit dabbadb

Please sign in to comment.