예제 #1
0
def sa_table():
    choices = ['a', 'b', 'c']
    meta = sa.MetaData()
    post = sa.Table(
        'test_post', meta,
        sa.Column('id', sa.Integer, nullable=False),
        sa.Column('title', sa.String(200), nullable=False),
        sa.Column('category', sa.String(200), nullable=True),
        sa.Column('body', sa.Text, nullable=False),
        sa.Column('views', sa.Integer, nullable=False),
        sa.Column('average_note', sa.Float, nullable=False),
        # sa.Column('pictures', postgresql.JSON, server_default='{}'),
        sa.Column('published_at', sa.DateTime, nullable=False),
        # sa.Column('tags', postgresql.ARRAY(sa.Integer), server_default='{}'),
        sa.Column('status',
                  sa.Enum(*choices, name="enum_name", native_enum=False),
                  server_default="a", nullable=False),
        sa.Column('visible', sa.Boolean, nullable=False),

        # Indexes #
        sa.PrimaryKeyConstraint('id', name='post_id_pkey'))
    return post
예제 #2
0
파일: db.py 프로젝트: fitahol/aiohttprest
"""

__created__ = '06/01/2017'
__author__ = 'deling.ma'
"""
import aioodbc
import aiomysql.sa
import sqlalchemy as sa


async def init_db(app):
    db_conf = app['config']['db']
    engine = await aiomysql.sa.create_engine(db=db_conf['database'],
                                             user=db_conf['user'],
                                             password=db_conf['password'],
                                             host=db_conf['host'],
                                             port=db_conf['port'],
                                             loop=app.loop)
    if "sqlite" in db_conf["engine"]:
        dsn = 'Driver=SQLite;Database=%s' % db_conf['database']
        engine = await aioodbc.create_pool(dsn=dsn, loop=app.loop)
    app['db'] = engine


async def close_db(app):
    app['db'].close()
    await app['db'].wait_closed()


meta = sa.MetaData()