flask_migrate pain
This commit is contained in:
parent
c4bb8db789
commit
65035111f2
8
bootstrap/init_db.py
Normal file
8
bootstrap/init_db.py
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
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)
|
17
bootstrap/manager.py
Normal file
17
bootstrap/manager.py
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
from flask_migrate import MigrateCommand
|
||||||
|
from flask_script import Manager
|
||||||
|
|
||||||
|
from config import SQLALCHEMY_DATABASE_URI
|
||||||
|
from oshipka.persistance import db, migrate
|
||||||
|
from oshipka.webapp import app
|
||||||
|
|
||||||
|
app.config["SQLALCHEMY_DATABASE_URI"] = SQLALCHEMY_DATABASE_URI
|
||||||
|
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
|
||||||
|
db.init_app(app)
|
||||||
|
migrate.init_app(app, db)
|
||||||
|
manager = Manager(app)
|
||||||
|
manager.add_command('db', MigrateCommand)
|
||||||
|
from webapp.models import *
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
manager.run()
|
@ -1,6 +1,5 @@
|
|||||||
from config import TEMPLATES_FOLDER, STATIC_FOLDER
|
from config import TEMPLATES_FOLDER, STATIC_FOLDER
|
||||||
from oshipka import init_db
|
from oshipka.persistance import populate_static, init_db
|
||||||
from oshipka.persistance import populate_static
|
|
||||||
|
|
||||||
from populate import populate_db
|
from populate import populate_db
|
||||||
from webapp.app import app
|
from webapp.app import app
|
||||||
|
33
oshipka.sh
33
oshipka.sh
@ -8,7 +8,7 @@ echo "oshipka is at: $OSHIPKA_PATH"
|
|||||||
|
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
HELP="
|
HELP="
|
||||||
Usage $0 [ bootstrap | model | init | worker | web | venv | install | link | cert ]
|
Usage $0 [ bootstrap | model | db_migrate | db_upgrade | 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
|
model [MODEL_NAME] Create or update a model
|
||||||
@ -99,6 +99,11 @@ bootstrap() {
|
|||||||
cd ${PROJECT_PATH}
|
cd ${PROJECT_PATH}
|
||||||
init_venv
|
init_venv
|
||||||
link_dev_oshipka
|
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 upgrade
|
||||||
}
|
}
|
||||||
|
|
||||||
run_in_prod() {
|
run_in_prod() {
|
||||||
@ -114,6 +119,28 @@ model() {
|
|||||||
python "${OSHIPKA_PATH}/vm_gen/vm_gen.py" "`pwd`"
|
python "${OSHIPKA_PATH}/vm_gen/vm_gen.py" "`pwd`"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
db_migrate() {
|
||||||
|
shift
|
||||||
|
source venv/bin/activate
|
||||||
|
python init_db.py
|
||||||
|
db_upgrade
|
||||||
|
python manager.py db migrate
|
||||||
|
_post_migrate
|
||||||
|
}
|
||||||
|
|
||||||
|
db_upgrade() {
|
||||||
|
shift
|
||||||
|
source venv/bin/activate
|
||||||
|
python init_db.py
|
||||||
|
python manager.py db upgrade
|
||||||
|
}
|
||||||
|
|
||||||
|
_post_migrate() {
|
||||||
|
for i in migrations/versions/*.py; do
|
||||||
|
sed -i "s/sqlalchemy_utils.types.choice.ChoiceType(length=255), /sa.String(), / " "$i";
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
command_main() {
|
command_main() {
|
||||||
INITIAL_COMMAND=$1
|
INITIAL_COMMAND=$1
|
||||||
case "$INITIAL_COMMAND" in
|
case "$INITIAL_COMMAND" in
|
||||||
@ -121,6 +148,10 @@ command_main() {
|
|||||||
;;
|
;;
|
||||||
model) model "$@"
|
model) model "$@"
|
||||||
;;
|
;;
|
||||||
|
db_migrate) db_migrate "$@"
|
||||||
|
;;
|
||||||
|
db_upgrade) db_upgrade "$@"
|
||||||
|
;;
|
||||||
init) init "$@"
|
init) init "$@"
|
||||||
;;
|
;;
|
||||||
worker) worker "$@"
|
worker) worker "$@"
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
from oshipka.persistance import init_db
|
|
||||||
from oshipka.webapp import app
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
from oshipka.persistance import init_db
|
||||||
|
from oshipka.webapp import app
|
||||||
|
|
||||||
init_db(app)
|
init_db(app)
|
||||||
|
|
||||||
app.run(debug=True)
|
app.run(debug=True)
|
||||||
|
@ -7,7 +7,10 @@ from importlib import import_module
|
|||||||
from json import JSONEncoder
|
from json import JSONEncoder
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
from config import SQLALCHEMY_DATABASE_URI, MAKEDIRS, DATABASE_FILE, SEARCH_INDEX_PATH, STATIC_DATA_DIR
|
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 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
|
||||||
from flask_sqlalchemy import SQLAlchemy
|
from flask_sqlalchemy import SQLAlchemy
|
||||||
@ -20,9 +23,10 @@ from sqlalchemy_utils import Choice
|
|||||||
from tww.lib import solve_query, resolve_timezone, dt_tz_translation, time_ago
|
from tww.lib import solve_query, resolve_timezone, dt_tz_translation, time_ago
|
||||||
from whooshalchemy import IndexService
|
from whooshalchemy import IndexService
|
||||||
from oshipka.search import add_to_index, remove_from_index, query_index
|
from oshipka.search import add_to_index, remove_from_index, query_index
|
||||||
from util.strings import camel_case_to_snake_case
|
from oshipka.util.strings import camel_case_to_snake_case
|
||||||
|
|
||||||
db = SQLAlchemy()
|
db = SQLAlchemy()
|
||||||
|
migrate = Migrate()
|
||||||
|
|
||||||
|
|
||||||
class Ownable(object):
|
class Ownable(object):
|
||||||
@ -232,15 +236,19 @@ def init_db(app):
|
|||||||
app.register_blueprint(oshipka_bp)
|
app.register_blueprint(oshipka_bp)
|
||||||
|
|
||||||
db.init_app(app)
|
db.init_app(app)
|
||||||
|
migrate.init_app(app, db)
|
||||||
security.init_app(app, user_datastore)
|
security.init_app(app, user_datastore)
|
||||||
|
|
||||||
register_filters(app)
|
register_filters(app)
|
||||||
|
|
||||||
for dir in MAKEDIRS:
|
for dir in MAKEDIRS:
|
||||||
os.makedirs(dir, exist_ok=True)
|
os.makedirs(dir, exist_ok=True)
|
||||||
|
if not os.path.exists(os.path.join(basepath, 'migrations')):
|
||||||
|
with app.app_context():
|
||||||
|
migrate_init()
|
||||||
if not os.path.exists(DATABASE_FILE):
|
if not os.path.exists(DATABASE_FILE):
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
db.create_all()
|
migrate_upgrade()
|
||||||
rv = True
|
rv = True
|
||||||
global index_service
|
global index_service
|
||||||
index_service.proxied = IndexService(config=app.config, session=db.session)
|
index_service.proxied = IndexService(config=app.config, session=db.session)
|
||||||
|
@ -5,7 +5,7 @@ import inflect
|
|||||||
from flask import flash, render_template, redirect, request, url_for
|
from flask import flash, render_template, redirect, request, url_for
|
||||||
|
|
||||||
from oshipka.persistance import db
|
from oshipka.persistance import db
|
||||||
from util.strings import camel_case_to_snake_case
|
from oshipka.util.strings import camel_case_to_snake_case
|
||||||
|
|
||||||
|
|
||||||
def get_instance(model_view, uuid):
|
def get_instance(model_view, uuid):
|
||||||
|
@ -10,6 +10,7 @@ Flask-Babel==1.0.0
|
|||||||
Flask-BabelEx==0.9.4
|
Flask-BabelEx==0.9.4
|
||||||
Flask-Login==0.5.0
|
Flask-Login==0.5.0
|
||||||
Flask-Mail==0.9.1
|
Flask-Mail==0.9.1
|
||||||
|
Flask-Migrate==2.5.3
|
||||||
Flask-Principal==0.4.0
|
Flask-Principal==0.4.0
|
||||||
Flask-Security==3.0.0
|
Flask-Security==3.0.0
|
||||||
Flask-SQLAlchemy==2.4.1
|
Flask-SQLAlchemy==2.4.1
|
||||||
|
5
vm_gen/templates/_model_m_n_py
Normal file
5
vm_gen/templates/_model_m_n_py
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
[[ name ]] = db.Table('[[ name ]]',
|
||||||
|
[%- for column in columns %]
|
||||||
|
db.Column('[[ column.name ]]_id', db.Integer(), db.ForeignKey('[[ column.name ]].id')),
|
||||||
|
[%- endfor %]
|
||||||
|
)
|
26
vm_gen/templates/_model_py
Normal file
26
vm_gen/templates/_model_py
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
[%- if _choice_types %]
|
||||||
|
from sqlalchemy_utils import ChoiceType
|
||||||
|
[%- endif %]
|
||||||
|
|
||||||
|
[%- if _secondaries %]
|
||||||
|
from webapp.models import [% for secondary in _secondaries %][[ secondary ]][%- if not loop.last %], [% endif %][% endfor %]
|
||||||
|
[%- endif %]
|
||||||
|
|
||||||
|
class [[ name ]](db.Model, ModelController):
|
||||||
|
[%- include "_model_choice_header_py" %]
|
||||||
|
[%- include "_model_searchable_header_py" %]
|
||||||
|
|
||||||
|
[%- for column in columns %]
|
||||||
|
[%- if column._type == 'relationship' %]
|
||||||
|
[%- include "_relationship_py" %]
|
||||||
|
[%- else %]
|
||||||
|
[[ column.name ]] = db.Column([[ column._type ]],
|
||||||
|
[%- if column.default %]default="[[ column.default ]]",[%- endif %]
|
||||||
|
[%- if column.index %]index=True,[%- endif %])
|
||||||
|
[%- endif %]
|
||||||
|
[%- endfor %]
|
||||||
|
|
||||||
|
[%- if extra_code %]
|
||||||
|
|
||||||
|
[[ extra_code ]]
|
||||||
|
[%- endif %]
|
@ -1,34 +1,13 @@
|
|||||||
from oshipka.persistance import db, ModelController
|
from oshipka.persistance import db, ModelController
|
||||||
|
|
||||||
[%- if _choice_types %]
|
|
||||||
from sqlalchemy_utils import ChoiceType
|
|
||||||
[%- endif %]
|
|
||||||
|
|
||||||
[%- if _secondaries %]
|
|
||||||
from webapp.models import [% for secondary in _secondaries %][[ secondary ]][%- if not loop.last %], [% endif %][% endfor %]
|
|
||||||
[%- endif %]
|
|
||||||
|
|
||||||
[%- if imports %]
|
[%- if imports %]
|
||||||
[%- for import in imports %]
|
[%- for import in imports %]
|
||||||
[[ import ]]
|
[[ import ]]
|
||||||
[%- endfor %]
|
[%- endfor %]
|
||||||
[%- endif %]
|
[%- endif %]
|
||||||
|
|
||||||
class [[ name ]](db.Model, ModelController):
|
[%- if type == 'm_n' %]
|
||||||
[%- include "_model_choice_header_py" %]
|
[% include "_model_m_n_py" %]
|
||||||
[%- include "_model_searchable_header_py" %]
|
[%- else %]
|
||||||
|
[% include "_model_py" %]
|
||||||
[%- for column in columns %]
|
[%- endif %]
|
||||||
[%- if column._type == 'relationship' %]
|
|
||||||
[%- include "_relationship_py" %]
|
|
||||||
[%- else %]
|
|
||||||
[[ column.name ]] = db.Column([[ column._type ]],
|
|
||||||
[%- if column.default %]default="[[ column.default ]]",[%- endif %]
|
|
||||||
[%- if column.index %]index=True,[%- endif %])
|
|
||||||
[%- endif %]
|
|
||||||
[%- endfor %]
|
|
||||||
|
|
||||||
[%- if extra_code %]
|
|
||||||
|
|
||||||
[[ extra_code ]]
|
|
||||||
[%- endif %]
|
|
@ -1,13 +1,12 @@
|
|||||||
import os
|
import os
|
||||||
import shutil
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
import autopep8
|
import autopep8
|
||||||
import inflect
|
import inflect
|
||||||
import yaml
|
import yaml
|
||||||
from jinja2 import Environment, FileSystemLoader, select_autoescape
|
from jinja2 import Environment, FileSystemLoader
|
||||||
|
|
||||||
from util.strings import snake_case_to_camel_case, camel_case_to_snake_case
|
from oshipka.util.strings import snake_case_to_camel_case, camel_case_to_snake_case
|
||||||
|
|
||||||
pep_options = {'max_line_length': 120}
|
pep_options = {'max_line_length': 120}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user