本文整理汇总了Python中sqlalchemy_utils.database_exists方法的典型用法代码示例。如果您正苦于以下问题:Python sqlalchemy_utils.database_exists方法的具体用法?Python sqlalchemy_utils.database_exists怎么用?Python sqlalchemy_utils.database_exists使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy_utils
的用法示例。
在下文中一共展示了sqlalchemy_utils.database_exists方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _setup_realdb
# 需要导入模块: import sqlalchemy_utils [as 别名]
# 或者: from sqlalchemy_utils import database_exists [as 别名]
def _setup_realdb(realdburl):
"""
Called when we want to run unit tests against a real DB.
This is useful since different DB drivers are pickier about queries etc
(such as pyscopg2 and postgres)
"""
from sqlalchemy import create_engine
from sqlalchemy_utils import database_exists, create_database
db_name = "flask_security_test_%s" % str(time.time()).replace(".", "_")
db_uri = realdburl + db_name
engine = create_engine(db_uri)
if not database_exists(engine.url):
create_database(engine.url)
print("Setting up real DB at " + db_uri)
return db_uri, {"engine": engine}
示例2: exists
# 需要导入模块: import sqlalchemy_utils [as 别名]
# 或者: from sqlalchemy_utils import database_exists [as 别名]
def exists(self, db=None):
"""
check for existence of a database object
"""
check_db = db if db is not None else self.db_name
# TODO: do this only for SQLAlchemy
if not self.__conn:
return False
database_url = self.set_url_db(self.db_url, check_db)
logger.debug("check database URL '{}'".format(database_url))
try:
if sqlalchemy_utils.database_exists(database_url):
logger.debug("DB '{}' exists".format(check_db))
return True
except sqlalchemy.exc.NoSuchModuleError as me:
logger.error("cannot check if database {} exists: {}".
format(check_db, me))
raise osdbError("cannot handle {} dialect".
format(self.dialect)) from None
logger.debug("DB does not exist")
return False
示例3: init_db
# 需要导入模块: import sqlalchemy_utils [as 别名]
# 或者: from sqlalchemy_utils import database_exists [as 别名]
def init_db(db_url):
from sqlalchemy_utils import database_exists
extra_db_args = {'echo': True}
if is_sqlite_db(db_url):
extra_db_args['connect_args'] = {'check_same_thread': False}
extra_db_args['echo'] = False
engine = create_engine(db_url, **extra_db_args)
if not database_exists(engine.url) and not is_sqlite_db(db_url):
raise BentoMLException(
f'Database does not exist or Database name is missing in config '
f'db.url: {db_url}'
)
create_all_or_upgrade_db(engine, db_url)
return sessionmaker(bind=engine)
示例4: postgres_test_db
# 需要导入模块: import sqlalchemy_utils [as 别名]
# 或者: from sqlalchemy_utils import database_exists [as 别名]
def postgres_test_db():
"""
Fixture to yield a PostgreSQL database to be used in the unit tests.
The database will only be created once per test session, but the
workflow_runs table will be cleared for each test.
"""
postgres_test_db = testing.postgresql.Postgresql()
engine = create_engine(postgres_test_db.url())
from sqlalchemy_utils import database_exists, create_database
if not database_exists(engine.url):
create_database(engine.url)
Base.metadata.create_all(bind=engine, tables=[WorkflowRuns.__table__])
yield postgres_test_db
postgres_test_db.stop()
示例5: __init__
# 需要导入模块: import sqlalchemy_utils [as 别名]
# 或者: from sqlalchemy_utils import database_exists [as 别名]
def __init__(self, database="featurehub"):
"""Create the ORMManager and connect to DB.
Parameters
----------
database : str, optional (default="featurehub")
Name of database within DBMS.
"""
self.__orm = ORMManager(database)
if not database_exists(self.__orm.engine.url):
print("Database {} does not exist.".format(database),
file=sys.stderr)
print("You might want to create it by calling set_up method",
file=sys.stderr)
示例6: infer_uri
# 需要导入模块: import sqlalchemy_utils [as 别名]
# 或者: from sqlalchemy_utils import database_exists [as 别名]
def infer_uri(base, source, engine, sys):
if ':' in source:
if engine:
raise Exception(f'Do not specify engine with (what looks like) a URI')
uri = source
elif len(source) < 8:
if not engine:
current = sys.get_constant(SystemConstant.DB_URI, none=True)
if not current:
raise Exception(f'Specify {mm(SQLITE)} or {mm(POSTGRESQL)}')
engine = scheme(current)
if engine == SQLITE:
uri = sqlite_uri(base, version=source)
elif engine == POSTGRESQL:
uri = postgresql_uri(version=source)
else:
raise Exception(f'Unknown engine {engine}')
else:
raise Exception(f'Unexpected version or URI {source}')
if database_exists(uri):
return uri
else:
raise Exception(f'No database at {uri}')
示例7: delete
# 需要导入模块: import sqlalchemy_utils [as 别名]
# 或者: from sqlalchemy_utils import database_exists [as 别名]
def delete(uri, sys):
try:
if database_exists(uri):
log.debug(f'Deleting database at {uri}')
uri_parts = urisplit(uri)
if uri_parts.scheme == SQLITE:
path = clean_path(uri_parts.path)
log.warning(f'Deleting {path}')
unlink(path)
elif uri_parts.scheme == POSTGRESQL:
drop_database(uri)
else:
raise Exception(f'Unsupported URI {uri}')
else:
log.warning(f'No database at {uri} (so not deleting)')
finally:
sys.delete_constant(SystemConstant.DB_URI)
sys.delete_constant(SystemConstant.DB_VERSION)
示例8: create_db
# 需要导入模块: import sqlalchemy_utils [as 别名]
# 或者: from sqlalchemy_utils import database_exists [as 别名]
def create_db():
if not database_exists(DB_URI):
LOG.debug("db not exist, create database")
create_database(DB_URI)
# Create all tables
# Use flask-migrate instead of db.create_all()
flask_migrate.upgrade()
示例9: reset_db
# 需要导入模块: import sqlalchemy_utils [as 别名]
# 或者: from sqlalchemy_utils import database_exists [as 别名]
def reset_db():
if database_exists(DB_URI):
drop_database(DB_URI)
create_db()
示例10: _fail_unless_database_exists
# 需要导入模块: import sqlalchemy_utils [as 别名]
# 或者: from sqlalchemy_utils import database_exists [as 别名]
def _fail_unless_database_exists(engine):
if not database_exists(engine.url):
raise DatabaseNotInitializedError('Database must be created for this operation')
示例11: _ensure_project_exists
# 需要导入模块: import sqlalchemy_utils [as 别名]
# 或者: from sqlalchemy_utils import database_exists [as 别名]
def _ensure_project_exists(self):
if not database_exists(self._engine.url):
create_database(self._engine.url)
Base.metadata.create_all(self._engine)
session = self._Session()
if not session.query(Project).filter(Project.project_name == self.project_name).first():
project = Project(project_name=self.project_name)
session.add(project)
session.commit()
session.close()
示例12: __init__
# 需要导入模块: import sqlalchemy_utils [as 别名]
# 或者: from sqlalchemy_utils import database_exists [as 别名]
def __init__(self, project_name, database_url=_default_database_url):
start_time = datetime.datetime.now()
self.project_name = project_name
self._engine = create_engine(database_url)
self._Session = sessionmaker(bind=self._engine)
self._init_properties()
if database_exists(self._engine.url):
session = self._Session()
self._build_repository_map(session)
self._build_author_map(session)
self._build_commit_map(session)
session.close()
print('Init time {}'.format(datetime.datetime.now() - start_time))
示例13: createdb_command
# 需要导入模块: import sqlalchemy_utils [as 别名]
# 或者: from sqlalchemy_utils import database_exists [as 别名]
def createdb_command():
"""Creates the database + tables."""
from sqlalchemy_utils import database_exists, create_database
if not database_exists(DB_URL):
print('Creating database.')
create_database(DB_URL)
print('Creating tables.')
db.create_all()
print('Shiny!')
示例14: resetdb_command
# 需要导入模块: import sqlalchemy_utils [as 别名]
# 或者: from sqlalchemy_utils import database_exists [as 别名]
def resetdb_command():
"""Destroys and creates the database + tables."""
from sqlalchemy_utils import database_exists, create_database, drop_database
if database_exists(DB_URL):
print('Deleting database.')
drop_database(DB_URL)
if not database_exists(DB_URL):
print('Creating database.')
create_database(DB_URL)
#db.drop_all()
print('Creating tables.')
db.create_all()
print('Shiny!')
示例15: __create_schema
# 需要导入模块: import sqlalchemy_utils [as 别名]
# 或者: from sqlalchemy_utils import database_exists [as 别名]
def __create_schema() -> None:
engine = create_engine(APP.config['SQLALCHEMY_DATABASE_URI'])
if not database_exists(engine.url):
create_database(engine.url)
db.DB.create_all()
engine.dispose()