source: mauRepo/MolesManager/trunk/src/drop_everything.py @ 8089

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/mauRepo/MolesManager/trunk/src/drop_everything.py@8291
Revision 8089, 1.1 KB checked in by mnagni, 8 years ago (diff)

Played with DOJO
New EA library
Addede CEDA_Process to the migration

Line 
1'''
2Created on 14 Feb 2012
3
4@author: mnagni
5'''
6from sqlalchemy.engine import reflection
7from sqlalchemy import create_engine
8from sqlalchemy.schema import (
9    MetaData,
10    Table,
11    DropTable,
12    ForeignKeyConstraint,
13    DropConstraint,
14    )
15
16engine = create_engine('mssql+pyodbc://scott:tiger@ms_2005/', echo=True)
17
18conn = engine.connect()
19
20# the transaction only applies if the DB supports
21# transactional DDL, i.e. Postgresql, MS SQL Server
22trans = conn.begin()
23
24inspector = reflection.Inspector.from_engine(engine)
25
26# gather all data first before dropping anything.
27# some DBs lock after things have been dropped in
28# a transaction.
29
30metadata = MetaData()
31
32tbs = []
33all_fks = []
34
35for table_name in inspector.get_table_names():
36    fks = []
37    for fk in inspector.get_foreign_keys(table_name):
38        if not fk['name']:
39            continue
40        fks.append(
41            ForeignKeyConstraint((),(),name=fk['name'])
42            )
43    t = Table(table_name,metadata,*fks)
44    tbs.append(t)
45    all_fks.extend(fks)
46
47for fkc in all_fks:
48    conn.execute(DropConstraint(fkc))
49
50for table in tbs:
51    conn.execute(DropTable(table))
52
53trans.commit()
54
Note: See TracBrowser for help on using the repository browser.