Get alembic up and running

I took a stab at a reasonable schema just to get started. For now I want
workflows that are working. I'll pound the thing into the correct shape
in time
This commit is contained in:
Eli Ribble 2016-05-02 08:44:54 -06:00
parent 3e4eab0802
commit 681a62bbf5
7 changed files with 304 additions and 0 deletions

68
alembic.ini Normal file
View File

@ -0,0 +1,68 @@
# A generic, single database configuration.
[alembic]
# path to migration scripts
script_location = alembic
# template used to generate migration files
# file_template = %%(rev)s_%%(slug)s
# max length of characters to apply to the
# "slug" field
#truncate_slug_length = 40
# set to 'true' to run the environment during
# the 'revision' command, regardless of autogenerate
# revision_environment = false
# set to 'true' to allow .pyc and .pyo files without
# a source .py file to be detected as revisions in the
# versions/ directory
# sourceless = false
# version location specification; this defaults
# to alembic/versions. When using multiple version
# directories, initial revisions must be specified with --version-path
# version_locations = %(here)s/bar %(here)s/bat alembic/versions
# the output encoding used when revision files
# are written from script.py.mako
# output_encoding = utf-8
sqlalchemy.url = postgresql://vanth_dev:letmein@localhost/vanth
# Logging configuration
[loggers]
keys = root,sqlalchemy,alembic
[handlers]
keys = console
[formatters]
keys = generic
[logger_root]
level = WARN
handlers = console
qualname =
[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine
[logger_alembic]
level = INFO
handlers =
qualname = alembic
[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic
[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S

1
alembic/README Normal file
View File

@ -0,0 +1 @@
Generic single-database configuration.

71
alembic/env.py Normal file
View File

@ -0,0 +1,71 @@
from __future__ import with_statement
from alembic import context
from sqlalchemy import engine_from_config, pool
from logging.config import fileConfig
from vanth.tables import metadata
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
# Interpret the config file for Python logging.
# This line sets up loggers basically.
fileConfig(config.config_file_name)
# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
target_metadata = metadata
# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.
def run_migrations_offline():
"""Run migrations in 'offline' mode.
This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script output.
"""
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url, target_metadata=target_metadata, literal_binds=True)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online():
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
connectable = engine_from_config(
config.get_section(config.config_ini_section),
prefix='sqlalchemy.',
poolclass=pool.NullPool)
with connectable.connect() as connection:
context.configure(
connection=connection,
target_metadata=target_metadata
)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()

24
alembic/script.py.mako Normal file
View File

@ -0,0 +1,24 @@
"""${message}
Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}
"""
# revision identifiers, used by Alembic.
revision = ${repr(up_revision)}
down_revision = ${repr(down_revision)}
branch_labels = ${repr(branch_labels)}
depends_on = ${repr(depends_on)}
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}
def upgrade():
${upgrades if upgrades else "pass"}
def downgrade():
${downgrades if downgrades else "pass"}

View File

@ -0,0 +1,66 @@
"""create initial tables
Revision ID: 58ea73d3d07b
Revises:
Create Date: 2016-05-02 08:42:38.061749
"""
# revision identifiers, used by Alembic.
revision = '58ea73d3d07b'
down_revision = None
branch_labels = None
depends_on = None
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
def upgrade():
op.create_table('credit_card',
sa.Column('uuid', postgresql.UUID(as_uuid=True), nullable=False),
sa.Column('brand', sa.String(length=20), nullable=False),
sa.Column('card_id', sa.String(length=100), nullable=False),
sa.Column('country', sa.String(length=1024), nullable=False),
sa.Column('cvc_check', sa.String(length=100), nullable=False),
sa.Column('expiration_month', sa.Integer(), nullable=False),
sa.Column('expiration_year', sa.Integer(), nullable=False),
sa.Column('last_four', sa.Integer(), nullable=False),
sa.Column('token', sa.String(), nullable=False),
sa.Column('user_uri', sa.String(length=2048), nullable=False),
sa.Column('created', sa.DateTime(), server_default=sa.text('now()'), nullable=False),
sa.Column('updated', sa.DateTime(), server_default=sa.text('now()'), nullable=False),
sa.Column('deleted', sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint('uuid', name=op.f('pk_credit_card'))
)
op.create_table('ofxrecord',
sa.Column('uuid', postgresql.UUID(as_uuid=True), nullable=False),
sa.Column('fid', sa.String(length=255), nullable=False),
sa.Column('amount', sa.Float(), nullable=False),
sa.Column('available', sa.Date(), nullable=True),
sa.Column('name', sa.String(length=1024), nullable=False),
sa.Column('posted', sa.Date(), nullable=True),
sa.Column('memo', sa.String(length=2048), nullable=True),
sa.Column('type', sa.String(length=255), nullable=True),
sa.Column('created', sa.DateTime(), server_default=sa.text('now()'), nullable=False),
sa.Column('updated', sa.DateTime(), server_default=sa.text('now()'), nullable=False),
sa.PrimaryKeyConstraint('uuid', name=op.f('pk_ofxrecord'))
)
op.create_table('ofxsource',
sa.Column('uuid', postgresql.UUID(as_uuid=True), nullable=False),
sa.Column('name', sa.String(length=255), nullable=False),
sa.Column('fid', sa.String(length=255), nullable=False),
sa.Column('bankid', sa.String(length=255), nullable=False),
sa.Column('created', sa.DateTime(), server_default=sa.text('now()'), nullable=False),
sa.Column('updated', sa.DateTime(), server_default=sa.text('now()'), nullable=False),
sa.PrimaryKeyConstraint('uuid', name=op.f('pk_ofxsource'))
)
op.create_table('ofxaccount',
)
def downgrade():
op.drop_table('ofxaccount')
op.drop_table('ofxsource')
op.drop_table('ofxrecord')
op.drop_table('credit_card')

20
bin/setup_dev.sh Executable file
View File

@ -0,0 +1,20 @@
#!/bin/bash
name=${PWD##*/}
name=${name//[-._]/}
printf 'Setting up %s\n' $name
printf "...Creating databases\n"
dropdb ${name}
dropdb ${name}_test
createdb ${name}
createdb ${name}_test
psql -d ${name} -c "CREATE EXTENSION IF NOT EXISTS pgcrypto;"
psql -d ${name}_test -c "CREATE EXTENSION IF NOT EXISTS pgcrypto;"
psql -d ${name} -c "CREATE USER vanth_dev WITH PASSWORD 'letmein';"
psql -d ${name} -c "GRANT ALL PRIVILEGES ON DATABASE ${name} to dev;"
psql -d ${name}_test -c "GRANT ALL PRIVILEGES ON DATABASE ${name}_test to dev;"

54
vanth/tables.py Normal file
View File

@ -0,0 +1,54 @@
from sqlalchemy import Boolean, Column, Date, DateTime, Float, ForeignKey, Integer, MetaData, String, Table, func
from sqlalchemy.dialects.postgresql import UUID, ENUM
from sqlalchemy.dialects import postgres
import chryso.constants
metadata = MetaData(naming_convention=chryso.constants.CONVENTION)
CreditCard = Table('credit_card', metadata,
Column('uuid', UUID(as_uuid=True), primary_key=True),
Column('brand', String(20), nullable=False), # The brand of the card, like 'visa'
Column('card_id', String(100), nullable=False), # The ID of the card from Stripe
Column('country', String(1024), nullable=False), # The Country of the card, like 'US'
Column('cvc_check', String(100), nullable=False), # The CVC value from Stripe, like 'unchecked'
Column('expiration_month', Integer(), nullable=False), # The month the card expires
Column('expiration_year', Integer(), nullable=False), # The year the card expires
Column('last_four', Integer(), nullable=False), # The last four digits of the card
Column('token', String(), nullable=False), # The token we can use with Stripe to do stuff
Column('user_uri', String(2048), nullable=False), # The URI of the user that created the record
Column('created', DateTime(), nullable=False, server_default=func.now()),
Column('updated', DateTime(), nullable=False, server_default=func.now(), onupdate=func.now()),
Column('deleted', DateTime(), nullable=True),
)
OFXSource = Table('ofxsource', metadata,
Column('uuid', UUID(as_uuid=True), primary_key=True),
Column('name', String(255), nullable=False), # The name of the institution such as 'America First Credit Union'
Column('fid', String(255), nullable=False), # The FID of the institution, such as 54324
Column('bankid', String(255), nullable=False), # The bank ID of the institution such as 324377516
Column('created', DateTime(), nullable=False, server_default=func.now()),
Column('updated', DateTime(), nullable=False, server_default=func.now(), onupdate=func.now()),
)
OFXAccount = Table('ofxaccount', metadata,
Column('uuid', UUID(as_uuid=True), primary_key=True),
Column('user_id', String(255), nullable=False), # The user ID for the bank
Column('password', String(255), nullable=False), # The encrypted password for the account
Column('type', String(255), nullable=False), # The account type, like 'checking'
Column('source', None, ForeignKey(OFXSource.c.uuid, name='fk_ofxsource'), nullable=False),
Column('created', DateTime(), nullable=False, server_default=func.now()),
Column('updated', DateTime(), nullable=False, server_default=func.now(), onupdate=func.now()),
)
OFXRecord = Table('ofxrecord', metadata,
Column('uuid', UUID(as_uuid=True), primary_key=True),
Column('fid', String(255), nullable=False), # The Financial institution's ID
Column('amount', Float(), nullable=False), # The amount of the record, like -177.91
Column('available', Date(), nullable=True), # The date the record was available
Column('name', String(1024), nullable=False), # The name of the record, like 'UT SLC SAMSCLUB #4719'
Column('posted', Date(), nullable=True), # The date the record posted
Column('memo', String(2048), nullable=True), # The memo of the transaction, like 'POINT OF SALE PURCHASE #0005727'
Column('type', String(255), nullable=True), # The type of the record, like 'POS'
Column('created', DateTime(), nullable=False, server_default=func.now()),
Column('updated', DateTime(), nullable=False, server_default=func.now(), onupdate=func.now()),
)