示例#1
0
def ZEODatabaseConfigurationFactory(key, dbconfig):
    if not ZEOSERVER:
        raise Exception("You must install the ZEO package before you can use "
                        "it as a dabase adapter.")
    config = dbconfig.get('configuration', {})
    # Try to open it normal to create the root object
    address = (dbconfig['address'], dbconfig['port'])

    zeoconfig = dbconfig.get('zeoconfig', {})
    cs = ZEO.ClientStorage.ClientStorage(address, **zeoconfig)
    db = DB(cs)

    try:
        conn = db.open()
        rootobj = conn.root()
        if not IDatabase.providedBy(rootobj):
            alsoProvides(rootobj, IDatabase)
        transaction.commit()
    except:
        pass
    finally:
        rootobj = None
        conn.close()
        db.close()

    # Set request aware database for app
    cs = ZEO.ClientStorage.ClientStorage(address, **zeoconfig)
    db = RequestAwareDB(cs, **config)
    return Database(key, db)
示例#2
0
def RelStorageConfigurationFactory(key, dbconfig):
    if not RELSTORAGE:
        raise Exception("You must install the relstorage package before you can use "
                        "it as a dabase adapter.")
    config = dbconfig.get('configuration', {})
    options = Options(**dbconfig['options'])
    if dbconfig['type'] == 'postgres':
        from relstorage.adapters.postgresql import PostgreSQLAdapter
        dsn = "dbname={dbname} user={user} host={host} password={password} port={port}".format(**dbconfig['dsn'])  # noqa
        adapter = PostgreSQLAdapter(dsn=dsn, options=options)
    rs = RelStorage(adapter=adapter, options=options)
    db = DB(rs)
    try:
        conn = db.open()
        rootobj = conn.root()
        if not IDatabase.providedBy(rootobj):
            alsoProvides(rootobj, IDatabase)
        transaction.commit()
    except:
        pass
    finally:
        rootobj = None
        conn.close()
        db.close()
    rs = RelStorage(adapter=adapter, options=options)
    db = RequestAwareDB(rs, **config)
    return Database(key, db)
示例#3
0
def ZEODatabaseConfigurationFactory(key, dbconfig):
    if not ZEOSERVER:
        raise Exception("You must install the ZEO package before you can use "
                        "it as a dabase adapter.")
    config = dbconfig.get('configuration', {})
    # Try to open it normal to create the root object
    address = (dbconfig['address'], dbconfig['port'])

    zeoconfig = dbconfig.get('zeoconfig', {})
    cs = ZEO.ClientStorage.ClientStorage(address, **zeoconfig)
    db = DB(cs)

    try:
        conn = db.open()
        rootobj = conn.root()
        if not IDatabase.providedBy(rootobj):
            alsoProvides(rootobj, IDatabase)
        transaction.commit()
    except:
        pass
    finally:
        rootobj = None
        conn.close()
        db.close()

    # Set request aware database for app
    cs = ZEO.ClientStorage.ClientStorage(address, **zeoconfig)
    db = RequestAwareDB(cs, **config)
    return Database(key, db)
示例#4
0
def RelStorageConfigurationFactory(key, dbconfig):
    if not RELSTORAGE:
        raise Exception("You must install the relstorage package before you can use "
                        "it as a dabase adapter.")
    config = dbconfig.get('configuration', {})
    options = Options(**dbconfig['options'])
    if dbconfig['type'] == 'postgres':
        from relstorage.adapters.postgresql import PostgreSQLAdapter
        dsn = "dbname={dbname} user={username} host={host} password={password} port={port}".format(**dbconfig['dsn'])  # noqa
        adapter = PostgreSQLAdapter(dsn=dsn, options=options)
    rs = RelStorage(adapter=adapter, options=options)
    db = DB(rs)
    try:
        conn = db.open()
        rootobj = conn.root()
        if not IDatabase.providedBy(rootobj):
            alsoProvides(rootobj, IDatabase)
        transaction.commit()
    except:
        pass
    finally:
        rootobj = None
        conn.close()
        db.close()
    rs = RelStorage(adapter=adapter, options=options)
    db = RequestAwareDB(rs, **config)
    return Database(key, db)
示例#5
0
def DemoDatabaseConfigurationFactory(key, dbconfig):
    storage = DemoStorage(name=dbconfig['name'])
    db = DB(storage)
    alsoProvides(db.open().root(), IDatabase)
    transaction.commit()
    db.close()
    # Set request aware database for app
    db = RequestAwareDB(storage)
    return Database(key, db)
示例#6
0
def DemoDatabaseConfigurationFactory(key, dbconfig):
    storage = DemoStorage(name=dbconfig['name'])
    db = DB(storage)
    alsoProvides(db.open().root(), IDatabase)
    transaction.commit()
    db.close()
    # Set request aware database for app
    db = RequestAwareDB(storage)
    return Database(key, db)
示例#7
0
    def checkConfigureViaZConfig(self):
        replica_conf = os.path.join(os.path.dirname(relstorage.tests.__file__),
                                    'replicas.conf')
        dsn = 'dbname=' + self.dbname
        conf = u"""
        %%import relstorage
        %%import newt.db
        <zodb main>
            <relstorage>
            name xyz
            read-only false
            keep-history %s
            replica-conf %s
            blob-chunk-size 10MB
            <newt>
            <postgresql>
                driver auto
                dsn %s
            </postgresql>
            </newt>
            </relstorage>
        </zodb>
        """ % (
            self.keep_history and 'true' or 'false',
            replica_conf,
            dsn,
        )

        schema_xml = u"""
        <schema>
        <import package="ZODB"/>
        <section type="ZODB.database" name="main" attribute="database"/>
        </schema>
        """
        import ZConfig
        from io import StringIO
        schema = ZConfig.loadSchemaFile(StringIO(schema_xml))
        config, _ = ZConfig.loadConfigFile(schema, StringIO(conf))

        db = config.database.open()
        try:
            storage = db.storage
            self.assertEqual(storage.isReadOnly(), False)
            self.assertEqual(storage.getName(), "xyz")
            adapter = storage._adapter
            from relstorage.adapters.postgresql import PostgreSQLAdapter
            self.assertIsInstance(adapter, PostgreSQLAdapter)
            self.assertEqual(adapter._dsn, dsn)
            self.assertEqual(adapter.keep_history, self.keep_history)
            self.assertEqual(adapter.connmanager.replica_selector.replica_conf,
                             replica_conf)
            self.assertEqual(storage._options.blob_chunk_size, 10485760)

            from .._adapter import Adapter
            self.assertEqual(Adapter, storage._adapter.__class__)
        finally:
            db.close()
示例#8
0
    def test_compute_missing_wo_updater(self):
        # When redoing, if we aren't running the updater, we shouldn't
        # create the follow table and should use the max(tid) from
        # object state as the end tid.
        import ZODB.config
        from .. import follow, Object, pg_connection, _util, updater

        db = ZODB.config.databaseFromString("""\
        %%import relstorage
        <zodb>
          <relstorage>
            keep-history false
            <postgresql>
              dsn %s
            </postgresql>
          </relstorage>
        </zodb>
        """ % self.dsn)
        with db.transaction() as conn:
            conn.root.x = Object(a=1)
        db.close()

        conn = pg_connection(self.dsn)
        cursor = conn.cursor()
        self.assertFalse(_util.table_exists(cursor, 'newt'))
        self.assertFalse(_util.table_exists(cursor, follow.PROGRESS_TABLE))

        # If we try to run redo now, we'll get an error, because the
        # net table doesn't exist:
        with self.assertRaises(AssertionError):
            updater.main(['--compute-missing', self.dsn])

        # create newt table
        from .. import connection
        connection(self.dsn).close()

        # The table is empty:
        cursor.execute("select count(*) from newt")
        [[c]] = cursor
        self.assertEqual(0, c)

        # Now run the redo:
        updater.main(['--compute-missing', self.dsn])

        # We have rows:
        cursor.execute("select count(*) from newt")
        [[c]] = cursor
        self.assertEqual(2, c)

        # The progres table still doesn't exist
        self.assertFalse(_util.table_exists(cursor, follow.PROGRESS_TABLE))

        cursor.close()
        conn.close()
示例#9
0
def ZODBDatabaseConfigurationFactory(key, dbconfig):
    config = dbconfig.get('configuration', {})
    fs = ZODB.FileStorage.FileStorage(dbconfig['path'])
    db = DB(fs)
    try:
        rootobj = db.open().root()
        if not IDatabase.providedBy(rootobj):
            alsoProvides(rootobj, IDatabase)
        transaction.commit()
        rootobj = None
    except:
        pass
    finally:
        db.close()
    # Set request aware database for app
    db = RequestAwareDB(dbconfig['path'], **config)
    return Database(key, db)
示例#10
0
def ZODBDatabaseConfigurationFactory(key, dbconfig):
    config = dbconfig.get('configuration', {})
    fs = ZODB.FileStorage.FileStorage(dbconfig['path'])
    db = DB(fs)
    try:
        rootobj = db.open().root()
        if not IDatabase.providedBy(rootobj):
            alsoProvides(rootobj, IDatabase)
        transaction.commit()
        rootobj = None
    except:
        pass
    finally:
        db.close()
    # Set request aware database for app
    db = RequestAwareDB(dbconfig['path'], **config)
    return Database(key, db)
示例#11
0
    def test_newt_on_existing_db(self):
        import ZODB.config
        import newt.db
        db = ZODB.config.databaseFromString("""\
        %%import relstorage
        <zodb>
          <relstorage>
            keep-history false
            <postgresql>
              dsn %s
            </postgresql>
          </relstorage>
        </zodb>
        """ % self.dsn)
        with db.transaction() as conn:
            conn.root.x = Object(a=1)
        db.close()

        conn = newt.db.connection(self.dsn)
        self.assertEqual(conn.root.x.a, 1)
        conn.close()
示例#12
0
def NewtConfigurationFactory(key, dbconfig):
    if not NEWT:
        raise Exception("You must install the newt.db package before you can use "
                        "it as a dabase adapter.")
    config = dbconfig.get('configuration', {})
    dsn = "dbname={dbname} user={username} host={host} password={password} port={port}".format(**dbconfig['dsn'])  # noqa
    adapter = newt.db.storage(dsn=dsn, **dbconfig['options'])
    db = newt.db.DB(dsn, **dbconfig['options'])
    try:
        conn = db.open()
        rootobj = conn.root()
        if not IDatabase.providedBy(rootobj):
            alsoProvides(rootobj, IDatabase)
        transaction.commit()
    except:
        pass
    finally:
        rootobj = None
        conn.close()
        db.close()
    adapter = newt.db.storage(dsn, **dbconfig['options'])
    db = newt.db._db.NewtDB(RequestAwareDB(adapter, **config))
    return Database(key, db)
示例#13
0
def NewtConfigurationFactory(key, dbconfig):
    if not NEWT:
        raise Exception("You must install the newt.db package before you can use "
                        "it as a dabase adapter.")
    config = dbconfig.get('configuration', {})
    dsn = "dbname={dbname} user={username} host={host} password={password} port={port}".format(**dbconfig['dsn'])  # noqa
    adapter = newt.db.storage(dsn=dsn, **dbconfig['options'])
    db = newt.db.DB(dsn, **dbconfig['options'])
    try:
        conn = db.open()
        rootobj = conn.root()
        if not IDatabase.providedBy(rootobj):
            alsoProvides(rootobj, IDatabase)
        transaction.commit()
    except:
        pass
    finally:
        rootobj = None
        conn.close()
        db.close()
    adapter = newt.db.storage(dsn, **dbconfig['options'])
    db = newt.db._db.NewtDB(RequestAwareDB(adapter, **config))
    return Database(key, db)