Add db migrations
This patch adds support for db migrations, it creates inital migration with higgins_service table. Change-Id: I6adad5f1d00e25ca38e9ba592d0d314b1b6cc74f
This commit is contained in:
parent
8ac9576d1f
commit
e97835f627
@ -13,6 +13,7 @@
|
||||
# under the License.
|
||||
|
||||
from oslo_config import cfg
|
||||
from oslo_db import options
|
||||
|
||||
sql_opts = [
|
||||
cfg.StrOpt('mysql_engine',
|
||||
@ -21,3 +22,4 @@ sql_opts = [
|
||||
]
|
||||
|
||||
cfg.CONF.register_opts(sql_opts, 'database')
|
||||
options.set_defaults(cfg.CONF)
|
||||
|
68
higgins/db/sqlalchemy/alembic.ini
Normal file
68
higgins/db/sqlalchemy/alembic.ini
Normal file
@ -0,0 +1,68 @@
|
||||
# A generic, single database configuration.
|
||||
|
||||
[alembic]
|
||||
# path to migration scripts
|
||||
script_location = %(here)s/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 = driver://user:pass@localhost/dbname
|
||||
|
||||
|
||||
# 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
|
11
higgins/db/sqlalchemy/alembic/README
Normal file
11
higgins/db/sqlalchemy/alembic/README
Normal file
@ -0,0 +1,11 @@
|
||||
Please see https://alembic.readthedocs.org/en/latest/index.html for general documentation
|
||||
|
||||
To create alembic migrations use:
|
||||
$ higgins-db-manage revision --message "description of revision" --autogenerate
|
||||
|
||||
Stamp db with most recent migration version, without actually running migrations
|
||||
$ higgins-db-manage stamp head
|
||||
|
||||
Upgrade can be performed by:
|
||||
$ higgins-db-manage upgrade
|
||||
$ higgins-db-manage upgrade head
|
57
higgins/db/sqlalchemy/alembic/env.py
Normal file
57
higgins/db/sqlalchemy/alembic/env.py
Normal file
@ -0,0 +1,57 @@
|
||||
# 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 __future__ import with_statement
|
||||
from alembic import context
|
||||
from logging.config import fileConfig
|
||||
|
||||
from higgins.db.sqlalchemy import api as sqla_api
|
||||
from higgins.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
|
||||
|
||||
# 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 = models.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_online():
|
||||
"""Run migrations in 'online' mode.
|
||||
|
||||
In this scenario we need to create an Engine
|
||||
and associate a connection with the context.
|
||||
|
||||
"""
|
||||
engine = sqla_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()
|
20
higgins/db/sqlalchemy/alembic/script.py.mako
Normal file
20
higgins/db/sqlalchemy/alembic/script.py.mako
Normal file
@ -0,0 +1,20 @@
|
||||
"""${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"}
|
@ -0,0 +1,48 @@
|
||||
# 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.
|
||||
|
||||
|
||||
"""create_table_higgins_service
|
||||
|
||||
Revision ID: a9a92eebd9a8
|
||||
Revises:
|
||||
Create Date: 2016-05-27 10:53:10.367610
|
||||
|
||||
"""
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = 'a9a92eebd9a8'
|
||||
down_revision = None
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
def upgrade():
|
||||
op.create_table(
|
||||
'higgins_service',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=True),
|
||||
sa.Column('updated_at', sa.DateTime(), nullable=True),
|
||||
sa.Column('report_count', sa.Integer(), nullable=False),
|
||||
sa.Column('host', sa.String(length=255), nullable=True),
|
||||
sa.Column('binary', sa.String(length=255), nullable=True),
|
||||
sa.Column('disabled', sa.Boolean(), nullable=True),
|
||||
sa.Column('disabled_reason', sa.String(length=255), nullable=True),
|
||||
sa.Column('last_seen_up', sa.DateTime(), nullable=True),
|
||||
sa.Column('forced_down', sa.Boolean(), nullable=True),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('host', 'binary',
|
||||
name='uniq_higgins_service0host0binary')
|
||||
)
|
81
higgins/db/sqlalchemy/migration.py
Normal file
81
higgins/db/sqlalchemy/migration.py
Normal file
@ -0,0 +1,81 @@
|
||||
# Copyright 2010 United States Government as represented by the
|
||||
# Administrator of the National Aeronautics and Space Administration.
|
||||
# All Rights Reserved.
|
||||
#
|
||||
# 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
|
||||
|
||||
from oslo_config import cfg
|
||||
from oslo_db.sqlalchemy.migration_cli import manager
|
||||
|
||||
_MANAGER = None
|
||||
|
||||
|
||||
def get_manager():
|
||||
global _MANAGER
|
||||
if not _MANAGER:
|
||||
alembic_path = os.path.abspath(
|
||||
os.path.join(os.path.dirname(__file__), 'alembic.ini'))
|
||||
migrate_path = os.path.abspath(
|
||||
os.path.join(os.path.dirname(__file__), 'alembic'))
|
||||
migration_config = {'alembic_ini_path': alembic_path,
|
||||
'alembic_repo_path': migrate_path,
|
||||
'db_url': cfg.CONF.database.connection}
|
||||
_MANAGER = manager.MigrationManager(migration_config)
|
||||
|
||||
return _MANAGER
|
||||
|
||||
|
||||
def version():
|
||||
"""Current database version.
|
||||
|
||||
:returns: Database version
|
||||
:rtype: string
|
||||
"""
|
||||
return get_manager().version()
|
||||
|
||||
|
||||
def upgrade(version):
|
||||
"""Used for upgrading database.
|
||||
|
||||
:param version: Desired database version
|
||||
:type version: string
|
||||
"""
|
||||
version = version or 'head'
|
||||
|
||||
get_manager().upgrade(version)
|
||||
|
||||
|
||||
def stamp(revision):
|
||||
"""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
|
||||
"""
|
||||
get_manager().stamp(revision)
|
||||
|
||||
|
||||
def revision(message=None, autogenerate=False):
|
||||
"""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
|
||||
"""
|
||||
return get_manager().revision(message=message, autogenerate=autogenerate)
|
@ -48,6 +48,11 @@ output_file = higgins/locale/higgins.pot
|
||||
console_scripts =
|
||||
higgins-api = higgins.cmd.api:main
|
||||
higgins-conductor = higgins.cmd.conductor:main
|
||||
higgins-db-manage = higgins.cmd.db_manage:main
|
||||
|
||||
oslo.config.opts =
|
||||
higgins = higgins.opts:list_opts
|
||||
|
||||
higgins.database.migration_backend =
|
||||
sqlalchemy = higgins.db.sqlalchemy.migration
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user