2008-01-12 21:46:28 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2007-02-12 22:39:14 +01:00
|
|
|
#
|
2008-01-12 21:46:28 +01:00
|
|
|
# Copyright (C) 2007, 2008 by Jan Dittberner.
|
2007-02-12 22:39:14 +01:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful, but
|
|
|
|
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
|
|
|
# USA.
|
|
|
|
#
|
|
|
|
# Version: $Id$
|
|
|
|
|
|
|
|
from sqlalchemy import *
|
2008-03-06 20:09:59 +01:00
|
|
|
from sqlalchemy.exceptions import NoSuchTableError
|
2007-07-05 11:00:34 +02:00
|
|
|
import sys
|
|
|
|
import migrate.versioning.api
|
|
|
|
from settings import *
|
2007-02-12 22:39:14 +01:00
|
|
|
|
2007-07-09 08:46:36 +02:00
|
|
|
try:
|
|
|
|
dbversion = migrate.versioning.api.db_version(
|
|
|
|
config.get('database', 'uri'),
|
|
|
|
config.get('database', 'repository'))
|
|
|
|
if dbversion < required_version:
|
2008-06-06 21:20:18 +02:00
|
|
|
print("""Database version is %d but required version is %d. Trying
|
|
|
|
automatic upgrade.""" %
|
2008-04-04 20:36:46 +02:00
|
|
|
(dbversion, required_version))
|
|
|
|
try:
|
|
|
|
migrate.versioning.api.upgrade(
|
|
|
|
config.get('database', 'uri'),
|
|
|
|
config.get('database', 'repository'),
|
|
|
|
required_version)
|
|
|
|
except:
|
|
|
|
print "Automatic upgrade failed."
|
|
|
|
raise
|
|
|
|
elif dbversion > required_version:
|
2008-06-06 21:20:18 +02:00
|
|
|
print("""Database version is %d which is higher than the required
|
|
|
|
version %d. I cannot handle this situation without possible data loss.""" %
|
2008-04-04 20:36:46 +02:00
|
|
|
(dbversion, required_version))
|
2007-07-09 08:46:36 +02:00
|
|
|
sys.exit(1)
|
2008-06-06 21:20:18 +02:00
|
|
|
except NoSuchTableError, nste:
|
2008-04-04 20:36:46 +02:00
|
|
|
print """The database is not versioned. Trying automatic versioning."""
|
|
|
|
try:
|
|
|
|
migrate.versioning.api.version_control(
|
|
|
|
config.get('database', 'uri'),
|
|
|
|
config.get('database', 'repository'))
|
|
|
|
migrate.versioning.api.upgrade(
|
|
|
|
config.get('database', 'uri'),
|
|
|
|
config.get('database', 'repository', required_version))
|
|
|
|
except:
|
|
|
|
print "Automatic setup failed."
|
|
|
|
raise
|
2007-02-13 19:18:09 +01:00
|
|
|
|
2008-04-04 20:36:46 +02:00
|
|
|
meta = MetaData(config.get('database', 'uri'))
|
2007-07-09 08:46:36 +02:00
|
|
|
#meta.engine.echo = True
|
2008-01-12 21:46:28 +01:00
|
|
|
client_table = Table('client', meta, schema = dbschema, autoload = True)
|
|
|
|
sysuser_table = Table('sysuser', meta, schema = dbschema, autoload = True)
|
|
|
|
domain_table = Table('domain', meta, schema = dbschema, autoload = True)
|
|
|
|
record_table = Table('record', meta, schema = dbschema, autoload = True)
|
|
|
|
supermaster_table = Table('supermaster', meta, schema = dbschema,
|
|
|
|
autoload = True)
|
|
|
|
mailaccount_table = Table('mailaccount', meta, schema = dbschema,
|
|
|
|
autoload = True)
|
|
|
|
mailaddress_table = Table('mailaddress', meta, schema = dbschema,
|
|
|
|
autoload = True)
|
|
|
|
mailtarget_table = Table('mailtarget', meta, schema = dbschema,
|
|
|
|
autoload = True)
|