Friday 15 April 2011

sql - Is it possible to rollback create_all in sqlalchemy? -


According to the question for some DBMS, it is possible that it is possible to prepare rollback table statements. Especially it is possible for SQLite (although it is undocumented).

So my question is, is it possible to rollback everyone in sub-section? I was trying to write some test codes, but it does not look like:

  & gt; & Gt; & Gt; Engine = Creating_Agin ('sqlite: ///: Memory:') & gt; & Gt; & Gt; Engine engine (sqlite: ///: memory :) & gt; & Gt; & Gt; Session = session maker (bind = engine) & gt; & Gt; & Gt; Connection = engine.connect ()> gt; & Gt; & Gt; Session = session (dam = connection) & gt; & Gt; & Gt; Engine.table_names () []> & Gt; & Gt; Transaction = connection.begin () & gt; & Gt; & Gt; Base = declarative_base ()> gt; & Gt; & Gt; Class test (base): ... __tablename__ = 'test' ... id = column (integer, primary_key = true) ... & gt; & Gt; & Gt; Base.metadata.bind = Engine & gt; & Gt; & Gt; Base.metadata.create_all () & gt; & Gt; & Gt; Engine.table_names () [u'TEST '] & gt; & Gt; & Gt; Transaction.rollback ()> gt; & Gt; & Gt; Session.close () & gt; & Gt; & Gt; Connection.close () & gt; & Gt; & Gt;  
  • Use Postgresql or SQL Server MySQL supports DDL for Oracle not transaction Although recent versions of SQLite have DDL support of transactions Python.

  • Simple recipe:

     with  engine. (Like) conn: metadata.create_all (conn)   

    If you increase an exception inside "with:", the transaction will be brought back.

  • Want to see it yourself, okay: to inspect sqlalchemy import from need to run 0.8 with the engine is. Conn: trans = conn.begin () metadata.create_all (conn) inspector = inspection (conn) table_names = to gain an understanding of the transactional scope, inspector.gate_table_name () trans. Rollback () inspector = inspection (con) rolled_back_table_names = inspector.get_table_names ()

    I recommend that you read

  • No comments:

    Post a Comment