From eb2f040006d74c67864df1c793880a076fad3c8a Mon Sep 17 00:00:00 2001
From: Pavel Kirilin <win10@list.ru>
Date: Sun, 1 Mar 2020 15:42:50 +0400
Subject: [PATCH] Makefile update, programmatic migrations. Description: -
 Removed migrate step for the prod and the run targets. - Alembic now triggers
 automatically within the wait_script. - Added "restart: always" in the
 docker-compose configuration.

Signed-off-by: Pavel Kirilin <win10@list.ru>
---
 Makefile                 | 6 +++---
 docker-compose.prod.yml  | 2 ++
 src/actions/__init__.py  | 4 ++++
 src/utils/wait_script.py | 5 +++++
 4 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/Makefile b/Makefile
index 14f40e4..18f0a0e 100644
--- a/Makefile
+++ b/Makefile
@@ -23,11 +23,11 @@ _run_local:
 	@docker-compose up -d
 
 ## Runs application. Builds, creates, starts, and attaches to containers for a service. | Common
-run: _run_local service_wait migrate
+run: _run_local service_wait
 	@docker-compose logs -f
 
 
-prod: service service_wait migrate
+prod: service service_wait
 	@docker-compose -f docker-compose.prod.yml logs -f
 
 ## Clean all containers
@@ -61,7 +61,7 @@ migrations:
 
 ## Upgrades database.
 migrate:
-	@docker exec -i telegram_system_bot alembic upgrade head;
+	@docker exec -i telegram_system_bot alembic upgrade head
 
 ## Waits postgresql to be online
 wait_resources:
diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml
index aec3e50..e0e1c50 100644
--- a/docker-compose.prod.yml
+++ b/docker-compose.prod.yml
@@ -7,6 +7,7 @@ networks:
 
 services:
   system_bot:
+    restart: always
     container_name: 'telegram_system_bot'
     build: .
     env_file:
@@ -29,6 +30,7 @@ services:
       - system_bot_postgres
 
   system_bot_postgres:
+    restart: always
     container_name: 'system_bot_postgres'
     image: 'postgres:11.6-alpine'
     environment:
diff --git a/src/actions/__init__.py b/src/actions/__init__.py
index 4627ed1..3d6bdce 100644
--- a/src/actions/__init__.py
+++ b/src/actions/__init__.py
@@ -1,3 +1,5 @@
+import os
+
 from .basic_actions import *
 from .docker import *
 from .interactive_session import *
@@ -10,6 +12,8 @@ from ..models.crud.server_crud import fn_get_all_chat_ids
 
 
 async def notify_about_updates():
+    if not os.path.exists('app.info'):
+        return
     with open('app.info') as f:
         app_info = f.read()
     for chat_id in await fn_get_all_chat_ids(settings.engine):
diff --git a/src/utils/wait_script.py b/src/utils/wait_script.py
index 8672ea7..3f7a179 100644
--- a/src/utils/wait_script.py
+++ b/src/utils/wait_script.py
@@ -2,8 +2,12 @@ import random
 import socket
 import time
 
+import alembic.config
+
 from src.settings import settings
 
+alembicArgs = ['--raiseerr', 'upgrade', 'head']
+
 if __name__ == "__main__":
     postgres_connected = "FAILED"
     while True:
@@ -11,6 +15,7 @@ if __name__ == "__main__":
             with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
                 sock.connect((settings.postgres_host, settings.postgres_port))
                 print("[  OK  ] Connect to postgres")
+                alembic.config.main(argv=alembicArgs)
                 break
         except socket.error:
             print(f"[FAILED] Connect to postgres")
-- 
GitLab