Example #1
0
    def make(self, dependencies):
        """Create a L{ZStorm} resource to be used by tests.

        @return: A L{ZStorm} object that will be shared among all tests using
            this resource manager.
        """
        if self._zstorm is None:

            if self.use_global_zstorm:
                self._zstorm = global_zstorm
            else:
                self._zstorm = ZStorm()
            self._schema_zstorm = ZStorm()

            databases = self._databases

            # Adapt the old databases format to the new one, for backward
            # compatibility. This should be eventually dropped.
            if isinstance(databases, dict):
                databases = [{
                    "name": name,
                    "uri": uri,
                    "schema": schema
                } for name, (uri, schema) in databases.iteritems()]

            # Provide the global IZStorm utility before applying patches, so
            # patch code can get the ztorm object if needed (e.g. looking up
            # other stores).
            provideUtility(self._zstorm)

            self._set_create_hook()

            for database in databases:
                name = database["name"]
                uri = database["uri"]
                schema = database.get("schema")
                schema_uri = database.get("schema-uri", uri)
                self._zstorm.set_default_uri(name, uri)
                if schema is not None:
                    # The configuration for this database does not include a
                    # schema definition, so we just setup the store (the user
                    # code should apply the schema elsewhere, if any)
                    self._schemas[name] = schema
                    self._schema_zstorm.set_default_uri(name, schema_uri)
                    self._ensure_schema(name, schema)

            # Commit all schema changes across all stores
            transaction.commit()

        elif getUtility(IZStorm) is not self._zstorm:
            # This probably means that the test code has overwritten our
            # utility, let's re-register it.
            provideUtility(self._zstorm)

        return self._zstorm
Example #2
0
 def setUp(self):
     super(TemporaryDatabaseMixin, self).setUp()
     self.uri = 'sqlite:///%s' % self.fs.makePath()
     self.zstorm = ZStorm()
     self.zstorm.set_default_uri('main', self.uri)
     provideUtility(self.zstorm)
     self.store = self.zstorm.get('main')
Example #3
0
 def get_store():
     """
     Returns a reference to Storm Store
     """
     zstorm = ZStorm()
     zstorm.set_default_uri(GLSetting.store_name, GLSetting.db_uri)
     return zstorm.get(GLSetting.store_name)
Example #4
0
    def make(self, dependencies):
        """Create a L{ZStorm} resource to be used by tests.

        @return: A L{ZStorm} object that will be shared among all tests using
            this resource manager.
        """
        if self._zstorm is None:

            zstorm = ZStorm()
            schema_zstorm = ZStorm()
            databases = self._databases

            # Adapt the old databases format to the new one, for backward
            # compatibility. This should be eventually dropped.
            if isinstance(databases, dict):
                databases = [{
                    "name": name,
                    "uri": uri,
                    "schema": schema
                } for name, (uri, schema) in databases.iteritems()]

            for database in databases:
                name = database["name"]
                uri = database["uri"]
                schema = database["schema"]
                schema_uri = database.get("schema-uri", uri)
                self._schemas[name] = schema
                zstorm.set_default_uri(name, uri)
                schema_zstorm.set_default_uri(name, schema_uri)
                store = zstorm.get(name)
                self._set_commit_proxy(store)
                schema_store = schema_zstorm.get(name)
                schema.upgrade(schema_store)
                # Clean up tables here to ensure that the first test run starts
                # with an empty db
                schema.delete(schema_store)

            provideUtility(zstorm)
            self._zstorm = zstorm
            self._schema_zstorm = schema_zstorm

        elif getUtility(IZStorm) is not self._zstorm:
            # This probably means that the test code has overwritten our
            # utility, let's re-register it.
            provideUtility(self._zstorm)

        return self._zstorm
Example #5
0
 def test_make_zstorm_overwritten(self):
     """
     L{ZStormResourceManager.make} registers its own ZStorm again if a test
     has registered a new ZStorm utility overwriting the resource one.
     """
     zstorm = self.resource.make([])
     provideUtility(ZStorm())
     self.resource.make([])
     self.assertIs(zstorm, getUtility(IZStorm))
Example #6
0
def setupStore(uri, name):
    """Setup the main store.

    @param uri: The URI for the main database.
    """
    zstorm = ZStorm()
    zstorm.set_default_uri(name, uri)
    provideUtility(zstorm)
    return zstorm.get(name)
Example #7
0
def setupStore(config):
    """Setup the main store.

    A C{ZStorm} instance is configured and registered as a global utility.

    @param config: A configuration instance.
    @return: A configured C{ZStorm} instance.
    """
    zstorm = ZStorm()
    provideUtility(zstorm)
    uri = config.get('store', 'main-uri')
    zstorm.set_default_uri('main', uri)
    return zstorm
Example #8
0
def setup_environment(cfg):
    from zope.component import getGlobalSiteManager
    from storm.zope.zstorm import ZStorm, IZStorm
    gsm = getGlobalSiteManager()
    ex = gsm.queryUtility(IZStorm)
    if ex:
        for name, store in ex.iterstores():
            ex.remove(store)
            try:
                store.close()
            except Exception:
                log.exception("Failed to close a store")
        gsm.unregisterUtility(ex)

    zs = ZStorm()
    gsm.registerUtility(zs)
    zs.set_default_uri("tilde", cfg["dburl"])
    return cfg
Example #9
0
    def get_store():
        """
        Returns a reference to Storm Store
        """
        zstorm = ZStorm()
        zstorm.set_default_uri(GLSettings.store_name, GLSettings.db_uri)

        retry_count = 0
        last_error = None
        while retry_count < 5:
            try:
                return zstorm.get(GLSettings.store_name)
            except psycopg2.OperationalError as e:
                retry_count += 1
                last_error = e if retry_count == 5 else None
                continue
        raise Exception('could not connect to database after 5 retries: %s' %
                        (last_error, ))
    def setUp(self):
        super(PatchTest, self).setUp()

        self.zstorm = ZStorm()

        self.patchdir = self.make_path()
        self.pkgdir = os.path.join(self.patchdir, "mypackage")
        os.makedirs(self.pkgdir)

        f = open(os.path.join(self.pkgdir, "__init__.py"), "w")
        f.write("shared_data = []")
        f.close()

        # Order of creation here is important to try to screw up the
        # patch ordering, as os.listdir returns in order of mtime (or
        # something).
        for pname, data in [("patch_380.py", patch_test_1),
                            ("patch_42.py", patch_test_0)]:
            self.add_module(pname, data)

        sys.path.append(self.patchdir)

        self.filename = self.make_path()
        self.uri = "sqlite:///%s" % self.filename
        self.store = self.zstorm.create(None, self.uri)

        self.store.execute("CREATE TABLE patch "
                           "(version INTEGER NOT NULL PRIMARY KEY)")

        self.assertFalse(self.store.get(Patch, (42)))
        self.assertFalse(self.store.get(Patch, (380)))

        import mypackage
        self.mypackage = mypackage
        self.patch_applier = PatchApplier(self.store, self.mypackage)

        # Create another store just to keep track of the state of the
        # whole transaction manager.  See the assertion functions below.
        self.another_store = self.zstorm.create(None, "sqlite:")
        self.another_store.execute("CREATE TABLE test (id INT)")
        self.another_store.commit()
        self.prepare_for_transaction_check()
Example #11
0
    def make(self, dependencies):
        """Create a L{ZStorm} resource to be used by tests.

        @return: A L{ZStorm} object that will be shared among all tests using
            this resource manager.
        """
        if self._zstorm is None:

            if self.use_global_zstorm:
                self._zstorm = global_zstorm
            else:
                self._zstorm = ZStorm()
            self._schema_zstorm = ZStorm()

            databases = self._databases

            # Adapt the old databases format to the new one, for backward
            # compatibility. This should be eventually dropped.
            if isinstance(databases, dict):
                databases = [{
                    "name": name,
                    "uri": uri,
                    "schema": schema
                } for name, (uri, schema) in databases.items()]

            # Provide the global IZStorm utility before applying patches, so
            # patch code can get the ztorm object if needed (e.g. looking up
            # other stores).
            provideUtility(self._zstorm)

            self._set_create_hook()

            enforce_schema = False
            for database in databases:
                name = database["name"]
                uri = database["uri"]
                schema = database.get("schema")
                schema_uri = database.get("schema-uri", uri)
                self._zstorm.set_default_uri(name, uri)
                if schema is not None:
                    # The configuration for this database does not include a
                    # schema definition, so we just setup the store (the user
                    # code should apply the schema elsewhere, if any)
                    self._schemas[name] = schema
                    self._schema_zstorm.set_default_uri(name, schema_uri)
                    schema.autocommit(False)
                    store = self._schema_zstorm.get(name)
                    if not self._sharding or self.vertical_patching:
                        self._sharding.append(Sharding())
                    sharding = self._sharding[-1]
                    sharding.add(store, schema)
                    if self._has_patch_package_changed(name, schema):
                        enforce_schema = True

            if enforce_schema:
                for sharding in self._sharding:
                    try:
                        sharding.upgrade()
                    except UnknownPatchError:
                        sharding.drop()
                        sharding.create()
                    except:
                        # An unknown error occured, let's drop all timestamps
                        # so subsequent runs won't assume that everything is
                        # fine
                        self._purge_schema_stamp_dir()
                        raise
                    else:
                        sharding.delete()

            # Commit all schema changes across all stores
            transaction.commit()

        elif getUtility(IZStorm) is not self._zstorm:
            # This probably means that the test code has overwritten our
            # utility, let's re-register it.
            provideUtility(self._zstorm)

        return self._zstorm
Example #12
0
 def setUp(self):
     self.zstorm = ZStorm()
Example #13
0
 def test_utility(self):
     provideUtility(ZStorm())
     self.assertTrue(isinstance(getUtility(IZStorm), ZStorm))
Example #14
0
    def make(self, dependencies):
        """Create a L{ZStorm} resource to be used by tests.

        @return: A L{ZStorm} object that will be shared among all tests using
            this resource manager.
        """
        if self._zstorm is None:

            if self.use_global_zstorm:
                zstorm = global_zstorm
            else:
                zstorm = ZStorm()
            schema_zstorm = ZStorm()
            databases = self._databases

            # Adapt the old databases format to the new one, for backward
            # compatibility. This should be eventually dropped.
            if isinstance(databases, dict):
                databases = [{
                    "name": name,
                    "uri": uri,
                    "schema": schema
                } for name, (uri, schema) in databases.iteritems()]

            # Provide the global IZStorm utility before applying patches, so
            # patch code can get the ztorm object if needed (e.g. looking up
            # other stores).
            provideUtility(zstorm)

            for database in databases:
                name = database["name"]
                uri = database["uri"]
                zstorm.set_default_uri(name, uri)
                schema = database.get("schema")
                if schema is None:
                    # The configuration for this database does not include a
                    # schema definition, so we just setup the store (the user
                    # code should apply the schema elsewhere, if any)
                    continue
                schema_uri = database.get("schema-uri", uri)
                self._schemas[name] = schema
                schema_zstorm.set_default_uri(name, schema_uri)
                store = zstorm.get(name)
                self._set_commit_proxy(store)
                schema_store = schema_zstorm.get(name)
                # Disable schema autocommits, we will commit everything at once
                schema.autocommit(False)
                try:
                    schema.upgrade(schema_store)
                except UnknownPatchError:
                    schema.drop(schema_store)
                    schema_store.commit()
                    schema.upgrade(schema_store)
                else:
                    # Clean up tables here to ensure that the first test run
                    # starts with an empty db
                    schema.delete(schema_store)

            # Commit all schema changes across all stores
            transaction.commit()

            self._zstorm = zstorm
            self._schema_zstorm = schema_zstorm

        elif getUtility(IZStorm) is not self._zstorm:
            # This probably means that the test code has overwritten our
            # utility, let's re-register it.
            provideUtility(self._zstorm)

        return self._zstorm