|
1 | | -# $Id: rdbms_common.py,v 1.75 2004-02-11 23:55:09 richard Exp $ |
| 1 | +# $Id: rdbms_common.py,v 1.76 2004-03-05 00:08:09 richard Exp $ |
2 | 2 | ''' Relational database (SQL) backend common code. |
3 | 3 |
|
4 | 4 | Basics: |
|
19 | 19 | probably a bit of work to be done if a database is used that actually |
20 | 20 | honors column typing, since the initial databases don't (sqlite stores |
21 | 21 | everything as a string.) |
| 22 | +
|
| 23 | +The schema of the hyperdb being mapped to the database is stored in the |
| 24 | +database itself as a repr()'ed dictionary of information about each Class |
| 25 | +that maps to a table. If that information differs from the hyperdb schema, |
| 26 | +then we update it. We also store in the schema dict a __version__ which |
| 27 | +allows us to upgrade the database schema when necessary. See upgrade_db(). |
22 | 28 | ''' |
23 | 29 | __docformat__ = 'restructuredtext' |
24 | 30 |
|
@@ -77,7 +83,9 @@ def clearCache(self): |
77 | 83 | self.cache_lru = [] |
78 | 84 |
|
79 | 85 | def sql_open_connection(self): |
80 | | - ''' Open a connection to the database, creating it if necessary |
| 86 | + ''' Open a connection to the database, creating it if necessary. |
| 87 | +
|
| 88 | + Must call self.load_dbschema() |
81 | 89 | ''' |
82 | 90 | raise NotImplemented |
83 | 91 |
|
@@ -106,24 +114,26 @@ def sql_stringquote(self, value): |
106 | 114 | ''' |
107 | 115 | return re.sub("'", "''", str(value)) |
108 | 116 |
|
| 117 | + def load_dbschema(self): |
| 118 | + ''' Load the schema definition that the database currently implements |
| 119 | + ''' |
| 120 | + self.cursor.execute('select schema from schema') |
| 121 | + self.database_schema = eval(self.cursor.fetchone()[0]) |
| 122 | + |
109 | 123 | def save_dbschema(self, schema): |
110 | 124 | ''' Save the schema definition that the database currently implements |
111 | 125 | ''' |
112 | 126 | s = repr(self.database_schema) |
113 | 127 | self.sql('insert into schema values (%s)', (s,)) |
114 | 128 |
|
115 | | - def load_dbschema(self): |
116 | | - ''' Load the schema definition that the database currently implements |
117 | | - ''' |
118 | | - self.cursor.execute('select schema from schema') |
119 | | - return eval(self.cursor.fetchone()[0]) |
120 | | - |
121 | 129 | def post_init(self): |
122 | 130 | ''' Called once the schema initialisation has finished. |
123 | 131 |
|
124 | 132 | We should now confirm that the schema defined by our "classes" |
125 | 133 | attribute actually matches the schema in the database. |
126 | 134 | ''' |
| 135 | + self.upgrade_db() |
| 136 | + |
127 | 137 | # now detect changes in the schema |
128 | 138 | save = 0 |
129 | 139 | for classname, spec in self.classes.items(): |
@@ -155,6 +165,21 @@ def post_init(self): |
155 | 165 | # commit |
156 | 166 | self.conn.commit() |
157 | 167 |
|
| 168 | + # update this number when we need to make changes to the SQL structure |
| 169 | + # of the backen database |
| 170 | + current_db_version = 2 |
| 171 | + def upgrade_db(self): |
| 172 | + ''' Update the SQL database to reflect changes in the backend code. |
| 173 | + ''' |
| 174 | + version = self.database_schema.get('__version', 1) |
| 175 | + if version == 1: |
| 176 | + # version 1 doesn't have the OTK, session and indexing in the |
| 177 | + # database |
| 178 | + self.create_version_2_tables() |
| 179 | + |
| 180 | + self.database_schema['__version'] = self.current_db_version |
| 181 | + |
| 182 | + |
158 | 183 | def refresh_database(self): |
159 | 184 | self.post_init() |
160 | 185 |
|
|
0 commit comments