source: cows_wps/trunk/clear_db.py @ 5615

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows_wps/trunk/clear_db.py@7084
Revision 5615, 1.5 KB checked in by spascoe, 10 years ago (diff)

COWS WPS package copied from
 http://proj.badc.rl.ac.uk/svn/dcip/cows-wps/trunk.

This is a stripped down version of the DDP WPS. Some features are
removed and others have been deactivated until we reimplement them in a
more generic way.

Line 
1#!/usr/bin/env python
2"""
3Delete all entries in the database.
4
5"""
6
7import sys
8from ConfigParser import SafeConfigParser
9from cows_wps.model.orm import tables
10import sqlalchemy as SA
11
12def clear_db(engine):
13    """
14    Clear all data from the database.
15   
16    This should go somewhere else.
17
18    """
19    from cows_wps.model.orm import tables
20    conn = engine.connect()
21    t = conn.begin()
22    try:
23        conn.execute(tables.cache.delete())
24        conn.execute(tables.request.delete())
25        conn.execute(tables.sge_job.delete())
26        conn.execute(tables.job.delete())
27        conn.execute('drop table cache')
28        conn.execute('drop table request')
29        conn.execute('drop table sge_job')
30        conn.execute('drop table job')
31        t.commit()
32    except:
33        t.rollback()
34        raise
35
36
37def main(args=sys.argv[1:]):
38    try:
39        wps_ini = args[0]
40    except IndexError:
41        wps_ini = './wps.ini'
42
43    c = SafeConfigParser()
44    c.read(wps_ini)
45
46    db_con_url = c.get('app:main', 'wps.database_url')
47
48    print '''============================================================================
49WARNING: Do you really want to delete all data from the database?
50URL = %s
51(y/n): ''' % db_con_url,
52    line = sys.stdin.readline()
53    if line.strip()[0].lower() != 'y':
54        print 'Aborted.'
55        raise SystemExit(1)
56
57    engine = SA.create_engine(db_con_url)
58    clear_db(engine)
59    print 'Done.'
60    print 'Now run "paster setup-app %s" to recreate the database.' % wps_ini
61    print
62
63if __name__ == '__main__':
64    main()
Note: See TracBrowser for help on using the repository browser.