Esempio n. 1
0
    def getConfig(cls, databaseUrl, writeCallback =None):
        logger = Logger(
            databaseUrl.replace(u'://', u'~').replace(u'/', u'--').replace(u'.vdb', u''),
            useStorageBuffer=True
        )
        if writeCallback is not None:
            logger.addWriteCallback(writeCallback)

        migrationPath = PyGlassModelUtils.getMigrationPathFromDatabaseUrl(databaseUrl)
        config = alembicConfig.Config(os.path.join(migrationPath, 'alembic.ini'), stdout=logger)

        config.set_main_option(
            'script_location',
            migrationPath
        )

        config.set_main_option(
            'sqlalchemy.url',
            PyGlassModelUtils.getEngineUrl(databaseUrl)
        )

        config.set_main_option(
            'url',
            PyGlassModelUtils.getEngineUrl(databaseUrl)
        )

        return config
Esempio n. 2
0
    def getConfig(cls, databaseUrl, resourcesPath, localResourcesPath, writeCallback =None):
        """ Retrieves the Alembic configuration for the specified database URL stored within the
            resources and local resources path for the target application """

        logger = Logger(
            databaseUrl.replace('://', '~').replace('/', '--').replace('.vdb', ''),
            useStorageBuffer=True)

        if writeCallback is not None:
            logger.addWriteCallback(writeCallback)

        migrationPath = PyGlassModelUtils.getMigrationPathFromDatabaseUrl(
            databaseUrl, resourcesPath=resourcesPath)

        config = alembicConfig.Config(os.path.join(migrationPath, 'alembic.ini'), stdout=logger)

        engineUrl = PyGlassModelUtils.getEngineUrl(
            databaseUrl=databaseUrl, localResourcesPath=localResourcesPath)

        # These options are overridden during loading to prevent issues of absolute path corruption
        # when running in different deployment modes and when installed on different computers
        config.set_main_option('url', engineUrl)
        config.set_section_option('alembic', 'script_location', migrationPath)
        config.set_section_option('alembic', 'sqlalchemy.url', engineUrl)
        config.set_section_option('alembic', 'url', migrationPath)

        return config
Esempio n. 3
0
    def initializeDatabase(cls, databaseUrl):
        migrationRootPath = PyGlassModelUtils.getMigrationPathFromDatabaseUrl(databaseUrl, root=True)
        migrationPath     = PyGlassModelUtils.getMigrationPathFromDatabaseUrl(databaseUrl)
        configPath        = os.path.join(migrationPath, 'alembic.ini')
        workingPath       = os.curdir

        if not os.path.exists(migrationRootPath):
            os.makedirs(migrationRootPath)

        if os.path.exists(migrationPath + 'alembic.ini'):
            return False

        os.chdir(migrationRootPath)

        config                  = alembicConfig.Config()
        config.config_file_name = configPath

        alembicCmd.init(
            config=config,
            directory=migrationPath
        )

        # Refresh config with proper settings
        cp = ConfigParser.ConfigParser()
        cp.read(configPath)
        cp.set('alembic', 'sqlalchemy.url', PyGlassModelUtils.getEngineUrl(databaseUrl))

        f = open(configPath, 'w+')
        cp.write(f)
        f.close()

        os.chdir(workingPath)
        return True
    def __new__(mcs, name, bases, attrs):

        binding = ConcretePyGlassModelsMeta._engines.get(name)
        if binding is None:

            # Retrieve the database URL from the __init__.py for the package in which the model class
            # resides.
            module  = attrs['__module__']
            package = module[:module.rfind('.')]
            res     = __import__(package, globals(), locals(), ['DATABASE_URL'])

            try:
                sourceUrl = getattr(res, 'DATABASE_URL')
            except Exception, err:
                PyGlassModelUtils.logger.writeError([
                    u'ERROR: Unable to get DATABASE_URL from %s.__init__.py' % package,
                    u'NAME: ' + unicode(name)
                ], err)
                raise

            try:
                url = PyGlassModelUtils.getEngineUrl(sourceUrl)
            except Exception, err:
                PyGlassModelUtils.logger.writeError([
                    u'ERROR: Unable to get url from database url',
                    u'DATABASE URL: ' + unicode(sourceUrl)
                ], err)
                raise
Esempio n. 5
0
    def getCurrentDatabaseRevision(cls, databaseUrl, localResourcesPath =None):
        url = PyGlassModelUtils.getEngineUrl(
            databaseUrl=databaseUrl,
            localResourcesPath=localResourcesPath)
        engine = sqla.create_engine(url)
        conn = engine.connect()

        context = MigrationContext.configure(conn)
        result = context.get_current_revision()
        conn.close()
        return result
Esempio n. 6
0
    def initializeDatabase(cls, databaseUrl, resourcesPath =None, localResourcesPath =None):

        migrationRootPath = PyGlassModelUtils.getMigrationPathFromDatabaseUrl(
            databaseUrl=databaseUrl,
            root=True,
            resourcesPath=resourcesPath)

        migrationPath = PyGlassModelUtils.getMigrationPathFromDatabaseUrl(
            databaseUrl=databaseUrl,
            resourcesPath=resourcesPath)

        configPath = FileUtils.makeFilePath(migrationPath, 'alembic.ini')

        if not os.path.exists(migrationRootPath):
            os.makedirs(migrationRootPath)

        if os.path.exists(migrationPath + 'alembic.ini'):
            return False

        os.chdir(migrationRootPath)

        config = alembicConfig.Config()
        config.config_file_name = configPath

        alembicCmd.init(config=config, directory=migrationPath)

        # Refresh config with proper settings
        cp = configparser.ConfigParser()
        cp.read(configPath)
        cp.set('alembic', 'sqlalchemy.url', PyGlassModelUtils.getEngineUrl(
            databaseUrl=databaseUrl,
            localResourcesPath=localResourcesPath))

        f = open(configPath, 'w+')
        cp.write(f)
        f.close()

        return True
    def __new__(mcs, name, bases, attrs):

        binding = ConcretePyGlassModelsMeta._engines.get(name)
        if binding is None:

            # Retrieve the database URL from the __init__.py for the package in which the model class
            # resides.
            try:
                module  = attrs['__module__']
                package = module[:module.rfind('.')]
                res = __import__(package, globals(), locals(), [StringUtils.toStr2('DATABASE_URL')])
            except Exception as err:
                PyGlassModelUtils.logger.writeError([
                    'ERROR: Unable to initialize model',
                    'NAME: %s' % name ])
                raise

            try:
                sourceUrl = getattr(res, 'DATABASE_URL')
            except Exception as err:
                PyGlassModelUtils.logger.writeError([
                    'ERROR: Unable to get DATABASE_URL from %s.__init__.py' % package,
                    'NAME: %s' % name ], err)
                raise

            try:
                url = PyGlassModelUtils.getEngineUrl(sourceUrl)
            except Exception as err:
                PyGlassModelUtils.logger.writeError([
                    'ERROR: Unable to get url from database url',
                    'DATABASE URL: ' + StringUtils.toUnicode(sourceUrl) ], err)
                raise

            try:
                engine = create_engine(url, echo=False)
            except Exception as err:
                PyGlassModelUtils.logger.writeError([
                    'DATABASE FAILURE: Unable to create engine for database',
                    'URL: ' + StringUtils.toUnicode(url),
                    'NAME: ' + StringUtils.toUnicode(name)
                ], err)
                raise

            try:
                Session = scoped_session(sessionmaker(bind=engine))
            except Exception as err:
                PyGlassModelUtils.logger.writeError([
                    'DATABASE FAILURE: Unable to create bound Session.',
                    'URL: ' + StringUtils.toUnicode(url),
                    'ENGINE: ' + StringUtils.toUnicode(engine)
                ], err)
                raise

            try:
                Base = declarative_base(bind=engine)
            except Exception as err:
                PyGlassModelUtils.logger.writeError([
                    'DATABASE FAILURE: Unable to create database engine Base.',
                    'URL: ' + StringUtils.toUnicode(url),
                    'ENGINE: ' + StringUtils.toUnicode(engine)
                ], err)
                raise

            try:
                Base.metadata.create_all(engine)
            except Exception as err:
                PyGlassModelUtils.logger.writeError([
                    'DATABASE FAILURE: Unable to create models.'
                    'URL: ' + StringUtils.toUnicode(url),
                    'ENGINE: ' + StringUtils.toUnicode(engine),
                    'BASE: ' + StringUtils.toUnicode(Base)
                ], err)
                raise

            binding = {
                'engine':engine,
                'SessionClass':Session,
                'BaseClass':Base,
                'url':url,
                'databaseUrl':sourceUrl }
            mcs._engines[name] = binding

        attrs['ENGINE']        = binding['engine']
        attrs['SESSION_CLASS'] = binding['SessionClass']
        attrs['BASE']          = binding['BaseClass']
        attrs['URL']           = binding['url']
        attrs['MODEL_NAME']    = name

        attrs['DATABASE_URL']  = binding['databaseUrl']
        attrs['BINDING']       = binding

        # Add the declarative base to inheritance
        declaredBase = (binding['BaseClass'],)
        try:
            bases = bases + declaredBase
        except Exception as err:
            bases = declaredBase

        out = AbstractPyGlassModelsMeta.__new__(mcs, name, bases, attrs)
        return out