Cleanup SQLA migrate changes + "Un-Abstract" the type definitions

Abstracting the type definitions provides little gain for high risk. The
abstraction can *never* be updated, otherwise it would affect the result of
existing migrations that depend on the abstraction.
This commit is contained in:
Kiall Mac Innes 2012-10-28 18:01:34 +00:00
parent 0310f220b7
commit b8bbfd3d34
7 changed files with 77 additions and 108 deletions

View File

@ -35,7 +35,8 @@
"*.egg",
"*.egg-info",
".tox",
"venv"
"venv",
".venv"
],
"path": "."
}

View File

@ -24,7 +24,7 @@ from moniker import storage # Import for database_connection cfg def.
LOG = logging.getLogger(__name__)
REPOSITORY = os.path.abspath(os.path.join(os.path.dirname(__file__), '..',
'database', 'sqlalchemy',
'storage', 'impl_sqlalchemy',
'migrate_repo'))
@ -34,6 +34,11 @@ class InitCommand(Command):
def take_action(self, parsed_args):
url = cfg.CONF.database_connection
if not os.path.exists(REPOSITORY):
raise Exception('Migration Respository Not Found')
LOG.warn(url)
try:
LOG.info('Attempting to initialize database')
versioning_api.version_control(url=url, repository=REPOSITORY)
@ -49,6 +54,11 @@ class SyncCommand(Command):
# TODO: Support specifying version
url = cfg.CONF.database_connection
if not os.path.exists(REPOSITORY):
raise Exception('Migration Respository Not Found')
LOG.warn(url)
try:
LOG.info('Attempting to synchronize database')
versioning_api.upgrade(url=url, repository=REPOSITORY,

View File

@ -1,7 +0,0 @@
#!/usr/bin/env python
# this is a tempoary file - need to create a "moniker-manage" script as glance
# and reddwarf have to run the migration using the main conf file for db creds
from migrate.versioning.shell import main
if __name__ == '__main__':
main(url='sqlite:///test.sqlite', debug='True', repository='migrate_repo')

View File

@ -1,75 +0,0 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2012 Hewlett-Packard Development Company, L.P.
# All Rights Reserved.
#
# Author: Patrick Galbraith <patg@hp.com>
#
# 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.
"""
Various conveniences used for migration scripts
"""
import sqlalchemy.types
from sqlalchemy.schema import MetaData
from moniker.openstack.common import log as logging
import moniker.storage.impl_sqlalchemy.types
logger = logging.getLogger(__name__)
String = lambda length: sqlalchemy.types.String(
length=length, convert_unicode=False, assert_unicode=None,
unicode_error=None, _warn_on_bytestring=False)
Text = lambda: sqlalchemy.types.Text(
length=None, convert_unicode=False, assert_unicode=None,
unicode_error=None, _warn_on_bytestring=False)
RECORD_TYPES = ['A', 'AAAA', 'CNAME', 'MX', 'SRV', 'TXT', 'NS']
# headaches getting this to work
Enum = sqlalchemy.types.Enum(name=None, *RECORD_TYPES)
Boolean = lambda: sqlalchemy.types.Boolean(create_constraint=True, name=None)
DateTime = lambda: sqlalchemy.types.DateTime(timezone=False)
Integer = lambda: sqlalchemy.types.Integer()
UUID = lambda: moniker.storage.impl_sqlalchemy.types.UUID()
Inet = lambda: moniker.storage.impl_sqlalchemy.types.Inet()
def create_tables(tables):
for table in tables:
logger.info("creating table %(table)s" % locals())
table.create()
def drop_tables(tables):
for table in tables:
logger.info("dropping table %(table)s" % locals())
table.drop()
def Table(name, metadata, *args, **kwargs):
return sqlalchemy.schema.Table(name, metadata, *args,
mysql_engine='INNODB', **kwargs)

View File

@ -0,0 +1,45 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2012 Hewlett-Packard Development Company, L.P.
# All Rights Reserved.
#
# Author: Patrick Galbraith <patg@hp.com>
#
# 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.
"""
Various conveniences used for migration scripts
"""
from sqlalchemy.schema import Table as SqlaTable
from moniker.openstack.common import log as logging
LOG = logging.getLogger(__name__)
def create_tables(tables):
for table in tables:
LOG.debug("Creating table %(table)s" % locals())
table.create()
def drop_tables(tables):
for table in tables:
LOG.debug("Dropping table %(table)s" % locals())
table.drop()
def Table(*args, **kwargs):
if not 'mysql_engine' in kwargs:
kwargs['mysql_engine'] = 'INNODB'
return SqlaTable(*args, **kwargs)

View File

@ -19,19 +19,26 @@
# should this be in schema.py?
from uuid import uuid4
from migrate import *
from sqlalchemy import ForeignKey
from sqlalchemy.schema import (Column, MetaData)
from sqlalchemy.orm import relationship, backref
from moniker.storage.impl_sqlalchemy.migrate_repo.schema import (
Table, Integer, String, Text, create_tables,
UUID, drop_tables, DateTime, RECORD_TYPES)
from moniker.storage.impl_sqlalchemy.types import (
Inet)
from sqlalchemy import ForeignKey, Enum, Integer, String, DateTime, Text
from sqlalchemy.schema import Column, MetaData
from moniker.openstack.common import timeutils
from moniker.storage.impl_sqlalchemy.migrate_repo.utils import (
Table, create_tables, drop_tables)
from moniker.storage.impl_sqlalchemy.types import Inet, UUID
meta = MetaData()
RECORD_TYPES = ['A', 'AAAA', 'CNAME', 'MX', 'SRV', 'TXT', 'NS']
servers = Table('servers', meta,
Column('id', UUID(), default=uuid4, primary_key=True),
Column('created_at', DateTime(), default=timeutils.utcnow),
Column('updated_at', DateTime(), onupdate=timeutils.utcnow),
Column('version', Integer(), default=1, nullable=False),
Column('name', String(255), nullable=False, unique=True),
Column('ipv4', Inet(), nullable=False, unique=True),
Column('ipv6', Inet(), default=None, unique=True))
domains = Table('domains', meta,
Column('id', UUID(), default=uuid4, primary_key=True),
Column('created_at', DateTime(), default=timeutils.utcnow),
@ -44,18 +51,7 @@ domains = Table('domains', meta,
Column('refresh', Integer(), default=3600, nullable=False),
Column('retry', Integer(), default=3600, nullable=False),
Column('expire', Integer(), default=3600, nullable=False),
Column('minimum', Integer(), default=3600, nullable=False),
useexisting=True)
servers = Table('servers', meta,
Column('id', UUID(), default=uuid4, primary_key=True),
Column('created_at', DateTime(), default=timeutils.utcnow),
Column('updated_at', DateTime(), onupdate=timeutils.utcnow),
Column('version', Integer(), default=1, nullable=False),
Column('name', String(255), nullable=False, unique=True),
Column('ipv4', Inet(), nullable=False, unique=True),
Column('ipv6', Inet(), default=None, unique=True),
useexisting=True)
Column('minimum', Integer(), default=3600, nullable=False))
records = Table('records', meta,
Column('id', UUID(), default=uuid4, primary_key=True),
@ -69,8 +65,7 @@ records = Table('records', meta,
Column('priority', Integer(), default=None),
Column('ttl', Integer(), default=3600, nullable=False),
Column('domain_id', UUID(), ForeignKey('domains.id'),
nullable=False),
useexisting=True)
nullable=False))
def upgrade(migrate_engine):

View File

@ -55,7 +55,7 @@ setup(
postgresql = moniker.storage.impl_sqlalchemy:SQLAlchemyStorage
sqlite = moniker.storage.impl_sqlalchemy:SQLAlchemyStorage
[moniker.cli]
database init = InitCommand
database init = moniker.cli.database:InitCommand
database sync = moniker.cli.database:SyncCommand
""")
)