| 1 | # Copyright (C) 2007 Adriano Monteiro Marques |
|---|
| 2 | # |
|---|
| 3 | # Author: Guilherme Polo <ggpolo@gmail.com> |
|---|
| 4 | # |
|---|
| 5 | # This program is free software; you can redistribute it and/or modify |
|---|
| 6 | # it under the terms of the GNU General Public License as published by |
|---|
| 7 | # the Free Software Foundation; either version 2 of the License, or |
|---|
| 8 | # (at your option) any later version. |
|---|
| 9 | # |
|---|
| 10 | # This program is distributed in the hope that it will be useful, |
|---|
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | # GNU General Public License for more details. |
|---|
| 14 | # |
|---|
| 15 | # You should have received a copy of the GNU General Public License |
|---|
| 16 | # along with this program; if not, write to the Free Software |
|---|
| 17 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
|---|
| 18 | # USA |
|---|
| 19 | |
|---|
| 20 | """ |
|---|
| 21 | Setup database for umit. |
|---|
| 22 | """ |
|---|
| 23 | |
|---|
| 24 | import os |
|---|
| 25 | import sys |
|---|
| 26 | |
|---|
| 27 | _UMIT_ROOT = os.path.abspath( |
|---|
| 28 | os.path.join( |
|---|
| 29 | os.path.dirname(os.path.abspath(__file__)), os.pardir, os.pardir)) |
|---|
| 30 | _SQL_PATH = os.path.join(_UMIT_ROOT, 'share', 'umit', 'sql') |
|---|
| 31 | print _UMIT_ROOT, _SQL_PATH |
|---|
| 32 | sys.path.insert(0, _UMIT_ROOT) |
|---|
| 33 | from umitDB._database import sql, database |
|---|
| 34 | |
|---|
| 35 | def sqlfile_path(sqlname): |
|---|
| 36 | return os.path.join(_SQL_PATH, '%s-%s.sql' % (database, sqlname)) |
|---|
| 37 | |
|---|
| 38 | def acquire_conn_cursor(db): |
|---|
| 39 | """ |
|---|
| 40 | Get connection and cursor from database. |
|---|
| 41 | """ |
|---|
| 42 | conn = sql.connect(db) |
|---|
| 43 | cursor = conn.cursor() |
|---|
| 44 | |
|---|
| 45 | return conn, cursor |
|---|
| 46 | |
|---|
| 47 | def setup_tables(conn, cursor): |
|---|
| 48 | """ |
|---|
| 49 | Create all tables for database. |
|---|
| 50 | """ |
|---|
| 51 | tables = open(sqlfile_path('schema'), 'r').readlines() |
|---|
| 52 | tables = ''.join(line for line in tables) |
|---|
| 53 | |
|---|
| 54 | cursor.executescript(tables) |
|---|
| 55 | conn.commit() |
|---|
| 56 | |
|---|
| 57 | def setup_triggers(conn, cursor): |
|---|
| 58 | """ |
|---|
| 59 | Setup triggers for database. |
|---|
| 60 | """ |
|---|
| 61 | insert_triggers = open(sqlfile_path('insert-triggers'), 'r').readlines() |
|---|
| 62 | insert_triggers = ''.join(line for line in insert_triggers) |
|---|
| 63 | |
|---|
| 64 | update_triggers = open(sqlfile_path('update-triggers'), 'r').readlines() |
|---|
| 65 | update_triggers = ''.join(line for line in update_triggers) |
|---|
| 66 | |
|---|
| 67 | delete_triggers = open(sqlfile_path('delete-triggers'), 'r').readlines() |
|---|
| 68 | delete_triggers = ''.join(line for line in delete_triggers) |
|---|
| 69 | |
|---|
| 70 | cursor.executescript(insert_triggers) |
|---|
| 71 | cursor.executescript(update_triggers) |
|---|
| 72 | cursor.executescript(delete_triggers) |
|---|
| 73 | conn.commit() |
|---|
| 74 | |
|---|
| 75 | def setup_database(conn, cursor): |
|---|
| 76 | """ |
|---|
| 77 | Setup database. |
|---|
| 78 | """ |
|---|
| 79 | setup_tables(conn, cursor) |
|---|
| 80 | setup_triggers(conn, cursor) |
|---|
| 81 | |
|---|
| 82 | def drop_tables(conn, cursor): |
|---|
| 83 | """ |
|---|
| 84 | Drops all tables in database. |
|---|
| 85 | """ |
|---|
| 86 | drop_tables = open(sqlfile_path('drop-tables'), 'r').readlines() |
|---|
| 87 | drop_tables = ''.join(line for line in drop_tables) |
|---|
| 88 | |
|---|
| 89 | cursor.executescript(drop_tables) |
|---|
| 90 | conn.commit() |
|---|
| 91 | |
|---|
| 92 | def drop_triggers(conn, cursor): |
|---|
| 93 | """ |
|---|
| 94 | Drops all triggers in database. |
|---|
| 95 | """ |
|---|
| 96 | drop_triggers = open(sqlfile_path('drop-triggers'), 'r').readlines() |
|---|
| 97 | drop_triggers = ''.join(line for line in drop_triggers) |
|---|
| 98 | |
|---|
| 99 | cursor.executescript(drop_triggers) |
|---|
| 100 | conn.commit() |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | def clear_database(conn, cursor): |
|---|
| 104 | """ |
|---|
| 105 | Clear database. |
|---|
| 106 | """ |
|---|
| 107 | |
|---|
| 108 | drop_triggers(conn, cursor) |
|---|
| 109 | drop_tables(conn, cursor) |
|---|
| 110 | cursor.execute("VACUUM") |
|---|
| 111 | |
|---|
| 112 | conn.commit() |
|---|
| 113 | |
|---|
| 114 | # When invoked set up umit new generation database. |
|---|
| 115 | if __name__ == "__main__": |
|---|
| 116 | db = os.path.join(_UMIT_ROOT, 'share', 'umit', 'config', 'umitng.db') |
|---|
| 117 | conn, cursor = acquire_conn_cursor(db) |
|---|
| 118 | try: |
|---|
| 119 | setup_database(conn, cursor) |
|---|
| 120 | except sql.OperationalError, err: |
|---|
| 121 | sys.stderr.write("Creating a clean database failed!\n" |
|---|
| 122 | "\tReason: %s\n\n" % err) |
|---|
| 123 | raw_input( |
|---|
| 124 | "Press ENTER to clear the current database and create a new one") |
|---|
| 125 | clear_database(conn, cursor) |
|---|
| 126 | setup_database(conn, cursor) |
|---|
| 127 | |
|---|