From d895cd476deed2bbda4be1a82c279e613a76f582 Mon Sep 17 00:00:00 2001
From: Benjamin Bertrand <benjamin.bertrand@esss.se>
Date: Mon, 29 Jan 2018 14:34:42 +0100
Subject: [PATCH] Remove sqlalchemy create_all() call

Let alembic handle the database creation and migration
---
 Makefile        | 10 +++++++---
 README.rst      |  5 +++--
 app/commands.py |  6 ++----
 3 files changed, 12 insertions(+), 9 deletions(-)

diff --git a/Makefile b/Makefile
index dba3149..cdbd713 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-.PHONY: help build tag push refresh release db initdb test db_image test_image
+.PHONY: help build tag push refresh release db init_db upgrade_db test db_image test_image
 
 OWNER := registry.esss.lu.se/ics-infrastructure
 GIT_TAG := $(shell git describe --always)
@@ -40,8 +40,12 @@ release: ## build, tag, and push all stacks
 db: ## start postgres and redis for development
 	docker-compose up -d postgres redis
 
-initdb: ## initialize the dev database
-	docker-compose run --rm web flask initdb
+init_db: ## initialize the dev database
+	docker-compose run --rm web flask db upgrade head
+	docker-compose run --rm web flask create_defaults
+
+upgrade_db: ## upgrade the dev database
+	docker-compose run --rm web flask db upgrade head
 
 test:  ## run the tests (on current directory)
 	docker-compose run --rm web pytest --cov=app -v
diff --git a/README.rst b/README.rst
index 7f015c2..c1f10f5 100644
--- a/README.rst
+++ b/README.rst
@@ -19,9 +19,10 @@ You can use docker for development:
     or
     $ make db
     # Initialize the database
-    $ docker-compose run --rm web flask initdb
+    $ docker-compose run --rm web flask db upgrade head
+    $ docker-compose run --rm web flask create_defaults
     or
-    $ make initdb
+    $ make init_db
 
 3. Start the application::
 
diff --git a/app/commands.py b/app/commands.py
index 225653a..5c25236 100644
--- a/app/commands.py
+++ b/app/commands.py
@@ -63,10 +63,8 @@ def sync_users():
 
 def register_cli(app):
     @app.cli.command()
-    def initdb():
-        """Create the database tables and initialize them with default values"""
-        db.engine.execute('CREATE EXTENSION IF NOT EXISTS citext')
-        db.create_all()
+    def create_defaults():
+        """Create the database default values"""
         for instance in defaults:
             db.session.add(instance)
             try:
-- 
GitLab