Skip to content

Commit

Permalink
feat: use github action postgres & use infra for local
Browse files Browse the repository at this point in the history
  • Loading branch information
magrinj committed Jul 27, 2023
1 parent fe12cbf commit 7eedb82
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 33 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/ci-server.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,25 @@ on:
- main
pull_request_target:
jobs:
postgres-job:
runs-on: ubuntu-latest
container: node:10.18-jessie
steps:
- run: echo "Postgres job finished"
services:
postgres:
image: postgres
env:
POSTGRES_HOST: postgres
POSTGRES_PASSWORD: postgrespassword
POSTGRES_PORT: 5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
server-test:
needs: postgres-job
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand All @@ -27,3 +45,6 @@ jobs:
- name: Server / Run jest tests
run: |
cd server && yarn test
- name: Server / Run e2e tests
run: |
cd server && yarn test:e2e
4 changes: 4 additions & 0 deletions infra/dev/postgres/init.sql
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
-- Create the default database for development
CREATE DATABASE "default";

-- Create the tests database for e2e testing
CREATE DATABASE "tests";
2 changes: 1 addition & 1 deletion server/.env.test
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ REFRESH_TOKEN_EXPIRES_IN=30d
LOGIN_TOKEN_SECRET=secret_login_token
LOGIN_TOKEN_EXPIRES_IN=15m
FRONT_AUTH_CALLBACK_URL=http://localhost:3001/auth/callback
PG_DATABASE_URL=postgres://postgres:postgres@localhost:5432/default?connection_limit=1
PG_DATABASE_URL=postgres://postgres:postgrespassword@localhost:5432/tests?connection_limit=1
STORAGE_TYPE=local
STORAGE_LOCAL_PATH=.local-storage
16 changes: 0 additions & 16 deletions server/docker-compose.yml

This file was deleted.

31 changes: 21 additions & 10 deletions server/scripts/run-integration.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env bash
# src/run-integration.sh

# wait-for-it.sh need coreutils to work properly
# Check for MacOS
if [[ "$OSTYPE" == "darwin"* ]]; then
echo "🔵 MacOS detected."
Expand All @@ -26,25 +27,35 @@ fi
DIR="$(cd "$(dirname "$0")" && pwd)"
source $DIR/setenv.sh

if [ -z "$(docker ps --filter "name=test-db" --filter "status=running" -q)" ]; then
docker-compose up -d
if [ -z "$(docker ps --filter "name=postgres" --filter "status=running" -q)" ]; then
echo '🟡 - Waiting for database to be ready...'
echo '🟡 - This may take a while...'
echo "${PG_DATABASE_URL}"
$DIR/wait-for-it.sh "${PG_DATABASE_URL}" -- echo '🟢 - Database is ready!'
$DIR/wait-for-it.sh "${PG_DATABASE_URL}"
EXIT_CODE=$?
echo $EXIT_CODE
if [ $EXIT_CODE -ne 0 ]; then
echo '🔴 - Database connection failed!'
echo '🔴 - Please check if the database is running and accessible.'
echo '🔴 - If you are running the database in a container, please check if the container is running and accessible. ("make up" command should be run in "infra/dev" folder)'
echo '🔴 - Otherwise check if your local settings.'
echo '🔴 - Exiting...'
exit 1
else
echo '🟢 - Database is ready!'
fi
else
echo "🟢 - Database container is already running."
fi

if npx ts-node ./test/check-db.ts | grep -qw 1; then
echo "🟢 - Database is already initialized."
else
npx ts-node ./test/check-db.ts
EXIT_CODE=$?

if [ $EXIT_CODE -ne 0 ]; then
echo '🟡 - Database is not initialized. Running migrations...'
npx prisma migrate reset --force && yarn prisma:generate
else
echo "🟢 - Database is already initialized."
fi

yarn jest --config ./test/jest-e2e.json

echo '🟡 - Stopping the Docker container...'
docker-compose stop test-db
echo '🟢 - Docker container has been stopped.'
2 changes: 1 addition & 1 deletion server/scripts/setenv.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ ENV_PATH="${SCRIPT_DIR}/../.env.test"

# Check if the file exists
if [ -f "${ENV_PATH}" ]; then
echo "🟡 - Loading environment variables from "${ENV_PATH}"..."
echo "🔵 - Loading environment variables from "${ENV_PATH}"..."
# Export env vars
export $(grep -v '^#' ${ENV_PATH} | xargs)
else
Expand Down
32 changes: 27 additions & 5 deletions server/test/check-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,37 @@ import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

const schemaDatabaseExists = async (databaseName: string) => {
try {
const result = await prisma.$queryRawUnsafe<[any]>(
`SELECT 1 FROM pg_database WHERE datname = '${databaseName}';`,
);

return result.length > 0;
} catch {
return false;
}
};

async function main() {
const result =
await prisma.$queryRaw`SELECT 1 FROM pg_tables WHERE tablename='_prisma_migrations';`;
console.log(result);
const databaseName = 'tests';
// Check if schema exists
const databaseExistsResult = await schemaDatabaseExists(databaseName);

if (!databaseExistsResult) {
throw new Error(`Schema ${databaseName} does not exist`);
}

// Check if database is initialized
await prisma.$queryRaw`SELECT 1 FROM pg_tables WHERE tablename='_prisma_migrations';`;
}

main()
.catch((e) => {
throw e;
.then(() => {
process.exit(0);
})
.catch(() => {
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
Expand Down
File renamed without changes.

0 comments on commit 7eedb82

Please sign in to comment.