def list_databases(): """ return the name of the databases found in the BDD the result can be filtering by the Configuration entry ``db_filter`` ..warning:: For the moment only the ``prostgresql`` dialect is available :rtype: list of the database's names """ url = Configuration.get_url() db_filter = Configuration.get('db_filter') text = None if url.drivername in ('postgres', 'postgresql'): url = Configuration.get_url(db_name='postgres') text = "SELECT datname FROM pg_database" if db_filter: db_filter = db_filter.replace('%', '%%') text += " where datname like '%s'" % db_filter if text is None: return [] engine = create_engine(url) return [x[0] for x in engine.execute(text).fetchall() if x[0] not in ('template1', 'template0', 'postgres')]
def test_get_url_without_drivername(self): Configuration.update( db_name=None, db_driver_name=None, db_host=None, db_user_name=None, db_password=None, db_port=None) with self.assertRaises(ConfigurationException): Configuration.get_url()
def test_get_url2(self): Configuration.update( db_name='anyblok', db_driver_name='postgres', db_host='localhost', db_user_name=None, db_password=None, db_port=None,) url = Configuration.get_url(db_name='anyblok2') self.check_url(url, 'postgres://localhost/anyblok2')
def test_get_url5(self): Configuration.update( db_url='postgres:///anyblok', db_name='anyblok2', db_driver_name=None, db_host=None, db_user_name='jssuzanne', db_password='******', db_port=None) url = Configuration.get_url(db_name='anyblok3') self.check_url(url, 'postgres://*****:*****@/anyblok3')
def test_get_url3(self): Configuration.update( db_url='postgres:///anyblok', db_name=None, db_driver_name=None, db_host=None, db_user_name=None, db_password=None, db_port=None) url = Configuration.get_url() self.check_url(url, 'postgres:///anyblok')
def drop_database(database): """ Close the registry instance of the database and drop the database :param: database's name """ url = Configuration.get_url(db_name=database) if not database_exists(url): raise Exception("Database %r does not already exist") registry = RegistryManager.get(database) registry.close() SU_drop_database(url)
def dropdb(cls): """Drop the database specified in configuration. :: cls.init_configuration_manager() cls.dropdb() """ url = Configuration.get_url() if database_exists(url): drop_database(url)
def create_database(database): """ Create a new database, initialize it and return an AnyBlok registry :param: database's name :rtype: AnyBlok registry instance """ url = Configuration.get_url(db_name=database) if database_exists(url): raise Exception("Database %r already exist") db_template_name = Configuration.get('db_template_name', None) SU_create_database(url, template=db_template_name) registry = RegistryManager.get(database) return registry
def createdb(cls, keep_existing=False): """Create the database specified in configuration. :: cls.init_configuration_manager() cls.createdb() :param keep_existing: If false drop the previous db before create it """ url = Configuration.get_url() db_template_name = Configuration.get('db_template_name', None) if database_exists(url): if keep_existing: return False drop_database(url) create_database(url, template=db_template_name) return True