2024-09-04 23:04:41 +00:00
|
|
|
import 'reflect-metadata'
|
|
|
|
|
import { DrizzleService } from '$lib/server/api/services/drizzle.service'
|
|
|
|
|
import { type Table, getTableName, sql } from 'drizzle-orm'
|
|
|
|
|
import type { NodePgDatabase } from 'drizzle-orm/node-postgres'
|
2024-09-07 00:35:16 +00:00
|
|
|
import env from '../common/env'
|
2024-09-01 19:22:00 +00:00
|
|
|
import * as seeds from './seeds'
|
|
|
|
|
import * as schema from './tables'
|
2024-07-25 00:39:03 +00:00
|
|
|
|
2024-09-04 23:04:41 +00:00
|
|
|
const drizzleService = new DrizzleService()
|
|
|
|
|
|
2024-07-25 00:39:03 +00:00
|
|
|
if (!env.DB_SEEDING) {
|
2024-09-01 19:22:00 +00:00
|
|
|
throw new Error('You must set DB_SEEDING to "true" when running seeds')
|
2024-07-25 00:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
2024-09-04 23:04:41 +00:00
|
|
|
async function resetTable(db: NodePgDatabase<typeof schema>, table: Table) {
|
2024-09-01 19:22:00 +00:00
|
|
|
return db.execute(sql.raw(`TRUNCATE TABLE ${getTableName(table)} RESTART IDENTITY CASCADE`))
|
2024-07-25 00:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const table of [
|
2024-08-08 03:59:30 +00:00
|
|
|
schema.categoriesTable,
|
2024-08-07 17:01:38 +00:00
|
|
|
schema.categoriesToExternalIdsTable,
|
2024-08-08 03:59:30 +00:00
|
|
|
schema.categories_to_games_table,
|
2024-07-25 00:39:03 +00:00
|
|
|
schema.collection_items,
|
|
|
|
|
schema.collections,
|
2024-08-08 03:59:30 +00:00
|
|
|
schema.credentialsTable,
|
2024-09-04 23:04:41 +00:00
|
|
|
schema.expansionsTable,
|
|
|
|
|
schema.externalIdsTable,
|
2024-08-08 03:59:30 +00:00
|
|
|
schema.federatedIdentityTable,
|
2024-09-04 23:04:41 +00:00
|
|
|
schema.gamesTable,
|
|
|
|
|
schema.gamesToExternalIdsTable,
|
|
|
|
|
schema.mechanicsTable,
|
|
|
|
|
schema.mechanicsToExternalIdsTable,
|
2024-07-25 00:39:03 +00:00
|
|
|
schema.mechanics_to_games,
|
|
|
|
|
schema.password_reset_tokens,
|
2024-09-04 23:04:41 +00:00
|
|
|
schema.publishersTable,
|
|
|
|
|
schema.publishersToExternalIdsTable,
|
2024-07-25 00:39:03 +00:00
|
|
|
schema.publishers_to_games,
|
2024-08-29 23:12:40 +00:00
|
|
|
schema.recoveryCodesTable,
|
2024-09-04 23:04:41 +00:00
|
|
|
schema.rolesTable,
|
2024-07-25 00:39:03 +00:00
|
|
|
schema.sessionsTable,
|
2024-08-08 03:59:30 +00:00
|
|
|
schema.twoFactorTable,
|
|
|
|
|
schema.user_roles,
|
2024-07-25 00:39:03 +00:00
|
|
|
schema.usersTable,
|
|
|
|
|
schema.wishlist_items,
|
2024-09-04 23:04:41 +00:00
|
|
|
schema.wishlistsTable,
|
2024-07-25 00:39:03 +00:00
|
|
|
]) {
|
|
|
|
|
// await db.delete(table); // clear tables without truncating / resetting ids
|
2024-09-04 23:04:41 +00:00
|
|
|
await resetTable(drizzleService.db, table)
|
2024-07-25 00:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
2024-09-04 23:04:41 +00:00
|
|
|
await seeds.roles(drizzleService.db)
|
|
|
|
|
await seeds.users(drizzleService.db)
|
2024-07-25 00:39:03 +00:00
|
|
|
|
2024-09-04 23:04:41 +00:00
|
|
|
await drizzleService.dispose()
|
2024-09-01 19:22:00 +00:00
|
|
|
process.exit()
|