]> git.decadent.org.uk Git - dak.git/commitdiff
Merge branch 'master' into dbtests
authorTorsten Werner <twerner@debian.org>
Thu, 3 Feb 2011 16:53:27 +0000 (17:53 +0100)
committerTorsten Werner <twerner@debian.org>
Thu, 3 Feb 2011 16:53:27 +0000 (17:53 +0100)
dak/dakdb/update41.py
daklib/dbconn.py

index 338648fa2f08df10bb2b0916ee44ea2c52a8d3c2..813a3b709e17a83488245ef748a70d7e44f6ce52 100755 (executable)
@@ -2,8 +2,8 @@
 # coding=utf8
 
 """
-Remove useless type casts from primary keys to support sqlalchemy's
-reflection mechanism for all tables. Rename 2 sequences.
+Remove useless type casts from primary keys to support sqlalchemy's reflection
+mechanism for all tables. Rename 2 sequences and add 1 primary key.
 
 @contact: Debian FTP Master <ftpmaster@debian.org>
 @copyright: 2011 Torsten Werner <twerner@debian.org>
@@ -33,11 +33,14 @@ from socket import gethostname;
 ################################################################################
 def do_update(self):
     """
-    Remove useless type casts from primary keys and fix 2 sequences.
+    Remove useless type casts from primary keys, fix 2 sequences, and add 1
+    primary key.
     """
     print __doc__
     try:
         c = self.db.cursor()
+
+        # remove useless type casts
         for table in ('architecture', 'archive', 'bin_associations', \
             'binaries', 'component', 'dsc_files', 'files', \
             'fingerprint', 'location', 'maintainer', 'override_type', \
@@ -46,9 +49,15 @@ def do_update(self):
             c.execute("ALTER TABLE %s ALTER id SET DEFAULT nextval('%s_id_seq'::regclass)" % \
                 (table, table))
 
+        # rename sequences
         c.execute("ALTER SEQUENCE known_changes_id_seq RENAME TO changes_id_seq")
         c.execute("ALTER SEQUENCE queue_files_id_seq RENAME TO build_queue_files_id_seq")
 
+        # replace unique contraint by primary key
+        c.execute( \
+            "ALTER TABLE bin_contents DROP CONSTRAINT bin_contents_file_key");
+        c.execute("ALTER TABLE bin_contents ADD PRIMARY KEY (file, binary_id)");
+
         c.execute("UPDATE config SET value = '41' WHERE name = 'db_revision'")
         self.db.commit()
 
index 2237099d75eb296c44b95e478357c6d76ab82031..dc5518a9a698363b5e7411bd1fd1bc4e001f554e 100755 (executable)
@@ -53,7 +53,8 @@ from tempfile import mkstemp, mkdtemp
 from inspect import getargspec
 
 import sqlalchemy
-from sqlalchemy import create_engine, Table, MetaData, Column, Integer, desc
+from sqlalchemy import create_engine, Table, MetaData, Column, Integer, desc, \
+    Text, ForeignKey
 from sqlalchemy.orm import sessionmaker, mapper, relation, object_session, \
     backref, MapperExtension, EXT_CONTINUE, object_mapper
 from sqlalchemy import types as sqltypes
@@ -465,12 +466,9 @@ __all__.append('get_archive')
 
 ################################################################################
 
-class BinContents(object):
-    def __init__(self, *args, **kwargs):
-        pass
-
-    def __repr__(self):
-        return '<BinContents (%s, %s)>' % (self.binary, self.filename)
+class BinContents(ORMObject):
+    def properties(silf):
+        return ['file', 'binary']
 
 __all__.append('BinContents')
 
@@ -491,7 +489,7 @@ class DBBinary(ORMObject):
     def properties(self):
         return ['package', 'version', 'maintainer', 'source', 'architecture', \
             'poolfile', 'binarytype', 'fingerprint', 'install_date', \
-            'suites_count', 'binary_id']
+            'suites_count', 'binary_id', 'contents_count']
 
     def not_null_constraints(self):
         return ['package', 'version', 'maintainer', 'source',  'poolfile', \
@@ -2909,7 +2907,6 @@ class DBConn(object):
         )
 
         tables_no_primary = (
-            'bin_contents',
             'changes_pending_files_map',
             'changes_pending_source_files',
             'changes_pool_files',
@@ -2961,6 +2958,14 @@ class DBConn(object):
             table = Table(table_name, self.db_meta, autoload=True)
             setattr(self, 'tbl_%s' % table_name, table)
 
+        # bin_contents needs special attention until update #41 has been
+        # applied
+        self.tbl_bin_contents = Table('bin_contents', self.db_meta, \
+            Column('file', Text, primary_key = True),
+            Column('binary_id', Integer, ForeignKey('binaries.id'), \
+                primary_key = True),
+            autoload=True, useexisting=True)
+
         for view_name in views:
             view = Table(view_name, self.db_meta, autoload=True)
             setattr(self, 'view_%s' % view_name, view)
@@ -3233,6 +3238,12 @@ class DBConn(object):
                                  fingerprint = relation(Fingerprint, backref="uploadblocks"),
                                  uid = relation(Uid, backref="uploadblocks")))
 
+        mapper(BinContents, self.tbl_bin_contents,
+            properties = dict(
+                binary = relation(DBBinary,
+                    backref=backref('contents', lazy='dynamic')),
+                file = self.tbl_bin_contents.c.file))
+
     ## Connection functions
     def __createconn(self):
         from config import Config