postgresql - How do I get alembic to emit custom DDL on after_create? -
i've got couple of custom ddl statements want run after create table:
update_function = ddl(""" create or replace function update_timestamp() returns trigger $$ begin new.updated_at = now(); return new; end; $$ language 'pgplsql'; """) update_trigger = ddl(""" create trigger update %(table)s_timestamp before update on %(table)s each row execute procedure update_timestamp(); """)
and i've attached them this:
event.listen(session.__table__, 'after_create', update_function) event.listen(session.__table__, 'after_create', update_trigger)
when create_all
, sql expect:
create or replace function update_timestamp() returns trigger $$ begin new.updated_at = now(); return new; end; $$ language 'pgplsql'; create trigger update session_timestamp before update on session each row execute procedure update_timestamp();
but when upgrade using alembic, statements don't appear:
-- running upgrade c0d470e5c81 -> 6692fad7378 create table session ( created_at timestamp without time zone default 'current_timestamp', updated_at timestamp without time zone default 'current_timestamp', id varchar(32) not null, owner_id integer, database_id varchar(32), content text, primary key (id), foreign key(database_id) references database (id), foreign key(owner_id) references users (id) ); insert alembic_version (version_num) values ('6692fad7378');
is there way alembic trigger 'after_create' events?
the table level before_create/after_create events emitted (just not metadata level ones). need make sure whatever happens in env.py script involves event listeners being set up.
the code have here looks little suspect:
event.listen(session.__table__, 'after_create', update_function) event.listen(session.__table__, 'after_create', update_trigger)
session.__table__
here single table
instance , that's not you'd see in alembic script. alembic create_table
command creates table
locally , runs create on it, you'd need listen table objects globally:
from sqlalchemy import table event.listen(table, 'after_create', update_function) event.listen(table, 'after_create', update_trigger)
if these events 1 specific table though, wouldn't use events, you'd put ddl() triggers directly in migration script, right after calls create_table()
.
Comments
Post a Comment