From 0942489056bb2ea05ba407c63692770688733ffb Mon Sep 17 00:00:00 2001 From: Daniel Tsvetkov Date: Thu, 4 Jun 2020 16:40:52 +0200 Subject: [PATCH] refactor with population --- bootstrap/{init_db.py => init_populate.py} | 6 +-- bootstrap/worker.py | 6 ++- oshipka.sh | 48 ++++++++++++++++++---- oshipka/persistance/__init__.py | 1 + 4 files changed, 49 insertions(+), 12 deletions(-) rename bootstrap/{init_db.py => init_populate.py} (65%) diff --git a/bootstrap/init_db.py b/bootstrap/init_populate.py similarity index 65% rename from bootstrap/init_db.py rename to bootstrap/init_populate.py index e51c118..03d709a 100644 --- a/bootstrap/init_db.py +++ b/bootstrap/init_populate.py @@ -3,6 +3,6 @@ from oshipka.persistance import populate_static, init_db from populate import populate_db from webapp.app import app -if init_db(app): - populate_static(app) - populate_db(app) +init_db(app) +populate_static(app) +populate_db(app) diff --git a/bootstrap/worker.py b/bootstrap/worker.py index 97420bf..f69e1c7 100644 --- a/bootstrap/worker.py +++ b/bootstrap/worker.py @@ -1,11 +1,13 @@ -from oshipka import worker, init_db +from oshipka import worker + +from oshipka.persistance import populate_static, init_db from populate import populate_db from webapp.app import app if init_db(app): + populate_static(app) populate_db(app) - if __name__ == "__main__": worker.main() diff --git a/oshipka.sh b/oshipka.sh index f422ce9..03b5afc 100755 --- a/oshipka.sh +++ b/oshipka.sh @@ -8,12 +8,18 @@ echo "oshipka is at: $OSHIPKA_PATH" #!/usr/bin/env bash HELP=" -Usage $0 [ bootstrap | model | db_migrate | db_upgrade | init | worker | web | venv | install | link | cert ] +Usage $0 [ bootstrap | model | db_migrate | db_upgrade | db_populate | db_recreate | db_purge_recreate | init | worker | web | venv | install | link | cert ] bootstrap [PROJECT_PATH] Create a new project in PROJECT_PATH - model [MODEL_NAME] Create or update a model init Install dev env + model Create or update a model from files in webapp/view_models/*.yaml + db_migrate DB migration + db_upgrade DB upgrade to last migration + db_populate Populate db with data from data_static/ and populate.py + db_recreate Delete the database, recreate to latest migration and populate + db_purge_recreate Same as db_recreate but also purge the migrations + worker Start worker web Start webapp @@ -101,8 +107,8 @@ bootstrap() { link_dev_oshipka source venv/bin/activate python manager.py db init - python manager.py db migrate -m "Initial migration." - python init_db.py + python manager.py db migrate -m "001" + _post_migrate python manager.py db upgrade } @@ -122,8 +128,6 @@ model() { db_migrate() { shift source venv/bin/activate - python init_db.py - db_upgrade python manager.py db migrate _post_migrate } @@ -131,13 +135,37 @@ db_migrate() { db_upgrade() { shift source venv/bin/activate - python init_db.py python manager.py db upgrade } +db_purge_recreate() { + shift + source venv/bin/activate + rm -rf data/db.sqlite data/search_index migrations/ + python manager.py db init + db_migrate + db_upgrade + db_populate +} + +db_populate() { + shift + source venv/bin/activate + python init_populate.py +} + +db_recreate() { + shift + source venv/bin/activate + rm -rf data/db.sqlite data/search_index + db_upgrade + db_populate +} + _post_migrate() { for i in migrations/versions/*.py; do sed -i "s/sqlalchemy_utils.types.choice.ChoiceType(length=255), /sa.String(), / " "$i"; + sed -i "s/oshipka.persistance.LiberalBoolean(), /sa.Boolean(), / " "$i"; done } @@ -152,6 +180,12 @@ command_main() { ;; db_upgrade) db_upgrade "$@" ;; + db_populate) db_populate "$@" + ;; + db_recreate) db_recreate "$@" + ;; + db_purge_recreate) db_purge_recreate "$@" + ;; init) init "$@" ;; worker) worker "$@" diff --git a/oshipka/persistance/__init__.py b/oshipka/persistance/__init__.py index 01171bb..9808895 100644 --- a/oshipka/persistance/__init__.py +++ b/oshipka/persistance/__init__.py @@ -10,6 +10,7 @@ from uuid import uuid4 from config import SQLALCHEMY_DATABASE_URI, MAKEDIRS, DATABASE_FILE, SEARCH_INDEX_PATH, STATIC_DATA_DIR, basepath from flask_migrate import Migrate, Manager, MigrateCommand from flask_migrate import upgrade as migrate_upgrade +from flask_migrate import migrate as migrate_migrate from flask_migrate import init as migrate_init from flask_security import RoleMixin, UserMixin from flask_security import Security, SQLAlchemyUserDatastore