Browse Source
- Added a new database cleanup script to remove all data while preserving the schema, facilitating a clean state before seeding. - Updated package.json to include new commands for database cleaning and resetting. - Modified schema definitions to use an array format for index and constraint definitions, improving clarity and consistency. - Updated product seeding logic to utilize role-based assignments directly from mock data, enhancing flexibility in product-role relationships.main
5 changed files with 121 additions and 39 deletions
@ -0,0 +1,75 @@ |
|||||
|
/** |
||||
|
* Database Clean Script |
||||
|
* |
||||
|
* Removes all data from the database while preserving the schema. |
||||
|
* Useful for resetting to a clean state before seeding. |
||||
|
* Run with: pnpm db:clean |
||||
|
*/ |
||||
|
|
||||
|
import 'dotenv/config' |
||||
|
import { drizzle } from 'drizzle-orm/postgres-js' |
||||
|
import { sql } from 'drizzle-orm' |
||||
|
import postgres from 'postgres' |
||||
|
|
||||
|
async function clean() { |
||||
|
// Get database connection from environment
|
||||
|
const connectionString = process.env.DATABASE_URL |
||||
|
if (!connectionString) { |
||||
|
throw new Error('DATABASE_URL environment variable is not set') |
||||
|
} |
||||
|
|
||||
|
console.log('🧹 Starting database cleanup...\n') |
||||
|
|
||||
|
// Create database connection
|
||||
|
const client = postgres(connectionString) |
||||
|
const db = drizzle(client) |
||||
|
|
||||
|
try { |
||||
|
// Delete all data in reverse order of dependencies
|
||||
|
console.log('Deleting product-role assignments...') |
||||
|
await db.execute(sql`DELETE FROM product_role_visibility`) |
||||
|
|
||||
|
console.log('Deleting user-role assignments...') |
||||
|
await db.execute(sql`DELETE FROM user_roles`) |
||||
|
|
||||
|
console.log('Deleting order items...') |
||||
|
await db.execute(sql`DELETE FROM order_items`) |
||||
|
|
||||
|
console.log('Deleting orders...') |
||||
|
await db.execute(sql`DELETE FROM orders`) |
||||
|
|
||||
|
console.log('Deleting cart items...') |
||||
|
await db.execute(sql`DELETE FROM cart_items`) |
||||
|
|
||||
|
console.log('Deleting carts...') |
||||
|
await db.execute(sql`DELETE FROM carts`) |
||||
|
|
||||
|
console.log('Deleting products...') |
||||
|
await db.execute(sql`DELETE FROM products`) |
||||
|
|
||||
|
console.log('Deleting roles...') |
||||
|
await db.execute(sql`DELETE FROM roles`) |
||||
|
|
||||
|
console.log('Deleting users...') |
||||
|
await db.execute(sql`DELETE FROM users`) |
||||
|
|
||||
|
console.log('\n✅ Database cleaned successfully!') |
||||
|
console.log('💡 Run `pnpm db:seed` to populate with fresh data') |
||||
|
} catch (error) { |
||||
|
console.error('❌ Error cleaning database:', error) |
||||
|
throw error |
||||
|
} finally { |
||||
|
// Close database connection
|
||||
|
await client.end() |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// Run clean function
|
||||
|
clean() |
||||
|
.then(() => { |
||||
|
process.exit(0) |
||||
|
}) |
||||
|
.catch((error) => { |
||||
|
console.error('Fatal error:', error) |
||||
|
process.exit(1) |
||||
|
}) |
||||
Loading…
Reference in new issue