Skip to content

Instantly share code, notes, and snippets.

@whosaysni
Created August 6, 2014 03:30
Show Gist options
  • Save whosaysni/49de8a33e93ff8475b18 to your computer and use it in GitHub Desktop.
Save whosaysni/49de8a33e93ff8475b18 to your computer and use it in GitHub Desktop.
Leak me CREATE TABLE / SQLAlchemy で実行直前のCREATE TABLEを横取りする
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column
from sqlalchemy import Integer, DateTime, String, Text
from sqlalchemy import UniqueConstraint, Sequence
from sqlalchemy import create_engine
Base = declarative_base()
class SomeTable(Base):
__tablename__ = 'some_nice_table'
__table_args__ = (
UniqueConstraint('name', 'birthday'),
)
id = Column(Integer, Sequence('some_table_id_seq'), primary_key=True)
name = Column(String(256))
birthday = Column(DateTime)
memo = Column(Text)
sq_engine = create_engine('sqlite://')
pg_engine = create_engine('postgres://localhost/test')
sq_con = sq_engine.connect()
pg_con = pg_engine.connect()
def leakme(self, object, *multiparams, **params):
print object
print multiparams
print params
sq_con.__class__.execute = leakme
pg_con.__class__.execute = leakme
print '================= SQLITE ================='
SomeTable.__table__.create(bind=sq_engine)
print '================= POSTGRESQL ================='
SomeTable.__table__.create(bind=pg_engine)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment