Here are the examples of the python api sqlalchemy.DDL taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
5 Examples
3
View Complete Implementation : 20170123151655_add_trigger_for_meta_updated.py
Copyright MIT License
Author : opentrials
Copyright MIT License
Author : opentrials
def upgrade():
conn = op.get_bind()
func = sa.DDL("""CREATE FUNCTION set_meta_updated()
RETURNS TRIGGER
LANGUAGE plpgsql
AS $$
BEGIN
NEW.meta_updated := now();
RETURN NEW;
END;
$$;""")
conn.execute(func)
for table in updatable_tables:
trigger_params = {'trigger': ('%s_set_meta_updated' % table), 'table': table}
trigger = ("""CREATE TRIGGER %(trigger)s
BEFORE UPDATE ON %(table)s
FOR EACH ROW EXECUTE PROCEDURE set_meta_updated();""" % trigger_params)
conn.execute(trigger)
3
View Complete Implementation : 20170123151655_add_trigger_for_meta_updated.py
Copyright MIT License
Author : opentrials
Copyright MIT License
Author : opentrials
def downgrade():
conn = op.get_bind()
for table in updatable_tables:
trigger_params = {'trigger': ('%s_set_meta_updated' % table), 'table': table}
trigger = ('DROP TRIGGER %(trigger)s ON %(table)s;' % trigger_params)
conn.execute(trigger)
conn.execute(sa.DDL('DROP FUNCTION set_meta_updated();'))
0
View Complete Implementation : test_query.py
Copyright MIT License
Author : sqlalchemy
Copyright MIT License
Author : sqlalchemy
@clastmethod
def setup_clast(cls):
global metadata, cattable, matchtable
metadata = MetaData(testing.db)
cattable = Table(
"cattable",
metadata,
Column("id", Integer),
Column("description", String(50)),
PrimaryKeyConstraint("id", name="PK_cattable"),
)
matchtable = Table(
"matchtable",
metadata,
Column("id", Integer),
Column("satle", String(200)),
Column("category_id", Integer, ForeignKey("cattable.id")),
PrimaryKeyConstraint("id", name="PK_matchtable"),
)
DDL(
"""CREATE FULLTEXT INDEX
ON cattable (description)
KEY INDEX PK_cattable"""
).execute_at("after-create", matchtable)
DDL(
"""CREATE FULLTEXT INDEX
ON matchtable (satle)
KEY INDEX PK_matchtable"""
).execute_at("after-create", matchtable)
metadata.create_all()
cattable.insert().execute(
[
{"id": 1, "description": "Python"},
{"id": 2, "description": "Ruby"},
]
)
matchtable.insert().execute(
[
{
"id": 1,
"satle": "Web Development with Rails",
"category_id": 2,
},
{"id": 2, "satle": "Dive Into Python", "category_id": 1},
{
"id": 3,
"satle": "Programming Matz's Ruby",
"category_id": 2,
},
{"id": 4, "satle": "Guide to Django", "category_id": 1},
{"id": 5, "satle": "Python in a Nutshell", "category_id": 1},
]
)
DDL("WAITFOR DELAY '00:00:05'").execute(bind=engines.testing_engine())
0
View Complete Implementation : test_reflection.py
Copyright MIT License
Author : sqlalchemy
Copyright MIT License
Author : sqlalchemy
def setup(self):
self.col_num = 150
self.metadata = MetaData(testing.db)
t = Table(
"base_table",
self.metadata,
*[
Column("long_named_column_number_%d" % i, Integer)
for i in range(self.col_num)
]
)
self.view_str = view_str = (
"CREATE VIEW huge_named_view AS SELECT %s FROM base_table"
% (
",".join(
"long_named_column_number_%d" % i
for i in range(self.col_num)
)
)
)
astert len(view_str) > 4000
event.listen(t, "after_create", DDL(view_str))
event.listen(t, "before_drop", DDL("DROP VIEW huge_named_view"))
self.metadata.create_all()
0
View Complete Implementation : test_reflection.py
Copyright MIT License
Author : sqlalchemy
Copyright MIT License
Author : sqlalchemy
@testing.provide_metadata
def _run_test(self, specs, attributes):
columns = [Column("c%i" % (i + 1), t[0]) for i, t in enumerate(specs)]
# Early 5.0 releases seem to report more "general" for columns
# in a view, e.g. char -> varchar, tinyblob -> mediumblob
use_views = testing.db.dialect.server_version_info > (5, 0, 10)
m = self.metadata
Table("mysql_types", m, *columns)
if use_views:
event.listen(
m,
"after_create",
DDL(
"CREATE OR REPLACE VIEW mysql_types_v "
"AS SELECT * from mysql_types"
),
)
event.listen(
m, "before_drop", DDL("DROP VIEW IF EXISTS mysql_types_v")
)
m.create_all()
m2 = MetaData(testing.db)
tables = [Table("mysql_types", m2, autoload=True)]
if use_views:
tables.append(Table("mysql_types_v", m2, autoload=True))
for table in tables:
for i, (reflected_col, spec) in enumerate(zip(table.c, specs)):
expected_spec = spec[1]
reflected_type = reflected_col.type
is_(type(reflected_type), type(expected_spec))
for attr in attributes:
eq_(
getattr(reflected_type, attr),
getattr(expected_spec, attr),
"Column %s: Attribute %s value of %s does not "
"match %s for type %s"
% (
"c%i" % (i + 1),
attr,
getattr(reflected_type, attr),
getattr(expected_spec, attr),
spec[0],
),
)