4 Add created,modified columns for all tables.
6 @contact: Debian FTP Master <ftpmaster@debian.org>
7 @copyright: 2009 Barry deFreese <bdefreese@debian.org>
8 @license: GNU General Public License version 2 or later
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 print "Add created, modified fields for all tables."
30 updatetables = ['architecture', 'archive', 'bin_associations', 'bin_contents',
31 'binaries', 'binary_acl', 'binary_acl_map', 'build_queue', 'build_queue_files',
32 'changes', 'changes_pending_binaries', 'changes_pending_files',
33 'changes_pending_files_map', 'changes_pending_source', 'changes_pending_source_files',
34 'changes_pool_files', 'component', 'config', 'dsc_files', 'files', 'fingerprint',
35 'keyring_acl_map', 'keyrings', 'location', 'maintainer', 'new_comments', 'override',
36 'override_type', 'policy_queue', 'priority', 'section', 'source', 'source_acl',
37 'src_associations', 'src_format', 'src_uploaders', 'suite', 'suite_architectures',
38 'suite_build_queue_copy', 'suite_src_formats', 'uid', 'upload_blocks']
42 print "Create trigger function."
43 c.execute("""CREATE OR REPLACE FUNCTION tfunc_set_modified() RETURNS trigger AS $$
44 BEGIN NEW.modified = now(); return NEW; END;
45 $$ LANGUAGE 'plpgsql'""")
48 for updatetable in updatetables:
50 print "Add created field to %s." % updatetable
51 c.execute("ALTER TABLE %s ADD COLUMN created TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now()" % updatetable)
53 print "Add modified field to %s." % updatetable
54 c.execute("ALTER TABLE %s ADD COLUMN modified TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now()" % updatetable)
56 print "Create modified trigger."
57 c.execute("""CREATE TRIGGER modified_%s BEFORE UPDATE ON %s
58 FOR EACH ROW EXECUTE PROCEDURE tfunc_set_modified()""" % (updatetable, updatetable))
61 c.execute("UPDATE config SET value = '26' WHERE name = 'db_revision'")
64 except psycopg2.InternalError as msg:
66 raise DBUpdateError("Database error, rollback issued. Error message : %s" % (str(msg)))