hello -
you'd need to either run the appropriate SQL to detect if this schema exists (this is database dependent, see http://docs.sqlalchemy.org/en/latest/core/ddl.html#controlling-ddl-sequences for an example), or just emit the statement "CREATE SCHEMA IF NOT EXISTS <foobar>":
from sqlalchemy import MetaData, Integer, Table, Column, event, create_engine
from sqlalchemy.schema import CreateSchema
from sqlalchemy import exc
m = MetaData()
t = Table(
't', m, Column('x', Integer),
schema="some_schema"
)
def should_create(ddl, target, connection, **kw):
try:
connection.execute(
"SHOW CREATE SCHEMA %s" %
ddl.element).scalar()
return False
except exc.DBAPIError:
return True
event.listen(
m, "before_create",
CreateSchema("some_schema").execute_if(
callable_=should_create
)
)
e = create_engine("mysql://scott:tiger@localhost/test", echo=True)
m.create_all(e)
hello -
you'd need to either run the appropriate SQL to detect if this schema exists (this is database dependent, see http://docs.sqlalchemy.org/en/latest/core/ddl.html#controlling-ddl-sequences for an example), or just emit the statement "CREATE SCHEMA IF NOT EXISTS <foobar>":