From d3baaf633465d540000455a5d385102084b0bd25 Mon Sep 17 00:00:00 2001 From: Lucas Dutra Date: Thu, 4 Mar 2021 15:42:03 -0300 Subject: [PATCH] build: Set up new db-docker connection Co-authored-by: Youssef Muhamad Co-authored-by: Rogerio Junior --- Makefile | 3 +++ docker-compose.dev.yml | 10 +++++----- project/db/Dockerfile | 6 ------ project/db/create.sql | 13 ------------- project/db/create_dev.sql | 37 ++++++++++++++++++++++++++++++++++++ project/db/create_test.sql | 37 ++++++++++++++++++++++++++++++++++++ project/tests/test_config.py | 2 +- 7 files changed, 83 insertions(+), 25 deletions(-) delete mode 100755 project/db/Dockerfile delete mode 100755 project/db/create.sql create mode 100755 project/db/create_dev.sql create mode 100644 project/db/create_test.sql diff --git a/Makefile b/Makefile index ab32fb8..89f749f 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,9 @@ run: run-silent: sudo docker-compose -f docker-compose.dev.yml up -d +run-build: + sudo docker-compose -f docker-compose.dev.yml up --build + test: sudo docker-compose -f docker-compose.dev.yml run request python manage.py test diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index bee1d91..5667b13 100755 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -11,17 +11,17 @@ services: environment: - FLASK_ENV=development - APP_SETTINGS=project.config.DevelopmentConfig - - DATABASE_URL=postgres://postgres:postgres@db:5432/request_dev - - DATABASE_TEST_URL=postgres://postgres:postgres@db:5432/request_test + - DATABASE_URL=postgres://postgres:postgres@db:5432/requestlenditdb_dev + - DATABASE_TEST_URL=postgres://postgres:postgres@db:5432/requestlenditdb_test depends_on: - db db: - build: - context: ./project/db - dockerfile: Dockerfile + image: postgres:13.2-alpine ports: - 5106:5432 + volumes: + - ./project/db:/docker-entrypoint-initdb.d environment: - POSTGRES_USER=postgres - POSTGRES_PASSWORD=postgres \ No newline at end of file diff --git a/project/db/Dockerfile b/project/db/Dockerfile deleted file mode 100755 index 1d800aa..0000000 --- a/project/db/Dockerfile +++ /dev/null @@ -1,6 +0,0 @@ -# Base image -FROM postgres:10.4-alpine - - -# Run create.sql on init -ADD create.sql /docker-entrypoint-initdb.d \ No newline at end of file diff --git a/project/db/create.sql b/project/db/create.sql deleted file mode 100755 index 165917a..0000000 --- a/project/db/create.sql +++ /dev/null @@ -1,13 +0,0 @@ -CREATE DATABASE requestLendItDB_dev - WITH - ENCODING = UTF8 - LC_COLLATE = 'pt_BR.UTF-8' - LC_CTYPE = 'pt_BR.UTF-8' - TEMPLATE = template0; - -CREATE DATABASE requestLendItDB_test - WITH - ENCODING = UTF8 - LC_COLLATE = 'pt_BR.UTF-8' - LC_CTYPE = 'pt_BR.UTF-8' - TEMPLATE = template0; \ No newline at end of file diff --git a/project/db/create_dev.sql b/project/db/create_dev.sql new file mode 100755 index 0000000..540a55d --- /dev/null +++ b/project/db/create_dev.sql @@ -0,0 +1,37 @@ +CREATE DATABASE requestLendItDB_dev + WITH + ENCODING = UTF8 + LC_COLLATE = 'pt_BR.UTF-8' + LC_CTYPE = 'pt_BR.UTF-8' + TEMPLATE = template0; + +\c requestlenditdb_dev + +CREATE TABLE PRODUCT_CATEGORY ( + productCategoryId SMALLSERIAL NOT NULL, + name TEXT NOT NULL, + + CONSTRAINT PRODUCT_CATEGORY_PK PRIMARY KEY (productCategoryId) +); + +CREATE TABLE REQUEST ( + requestId UUID NOT NULL, + productName TEXT NOT NULL, + startDate DATE NOT NULL, + endDate DATE NOT NULL, + description TEXT NOT NULL, + requester TEXT NOT NULL, + lender TEXT NULL, + productCategoryId SMALLSERIAL NOT NULL, + + CONSTRAINT REQUEST_PK PRIMARY KEY (requestId), + + CONSTRAINT REQUEST_PRODUCT_CATEGORY_FK FOREIGN KEY (productCategoryId) + REFERENCES PRODUCT_CATEGORY (productCategoryId) + ON DELETE RESTRICT + ON UPDATE RESTRICT, + + CONSTRAINT VALID_REQUESTER_EMAIL CHECK (requester ~* '^[\w\-\.]+@([\w-]+\.)+[\w-]{2,4}$'), + CONSTRAINT VALID_LENDER_EMAIL CHECK (lender ~* '^[\w\-\.]+@([\w-]+\.)+[\w-]{2,4}$'), + CONSTRAINT VALID_LENDER_USER CHECK (requester <> lender) +); \ No newline at end of file diff --git a/project/db/create_test.sql b/project/db/create_test.sql new file mode 100644 index 0000000..c11309d --- /dev/null +++ b/project/db/create_test.sql @@ -0,0 +1,37 @@ +CREATE DATABASE requestLendItDB_test + WITH + ENCODING = UTF8 + LC_COLLATE = 'pt_BR.UTF-8' + LC_CTYPE = 'pt_BR.UTF-8' + TEMPLATE = template0; + +\c requestlenditdb_test + +CREATE TABLE PRODUCT_CATEGORY ( + productCategoryId SMALLSERIAL NOT NULL, + name TEXT NOT NULL, + + CONSTRAINT PRODUCT_CATEGORY_PK PRIMARY KEY (productCategoryId) +); + +CREATE TABLE REQUEST ( + requestId UUID NOT NULL, + productName TEXT NOT NULL, + startDate DATE NOT NULL, + endDate DATE NOT NULL, + description TEXT NOT NULL, + requester TEXT NOT NULL, + lender TEXT NULL, + productCategoryId SMALLSERIAL NOT NULL, + + CONSTRAINT REQUEST_PK PRIMARY KEY (requestId), + + CONSTRAINT REQUEST_PRODUCT_CATEGORY_FK FOREIGN KEY (productCategoryId) + REFERENCES PRODUCT_CATEGORY (productCategoryId) + ON DELETE RESTRICT + ON UPDATE RESTRICT, + + CONSTRAINT VALID_REQUESTER_EMAIL CHECK (requester ~* '^[\w\-\.]+@([\w-]+\.)+[\w-]{2,4}$'), + CONSTRAINT VALID_LENDER_EMAIL CHECK (lender ~* '^[\w\-\.]+@([\w-]+\.)+[\w-]{2,4}$'), + CONSTRAINT VALID_LENDER_USER CHECK (requester <> lender) +); \ No newline at end of file diff --git a/project/tests/test_config.py b/project/tests/test_config.py index 5bc268c..ba0da40 100755 --- a/project/tests/test_config.py +++ b/project/tests/test_config.py @@ -40,7 +40,7 @@ def create_app(self): return app def test_app_is_production(self): - elf.assertTrue(app.config["SECRET_KEY"] == "my_precious") + self.assertTrue(app.config["SECRET_KEY"] == "my_precious") self.assertFalse(app.config["TESTING"])