Skip to content

Commit

Permalink
Merge branch 'main' into share-email-field
Browse files Browse the repository at this point in the history
  • Loading branch information
tomas-salgado authored Apr 2, 2024
2 parents 7bf063a + 2ed8f24 commit fde94ee
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 21 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ jobs:
image: postgres
env:
POSTGRES_DB: colorstack
POSTGRES_PASSWORD: password
POSTGRES_USER: username
POSTGRES_PASSWORD: colorstack
POSTGRES_USER: colorstack
ports:
- 5432:5432
options: >-
Expand All @@ -31,7 +31,7 @@ jobs:
5s --health-retries 5
env:
DATABASE_URL: postgres://username:password@localhost:5432/colorstack
DATABASE_URL: postgres://colorstack:colorstack@localhost:5432/colorstack
REDIS_URL: redis://localhost:6379

steps:
Expand Down
16 changes: 4 additions & 12 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,24 +128,16 @@ You'll need to make sure that Postgres and Redis are running in the background.

#### Postgres Setup

Once Postgres is running, you can connect to it by running:
To set up your Postgres databases, you can run:

```sh
psql
```

You'll then need to create 2 Postgres databases locally named `colorstack` and
`colorstack-test`. Within the `psql` terminal, run:

```postgresql
CREATE DATABASE colorstack;
CREATE DATABASE "colorstack-test";
yarn db:setup
```

You should now be able to connect to each database like this:
You should now be able to connect to your database like this:

```sh
psql colorstack
psql colorstack -U colorstack
```

#### Executing Database Migrations
Expand Down
2 changes: 1 addition & 1 deletion apps/admin-dashboard/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

ADMIN_DASHBOARD_URL=http://localhost:3001
API_URL=http://localhost:8080
DATABASE_URL=postgresql://localhost:5432/colorstack
DATABASE_URL=postgresql://colorstack:colorstack@localhost:5432/colorstack
ENVIRONMENT=development
JWT_SECRET=_
REDIS_URL=redis://localhost:6379
Expand Down
2 changes: 1 addition & 1 deletion apps/api/.env.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Required to run the API in development...

API_URL=http://localhost:8080
DATABASE_URL=postgresql://localhost:5432/colorstack
DATABASE_URL=postgresql://colorstack:colorstack@localhost:5432/colorstack
ENVIRONMENT=development
JWT_SECRET=_
REDIS_URL=redis://localhost:6379
Expand Down
2 changes: 1 addition & 1 deletion apps/member-profile/.env.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Required to run the Member Profile in development...

API_URL=http://localhost:8080
DATABASE_URL=postgresql://localhost:5432/colorstack
DATABASE_URL=postgresql://colorstack:colorstack@localhost:5432/colorstack
ENVIRONMENT=development
JWT_SECRET=_
REDIS_URL=redis://localhost:6379
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"build": "turbo run build --cache-dir=.turbo",
"db:migrate": "yarn workspace @oyster/core db:migrate",
"db:seed": "yarn workspace @oyster/core db:seed",
"db:setup": "yarn workspace @oyster/core db:setup",
"db:types": "yarn workspace @oyster/core db:types",
"dev": "turbo run dev --cache-dir=.turbo",
"dev:apps": "yarn dev --filter='./apps/*'",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/.env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This is only needed for automatically generating types from the database
# using `kysely-codegen`. See the following:
# https://github.com/RobinBlomberg/kysely-codegen?tab=readme-ov-file#generating-type-definitions
DATABASE_URL=postgresql://localhost:5432/colorstack
DATABASE_URL=postgresql://colorstack:colorstack@localhost:5432/colorstack
ENVIRONMENT=development
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"db:migrate": "tsx ./src/infrastructure/database/scripts/migrate.ts && yarn db:types",
"db:migrate:down": "tsx ./src/infrastructure/database/scripts/migrate.ts --down && yarn db:types",
"db:seed": "tsx ./src/infrastructure/database/scripts/seed.ts && yarn db:types",
"db:setup": "tsx ./src/infrastructure/database/scripts/setup.ts",
"db:types": "kysely-codegen --dialect=postgres --camel-case",
"lint": "tsc --noEmit",
"test": "vitest run"
Expand Down
11 changes: 11 additions & 0 deletions packages/core/src/infrastructure/database/scripts/setup.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- Cleans up any existing databases/users.

DROP DATABASE IF EXISTS colorstack;
DROP DATABASE IF EXISTS colorstack_test;
DROP ROLE IF EXISTS colorstack;

-- Creates new databases and users.

CREATE ROLE colorstack WITH SUPERUSER LOGIN PASSWORD 'colorstack';
CREATE DATABASE colorstack OWNER colorstack;
CREATE DATABASE colorstack_test OWNER colorstack
33 changes: 33 additions & 0 deletions packages/core/src/infrastructure/database/scripts/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { exec } from 'child_process';
import path from 'path';
import { fileURLToPath } from 'url';

import { ENV } from '@/shared/env';

if (ENV.ENVIRONMENT !== 'development') {
throw new Error('Cannot setup database in non-development environment.');
}

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

// This is the full path to the `setup.sql` file.
const pathToInitFile = path.join(__dirname, 'setup.sql');

exec(
`psql -U postgres -d postgres -f ${pathToInitFile}`,
(error, stdout, stderr) => {
if (stdout) {
console.log(stdout);
}

if (stderr) {
// Log but don't throw for notices/warnings.
console.warn(stderr);
}

if (error) {
throw new Error(`psql exited with error: ${error}`);
}
}
);
4 changes: 2 additions & 2 deletions packages/core/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { defineConfig } from 'vitest/config';
// When we run `vitest`, this is the first file that's loaded and thus `dotenv`
// hasn't processed any environment variables, so `process.env.DATABASE_URL` is
// empty. When we run this locally, that's expected, so we'll use the
// "colorstack-test" database. However, in our CI pipeline, we already inject
// "colorstack_test" database. However, in our CI pipeline, we already inject
// the `DATABASE_URL` environment variable, so we'll use that instead of having
// to create a separate database (and thus connection) for testing.
const DATABASE_URL =
process.env.DATABASE_URL || 'postgresql://localhost:5432/colorstack-test';
process.env.DATABASE_URL || 'postgresql://localhost:5432/colorstack_test';

export default defineConfig({
plugins: [tsconfigPaths()],
Expand Down

0 comments on commit fde94ee

Please sign in to comment.