# -*- coding: utf-8 -*-
#
# Copyright (C) 2007, 2008 by Jan Dittberner.
#
# 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 *
from sqlalchemy.exceptions import NoSuchTableError
import sys
import migrate.versioning.api
from settings import *

try:
    dbversion = migrate.versioning.api.db_version(
        config.get('database', 'uri'),
        config.get('database', 'repository'))
    if dbversion < required_version:
        print("""Database version is %d but required version is %d. Trying
automatic upgrade.""" %
              (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:
        print("""Database version is %d which is higher than the required
version %d. I cannot handle this situation without possible data loss.""" %
              (dbversion, required_version))
        sys.exit(1)
except NoSuchTableError, nste:
    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

meta = MetaData(config.get('database', 'uri'))
#meta.engine.echo = True
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)