mirror of
https://github.com/soconnor0919/hristudio.git
synced 2025-12-11 22:54:45 -05:00
refactor(db): Improve table dropping and seeding logic
- Updated the dropAllTables function to drop tables in a specific order, considering foreign key dependencies, ensuring data integrity during the drop process. - Refactored the seed function to utilize role and permission data objects for cleaner insertion logic, enhancing readability and maintainability of the code.
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
import { sql } from '@vercel/postgres';
|
import { sql } from '@vercel/postgres';
|
||||||
import { db } from './index';
|
|
||||||
import { config } from 'dotenv';
|
import { config } from 'dotenv';
|
||||||
|
|
||||||
// load .env.local
|
// load .env.local
|
||||||
@@ -7,9 +6,18 @@ config({ path: '.env.local' });
|
|||||||
|
|
||||||
async function dropAllTables() {
|
async function dropAllTables() {
|
||||||
try {
|
try {
|
||||||
// drop all tables, regardless of name
|
// Drop tables in order considering foreign key dependencies
|
||||||
await sql`
|
await sql`
|
||||||
DROP TABLE IF EXISTS ${sql.raw(Object.values(tables).map(table => table.name).join(', '))}
|
DROP TABLE IF EXISTS
|
||||||
|
invitations,
|
||||||
|
user_roles,
|
||||||
|
role_permissions,
|
||||||
|
permissions,
|
||||||
|
roles,
|
||||||
|
participant,
|
||||||
|
study,
|
||||||
|
users
|
||||||
|
CASCADE;
|
||||||
`;
|
`;
|
||||||
console.log('All tables dropped successfully');
|
console.log('All tables dropped successfully');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -21,25 +21,29 @@ async function seed() {
|
|||||||
// Insert roles
|
// Insert roles
|
||||||
console.log("Inserting roles...");
|
console.log("Inserting roles...");
|
||||||
for (const [roleKey, roleName] of Object.entries(ROLES)) {
|
for (const [roleKey, roleName] of Object.entries(ROLES)) {
|
||||||
await db
|
const roleData = {
|
||||||
.insert(rolesTable)
|
|
||||||
.values({
|
|
||||||
name: roleName,
|
name: roleName,
|
||||||
description: getRoleDescription(roleKey),
|
description: getRoleDescription(roleKey),
|
||||||
})
|
} as const;
|
||||||
|
|
||||||
|
await db
|
||||||
|
.insert(rolesTable)
|
||||||
|
.values(roleData)
|
||||||
.onConflictDoNothing();
|
.onConflictDoNothing();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert permissions
|
// Insert permissions
|
||||||
console.log("Inserting permissions...");
|
console.log("Inserting permissions...");
|
||||||
for (const [permKey, permCode] of Object.entries(PERMISSIONS)) {
|
for (const [permKey, permCode] of Object.entries(PERMISSIONS)) {
|
||||||
await db
|
const permData = {
|
||||||
.insert(permissionsTable)
|
|
||||||
.values({
|
|
||||||
name: formatPermissionName(permKey),
|
name: formatPermissionName(permKey),
|
||||||
code: permCode,
|
code: permCode,
|
||||||
description: getPermissionDescription(permKey),
|
description: getPermissionDescription(permKey),
|
||||||
})
|
} as const;
|
||||||
|
|
||||||
|
await db
|
||||||
|
.insert(permissionsTable)
|
||||||
|
.values(permData)
|
||||||
.onConflictDoNothing();
|
.onConflictDoNothing();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user