Skip to content

Commit

Permalink
Simplify the repository structure, use flask_script
Browse files Browse the repository at this point in the history
  • Loading branch information
kaiyou committed Jun 2, 2018
1 parent d03ed10 commit 8ce4063
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 58 deletions.
3 changes: 3 additions & 0 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from flask import Flask, request, session, redirect, url_for
from flask_login import LoginManager
from flask_sqlalchemy import SQLAlchemy
from flask_script import Manager

app = Flask(__name__)
app.config.from_object('config')
Expand All @@ -15,6 +16,8 @@
login_manager.init_app(app)
db = SQLAlchemy(app)

script_manager = Manager(app)

def enable_github_oauth(GITHUB_ENABLE):
if not GITHUB_ENABLE:
return None, None
Expand Down
File renamed without changes.
8 changes: 0 additions & 8 deletions db_downgrade.py

This file was deleted.

17 changes: 0 additions & 17 deletions db_migrate.py

This file was deleted.

7 changes: 0 additions & 7 deletions db_upgrade.py

This file was deleted.

69 changes: 44 additions & 25 deletions create_db.py → manage.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,28 +1,56 @@
#!/usr/bin/env python3

import imp
import sys
import time
import os.path
import traceback

from migrate.versioning import api

from config import SQLALCHEMY_DATABASE_URI
from config import SQLALCHEMY_MIGRATE_REPO
from app import db
from app.models import Role, Setting, DomainTemplate


def start():
wait_time = get_waittime_from_env()

from app.models import Role, Setting, DomainTemplate
from app import db, script_manager


@script_manager.command
def db_downgrade():
v = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
api.downgrade(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, v - 1)
v = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
print('Current database version: ' + str(v))


@script_manager.command
def db_migrate():
v = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
migration = SQLALCHEMY_MIGRATE_REPO + ('/versions/%03d_migration.py' % (v+1))
tmp_module = imp.new_module('old_model')
old_model = api.create_model(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
exec(old_model, tmp_module.__dict__)
script = api.make_update_script_for_model(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, tmp_module.meta, db.metadata)
open(migration, "wt").write(script)
api.upgrade(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
v = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
print('New migration saved as ' + migration)
print('Current database version: ' + str(v))


@script_manager.command
def db_upgrade():
api.upgrade(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
v = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
print('Current database version: ' + str(v))


@script_manager.command
def create_db():
wait_time = int(os.environ.get('WAITFOR_DB', 1))
if not connect_db(wait_time):
print("ERROR: Couldn't connect to database server")
exit(1)

init_records()

def get_waittime_from_env():
return int(os.environ.get('WAITFOR_DB', 1))

def connect_db(wait_time):
for i in range(0, wait_time):
Expand All @@ -37,55 +65,46 @@ def connect_db(wait_time):

return False

def init_roles(db, role_names):

def init_roles(db, role_names):
# Get key name of data
name_of_roles = [r.name for r in role_names]

# Query to get current data
rows = db.session.query(Role).filter(Role.name.in_(name_of_roles)).all()
name_of_rows = [r.name for r in rows]

# Check which data that need to insert
roles = [r for r in role_names if r.name not in name_of_rows]

# Insert data
for role in roles:
db.session.add(role)

def init_settings(db, setting_names):

def init_settings(db, setting_names):
# Get key name of data
name_of_settings = [r.name for r in setting_names]

# Query to get current data
rows = db.session.query(Setting).filter(Setting.name.in_(name_of_settings)).all()

# Check which data that need to insert
name_of_rows = [r.name for r in rows]
settings = [r for r in setting_names if r.name not in name_of_rows]

# Insert data
for setting in settings:
db.session.add(setting)


def init_domain_templates(db, domain_template_names):

# Get key name of data
name_of_domain_templates = map(lambda r: r.name, domain_template_names)

# Query to get current data
rows = db.session.query(DomainTemplate).filter(DomainTemplate.name.in_(name_of_domain_templates)).all()

# Check which data that need to insert
name_of_rows = map(lambda r: r.name, rows)
domain_templates = filter(lambda r: r.name not in name_of_rows, domain_template_names)

# Insert data
for domain_template in domain_templates:
db.session.add(domain_template)


def init_records():
# Create initial user roles and turn off maintenance mode
init_roles(db, [
Expand All @@ -110,12 +129,12 @@ def init_records():
db_commit = db.session.commit()
commit_version_control(db_commit)


def commit_version_control(db_commit):
if not os.path.exists(SQLALCHEMY_MIGRATE_REPO):
api.create(SQLALCHEMY_MIGRATE_REPO, 'database repository')
api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
elif db_commit is not None:
api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, api.version(SQLALCHEMY_MIGRATE_REPO))

if __name__ == '__main__':
start()

1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Flask-WTF==0.14.2
Flask-Login==0.4.1
Flask-OAuthlib==0.9.4
Flask-SQLAlchemy==2.3.2
Flask-Script==2.0.6
SQLAlchemy==1.2.5
sqlalchemy-migrate==0.10.0
mysqlclient==1.3.12
Expand Down
2 changes: 1 addition & 1 deletion run_prod.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/sh

python create_db.py
python manage.py create_db
gunicorn -t 120 --workers 4 --bind 0.0.0.0:9191 --log-level info app:app

0 comments on commit 8ce4063

Please sign in to comment.