2024-09-01 19:22:00 +00:00
|
|
|
import { Table, getTableName, sql } from 'drizzle-orm'
|
|
|
|
|
import env from '../../../../env'
|
|
|
|
|
import { db, pool } from '../packages/drizzle'
|
|
|
|
|
import * as seeds from './seeds'
|
|
|
|
|
import * as schema from './tables'
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function resetTable(db: db, 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-07-25 00:39:03 +00:00
|
|
|
schema.expansions,
|
|
|
|
|
schema.externalIds,
|
2024-08-08 03:59:30 +00:00
|
|
|
schema.federatedIdentityTable,
|
2024-07-25 00:39:03 +00:00
|
|
|
schema.games,
|
|
|
|
|
schema.gamesToExternalIds,
|
|
|
|
|
schema.mechanics,
|
|
|
|
|
schema.mechanicsToExternalIds,
|
|
|
|
|
schema.mechanics_to_games,
|
|
|
|
|
schema.password_reset_tokens,
|
|
|
|
|
schema.publishers,
|
|
|
|
|
schema.publishersToExternalIds,
|
|
|
|
|
schema.publishers_to_games,
|
2024-08-29 23:12:40 +00:00
|
|
|
schema.recoveryCodesTable,
|
2024-07-25 00:39:03 +00:00
|
|
|
schema.roles,
|
|
|
|
|
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-08-08 03:59:30 +00:00
|
|
|
schema.wishlists,
|
2024-07-25 00:39:03 +00:00
|
|
|
]) {
|
|
|
|
|
// await db.delete(table); // clear tables without truncating / resetting ids
|
2024-09-01 19:22:00 +00:00
|
|
|
await resetTable(db, table)
|
2024-07-25 00:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
2024-09-01 19:22:00 +00:00
|
|
|
await seeds.roles(db)
|
|
|
|
|
await seeds.users(db)
|
2024-07-25 00:39:03 +00:00
|
|
|
|
2024-09-01 19:22:00 +00:00
|
|
|
await pool.end()
|
|
|
|
|
process.exit()
|