Skip to content

Instantly share code, notes, and snippets.

@Cguilliman
Created August 20, 2019 08:03
Show Gist options
  • Save Cguilliman/46b2c6ae15d83cbd9b980ac580c813ab to your computer and use it in GitHub Desktop.
Save Cguilliman/46b2c6ae15d83cbd9b980ac580c813ab to your computer and use it in GitHub Desktop.
# run.py file
from app.app import init
if __name__ == "__main__":
init()
# app/app.py file
from sanic import Sanic
from .settings import Settings
from .database import init_database
app = Sanic(__name__)
def init():
app.config.from_object(Settings)
init_database(app)
app.run(
host="127.0.0.1",
port="9000",
debug=True,
auto_reload=True,
)
# app/database.py
from gino.ext.sanic import Gino
DB = Gino()
def init_database(app):
global DB
DB.init_app(app)
def get_alembic_db():
global DB
from users.models import *
return DB
# users.models.py file
from app.database import DB as models
class User(models.Model):
__tablename__ = "users"
id = models.Column(models.Integer, primary_key=True)
username = models.Column(models.Unicode(100), nullable=False)
password = models.Column(models.Unicode(100), nullable=False)
is_active = models.Column(models.Boolean, default=False)
# alembic.ini
# ADD:
sqlalchemy.url = postgres://admin:qweqweqwe@localhost:5432/sanic_example
# be careful, path to migration scripts
script_location = alembic
# alembic/env.py
# ADD:
import os, sys
sys.path.insert(0, os.path.realpath(os.path.join(os.path.dirname(__file__), '..')))
from app.database import get_alembic_db
# CHANGE:
target_metadata = get_alembic_db()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment