Use alembic

Change-Id: I9078a34a7a53f0eabfe7523e23eaefc17f206636
This commit is contained in:
James E. Blair 2014-05-04 20:59:07 -07:00
parent 53b3bed5bb
commit ff8506a32b
6 changed files with 354 additions and 1 deletions

59
gertty/alembic.ini Normal file
View File

@ -0,0 +1,59 @@
# 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
sqlalchemy.url = sqlite:////tmp/gertty.db
# 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
gertty/alembic/README Normal file
View File

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

72
gertty/alembic/env.py Normal file
View File

@ -0,0 +1,72 @@
from __future__ import with_statement
from alembic import context
from sqlalchemy import engine_from_config, pool
#from logging.config import fileConfig
# 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
import gertty.db
target_metadata = gertty.db.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)
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.
"""
engine = engine_from_config(
config.get_section(config.config_ini_section),
prefix='sqlalchemy.',
poolclass=pool.NullPool)
connection = engine.connect()
context.configure(
connection=connection,
target_metadata=target_metadata
)
try:
with context.begin_transaction():
context.run_migrations()
finally:
connection.close()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()

View File

@ -0,0 +1,22 @@
"""${message}
Revision ID: ${up_revision}
Revises: ${down_revision}
Create Date: ${create_date}
"""
# revision identifiers, used by Alembic.
revision = ${repr(up_revision)}
down_revision = ${repr(down_revision)}
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,176 @@
"""Initial schema
Revision ID: 44402069e137
Revises: None
Create Date: 2014-05-04 17:10:23.127702
"""
# revision identifiers, used by Alembic.
revision = '44402069e137'
down_revision = None
from alembic import op
import sqlalchemy as sa
def upgrade():
### commands auto generated by Alembic - please adjust! ###
op.create_table('project',
sa.Column('key', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=255), nullable=False),
sa.Column('subscribed', sa.Boolean(), nullable=True),
sa.Column('description', sa.Text(), nullable=False),
sa.PrimaryKeyConstraint('key')
)
op.create_index(op.f('ix_project_name'), 'project', ['name'], unique=True)
op.create_index(op.f('ix_project_subscribed'), 'project', ['subscribed'], unique=False)
op.create_table('change',
sa.Column('key', sa.Integer(), nullable=False),
sa.Column('project_key', sa.Integer(), nullable=True),
sa.Column('id', sa.String(length=255), nullable=False),
sa.Column('number', sa.Integer(), nullable=False),
sa.Column('branch', sa.String(length=255), nullable=False),
sa.Column('change_id', sa.String(length=255), nullable=False),
sa.Column('topic', sa.String(length=255), nullable=True),
sa.Column('owner', sa.String(length=255), nullable=True),
sa.Column('subject', sa.Text(), nullable=False),
sa.Column('created', sa.DateTime(), nullable=False),
sa.Column('updated', sa.DateTime(), nullable=False),
sa.Column('status', sa.String(length=8), nullable=False),
sa.Column('hidden', sa.Boolean(), nullable=False),
sa.Column('reviewed', sa.Boolean(), nullable=False),
sa.ForeignKeyConstraint(['project_key'], ['project.key'], ),
sa.PrimaryKeyConstraint('key')
)
op.create_index(op.f('ix_change_branch'), 'change', ['branch'], unique=False)
op.create_index(op.f('ix_change_change_id'), 'change', ['change_id'], unique=False)
op.create_index(op.f('ix_change_created'), 'change', ['created'], unique=False)
op.create_index(op.f('ix_change_hidden'), 'change', ['hidden'], unique=False)
op.create_index(op.f('ix_change_id'), 'change', ['id'], unique=True)
op.create_index(op.f('ix_change_number'), 'change', ['number'], unique=True)
op.create_index(op.f('ix_change_owner'), 'change', ['owner'], unique=False)
op.create_index(op.f('ix_change_project_key'), 'change', ['project_key'], unique=False)
op.create_index(op.f('ix_change_reviewed'), 'change', ['reviewed'], unique=False)
op.create_index(op.f('ix_change_status'), 'change', ['status'], unique=False)
op.create_index(op.f('ix_change_topic'), 'change', ['topic'], unique=False)
op.create_index(op.f('ix_change_updated'), 'change', ['updated'], unique=False)
op.create_table('approval',
sa.Column('key', sa.Integer(), nullable=False),
sa.Column('change_key', sa.Integer(), nullable=True),
sa.Column('name', sa.String(length=255), nullable=True),
sa.Column('category', sa.String(length=255), nullable=False),
sa.Column('value', sa.Integer(), nullable=False),
sa.Column('pending', sa.Boolean(), nullable=False),
sa.ForeignKeyConstraint(['change_key'], ['change.key'], ),
sa.PrimaryKeyConstraint('key')
)
op.create_index(op.f('ix_approval_change_key'), 'approval', ['change_key'], unique=False)
op.create_index(op.f('ix_approval_pending'), 'approval', ['pending'], unique=False)
op.create_table('revision',
sa.Column('key', sa.Integer(), nullable=False),
sa.Column('change_key', sa.Integer(), nullable=True),
sa.Column('number', sa.Integer(), nullable=False),
sa.Column('message', sa.Text(), nullable=False),
sa.Column('commit', sa.String(length=255), nullable=False),
sa.Column('parent', sa.String(length=255), nullable=False),
sa.ForeignKeyConstraint(['change_key'], ['change.key'], ),
sa.PrimaryKeyConstraint('key')
)
op.create_index(op.f('ix_revision_change_key'), 'revision', ['change_key'], unique=False)
op.create_index(op.f('ix_revision_number'), 'revision', ['number'], unique=False)
op.create_table('label',
sa.Column('key', sa.Integer(), nullable=False),
sa.Column('change_key', sa.Integer(), nullable=True),
sa.Column('category', sa.String(length=255), nullable=False),
sa.Column('value', sa.Integer(), nullable=False),
sa.Column('description', sa.String(length=255), nullable=False),
sa.ForeignKeyConstraint(['change_key'], ['change.key'], ),
sa.PrimaryKeyConstraint('key')
)
op.create_index(op.f('ix_label_change_key'), 'label', ['change_key'], unique=False)
op.create_table('permitted_label',
sa.Column('key', sa.Integer(), nullable=False),
sa.Column('change_key', sa.Integer(), nullable=True),
sa.Column('category', sa.String(length=255), nullable=False),
sa.Column('value', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['change_key'], ['change.key'], ),
sa.PrimaryKeyConstraint('key')
)
op.create_index(op.f('ix_permitted_label_change_key'), 'permitted_label', ['change_key'], unique=False)
op.create_table('comment',
sa.Column('key', sa.Integer(), nullable=False),
sa.Column('revision_key', sa.Integer(), nullable=True),
sa.Column('id', sa.String(length=255), nullable=True),
sa.Column('in_reply_to', sa.String(length=255), nullable=True),
sa.Column('created', sa.DateTime(), nullable=False),
sa.Column('name', sa.String(length=255), nullable=True),
sa.Column('file', sa.Text(), nullable=False),
sa.Column('parent', sa.Boolean(), nullable=False),
sa.Column('line', sa.Integer(), nullable=True),
sa.Column('message', sa.Text(), nullable=False),
sa.Column('pending', sa.Boolean(), nullable=False),
sa.ForeignKeyConstraint(['revision_key'], ['revision.key'], ),
sa.PrimaryKeyConstraint('key')
)
op.create_index(op.f('ix_comment_created'), 'comment', ['created'], unique=False)
op.create_index(op.f('ix_comment_id'), 'comment', ['id'], unique=False)
op.create_index(op.f('ix_comment_pending'), 'comment', ['pending'], unique=False)
op.create_index(op.f('ix_comment_revision_key'), 'comment', ['revision_key'], unique=False)
op.create_table('message',
sa.Column('key', sa.Integer(), nullable=False),
sa.Column('revision_key', sa.Integer(), nullable=True),
sa.Column('id', sa.String(length=255), nullable=True),
sa.Column('created', sa.DateTime(), nullable=False),
sa.Column('name', sa.String(length=255), nullable=True),
sa.Column('message', sa.Text(), nullable=False),
sa.Column('pending', sa.Boolean(), nullable=False),
sa.ForeignKeyConstraint(['revision_key'], ['revision.key'], ),
sa.PrimaryKeyConstraint('key')
)
op.create_index(op.f('ix_message_created'), 'message', ['created'], unique=False)
op.create_index(op.f('ix_message_id'), 'message', ['id'], unique=False)
op.create_index(op.f('ix_message_pending'), 'message', ['pending'], unique=False)
op.create_index(op.f('ix_message_revision_key'), 'message', ['revision_key'], unique=False)
### end Alembic commands ###
def downgrade():
### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f('ix_message_revision_key'), table_name='message')
op.drop_index(op.f('ix_message_pending'), table_name='message')
op.drop_index(op.f('ix_message_id'), table_name='message')
op.drop_index(op.f('ix_message_created'), table_name='message')
op.drop_table('message')
op.drop_index(op.f('ix_comment_revision_key'), table_name='comment')
op.drop_index(op.f('ix_comment_pending'), table_name='comment')
op.drop_index(op.f('ix_comment_id'), table_name='comment')
op.drop_index(op.f('ix_comment_created'), table_name='comment')
op.drop_table('comment')
op.drop_index(op.f('ix_permitted_label_change_key'), table_name='permitted_label')
op.drop_table('permitted_label')
op.drop_index(op.f('ix_label_change_key'), table_name='label')
op.drop_table('label')
op.drop_index(op.f('ix_revision_number'), table_name='revision')
op.drop_index(op.f('ix_revision_change_key'), table_name='revision')
op.drop_table('revision')
op.drop_index(op.f('ix_approval_pending'), table_name='approval')
op.drop_index(op.f('ix_approval_change_key'), table_name='approval')
op.drop_table('approval')
op.drop_index(op.f('ix_change_updated'), table_name='change')
op.drop_index(op.f('ix_change_topic'), table_name='change')
op.drop_index(op.f('ix_change_status'), table_name='change')
op.drop_index(op.f('ix_change_reviewed'), table_name='change')
op.drop_index(op.f('ix_change_project_key'), table_name='change')
op.drop_index(op.f('ix_change_owner'), table_name='change')
op.drop_index(op.f('ix_change_number'), table_name='change')
op.drop_index(op.f('ix_change_id'), table_name='change')
op.drop_index(op.f('ix_change_hidden'), table_name='change')
op.drop_index(op.f('ix_change_created'), table_name='change')
op.drop_index(op.f('ix_change_change_id'), table_name='change')
op.drop_index(op.f('ix_change_branch'), table_name='change')
op.drop_table('change')
op.drop_index(op.f('ix_project_subscribed'), table_name='project')
op.drop_index(op.f('ix_project_name'), table_name='project')
op.drop_table('project')
### end Alembic commands ###

View File

@ -12,6 +12,10 @@
# License for the specific language governing permissions and limitations
# under the License.
import logging
import alembic
import alembic.config
import sqlalchemy
from sqlalchemy import create_engine, MetaData, Table, Column, Integer, String, Boolean, DateTime, Text, select, func
from sqlalchemy.schema import ForeignKey
@ -331,15 +335,34 @@ mapper(Approval, approval_table)
class Database(object):
def __init__(self, app):
self.log = logging.getLogger('gertty.db')
self.app = app
self.engine = create_engine(self.app.config.dburi)
metadata.create_all(self.engine)
#metadata.create_all(self.engine)
self.migrate()
self.session_factory = sessionmaker(bind=self.engine)
self.session = scoped_session(self.session_factory)
def getSession(self):
return DatabaseSession(self.session)
def migrate(self):
conn = self.engine.connect()
context = alembic.migration.MigrationContext.configure(conn)
current_rev = context.get_current_revision()
self.log.debug('Current migration revision: %s' % current_rev)
has_table = self.engine.dialect.has_table(conn, "project")
config = alembic.config.Config()
config.set_main_option("script_location", "gertty:alembic")
config.set_main_option("sqlalchemy.url", self.app.config.dburi)
if current_rev is None and has_table:
self.log.debug('Stamping database as initial revision')
alembic.command.stamp(config, "44402069e137")
alembic.command.upgrade(config, 'head')
class DatabaseSession(object):
def __init__(self, session):
self.session = session