Index: trunk/geniusql/objects.py =================================================================== --- trunk/geniusql/objects.py (revision 27) +++ trunk/geniusql/objects.py (revision 30) @@ -296,6 +296,11 @@ return i - def add_primary(self): - """Set the PRIMARY KEY for this Table, using its Column.key values.""" + def set_primary(self): + """Set the PRIMARY KEY for this Table, using its Column.key values. + + If self already possesses a primary key, this method will DROP it. + This is intended to be used with install or repair scripts. + """ + self.drop_primary() pk = [column.qname for column in self.itervalues() if column.key] if pk: @@ -549,4 +554,7 @@ def discover(self, tablename, conn=None): """Attach a new Table from the underlying DB to self (and return it). + + tablename: the database's name for the table. This may be different + from the schema's key for the table. Table objects (and their Column and Index subobjects) will be Index: trunk/geniusql/providers/mysql.py =================================================================== --- trunk/geniusql/providers/mysql.py (revision 23) +++ trunk/geniusql/providers/mysql.py (revision 30) @@ -185,4 +185,15 @@ """Remove any PRIMARY KEY for this Table.""" self.schema.db.execute('ALTER TABLE %s DROP PRIMARY KEY;' % self.qname) + + def set_primary(self): + """Set the PRIMARY KEY for this Table.""" + pk = [column.qname for column in self.itervalues() if column.key] + if pk: + # For MySQL, we MUST do this in a single statement. + self.schema.db.execute("ALTER TABLE %s DROP PRIMARY KEY, " + "ADD PRIMARY KEY (%s);" % + (self.qname, ", ".join(pk))) + else: + self.drop_primary() Index: trunk/geniusql/providers/sqlite.py =================================================================== --- trunk/geniusql/providers/sqlite.py (revision 29) +++ trunk/geniusql/providers/sqlite.py (revision 30) @@ -423,17 +423,18 @@ return {idkeys[0]: new_id} - def add_primary(self): + def set_primary(self): """Assert the PRIMARY KEY for this Table, using its Column.key values.""" - pk = [column.qname for column in self.itervalues() if column.key] - if pk: - self._start_temp() - self._finish_temp() + self._start_temp() + self._finish_temp() def drop_primary(self): """Remove any PRIMARY KEY for this Table.""" + pk_cols = [col for col in self.itervalues() if col.key] self._start_temp() - for col in self.itervalues(): + for col in pk_cols: col.key = False self._finish_temp() + for col in pk_cols: + col.key = True Index: trunk/geniusql/test/zoo_fixture.py =================================================================== --- trunk/geniusql/test/zoo_fixture.py (revision 28) +++ trunk/geniusql/test/zoo_fixture.py (revision 30) @@ -607,8 +607,7 @@ # Drop and re-add the PK on, oh, how about the Animal table? Animal = schema['Animal'] - Animal.drop_primary() - Animal.add_primary() - - Animal = schema.discover('Animal') + Animal.set_primary() + + Animal = schema.discover(Animal.name) if schema.db.pks_must_be_indexed: self.assertEqual(len(Animal.indices), 2)