DB: log schema upgrades
This commit is contained in:
		
							parent
							
								
									de8efdb9f9
								
							
						
					
					
						commit
						0f8b98dec5
					
				|  | @ -1,7 +1,8 @@ | ||||||
| from __future__ import unicode_literals | from __future__ import unicode_literals | ||||||
| import os, sys | import os | ||||||
| import sqlite3 | import sqlite3 | ||||||
| from pkg_resources import resource_string | from pkg_resources import resource_string | ||||||
|  | from twisted.python import log | ||||||
| 
 | 
 | ||||||
| class DBError(Exception): | class DBError(Exception): | ||||||
|     pass |     pass | ||||||
|  | @ -24,7 +25,7 @@ def dict_factory(cursor, row): | ||||||
|         d[col[0]] = row[idx] |         d[col[0]] = row[idx] | ||||||
|     return d |     return d | ||||||
| 
 | 
 | ||||||
| def get_db(dbfile, target_version=TARGET_VERSION, stderr=sys.stderr): | def get_db(dbfile, target_version=TARGET_VERSION): | ||||||
|     """Open or create the given db file. The parent directory must exist. |     """Open or create the given db file. The parent directory must exist. | ||||||
|     Returns the db connection object, or raises DBError. |     Returns the db connection object, or raises DBError. | ||||||
|     """ |     """ | ||||||
|  | @ -41,6 +42,7 @@ def get_db(dbfile, target_version=TARGET_VERSION, stderr=sys.stderr): | ||||||
|         raise DBError("failed foreign key check: %s" % (problems,)) |         raise DBError("failed foreign key check: %s" % (problems,)) | ||||||
| 
 | 
 | ||||||
|     if must_create: |     if must_create: | ||||||
|  |         log.msg("populating new database with schema v%s" % target_version) | ||||||
|         schema = get_schema(target_version) |         schema = get_schema(target_version) | ||||||
|         db.executescript(schema) |         db.executescript(schema) | ||||||
|         db.execute("INSERT INTO version (version) VALUES (?)", |         db.execute("INSERT INTO version (version) VALUES (?)", | ||||||
|  | @ -55,11 +57,14 @@ def get_db(dbfile, target_version=TARGET_VERSION, stderr=sys.stderr): | ||||||
|         raise DBError("db file is unusable: %s" % e) |         raise DBError("db file is unusable: %s" % e) | ||||||
| 
 | 
 | ||||||
|     while version < target_version: |     while version < target_version: | ||||||
|  |         log.msg(" need to upgrade from %s to %s" % (version, target_version)) | ||||||
|         try: |         try: | ||||||
|             upgrader = get_upgrader(version+1) |             upgrader = get_upgrader(version+1) | ||||||
|         except ValueError: # ResourceError?? |         except ValueError: # ResourceError?? | ||||||
|  |             log.msg(" unable to upgrade %s to %s" % (version, version+1)) | ||||||
|             raise DBError("Unable to upgrade %s to version %s, left at %s" |             raise DBError("Unable to upgrade %s to version %s, left at %s" | ||||||
|                           % (dbfile, version+1, version)) |                           % (dbfile, version+1, version)) | ||||||
|  |         log.msg(" executing upgrader v%s->v%s" % (version, version+1)) | ||||||
|         db.executescript(upgrader) |         db.executescript(upgrader) | ||||||
|         db.commit() |         db.commit() | ||||||
|         version = version+1 |         version = version+1 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user