Permalink
Browse files

Add alembic and initial migration, NEEDS WORK

  • Loading branch information...
1 parent 608de36 commit 7a6ce6d3275d9701479d720065c28b294eb9e7e3 @mitechie committed Jun 18, 2012
Showing with 278 additions and 0 deletions.
  1. +48 −0 alembic.ini
  2. +1 −0 dbversions/README
  3. +92 −0 dbversions/env.py
  4. +22 −0 dbversions/script.py.mako
  5. +115 −0 dbversions/versions/5920b225d05d_load_up_to_date.py
View
@@ -0,0 +1,48 @@
+# A generic, single database configuration.
+
+[alembic]
+# path to migration scripts
+script_location = dbversions
+
+# template used to generate migration files
+# file_template = %%(rev)s_%%(slug)s
+
+# set to 'true' to run the environment during
+# the 'revision' command, regardless of autogenerate
+# revision_environment = false
+
+
+# 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
View
@@ -0,0 +1 @@
+Generic single-database configuration.
View
@@ -0,0 +1,92 @@
+from __future__ import with_statement
+from alembic import context
+from ConfigParser import ConfigParser
+from os import path
+from sqlalchemy import engine_from_config, pool
+from logging.config import fileConfig
+
+from bookie.models import Base
+from bookie.models import initialize_sql
+
+
+def load_bookie_ini():
+ """Load the settings for the bookie.ini file."""
+ ini = ConfigParser()
+ ini_path = path.join(path.dirname(path.dirname(__file__)), 'bookie.ini')
+ ini.readfp(open(ini_path))
+ here = path.abspath(path.join(path.dirname(__file__), '../'))
+ ini.set('app:main', 'here', here)
+ initialize_sql(dict(ini.items("app:main")))
+ return ini
+
+# this is the Alembic Config object, which provides
+# access to the values within the .ini file in use.
+config = context.config
+bookie_config = load_bookie_ini()
+sa_url = bookie_config.get('app:main', 'sqlalchemy.url')
+config.set_main_option('sqlalchemy.url', sa_url)
+
+# 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 = Base.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)
+
+ 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
@@ -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"}
@@ -0,0 +1,115 @@
+"""load up to date
+
+Revision ID: 5920b225d05d
+Revises: None
+Create Date: 2012-06-17 21:26:51.865959
+
+"""
+
+# revision identifiers, used by Alembic.
+revision = '5920b225d05d'
+down_revision = None
+
+from alembic import op
+from datetime import datetime
+import sqlalchemy as sa
+
+
+def upgrade():
+ ### commands auto generated by Alembic - please adjust! ###
+ op.create_table('users',
+ sa.Column('id', sa.Integer(), nullable=False),
+ sa.Column('username', sa.Unicode(length=255), nullable=True),
+ sa.Column('name', sa.Unicode(length=255), nullable=True),
+ # The password field is really a mapped _password field. Think this is
+ # where the CheckConstraints come from.
+ sa.Column('password', sa.Unicode(length=60), nullable=True),
+ sa.Column('email', sa.Unicode(length=255), nullable=True),
+ sa.Column('activated', sa.Boolean(), nullable=True),
+ sa.Column('is_admin', sa.Boolean(), nullable=True),
+ sa.Column('last_login', sa.DateTime(), nullable=True),
+ sa.Column('signup', sa.DateTime(), nullable=True),
+ sa.Column('api_key', sa.Unicode(length=12), nullable=True),
+ sa.Column('invite_ct', sa.Integer(), nullable=True),
+ sa.Column('invited_by', sa.Unicode(length=255), nullable=True),
+ sa.CheckConstraint('TODO'),
+ sa.CheckConstraint('TODO'),
+ sa.PrimaryKeyConstraint('id'),
+ sa.UniqueConstraint('email'),
+ sa.UniqueConstraint('username')
+ )
+
+ op.create_table('tags',
+ sa.Column('tid', sa.Integer(), nullable=False),
+ sa.Column('name', sa.Unicode(length=255), nullable=True),
+ sa.PrimaryKeyConstraint('tid'),
+ sa.UniqueConstraint('name')
+ )
+
+ op.create_table('url_hash',
+ sa.Column('hash_id', sa.Unicode(length=22), nullable=False),
+ sa.Column('url', sa.UnicodeText(), nullable=True),
+ sa.Column('clicks', sa.Integer(), nullable=True),
+ sa.PrimaryKeyConstraint('hash_id')
+ )
+
+ op.create_table('bmarks',
+ sa.Column('bid', sa.Integer(), nullable=False),
+ sa.Column('hash_id', sa.Unicode(length=22), nullable=True),
+ sa.Column('description', sa.UnicodeText(), nullable=True),
+ sa.Column('extended', sa.UnicodeText(), nullable=True),
+ sa.Column('stored', sa.DateTime(), nullable=True),
+ sa.Column('updated', sa.DateTime(), nullable=True),
+ sa.Column('clicks', sa.Integer(), nullable=True),
+ sa.Column('inserted_by', sa.Unicode(length=255), nullable=True),
+ sa.Column('username', sa.Unicode(length=255), nullable=False),
+ sa.Column('tag_str', sa.UnicodeText(), nullable=True),
+ sa.ForeignKeyConstraint(['hash_id'], ['url_hash.hash_id'], ),
+ sa.ForeignKeyConstraint(['username'], ['users.username'], ),
+ sa.PrimaryKeyConstraint('bid'),
+ sa.UniqueConstraint('hash_id')
+ )
+
+ op.create_table(u'activations',
+ sa.Column('id', sa.Integer(), nullable=False),
+ sa.Column('code', sa.Unicode(length=60), nullable=True),
+ sa.Column('valid_until', sa.DateTime(), nullable=True),
+ sa.Column('created_by', sa.Unicode(length=255), nullable=True),
+ sa.ForeignKeyConstraint(['id'], ['users.id'], ),
+ sa.PrimaryKeyConstraint('id')
+ )
+
+ op.create_table('bmark_tags',
+ sa.Column('bmark_id', sa.Integer(), nullable=False),
+ sa.Column('tag_id', sa.Integer(), nullable=False),
+ sa.ForeignKeyConstraint(['bmark_id'], ['bmarks.bid'], ),
+ sa.ForeignKeyConstraint(['tag_id'], ['tags.tid'], ),
+ sa.PrimaryKeyConstraint('bmark_id', 'tag_id')
+ )
+
+ op.create_table('bmark_readable',
+ sa.Column('bid', sa.Integer(), nullable=False),
+ sa.Column('hash_id', sa.Unicode(length=22), nullable=True),
+ sa.Column('content', sa.UnicodeText(), nullable=True),
+ sa.Column('clean_content', sa.UnicodeText(), nullable=True),
+ sa.Column('imported', sa.DateTime(), nullable=True),
+ sa.Column('content_type', sa.Unicode(length=255), nullable=True),
+ sa.Column('status_code', sa.Integer(), nullable=True),
+ sa.Column('status_message', sa.Unicode(length=255), nullable=True),
+ sa.ForeignKeyConstraint(['bid'], ['bmarks.bid'], ),
+ sa.ForeignKeyConstraint(['hash_id'], ['bmarks.hash_id'], ),
+ sa.PrimaryKeyConstraint('bid')
+ )
+ ### end Alembic commands ###
+
+
+def downgrade():
+ ### commands auto generated by Alembic - please adjust! ###
+ op.drop_table('bmark_readable')
+ op.drop_table('bmark_tags')
+ op.drop_table(u'activations')
+ op.drop_table('bmarks')
+ op.drop_table('url_hash')
+ op.drop_table('tags')
+ op.drop_table('users')
+ ### end Alembic commands ###

0 comments on commit 7a6ce6d

Please sign in to comment.