Add alembic migrations

This commit is contained in:
Mike Fedosin
2016-08-18 12:52:22 +03:00
parent a98dd16687
commit 82fa1828df
10 changed files with 508 additions and 0 deletions

80
glare/cmd/db_manage.py Executable file
View File

@@ -0,0 +1,80 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from oslo_config import cfg
from oslo_db import options
from glare.db.migration import migration
CONF = cfg.CONF
options.set_defaults(CONF)
class DBCommand(object):
def upgrade(self, config):
migration.upgrade(CONF.command.revision, config=config)
def downgrade(self, config):
migration.downgrade(CONF.command.revision, config=config)
def revision(self, config):
migration.revision(CONF.command.message,
CONF.command.autogenerate,
config=config)
def stamp(self, config):
migration.stamp(CONF.command.revision, config=config)
def version(self, config):
print(migration.version())
def add_command_parsers(subparsers):
command_object = DBCommand()
parser = subparsers.add_parser('upgrade')
parser.set_defaults(func=command_object.upgrade)
parser.add_argument('--revision', nargs='?')
parser = subparsers.add_parser('downgrade')
parser.set_defaults(func=command_object.downgrade)
parser.add_argument('--revision', nargs='?')
parser = subparsers.add_parser('stamp')
parser.add_argument('--revision', nargs='?')
parser.set_defaults(func=command_object.stamp)
parser = subparsers.add_parser('revision')
parser.add_argument('-m', '--message')
parser.add_argument('--autogenerate', action='store_true')
parser.set_defaults(func=command_object.revision)
parser = subparsers.add_parser('version')
parser.set_defaults(func=command_object.version)
command_opt = cfg.SubCommandOpt('command',
title='Command',
help='Available commands',
handler=add_command_parsers)
CONF.register_cli_opt(command_opt)
def main():
config = migration.get_alembic_config()
CONF(project='glare')
CONF.command.func(config)
if __name__ == '__main__':
main()

View File

@@ -37,6 +37,8 @@ error_map = [{'catch': store_exc.NotFound,
'raise': exception.BadRequest},
{'catch': store_exc.Duplicate,
'raise': exception.Conflict},
{'catch': store_exc.Conflict,
'raise': exception.Conflict},
{'catch': store_exc.StorageFull,
'raise': exception.Forbidden},
{'catch': store_exc.StorageWriteDenied,

View File

View File

@@ -0,0 +1,54 @@
# A generic, single database configuration.
[alembic]
# path to migration scripts
script_location = glare/db/migration/alembic_migrations
# 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
sqlalchemy.url =
# 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 File

@@ -0,0 +1,15 @@
Please see https://alembic.readthedocs.org/en/latest/index.html for general documentation
To create alembic migrations use:
$ glare-db-manage revision --message --autogenerate
Stamp db with most recent migration version, without actually running migrations
$ glare-db-manage stamp --revision head
Upgrade can be performed by:
$ glare-db-manage upgrade
$ glare-db-manage upgrade --revision head
Downgrading db:
$ glare-db-manage downgrade
$ glare-db-manage downgrade --revision base

View File

@@ -0,0 +1,45 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from alembic import context
from glare.db.sqlalchemy import api
from glare.db.sqlalchemy import models
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
target_metadata = models.BASE.metadata
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 = api.get_engine()
with engine.connect() as connection:
context.configure(connection=connection,
target_metadata=target_metadata)
with context.begin_transaction():
context.run_migrations()
run_migrations_online()

View File

@@ -0,0 +1,37 @@
# Copyright ${create_date.year} OpenStack Foundation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""${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,193 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Initial version
Revision ID: 001
Revises: None
Create Date: 2016-08-18 12:28:37.372366
"""
# revision identifiers, used by Alembic.
revision = '001'
down_revision = None
from alembic import op
import sqlalchemy as sa
MYSQL_ENGINE = 'InnoDB'
MYSQL_CHARSET = 'utf8'
def upgrade():
op.create_table(
'glare_artifacts',
sa.Column('id', sa.String(36), primary_key=True, nullable=False),
sa.Column('name', sa.String(255), nullable=False),
sa.Column('type_name', sa.String(255), nullable=False),
sa.Column('version_prefix', sa.BigInteger(), nullable=False),
sa.Column('version_suffix', sa.String(255)),
sa.Column('version_meta', sa.String(255)),
sa.Column('description', sa.Text()),
sa.Column('visibility', sa.String(32), nullable=False),
sa.Column('status', sa.String(32), nullable=False),
sa.Column('owner', sa.String(255)),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.Column('updated_at', sa.DateTime(), nullable=False),
sa.Column('activated_at', sa.DateTime()),
sa.PrimaryKeyConstraint('id'),
mysql_engine=MYSQL_ENGINE,
mysql_charset=MYSQL_CHARSET
)
op.create_index('ix_glare_artifact_name_and_version',
'glare_artifacts',
['name', 'version_prefix', 'version_suffix']
)
op.create_index('ix_glare_artifact_type',
'glare_artifacts',
['type_name']
)
op.create_index('ix_glare_artifact_status',
'glare_artifacts',
['status']
)
op.create_index('ix_glare_artifact_owner',
'glare_artifacts',
['owner']
)
op.create_index('ix_glare_artifact_visibility',
'glare_artifacts',
['visibility']
)
op.create_table(
'glare_artifact_tags',
sa.Column('id', sa.String(36), primary_key=True, nullable=False),
sa.Column('artifact_id', sa.String(36),
sa.ForeignKey('glare_artifacts.id'), nullable=False),
sa.Column('value', sa.String(255), nullable=False),
sa.PrimaryKeyConstraint('id'),
mysql_engine=MYSQL_ENGINE,
mysql_charset=MYSQL_CHARSET
)
op.create_index('ix_glare_artifact_tags_artifact_id',
'glare_artifact_tags',
['artifact_id']
)
op.create_index('ix_glare_artifact_tags_artifact_id_tag_value',
'glare_artifact_tags',
['artifact_id', 'value']
)
op.create_table(
'glare_artifact_blobs',
sa.Column('id', sa.String(36), primary_key=True, nullable=False),
sa.Column('artifact_id', sa.String(36),
sa.ForeignKey('glare_artifacts.id'), nullable=False),
sa.Column('size', sa.BigInteger()),
sa.Column('checksum', sa.String(32)),
sa.Column('name', sa.String(255), nullable=False),
sa.Column('status', sa.String(32), nullable=False),
sa.Column('external', sa.Boolean()),
sa.Column('url', sa.Text()),
sa.Column('key_name', sa.String(255)),
sa.Column('content_type', sa.String(255)),
sa.PrimaryKeyConstraint('id'),
mysql_engine=MYSQL_ENGINE,
mysql_charset=MYSQL_CHARSET
)
op.create_index('ix_glare_artifact_blobs_artifact_id',
'glare_artifact_blobs',
['artifact_id']
)
op.create_index('ix_glare_artifact_blobs_name',
'glare_artifact_blobs',
['name']
)
op.create_table(
'glare_artifact_properties',
sa.Column('id', sa.String(36), primary_key=True, nullable=False),
sa.Column('artifact_id', sa.String(36),
sa.ForeignKey('glare_artifacts.id'), nullable=False),
sa.Column('name', sa.String(255), nullable=False),
sa.Column('string_value', sa.String(20000)),
sa.Column('int_value', sa.Integer()),
sa.Column('numeric_value', sa.Numeric()),
sa.Column('bool_value', sa.Boolean()),
sa.Column('position', sa.Integer()),
sa.Column('key_name', sa.String(255)),
sa.PrimaryKeyConstraint('id'),
mysql_engine=MYSQL_ENGINE,
mysql_charset=MYSQL_CHARSET
)
op.create_index('ix_glare_artifact_properties_artifact_id',
'glare_artifact_properties',
['artifact_id']
)
op.create_index('ix_glare_artifact_properties_name',
'glare_artifact_properties',
['name']
)
op.create_table(
'glare_artifact_locks',
sa.Column('id', sa.String(36), primary_key=True, nullable=False),
sa.PrimaryKeyConstraint('id'),
mysql_engine=MYSQL_ENGINE,
mysql_charset=MYSQL_CHARSET
)
# end Alembic commands #
def downgrade():
op.drop_index('ix_glare_artifact_properties_name',
table_name='glare_artifact_properties')
op.drop_index('ix_glare_artifact_properties_artifact_id',
table_name='glare_artifact_properties')
op.drop_index('ix_glare_artifact_blobs_name',
table_name='glare_artifact_blobs')
op.drop_index('ix_glare_artifact_blobs_artifact_id',
table_name='glare_artifact_blobs')
op.drop_index('ix_glare_artifact_tags_artifact_id_tag_value',
table_name='glare_artifact_tags')
op.drop_index('ix_glare_artifact_tags_artifact_id',
table_name='glare_artifact_tags')
op.drop_index('ix_glare_artifact_visibility',
table_name='glare_artifacts')
op.drop_index('ix_glare_artifact_owner',
table_name='glare_artifacts')
op.drop_index('ix_glare_artifact_status',
table_name='glare_artifacts')
op.drop_index('ix_glare_artifact_type',
table_name='glare_artifacts')
op.drop_index('ix_glare_artifact_name_and_version',
table_name='glare_artifacts')
op.drop_table('glare_artifact_locks')
op.drop_table('glare_artifact_properties')
op.drop_table('glare_artifact_blobs')
op.drop_table('glare_artifact_tags')
op.drop_table('glare_artifacts')
# end Alembic commands #

View File

@@ -0,0 +1,81 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import os
import alembic
from alembic import config as alembic_config
from alembic import migration as alembic_migration
from glare.db.sqlalchemy import api as db_api
def get_alembic_config():
path = os.path.join(os.path.dirname(__file__), 'alembic.ini')
config = alembic_config.Config(path)
config.set_main_option('script_location',
'glare.db.migration:alembic_migrations')
return config
def version(engine=None):
"""Returns current database version."""
engine = engine or db_api.get_engine()
with engine.connect() as conn:
context = alembic_migration.MigrationContext.configure(conn)
return context.get_current_revision()
def upgrade(revision, config=None):
"""Used for upgrading database.
:param version: Desired database version
:type version: string
"""
revision = revision or 'head'
config = config or get_alembic_config()
alembic.command.upgrade(config, revision or 'head')
def downgrade(revision, config=None):
"""Used for downgrading database.
:param version: Desired database version7
:type version: string
"""
revision = revision or 'base'
config = config or get_alembic_config()
return alembic.command.downgrade(config, revision)
def stamp(revision, config=None):
"""Stamps database with provided revision.
Don't run any migrations.
:param revision: Should match one from repository or head - to stamp
database with most recent revision
:type revision: string
"""
config = config or get_alembic_config()
return alembic.command.stamp(config, revision=revision)
def revision(message=None, autogenerate=False, config=None):
"""Creates template for migration.
:param message: Text that will be used for migration title
:type message: string
:param autogenerate: If True - generates diff based on current database
state
:type autogenerate: bool
"""
config = config or get_alembic_config()
return alembic.command.revision(config, message=message,
autogenerate=autogenerate)

View File

@@ -6,6 +6,7 @@ pbr>=1.6 # Apache-2.0
# < 0.8.0/0.8 does not work, see https://bugs.launchpad.net/bugs/1153983
SQLAlchemy<1.1.0,>=1.0.10 # MIT
alembic>=0.8.4 # MIT
eventlet!=0.18.3,>=0.18.2 # MIT
PasteDeploy>=1.5.0 # MIT
Routes!=2.0,!=2.1,!=2.3.0,>=1.12.3;python_version=='2.7' # MIT