Skip to content

Commit

Permalink
Upsert seed data, change script name
Browse files Browse the repository at this point in the history
  • Loading branch information
DukeManh committed Apr 22, 2022
1 parent 436df6a commit 9c3ad70
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/e2e-tests-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ jobs:
- name: Pull/Build Docker Containers Necessary for Running E2E Tests
run: docker compose --env-file ./config/env.development up -d ${{ env.DOCKER_CONTAINERS }}
- name: Apply database migrations
run: pnpm migrate
run: pnpm db:migrate
- run: pnpm jest:e2e
2 changes: 1 addition & 1 deletion config/env.development
Original file line number Diff line number Diff line change
Expand Up @@ -331,4 +331,4 @@ KONG_HTTPS_PORT=8912
POSTGRES_PORT=8913

## DB connection string
DATABASE_URL="postgresql://postgres:your-super-secret-and-long-postgres-password@localhost/postgres"
DATABASE_URL=postgresql://postgres:your-super-secret-and-long-postgres-password@localhost/postgres
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
"clean": "pnpm turbo run clean && pnpm -r exec rm -rf node_modules",
"prettier": "prettier --write \"./**/*.{md,jsx,json,html,css,js,yml,ts,tsx}\"",
"prettier-check": "prettier --check \"./**/*.{md,jsx,json,html,css,js,yml,ts,tsx}\"",
"migrate": "prisma migrate dev --schema=src/db/prisma/schema.prisma && prisma generate --schema=src/db/prisma/schema.prisma",
"seed": "prisma db seed",
"db:migrate": "prisma migrate dev --schema=src/db/prisma/schema.prisma && prisma generate --schema=src/db/prisma/schema.prisma",
"db:seed": "prisma db seed",
"db:init": "run-s db:migrate db:seed",
"test": "pnpm turbo run test",
"jest": "jest -c jest.config.js --",
"jest:e2e": "jest -c jest.config.e2e.js --forceExit --",
Expand Down
46 changes: 33 additions & 13 deletions src/db/seed/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,44 @@ const { PrismaClient } = require('@prisma/client');
const feeds = require('./feeds.json');
const quotes = require('./quotes.json');

(async function seedFeeds() {
(function seedFeeds() {
const prisma = new PrismaClient();

await Promise.all(
Promise.all(
feeds.map((feed) => {
return prisma.feeds.createMany({ data: feed });
return prisma.feeds.upsert({
where: {
id: feed.id,
},
update: {},
create: feed,
});
})
).catch((error) => {
console.error('Erorr seeding feeds');
console.error(error);
});
)
.then((records) => {
console.log(`Seeded ${records.length} feeds`);
return records.length;
})
.catch((error) => {
console.error(error);
console.error('Erorr seeding feeds\n\n');
});

await Promise.all(
Promise.all(
quotes.map((quote) => {
return prisma.quotes.createMany({ data: quote });
return prisma.quotes.upsert({
where: { quote_id: quote.quote_id },
update: {},
create: quote,
});
})
)
.then((records) => {
console.log(`Seeded ${records.length} quotes`);
return records;
})
).catch((error) => {
console.error('Erorr seeding quotes');
console.error(error);
});
.catch((error) => {
console.error(error);
console.error('Erorr seeding quotes');
});
})();
6 changes: 3 additions & 3 deletions src/web/docusaurus/docs/tools-and-technologies/prisma.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Whenever we start up the DB container or after updating our schema, we should ru
## Running migration

```bash
pnpm migrate
pnpm db:migrate
```

See also [DB maintenance on staging and production](../contributing/database-maintenance.md).
Expand All @@ -37,7 +37,7 @@ For a migration that has a "narrowing" nature such as dropping a table, dropping
Simply edit the schema in [schema.prisma](https://github.com/Seneca-CDOT/telescope/blob/master/src/db/prisma/schema.prisma) and apply the migration.

```bash
pnpm migrate
pnpm db:migrate
```

### Custom migration
Expand All @@ -53,7 +53,7 @@ Sometimes, we want to make changes other than the modifying the schema, for exam
After creating the empty migration file, you can write SQL queries inside the newly created `migration.sql`. After writing the desired statements, you can apply the migration:

```bash
pnpm migrate
pnpm db:migrate
```

### Seeding the database
Expand Down

0 comments on commit 9c3ad70

Please sign in to comment.