From c7f645534d4cad458543303ef9cac96f0fd42088 Mon Sep 17 00:00:00 2001 From: albamig Date: Tue, 22 Feb 2022 13:01:33 +0100 Subject: [PATCH 1/7] - Created SQL script file. - Injected in Dockerfile. issue #37 --- .../external/postgres/10-create-database.sql | 1 + .../external/postgres/Dockerfile | 4 ++-- .../create-multiple-postgresql-databases.sh | 22 ------------------- 3 files changed, 3 insertions(+), 24 deletions(-) create mode 100644 project/database/postgres/deploy/docker-compose/external/postgres/10-create-database.sql delete mode 100644 project/database/postgres/deploy/docker-compose/external/postgres/create-multiple-postgresql-databases.sh diff --git a/project/database/postgres/deploy/docker-compose/external/postgres/10-create-database.sql b/project/database/postgres/deploy/docker-compose/external/postgres/10-create-database.sql new file mode 100644 index 0000000..7086f43 --- /dev/null +++ b/project/database/postgres/deploy/docker-compose/external/postgres/10-create-database.sql @@ -0,0 +1 @@ +-- Create microservices databases \ No newline at end of file diff --git a/project/database/postgres/deploy/docker-compose/external/postgres/Dockerfile b/project/database/postgres/deploy/docker-compose/external/postgres/Dockerfile index 8f76803..2c7c510 100644 --- a/project/database/postgres/deploy/docker-compose/external/postgres/Dockerfile +++ b/project/database/postgres/deploy/docker-compose/external/postgres/Dockerfile @@ -1,2 +1,2 @@ -FROM postgres -COPY create-multiple-postgresql-databases.sh /docker-entrypoint-initdb.d/ +FROM postgres:14.2 +COPY init/10-create-database.sql /docker-entrypoint-initdb.d/10-create-database.sql \ No newline at end of file diff --git a/project/database/postgres/deploy/docker-compose/external/postgres/create-multiple-postgresql-databases.sh b/project/database/postgres/deploy/docker-compose/external/postgres/create-multiple-postgresql-databases.sh deleted file mode 100644 index 711a438..0000000 --- a/project/database/postgres/deploy/docker-compose/external/postgres/create-multiple-postgresql-databases.sh +++ /dev/null @@ -1,22 +0,0 @@ -#!/bin/bash - -set -e -set -u - -function create_user_and_database() { - local database="$1" - echo " Creating user and database '$database'" - psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL - CREATE USER $database; - CREATE DATABASE $database; - GRANT ALL PRIVILEGES ON DATABASE $database TO $database; -EOSQL -} - -if [ -n "$POSTGRES_MULTIPLE_DATABASES" ]; then - echo "Multiple database creation requested: $POSTGRES_MULTIPLE_DATABASES" - for db in "$(echo "$POSTGRES_MULTIPLE_DATABASES" | tr ',' ' ')"; do - create_user_and_database "$db" - done - echo "Multiple databases created" -fi From fe5a3c0f55d0a3971efea2a1eb706dd2a1ea1d1b Mon Sep 17 00:00:00 2001 From: albamig Date: Wed, 23 Feb 2022 10:01:47 +0100 Subject: [PATCH 2/7] - Added db creation. issue #37 --- microservice/language/python/init/_utils.py | 8 ++++++++ project/database/postgres/deploy/docker-compose/_utils.py | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/microservice/language/python/init/_utils.py b/microservice/language/python/init/_utils.py index 30a3216..4730b3d 100644 --- a/microservice/language/python/init/_utils.py +++ b/microservice/language/python/init/_utils.py @@ -28,6 +28,14 @@ def build_deploy_playbook(path: Path) -> str: def build_docker_compose(path: Path, microservice_name: str) -> str: """Build Docker Compose file content.""" + db_creation_path = path / "external/10-create-database.sql" + if not db_creation_path.exists(): + raise ValueError("external/postgres/10-create-database.sql script must exist") + + with db_creation_path.open("a") as db_creation_file: + db_creation_file.write(f"\nCREATE DATABASE {microservice_name}_db") + db_creation_file.write(f"\nCREATE DATABASE {microservice_name}_query_db") + if not path.exists(): raise ValueError("A base Compose file must exist.") diff --git a/project/database/postgres/deploy/docker-compose/_utils.py b/project/database/postgres/deploy/docker-compose/_utils.py index 1e26618..29cda49 100644 --- a/project/database/postgres/deploy/docker-compose/_utils.py +++ b/project/database/postgres/deploy/docker-compose/_utils.py @@ -25,8 +25,8 @@ def build_docker_compose(path: Path) -> str: "build": "external/postgres", "command": "postgres -c 'max_connections=200'", "ports": ["5432"], - "volumes": ["postgres:/var/lib/postgresql/data",], - "environment": {"POSTGRES_USER": "minos", "POSTGRES_PASSWORD": "min0s",}, + "volumes": ["postgres:/var/lib/postgresql/data", ], + "environment": {"POSTGRES_USER": "minos", "POSTGRES_PASSWORD": "min0s", }, } data["services"]["postgres"] = container From 97d2fd29eeedcbbcc7aaad71c4cde5cfd7e6995c Mon Sep 17 00:00:00 2001 From: albamig Date: Wed, 23 Feb 2022 10:06:54 +0100 Subject: [PATCH 3/7] - Added db creation in external/postgres/. issue #37 --- microservice/language/python/init/_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/microservice/language/python/init/_utils.py b/microservice/language/python/init/_utils.py index 4730b3d..d3675b0 100644 --- a/microservice/language/python/init/_utils.py +++ b/microservice/language/python/init/_utils.py @@ -28,7 +28,7 @@ def build_deploy_playbook(path: Path) -> str: def build_docker_compose(path: Path, microservice_name: str) -> str: """Build Docker Compose file content.""" - db_creation_path = path / "external/10-create-database.sql" + db_creation_path = path / "external/postgres/10-create-database.sql" if not db_creation_path.exists(): raise ValueError("external/postgres/10-create-database.sql script must exist") From 4aa007a39287182763dea38b3b8f188a379bbfcf Mon Sep 17 00:00:00 2001 From: albamig Date: Thu, 24 Feb 2022 13:18:07 +0100 Subject: [PATCH 4/7] - Added parent. issue #37 --- microservice/language/python/init/_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/microservice/language/python/init/_utils.py b/microservice/language/python/init/_utils.py index d3675b0..5da41c5 100644 --- a/microservice/language/python/init/_utils.py +++ b/microservice/language/python/init/_utils.py @@ -28,7 +28,7 @@ def build_deploy_playbook(path: Path) -> str: def build_docker_compose(path: Path, microservice_name: str) -> str: """Build Docker Compose file content.""" - db_creation_path = path / "external/postgres/10-create-database.sql" + db_creation_path = path.parent / "external/postgres/10-create-database.sql" if not db_creation_path.exists(): raise ValueError("external/postgres/10-create-database.sql script must exist") From d693a8d0f6b9b311136e7ebc4b9d1126aa17d67d Mon Sep 17 00:00:00 2001 From: albamig Date: Wed, 9 Mar 2022 07:44:25 +0100 Subject: [PATCH 5/7] - Added semicolon at the end of sentences. issue #37 --- microservice/language/python/init/_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/microservice/language/python/init/_utils.py b/microservice/language/python/init/_utils.py index 5da41c5..8f5fca7 100644 --- a/microservice/language/python/init/_utils.py +++ b/microservice/language/python/init/_utils.py @@ -33,8 +33,8 @@ def build_docker_compose(path: Path, microservice_name: str) -> str: raise ValueError("external/postgres/10-create-database.sql script must exist") with db_creation_path.open("a") as db_creation_file: - db_creation_file.write(f"\nCREATE DATABASE {microservice_name}_db") - db_creation_file.write(f"\nCREATE DATABASE {microservice_name}_query_db") + db_creation_file.write(f"\nCREATE DATABASE {microservice_name}_db;") + db_creation_file.write(f"\nCREATE DATABASE {microservice_name}_query_db;") if not path.exists(): raise ValueError("A base Compose file must exist.") From 609c4479be5a74a7ac1c45018e59e0b0312c7bc7 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Wed, 9 Mar 2022 06:50:13 +0000 Subject: [PATCH 6/7] Restyled by black --- microservice/language/python/init/_utils.py | 8 ++------ .../postgres/deploy/docker-compose/_utils.py | 12 ++++-------- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/microservice/language/python/init/_utils.py b/microservice/language/python/init/_utils.py index 8f5fca7..8278bfd 100644 --- a/microservice/language/python/init/_utils.py +++ b/microservice/language/python/init/_utils.py @@ -1,10 +1,6 @@ -from __future__ import ( - annotations, -) +from __future__ import annotations -from pathlib import ( - Path, -) +from pathlib import Path import yaml diff --git a/project/database/postgres/deploy/docker-compose/_utils.py b/project/database/postgres/deploy/docker-compose/_utils.py index 29cda49..0714e64 100644 --- a/project/database/postgres/deploy/docker-compose/_utils.py +++ b/project/database/postgres/deploy/docker-compose/_utils.py @@ -1,10 +1,6 @@ -from __future__ import ( - annotations, -) +from __future__ import annotations -from pathlib import ( - Path, -) +from pathlib import Path import yaml @@ -25,8 +21,8 @@ def build_docker_compose(path: Path) -> str: "build": "external/postgres", "command": "postgres -c 'max_connections=200'", "ports": ["5432"], - "volumes": ["postgres:/var/lib/postgresql/data", ], - "environment": {"POSTGRES_USER": "minos", "POSTGRES_PASSWORD": "min0s", }, + "volumes": ["postgres:/var/lib/postgresql/data",], + "environment": {"POSTGRES_USER": "minos", "POSTGRES_PASSWORD": "min0s",}, } data["services"]["postgres"] = container From 9c37808b9bffec7c896403c0fa27fcc53066db12 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Wed, 9 Mar 2022 06:50:18 +0000 Subject: [PATCH 7/7] Restyled by isort --- microservice/language/python/init/_utils.py | 8 ++++++-- project/database/postgres/deploy/docker-compose/_utils.py | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/microservice/language/python/init/_utils.py b/microservice/language/python/init/_utils.py index 8278bfd..8f5fca7 100644 --- a/microservice/language/python/init/_utils.py +++ b/microservice/language/python/init/_utils.py @@ -1,6 +1,10 @@ -from __future__ import annotations +from __future__ import ( + annotations, +) -from pathlib import Path +from pathlib import ( + Path, +) import yaml diff --git a/project/database/postgres/deploy/docker-compose/_utils.py b/project/database/postgres/deploy/docker-compose/_utils.py index 0714e64..1e26618 100644 --- a/project/database/postgres/deploy/docker-compose/_utils.py +++ b/project/database/postgres/deploy/docker-compose/_utils.py @@ -1,6 +1,10 @@ -from __future__ import annotations +from __future__ import ( + annotations, +) -from pathlib import Path +from pathlib import ( + Path, +) import yaml