Create and run database migrations for PostgreSQL using Drizzle ORM
Create migration scripts for database schema changes in the learning-cards PostgreSQL database using Drizzle ORM.
Primary Approach: Use Drizzle Kit (Recommended)
Drizzle Kit automatically generates migrations by comparing schema changes:
# 1. Update schema files in src/infrastructure/database/schema/
# 2. Generate migration
npx drizzle-kit generate
# 3. Apply migration
npx drizzle-kit push
# Or use push for prototyping (applies changes directly without migration files)
npx drizzle-kit push
Schema files are located in src/infrastructure/database/schema/:
auth.schema.ts - Authentication tablescontent.schema.tsgroups.schema.ts - Group/organization tablesgamification.schema.ts - Gamification featuresanalytics.schema.ts - Analytics and trackinglearning-path.schema.ts - Learning pathsindex.ts - Schema exportsimport { pgTable, text, integer, timestamp, boolean, varchar, uuid } from 'drizzle-orm/pg-core';
import { relations } from 'drizzle-orm';
// Define table
export const exampleTable = pgTable('example_table', {
id: uuid('id').defaultRandom().primaryKey(),
name: varchar('name', { length: 255 }).notNull(),
description: text('description'),
count: integer('count').default(0).notNull(),
isActive: boolean('is_active').default(true).notNull(),
createdAt: timestamp('created_at').defaultNow().notNull(),
updatedAt: timestamp('updated_at').defaultNow().notNull(),
});
// Define relations (optional)
export const exampleTableRelations = relations(exampleTable, ({ one, many }) => ({
// Define relationships here
}));
// Export types
export type ExampleTable = typeof exampleTable.$inferSelect;
export type NewExampleTable = typeof exampleTable.$inferInsert;
Primary Keys:
// Auto-incrementing integer