Contact: fumanchu@aminus.org

Log in as guest/geniusql to create tickets

Changeset 30

Show
Ignore:
Timestamp:
03/12/07 17:39:50
Author:
fumanchu
Message:

Changed add_primary to set_primary; it now drops first (since MySQL cannot drop and add in separate statements).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/geniusql/objects.py

    r29 r30  
    296296        return i 
    297297     
    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() 
    300305        pk = [column.qname for column in self.itervalues() if column.key] 
    301306        if pk: 
     
    549554    def discover(self, tablename, conn=None): 
    550555        """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. 
    551559         
    552560        Table objects (and their Column and Index subobjects) will be 
  • trunk/geniusql/providers/mysql.py

    r29 r30  
    185185        """Remove any PRIMARY KEY for this Table.""" 
    186186        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() 
    187198 
    188199 
  • trunk/geniusql/providers/sqlite.py

    r29 r30  
    423423        return {idkeys[0]: new_id} 
    424424     
    425     def add_primary(self): 
     425    def set_primary(self): 
    426426        """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() 
    431429     
    432430    def drop_primary(self): 
    433431        """Remove any PRIMARY KEY for this Table.""" 
     432        pk_cols = [col for col in self.itervalues() if col.key] 
    434433        self._start_temp() 
    435         for col in self.itervalues()
     434        for col in pk_cols
    436435            col.key = False 
    437436        self._finish_temp() 
     437        for col in pk_cols: 
     438            col.key = True 
    438439 
    439440 
  • trunk/geniusql/test/zoo_fixture.py

    r29 r30  
    607607        # Drop and re-add the PK on, oh, how about the Animal table? 
    608608        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) 
    613612        if schema.db.pks_must_be_indexed: 
    614613            self.assertEqual(len(Animal.indices), 2)