Changeset 30
- Timestamp:
- 03/12/07 17:39:50
- Files:
-
- trunk/geniusql/objects.py (modified) (2 diffs)
- trunk/geniusql/providers/mysql.py (modified) (1 diff)
- trunk/geniusql/providers/sqlite.py (modified) (1 diff)
- trunk/geniusql/test/zoo_fixture.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/geniusql/objects.py
r29 r30 296 296 return i 297 297 298 def add_primary(self): 299 """Set the PRIMARY KEY for this Table, using its Column.key values.""" 298 def set_primary(self): 299 """Set the PRIMARY KEY for this Table, using its Column.key values. 300 301 If self already possesses a primary key, this method will DROP it. 302 This is intended to be used with install or repair scripts. 303 """ 304 self.drop_primary() 300 305 pk = [column.qname for column in self.itervalues() if column.key] 301 306 if pk: … … 549 554 def discover(self, tablename, conn=None): 550 555 """Attach a new Table from the underlying DB to self (and return it). 556 557 tablename: the database's name for the table. This may be different 558 from the schema's key for the table. 551 559 552 560 Table objects (and their Column and Index subobjects) will be trunk/geniusql/providers/mysql.py
r29 r30 185 185 """Remove any PRIMARY KEY for this Table.""" 186 186 self.schema.db.execute('ALTER TABLE %s DROP PRIMARY KEY;' % self.qname) 187 188 def set_primary(self): 189 """Set the PRIMARY KEY for this Table.""" 190 pk = [column.qname for column in self.itervalues() if column.key] 191 if pk: 192 # For MySQL, we MUST do this in a single statement. 193 self.schema.db.execute("ALTER TABLE %s DROP PRIMARY KEY, " 194 "ADD PRIMARY KEY (%s);" % 195 (self.qname, ", ".join(pk))) 196 else: 197 self.drop_primary() 187 198 188 199 trunk/geniusql/providers/sqlite.py
r29 r30 423 423 return {idkeys[0]: new_id} 424 424 425 def add_primary(self):425 def set_primary(self): 426 426 """Assert the PRIMARY KEY for this Table, using its Column.key values.""" 427 pk = [column.qname for column in self.itervalues() if column.key] 428 if pk: 429 self._start_temp() 430 self._finish_temp() 427 self._start_temp() 428 self._finish_temp() 431 429 432 430 def drop_primary(self): 433 431 """Remove any PRIMARY KEY for this Table.""" 432 pk_cols = [col for col in self.itervalues() if col.key] 434 433 self._start_temp() 435 for col in self.itervalues():434 for col in pk_cols: 436 435 col.key = False 437 436 self._finish_temp() 437 for col in pk_cols: 438 col.key = True 438 439 439 440 trunk/geniusql/test/zoo_fixture.py
r29 r30 607 607 # Drop and re-add the PK on, oh, how about the Animal table? 608 608 Animal = schema['Animal'] 609 Animal.drop_primary() 610 Animal.add_primary() 611 612 Animal = schema.discover('Animal') 609 Animal.set_primary() 610 611 Animal = schema.discover(Animal.name) 613 612 if schema.db.pks_must_be_indexed: 614 613 self.assertEqual(len(Animal.indices), 2)
