2011-01-12 16:57:04 -08:00
|
|
|
# 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
|
|
|
|
|
2013-11-15 12:14:57 -05:00
|
|
|
from migrate import exceptions as versioning_exceptions
|
|
|
|
from migrate.versioning import api as versioning_api
|
|
|
|
from migrate.versioning.repository import Repository
|
|
|
|
import sqlalchemy
|
|
|
|
|
2011-09-21 14:10:39 +01:00
|
|
|
from nova import exception
|
2013-01-17 15:38:02 -05:00
|
|
|
from nova.openstack.common.db.sqlalchemy import session as db_session
|
2013-06-13 16:12:23 -07:00
|
|
|
from nova.openstack.common.gettextutils import _
|
2012-04-08 11:50:12 +10:00
|
|
|
|
2014-01-13 19:19:23 -05:00
|
|
|
INIT_VERSION = 215
|
2012-03-08 16:43:32 +08:00
|
|
|
_REPOSITORY = None
|
|
|
|
|
2013-01-17 15:38:02 -05:00
|
|
|
get_engine = db_session.get_engine
|
|
|
|
|
2011-01-12 16:57:04 -08:00
|
|
|
|
|
|
|
def db_sync(version=None):
|
2011-09-21 14:10:39 +01:00
|
|
|
if version is not None:
|
|
|
|
try:
|
|
|
|
version = int(version)
|
|
|
|
except ValueError:
|
2012-05-03 14:29:50 -04:00
|
|
|
raise exception.NovaException(_("version should be an integer"))
|
2011-09-21 14:10:39 +01:00
|
|
|
|
|
|
|
current_version = db_version()
|
2012-03-08 16:43:32 +08:00
|
|
|
repository = _find_migrate_repo()
|
2011-09-21 14:10:39 +01:00
|
|
|
if version is None or version > current_version:
|
2012-03-08 16:43:32 +08:00
|
|
|
return versioning_api.upgrade(get_engine(), repository, version)
|
2011-09-21 14:10:39 +01:00
|
|
|
else:
|
2012-03-08 16:43:32 +08:00
|
|
|
return versioning_api.downgrade(get_engine(), repository,
|
2011-09-21 14:10:39 +01:00
|
|
|
version)
|
2011-01-12 16:57:04 -08:00
|
|
|
|
|
|
|
|
|
|
|
def db_version():
|
2012-03-08 16:43:32 +08:00
|
|
|
repository = _find_migrate_repo()
|
2011-01-12 16:57:04 -08:00
|
|
|
try:
|
2012-03-08 16:43:32 +08:00
|
|
|
return versioning_api.db_version(get_engine(), repository)
|
2011-01-12 16:57:04 -08:00
|
|
|
except versioning_exceptions.DatabaseNotControlledError:
|
|
|
|
meta = sqlalchemy.MetaData()
|
2012-03-08 16:43:32 +08:00
|
|
|
engine = get_engine()
|
2011-01-12 16:57:04 -08:00
|
|
|
meta.reflect(bind=engine)
|
2012-05-02 14:48:25 -04:00
|
|
|
tables = meta.tables
|
|
|
|
if len(tables) == 0:
|
2013-10-24 10:36:58 -04:00
|
|
|
db_version_control(INIT_VERSION)
|
2012-05-02 14:48:25 -04:00
|
|
|
return versioning_api.db_version(get_engine(), repository)
|
|
|
|
else:
|
|
|
|
# Some pre-Essex DB's may not be version controlled.
|
|
|
|
# Require them to upgrade using Essex first.
|
2012-08-04 18:52:08 +01:00
|
|
|
raise exception.NovaException(
|
|
|
|
_("Upgrade DB using Essex release first."))
|
2011-01-12 16:57:04 -08:00
|
|
|
|
|
|
|
|
2013-10-24 10:36:58 -04:00
|
|
|
def db_initial_version():
|
|
|
|
return INIT_VERSION
|
|
|
|
|
|
|
|
|
2011-01-12 16:57:04 -08:00
|
|
|
def db_version_control(version=None):
|
2012-03-08 16:43:32 +08:00
|
|
|
repository = _find_migrate_repo()
|
|
|
|
versioning_api.version_control(get_engine(), repository, version)
|
2011-01-12 16:57:04 -08:00
|
|
|
return version
|
|
|
|
|
|
|
|
|
|
|
|
def _find_migrate_repo():
|
|
|
|
"""Get the path for the migrate repository."""
|
2012-03-08 16:43:32 +08:00
|
|
|
global _REPOSITORY
|
2011-01-12 16:57:04 -08:00
|
|
|
path = os.path.join(os.path.abspath(os.path.dirname(__file__)),
|
|
|
|
'migrate_repo')
|
|
|
|
assert os.path.exists(path)
|
2012-03-08 16:43:32 +08:00
|
|
|
if _REPOSITORY is None:
|
|
|
|
_REPOSITORY = Repository(path)
|
|
|
|
return _REPOSITORY
|