Skip to content

Commit

Permalink
🔨 chore (library): Improved logging in migrate and seed scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanprince committed Mar 5, 2024
1 parent 40618ce commit c0ae98a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
4 changes: 3 additions & 1 deletion apps/library/src/server/db/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ async function runMigrate() {
const sql = postgres(env.DATABASE_URL, { max: 1, onnotice: () => {} });
const db = drizzle(sql, { logger: true });

console.log("⏳ Running migrations...\n");
console.log("⏳ RUNNING MIGRATION SCRIPT (migrate.ts)\n");
const start = Date.now();

console.log("⏳ Starting migration process...\n");
await migrate(db, { migrationsFolder: "./src/server/db/migrations" });
console.log("\n⏳ Finished migration process...");

await sql.end();
const end = Date.now();
Expand Down
15 changes: 13 additions & 2 deletions apps/library/src/server/db/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@ const postgresClient = postgres(env.DATABASE_URL, {
const db = drizzle(postgresClient, { logger: true });

async function seed() {
console.log("🌱 Seeding db migration...");
console.log("🌱 RUNNING SEED SCRIPT (seed.ts)");

console.log("🌱 Deleting all data...\n");

const start = Date.now();

// First deletes all books from the database
await db.delete(book);

console.log("\n🌱 Deleted all data successfully...");

console.log("🌱 Starting seeding process...\n");

// Inserts 10 books into the database
await db.insert(book).values([
{
Expand Down Expand Up @@ -145,15 +151,20 @@ async function seed() {
year: 1997,
},
]);

console.log("\n🌱 Finished seeding process...");

const end = Date.now();
console.log(`\n✅ Seeding complete & took ${end - start}ms`);
}

seed()
.catch((e) => {
console.error("\n❌ Seeding failed");
console.error(e);
process.exit(1);
})
// eslint-disable-next-line @typescript-eslint/no-misused-promises
.finally(async () => {
console.log("🌱 Seeding complete!\n");
await postgresClient.end();
});

0 comments on commit c0ae98a

Please sign in to comment.