Skip to content

Commit

Permalink
Add prisma seed for dev dump
Browse files Browse the repository at this point in the history
Add prisma seed for dev dump.
Update scripts to run it automatically in docker-compose.
  • Loading branch information
rkperes committed May 11, 2024
1 parent b12e063 commit ea3165a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ services:
command: >
sh -c "npm install &&
npx prisma generate &&
npx prisma migrate dev &&
npm run migrations:dev &&
npm run start:dev -- --preserveWatchOutput"
db:
container_name: sos-rs-db
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json",
"migrations:run": "npx prisma migrate deploy",
"migrations:dev": "npx prisma migrate dev",
"migrations:dev": "npx prisma migrate dev && npx prisma db seed",
"docker:compose": "docker-compose -f docker-compose.dev.yml up"
},
"prisma": {
"seed": "ts-node prisma/seed.ts"
},
"dependencies": {
"@fastify/static": "^7.0.3",
"@nestjs/common": "^10.0.0",
Expand Down
35 changes: 35 additions & 0 deletions prisma/seed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { PrismaClient } from '@prisma/client';
import * as fs from 'fs';

const prisma = new PrismaClient();

async function main() {
const devDump = fs.readFileSync('./prisma/dev_dump.sql', 'utf-8');

for (const query of devDump.split('\n')) {
if (query.trim() === '') {
continue;
}

// ignore prisma migrations at it is already applied by migrate command
// if (query.includes('_prisma_migrations')) {
// continue;
// }

try {
await prisma.$executeRawUnsafe(query);
} catch (e) {
console.error(e);
}
}
}

main()
.then(async () => {
await prisma.$disconnect();
})
.catch(async (e) => {
console.error(e);
await prisma.$disconnect();
process.exit(1);
});

0 comments on commit ea3165a

Please sign in to comment.