|
| 1 | +import { sql } from "@vercel/postgres"; |
| 2 | + |
| 3 | +export async function migrate(direction: "up" | "down") { |
| 4 | + if (direction === "up") { |
| 5 | + await migrateUp(); |
| 6 | + } else { |
| 7 | + await migrateDown(); |
| 8 | + } |
| 9 | +} |
| 10 | + |
| 11 | +export async function migrateUp() { |
| 12 | + const createTable = await sql` |
| 13 | + CREATE TABLE IF NOT EXISTS todos ( |
| 14 | + id VARCHAR(255) PRIMARY KEY, |
| 15 | + user_id VARCHAR(255) NOT NULL, |
| 16 | + title VARCHAR(255) NOT NULL, |
| 17 | + description TEXT, |
| 18 | + status VARCHAR(50) NOT NULL DEFAULT 'pending', |
| 19 | + priority INTEGER NOT NULL DEFAULT 3, |
| 20 | + due_date TIMESTAMP WITH TIME ZONE, |
| 21 | + created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, |
| 22 | + updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, |
| 23 | + completed_at TIMESTAMP WITH TIME ZONE, |
| 24 | + tags TEXT[], -- Array of tags |
| 25 | + assigned_to VARCHAR(255) |
| 26 | + ); |
| 27 | + `; |
| 28 | + |
| 29 | + console.log(`Created "todos" table`); |
| 30 | + |
| 31 | + return { |
| 32 | + createTable, |
| 33 | + }; |
| 34 | +} |
| 35 | + |
| 36 | +export async function migrateDown() { |
| 37 | + const dropTable = await sql` |
| 38 | + DROP TABLE IF EXISTS todos; |
| 39 | + `; |
| 40 | + |
| 41 | + console.log(`Dropped "todos" table`); |
| 42 | +} |
| 43 | + |
| 44 | +async function main() { |
| 45 | + const direction = process.argv[2]; |
| 46 | + await migrate(direction as "up" | "down"); |
| 47 | +} |
| 48 | + |
| 49 | +main().catch(console.error); |
0 commit comments