From bc027f372f87b98488cba64af78f1a43abc050ae Mon Sep 17 00:00:00 2001 From: alexis-moins Date: Mon, 20 Nov 2023 11:03:50 +0100 Subject: [PATCH 1/3] feat(CI): add first version of backend CI to run tests --- .github/workflows/backend.yaml | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 .github/workflows/backend.yaml diff --git a/.github/workflows/backend.yaml b/.github/workflows/backend.yaml new file mode 100644 index 0000000..44401a1 --- /dev/null +++ b/.github/workflows/backend.yaml @@ -0,0 +1,32 @@ +# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs + +name: Node.js CI + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + workflow_dispatch: + +jobs: + build: + + runs-on: ubuntu-latest + + strategy: + matrix: + node-version: [18.x] + # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ + + steps: + - uses: actions/checkout@v3 + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v3 + with: + node-version: ${{ matrix.node-version }} + cache: 'npm' + - run: npm ci + - run: npm run build --if-present + - run: npm test From a3c580fc84a5921baa4d765a21daddd383197ebf Mon Sep 17 00:00:00 2001 From: alexis-moins Date: Mon, 20 Nov 2023 12:02:17 +0100 Subject: [PATCH 2/3] feat(CI/CD): add entrypoint scripts for backend docker --- .github/workflows/backend.yaml | 28 ++++++++++------------------ back/Dockerfile | 7 ++++--- back/entrypoint.sh | 8 ++++++++ back/package.json | 2 +- back/test-entrypoint.sh | 8 ++++++++ docker-compose.yaml | 9 ++++++--- scripts/test-entrypoint.sh | 6 ++++++ 7 files changed, 43 insertions(+), 25 deletions(-) create mode 100755 back/entrypoint.sh create mode 100755 back/test-entrypoint.sh create mode 100755 scripts/test-entrypoint.sh diff --git a/.github/workflows/backend.yaml b/.github/workflows/backend.yaml index 44401a1..10aaf92 100644 --- a/.github/workflows/backend.yaml +++ b/.github/workflows/backend.yaml @@ -1,7 +1,4 @@ -# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node -# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs - -name: Node.js CI +name: Backend tests on: push: @@ -12,21 +9,16 @@ on: jobs: build: - runs-on: ubuntu-latest - strategy: - matrix: - node-version: [18.x] - # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ - steps: - uses: actions/checkout@v3 - - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v3 - with: - node-version: ${{ matrix.node-version }} - cache: 'npm' - - run: npm ci - - run: npm run build --if-present - - run: npm test + + - name: Set environment variables + run: cp .env.local .env + + - name: Build test docker image + run: docker compose build --no-cache test + + - name: Start tests + run: docker-compose up database test diff --git a/back/Dockerfile b/back/Dockerfile index c7ddef8..c02cb39 100644 --- a/back/Dockerfile +++ b/back/Dockerfile @@ -2,6 +2,9 @@ FROM node:18-alpine WORKDIR /app +COPY entrypoint.sh . +COPY test-entrypoint.sh . + COPY package.json . COPY package-lock.json . @@ -9,11 +12,9 @@ RUN npm clean-install COPY . . -RUN npx prisma generate - EXPOSE 3000 # Enable file watching inside the container (hot-reload) ENV CHOKIDAR_USEPOLLING=true -CMD npm run dev +ENTRYPOINT [ "/app/entrypoint.sh" ] diff --git a/back/entrypoint.sh b/back/entrypoint.sh new file mode 100755 index 0000000..9e475fc --- /dev/null +++ b/back/entrypoint.sh @@ -0,0 +1,8 @@ +#!/bin/sh + +set -ex + +npx prisma generate +npx prisma migrate dev + +npm run dev diff --git a/back/package.json b/back/package.json index 1b5e781..8691c36 100644 --- a/back/package.json +++ b/back/package.json @@ -4,7 +4,7 @@ "license": "MIT", "scripts": { "dev": "tsx watch src/index.ts", - "test": "jest" + "test": "jest --passWithNoTests" }, "dependencies": { "@lucia-auth/adapter-prisma": "^3.0.2", diff --git a/back/test-entrypoint.sh b/back/test-entrypoint.sh new file mode 100755 index 0000000..2d04c69 --- /dev/null +++ b/back/test-entrypoint.sh @@ -0,0 +1,8 @@ +#!/bin/sh + +set -ex + +npx prisma generate +npx prisma migrate deploy + +npm run test diff --git a/docker-compose.yaml b/docker-compose.yaml index b96020f..e7a1673 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -3,9 +3,10 @@ version: "3.9" services: back: build: ./back - container_name: back + container_name: back restart: always + environment: DATABASE_URL: ${DATABASE_URL} @@ -15,15 +16,16 @@ services: volumes: - ./back/src:/app/src - ./back/prisma:/app/prisma + - ./back/package.json:/app/package.json depends_on: - database test: build: ./back - container_name: test - entrypoint: npm run test + container_name: test + entrypoint: /app/test-entrypoint.sh environment: DATABASE_URL: ${TEST_DATABASE_URL} @@ -31,6 +33,7 @@ services: volumes: - ./back/src:/app/src - ./back/prisma:/app/prisma + - ./back/package.json:/app/package.json depends_on: - database diff --git a/scripts/test-entrypoint.sh b/scripts/test-entrypoint.sh new file mode 100755 index 0000000..64dcae4 --- /dev/null +++ b/scripts/test-entrypoint.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +set -ex + +npx prisma migrate dev +npm run test From 29d8be4dd88818a885834a4ee45308719715935f Mon Sep 17 00:00:00 2001 From: alexis-moins Date: Mon, 20 Nov 2023 13:53:38 +0100 Subject: [PATCH 3/3] fix(backend.yaml): wait for services to be healthy --- .env.local | 12 ++--- .github/workflows/backend.yaml | 5 +- back/entrypoint.sh | 2 +- .../20231113135109_create_user/migration.sql | 9 ---- .../migration.sql | 50 ------------------- .../migration.sql | 46 +++++++++++++++++ back/prisma/migrations/migration_lock.toml | 2 +- back/prisma/schema.prisma | 2 +- docker-compose.yaml | 16 +++--- 9 files changed, 62 insertions(+), 82 deletions(-) delete mode 100644 back/prisma/migrations/20231113135109_create_user/migration.sql delete mode 100644 back/prisma/migrations/20231113152055_add_lucia_auth/migration.sql create mode 100644 back/prisma/migrations/20231120134558_initial_migration/migration.sql diff --git a/.env.local b/.env.local index 4cf96d5..cbee523 100644 --- a/.env.local +++ b/.env.local @@ -5,11 +5,9 @@ FRONTEND_PORT=3000 ADMINER_PORT=8080 ADMINER_DESIGN=lucas-sandery -MYSQL_ROOT_PASSWORD=root +POSTGRES_USER=john +POSTGRES_PASSWORD=doe -MYSQL_USER=john -MYSQL_PASSWORD=doe - -# Needs root privileges to manage DB -DATABASE_URL=mysql://root:root@database:3306/count_of_money -TEST_DATABASE_URL=mysql://root:root@database:3306/count_of_money_test +# Note: change user:password to match the fields above! +DATABASE_URL=postgresql://john:doe@database:5432/count_of_money?schema=public +TEST_DATABASE_URL=postgresql://john:doe@database:5432/count_of_money_test?schema=public diff --git a/.github/workflows/backend.yaml b/.github/workflows/backend.yaml index 10aaf92..9a9b628 100644 --- a/.github/workflows/backend.yaml +++ b/.github/workflows/backend.yaml @@ -17,8 +17,5 @@ jobs: - name: Set environment variables run: cp .env.local .env - - name: Build test docker image - run: docker compose build --no-cache test - - name: Start tests - run: docker-compose up database test + run: docker compose up test diff --git a/back/entrypoint.sh b/back/entrypoint.sh index 9e475fc..6857892 100755 --- a/back/entrypoint.sh +++ b/back/entrypoint.sh @@ -3,6 +3,6 @@ set -ex npx prisma generate -npx prisma migrate dev +npx prisma migrate deploy npm run dev diff --git a/back/prisma/migrations/20231113135109_create_user/migration.sql b/back/prisma/migrations/20231113135109_create_user/migration.sql deleted file mode 100644 index ba4dbbd..0000000 --- a/back/prisma/migrations/20231113135109_create_user/migration.sql +++ /dev/null @@ -1,9 +0,0 @@ --- CreateTable -CREATE TABLE `User` ( - `id` INTEGER NOT NULL AUTO_INCREMENT, - `email` VARCHAR(191) NOT NULL, - `password` VARCHAR(191) NOT NULL, - - UNIQUE INDEX `User_email_key`(`email`), - PRIMARY KEY (`id`) -) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; diff --git a/back/prisma/migrations/20231113152055_add_lucia_auth/migration.sql b/back/prisma/migrations/20231113152055_add_lucia_auth/migration.sql deleted file mode 100644 index 61e7c8f..0000000 --- a/back/prisma/migrations/20231113152055_add_lucia_auth/migration.sql +++ /dev/null @@ -1,50 +0,0 @@ -/* - Warnings: - - - The primary key for the `User` table will be changed. If it partially fails, the table could be left without primary key constraint. - - You are about to drop the column `email` on the `User` table. All the data in the column will be lost. - - You are about to drop the column `password` on the `User` table. All the data in the column will be lost. - - A unique constraint covering the columns `[id]` on the table `User` will be added. If there are existing duplicate values, this will fail. - -*/ --- DropIndex -DROP INDEX `User_email_key` ON `User`; - --- AlterTable -ALTER TABLE `User` DROP PRIMARY KEY, - DROP COLUMN `email`, - DROP COLUMN `password`, - MODIFY `id` VARCHAR(191) NOT NULL, - ADD PRIMARY KEY (`id`); - --- CreateTable -CREATE TABLE `Session` ( - `id` VARCHAR(191) NOT NULL, - `user_id` VARCHAR(191) NOT NULL, - `active_expires` BIGINT NOT NULL, - `idle_expires` BIGINT NOT NULL, - - UNIQUE INDEX `Session_id_key`(`id`), - INDEX `Session_user_id_idx`(`user_id`), - PRIMARY KEY (`id`) -) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; - --- CreateTable -CREATE TABLE `Key` ( - `id` VARCHAR(191) NOT NULL, - `hashed_password` VARCHAR(191) NULL, - `user_id` VARCHAR(191) NOT NULL, - - UNIQUE INDEX `Key_id_key`(`id`), - INDEX `Key_user_id_idx`(`user_id`), - PRIMARY KEY (`id`) -) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; - --- CreateIndex -CREATE UNIQUE INDEX `User_id_key` ON `User`(`id`); - --- AddForeignKey -ALTER TABLE `Session` ADD CONSTRAINT `Session_user_id_fkey` FOREIGN KEY (`user_id`) REFERENCES `User`(`id`) ON DELETE CASCADE ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE `Key` ADD CONSTRAINT `Key_user_id_fkey` FOREIGN KEY (`user_id`) REFERENCES `User`(`id`) ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/back/prisma/migrations/20231120134558_initial_migration/migration.sql b/back/prisma/migrations/20231120134558_initial_migration/migration.sql new file mode 100644 index 0000000..7ea9523 --- /dev/null +++ b/back/prisma/migrations/20231120134558_initial_migration/migration.sql @@ -0,0 +1,46 @@ +-- CreateTable +CREATE TABLE "User" ( + "id" TEXT NOT NULL, + + CONSTRAINT "User_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "Session" ( + "id" TEXT NOT NULL, + "user_id" TEXT NOT NULL, + "active_expires" BIGINT NOT NULL, + "idle_expires" BIGINT NOT NULL, + + CONSTRAINT "Session_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "Key" ( + "id" TEXT NOT NULL, + "hashed_password" TEXT, + "user_id" TEXT NOT NULL, + + CONSTRAINT "Key_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE UNIQUE INDEX "User_id_key" ON "User"("id"); + +-- CreateIndex +CREATE UNIQUE INDEX "Session_id_key" ON "Session"("id"); + +-- CreateIndex +CREATE INDEX "Session_user_id_idx" ON "Session"("user_id"); + +-- CreateIndex +CREATE UNIQUE INDEX "Key_id_key" ON "Key"("id"); + +-- CreateIndex +CREATE INDEX "Key_user_id_idx" ON "Key"("user_id"); + +-- AddForeignKey +ALTER TABLE "Session" ADD CONSTRAINT "Session_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Key" ADD CONSTRAINT "Key_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/back/prisma/migrations/migration_lock.toml b/back/prisma/migrations/migration_lock.toml index e5a788a..fbffa92 100644 --- a/back/prisma/migrations/migration_lock.toml +++ b/back/prisma/migrations/migration_lock.toml @@ -1,3 +1,3 @@ # Please do not edit this file manually # It should be added in your version-control system (i.e. Git) -provider = "mysql" \ No newline at end of file +provider = "postgresql" \ No newline at end of file diff --git a/back/prisma/schema.prisma b/back/prisma/schema.prisma index 73e7324..c6f22e5 100644 --- a/back/prisma/schema.prisma +++ b/back/prisma/schema.prisma @@ -3,7 +3,7 @@ generator client { } datasource db { - provider = "mysql" + provider = "postgresql" url = env("DATABASE_URL") } diff --git a/docker-compose.yaml b/docker-compose.yaml index e7a1673..1edd49a 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -19,7 +19,7 @@ services: - ./back/package.json:/app/package.json depends_on: - - database + - database test: build: ./back @@ -36,7 +36,7 @@ services: - ./back/package.json:/app/package.json depends_on: - - database + - database adminer: image: adminer @@ -54,20 +54,18 @@ services: - database:db database: - image: mysql:8.0 + image: postgres:16-alpine container_name: database restart: always environment: - MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD} - MYSQL_DATABASE: count_of_money - - MYSQL_USER: ${MYSQL_USER} - MYSQL_PASSWORD: ${MYSQL_PASSWORD} + POSTGRES_DB: count_of_money + POSTGRES_USER: ${POSTGRES_USER} + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} volumes: - - database-data:/var/lib/mysql + - database-data:/var/lib/postgresql/data volumes: database-data: