Changeset 4406

Show
Ignore:
Timestamp:
03/28/09 22:38:19 (4 years ago)
Author:
gpolo
Message:

Added merging of columns that do not reference other columns.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branch/merger/sqlitedb.py

    r4404 r4406  
    7777            res = old_cursor.execute("pragma table_info(%s)" % name) 
    7878            old_table_info = res.fetchall() 
     79            old_by_col = {} 
     80            for col in old_table_info: 
     81                colname = col.pop('name') 
     82                old_by_col[colname] = col 
    7983 
    80             for col_info in old_table_info: 
    81                 name = col_info.pop('name') 
    82  
    83                 if name not in new_by_col: 
     84            for cname, cinfo in old_by_col.iteritems(): 
     85                if cname not in new_by_col: 
    8486                    # sqlite can't handle deletion of columns, this merge 
    8587                    # can't be done. 
     
    8789                            "contains a column named '%s' which no longer " 
    8890                            "exists in the new table, sqlite can't handle " 
    89                             "this." % (name, col_info['name'])) 
    90                 elif col_info != new_by_col[name]: 
     91                            "this." % (name, cname)) 
     92                elif cinfo != new_by_col[cname]: 
    9193                    # sqlite also can't handle changes in column type and 
    9294                    # others 
    9395                    raise Exception("The table '%s' in the old database " 
    9496                            "differs in the column named '%s'. (%r != %r)" % ( 
    95                                 col_info, new_by_col[name])) 
     97                                name, cname, cinfo, new_by_col[cname])) 
    9698 
    97                 del new_by_col[name] 
     99                del new_by_col[cname] 
    98100 
    99101            # Attempt to create new columns 
    100             print 
     102            # XXX not checking if they reference other columns yet. 
    101103            for cname, cinfo in new_by_col.iteritems(): 
    102                 print cname, cinfo, "<<<" 
    103                 # XXX ToDo 
     104                if cinfo['pk']: 
     105                    # sqlite doesn't allow creating new columns as primary key 
     106                    raise Exception("The table '%s' in the new database " 
     107                            "has a new column named '%s' which is a primary " 
     108                            "key, but sqlite can't handle this." % ( 
     109                                name, cname)) 
     110 
     111                query = "ALTER TABLE %s ADD COLUMN %s %s" % ( 
     112                        name, cname, cinfo['type']) 
     113                if cinfo['notnull']: 
     114                    query += " NOT NULL" 
     115                if cinfo['dflt_value']: 
     116                    query += " DEFAULT %s" % cinfo['dflt_value'] 
     117                old_cursor.execute(query) 
    104118 
    105119            # XXX check for triggers here