refactor with population

This commit is contained in:
Daniel Tsvetkov 2020-06-04 16:40:52 +02:00
parent 65035111f2
commit 0942489056
4 changed files with 49 additions and 12 deletions

View File

@ -3,6 +3,6 @@ from oshipka.persistance import populate_static, init_db
from populate import populate_db from populate import populate_db
from webapp.app import app from webapp.app import app
if init_db(app): init_db(app)
populate_static(app) populate_static(app)
populate_db(app) populate_db(app)

View File

@ -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 populate import populate_db
from webapp.app import app from webapp.app import app
if init_db(app): if init_db(app):
populate_static(app)
populate_db(app) populate_db(app)
if __name__ == "__main__": if __name__ == "__main__":
worker.main() worker.main()

View File

@ -8,12 +8,18 @@ echo "oshipka is at: $OSHIPKA_PATH"
#!/usr/bin/env bash #!/usr/bin/env bash
HELP=" 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 bootstrap [PROJECT_PATH] Create a new project in PROJECT_PATH
model [MODEL_NAME] Create or update a model
init Install dev env 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 worker Start worker
web Start webapp web Start webapp
@ -101,8 +107,8 @@ bootstrap() {
link_dev_oshipka link_dev_oshipka
source venv/bin/activate source venv/bin/activate
python manager.py db init python manager.py db init
python manager.py db migrate -m "Initial migration." python manager.py db migrate -m "001"
python init_db.py _post_migrate
python manager.py db upgrade python manager.py db upgrade
} }
@ -122,8 +128,6 @@ model() {
db_migrate() { db_migrate() {
shift shift
source venv/bin/activate source venv/bin/activate
python init_db.py
db_upgrade
python manager.py db migrate python manager.py db migrate
_post_migrate _post_migrate
} }
@ -131,13 +135,37 @@ db_migrate() {
db_upgrade() { db_upgrade() {
shift shift
source venv/bin/activate source venv/bin/activate
python init_db.py
python manager.py db upgrade 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() { _post_migrate() {
for i in migrations/versions/*.py; do for i in migrations/versions/*.py; do
sed -i "s/sqlalchemy_utils.types.choice.ChoiceType(length=255), /sa.String(), / " "$i"; sed -i "s/sqlalchemy_utils.types.choice.ChoiceType(length=255), /sa.String(), / " "$i";
sed -i "s/oshipka.persistance.LiberalBoolean(), /sa.Boolean(), / " "$i";
done done
} }
@ -152,6 +180,12 @@ command_main() {
;; ;;
db_upgrade) db_upgrade "$@" db_upgrade) db_upgrade "$@"
;; ;;
db_populate) db_populate "$@"
;;
db_recreate) db_recreate "$@"
;;
db_purge_recreate) db_purge_recreate "$@"
;;
init) init "$@" init) init "$@"
;; ;;
worker) worker "$@" worker) worker "$@"

View File

@ -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 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 Migrate, Manager, MigrateCommand
from flask_migrate import upgrade as migrate_upgrade 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_migrate import init as migrate_init
from flask_security import RoleMixin, UserMixin from flask_security import RoleMixin, UserMixin
from flask_security import Security, SQLAlchemyUserDatastore from flask_security import Security, SQLAlchemyUserDatastore