From f44898ff873621decfdf9ca717ff3ddda9648c6e Mon Sep 17 00:00:00 2001 From: Ayrat Hudaygulov Date: Thu, 28 Sep 2023 00:01:21 +0100 Subject: [PATCH] added DB --- .github/workflows/deploy-azure-acr.yml | 17 ++++++++++++- .gitignore | 1 + README.md | 16 ++++++++++++ flyway.conf.example | 7 ++++++ src/VahterBanBot/VahterBanBot.fsproj | 2 ++ src/migrations/V1__initial.sql | 35 ++++++++++++++++++++++++++ 6 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 README.md create mode 100644 flyway.conf.example create mode 100644 src/migrations/V1__initial.sql diff --git a/.github/workflows/deploy-azure-acr.yml b/.github/workflows/deploy-azure-acr.yml index 6534afd..4f0657d 100644 --- a/.github/workflows/deploy-azure-acr.yml +++ b/.github/workflows/deploy-azure-acr.yml @@ -8,6 +8,11 @@ on: permissions: contents: read +env: + SQLFLUFF_DIALECT: postgres + DOCKER_IMAGE: redgate/flyway + SCHEMAS: public + jobs: build: runs-on: ubuntu-latest @@ -15,7 +20,17 @@ jobs: steps: - name: Checkout uses: actions/checkout@v2 - + + - name: Apply migrations + run: >- + docker run --rm + --volume ${{ github.workspace }}/migrations:/src/migrations:ro + "${{ env.DOCKER_IMAGE }}" + -url="${{ secrets.DB_PROD_URL }}" + -user="${{ secrets.DB_PROD_USERNAME }}" + -password="${{ secrets.DB_PROD_PASSWORD }}" + migrate -schemas="${{ env.SCHEMAS }}" + - name: 'Docker Login' uses: azure/docker-login@v1 with: diff --git a/.gitignore b/.gitignore index fc06b9d..b94f593 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ **/bin .idea .env +flyway.conf diff --git a/README.md b/README.md new file mode 100644 index 0000000..05f3be1 --- /dev/null +++ b/README.md @@ -0,0 +1,16 @@ +Database setup + +```postgresql +CREATE ROLE vahter_bot_ban_service WITH LOGIN PASSWORD 'vahter_bot_ban_service'; +GRANT vahter_bot_ban_service TO postgres; +CREATE DATABASE vahter_bot_ban OWNER vahter_bot_ban_service ENCODING 'UTF8'; +GRANT ALL ON DATABASE vahter_bot_ban TO vahter_bot_ban_service; +GRANT USAGE, CREATE ON SCHEMA public TO vahter_bot_ban_service; +CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; +``` + +Run migrations + +``` +flyway -configFiles=flyway.local.conf migrate +``` diff --git a/flyway.conf.example b/flyway.conf.example new file mode 100644 index 0000000..2145f75 --- /dev/null +++ b/flyway.conf.example @@ -0,0 +1,7 @@ +flyway.locations=filesystem:./src/migrations +flyway.url=jdbc:postgresql://localhost:5433/vahter_bot_ban +flyway.schemas=public +flyway.user=vahter_bot_ban_service +flyway.password=vahter_bot_ban_service +flyway.baselineOnMigrate=true +flyway.baselineVersion=0 diff --git a/src/VahterBanBot/VahterBanBot.fsproj b/src/VahterBanBot/VahterBanBot.fsproj index 64fa67b..b665644 100644 --- a/src/VahterBanBot/VahterBanBot.fsproj +++ b/src/VahterBanBot/VahterBanBot.fsproj @@ -13,11 +13,13 @@ + + diff --git a/src/migrations/V1__initial.sql b/src/migrations/V1__initial.sql new file mode 100644 index 0000000..84b838e --- /dev/null +++ b/src/migrations/V1__initial.sql @@ -0,0 +1,35 @@ +CREATE TABLE "user" +( + id BIGINT NOT NULL + PRIMARY KEY, + username TEXT NULL, + + banned_by BIGINT NULL + CONSTRAINT user_banned_by_fkey + REFERENCES "user" (id), + banned_at TIMESTAMPTZ NULL, + ban_reason TEXT NULL, + + created_at TIMESTAMPTZ NOT NULL DEFAULT timezone('utc'::TEXT, NOW()), + updated_at TIMESTAMPTZ NOT NULL DEFAULT timezone('utc'::TEXT, NOW()) +); + +CREATE UNIQUE INDEX "user_username_uindex" + ON "user" (username); + +CREATE INDEX "user_banned_by_index" + ON "user" (banned_by); + +CREATE TABLE "message" +( + id INT NOT NULL PRIMARY KEY, + user_id BIGINT NOT NULL + CONSTRAINT message_user_id_fkey + REFERENCES "user" (id) + ON DELETE CASCADE, + + created_at TIMESTAMPTZ NOT NULL DEFAULT timezone('utc'::TEXT, NOW()) +); + +CREATE INDEX "message_user_id_index" + ON "message" (user_id);