Commit a23463ba authored by Lukáš Lalinský's avatar Lukáš Lalinský

Alembic migrations

parent 6aaba47f
......@@ -35,8 +35,8 @@ class DatabaseConfig(object):
kwargs['password'] = self.password
return URL('postgresql', **kwargs)
def create_engine(self, superuser=False):
return create_engine(self.create_url(superuser=superuser))
def create_engine(self, superuser=False, **kwargs):
return create_engine(self.create_url(superuser=superuser), **kwargs)
def create_psql_args(self, superuser=False):
args = []
......
This diff is collapsed.
- git:
repo=https://bitbucket.org/acoustid/pg_acoustid.git
dest=/tmp/pg_acoustid
register: pg_acoustid_git
- command: make install clean
args:
chdir: /tmp/pg_acoustid
when: pg_acoustid_git.changed
#!/usr/bin/env python
from sqlalchemy import MetaData
from acoustid.script import run_script
......@@ -7,7 +8,10 @@ def main(script, opts, args):
from acoustid.tables import metadata
with script.engine.connect() as conn:
with conn.begin():
metadata.create_all(conn)
old_metadata = MetaData(conn)
old_metadata.reflect()
old_metadata.drop_all()
# metadata.create_all(conn)
run_script(main)
# 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
# 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
Generic single-database configuration.
\ No newline at end of file
from __future__ import with_statement
import os
from alembic import context
from sqlalchemy import engine_from_config, pool
from logging.config import fileConfig
config = context.config
fileConfig(config.config_file_name)
import acoustid.tables
target_metadata = acoustid.tables.metadata
import acoustid.config
acoustid_config = acoustid.config.Config(os.path.join(os.path.dirname(__file__), '..', 'acoustid.conf'))
def include_object(obj, name, type, reflected, compare_to):
if type == "table" and not obj.schema:
return True
if type == "column" and not obj.table.schema:
return True
return False
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 = acoustid_config.database.create_url()
context.configure(
url=url, target_metadata=target_metadata, literal_binds=True,
include_object=include_object)
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 = acoustid_config.database.create_engine(poolclass=pool.NullPool)
with connectable.connect() as connection:
context.configure(
connection=connection,
target_metadata=target_metadata,
include_object=include_object,
)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
"""${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"}
This diff is collapsed.
......@@ -14,3 +14,4 @@ ndg-httpsclient
pyasn1
pychromaprint>=1.1.1
mbdata==2015.6.20
alembic>=0.8.4
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment