FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
resequence_author_table.php
Go to the documentation of this file.
1 <?php
2 /***********************************************************
3  Copyright (C) 2018 Siemens AG
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public License
7  version 2 as published by the Free Software Foundation.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License along
15  with this program; if not, write to the Free Software Foundation, Inc.,
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  ***********************************************************/
18 
31 function ResequenceAuthorTablePKey($dbManager, $authorColumn)
32 {
33  if($dbManager == NULL){
34  echo "No connection object passed!\n";
35  return false;
36  }
37 
38  $sql = "
39 BEGIN;
40 DROP SEQUENCE IF EXISTS public.author_pk_seq CASCADE;
41 
42 ALTER TABLE ONLY public.author
43  DROP CONSTRAINT IF EXISTS author_agent_fk_fkey CASCADE;
44 ALTER TABLE ONLY public.author
45  DROP CONSTRAINT IF EXISTS author_pfile_fk_fkey CASCADE;
46 ALTER TABLE ONLY public.author
47  DROP CONSTRAINT IF EXISTS author_pkey CASCADE;
48 ";
49  $dbManager->queryOnce($sql);
50  $sql = "
51 CREATE SEQUENCE public.author_pk_seq;
52 SELECT setval('public.author_pk_seq',(SELECT greatest(1,max($authorColumn)) val FROM author));
53 ALTER TABLE public.author ALTER COLUMN $authorColumn SET DEFAULT nextval('author_pk_seq'::regclass);
54 
55 DROP INDEX IF EXISTS author_agent_fk_idx;
56 DROP INDEX IF EXISTS author_pfile_fk_index;
57 DROP INDEX IF EXISTS author_pfile_hash_idx;
58 DROP INDEX IF EXISTS author_pkey;
59 
60 ALTER SEQUENCE public.author_pk_seq RESTART WITH 1;
61 UPDATE public.author SET $authorColumn=nextval('public.author_pk_seq');
62 
63 ALTER TABLE ONLY public.author
64  ADD CONSTRAINT author_pkey PRIMARY KEY ($authorColumn);
65 
66 CREATE INDEX author_agent_fk_idx ON public.author USING btree (agent_fk);
67 CREATE INDEX author_pfile_fk_index ON public.author USING btree (pfile_fk);
68 CREATE INDEX author_pfile_hash_idx ON public.author USING btree (hash, pfile_fk);
69 ALTER TABLE ONLY public.author
70  ADD CONSTRAINT author_agent_fk_fkey FOREIGN KEY (agent_fk) REFERENCES public.agent(agent_pk) ON DELETE CASCADE;
71 ALTER TABLE ONLY public.author
72  ADD CONSTRAINT author_pfile_fk_fkey FOREIGN KEY (pfile_fk) REFERENCES public.pfile(pfile_pk) ON DELETE CASCADE;
73 
74 COMMIT;
75 ";
76  $dbManager->queryOnce($sql);
77 }
78 
85 function ResequenceCopyrightTablePKey($dbManager, $copyrightColumn)
86 {
87  if($dbManager == NULL){
88  echo "No connection object passed!\n";
89  return false;
90  }
91 
92  $sql = "
93 BEGIN;
94 ALTER TABLE ONLY public.copyright
95  DROP CONSTRAINT IF EXISTS copyright_pkey CASCADE;
96 ALTER TABLE ONLY public.copyright
97  DROP CONSTRAINT IF EXISTS copyright_agent_fk_fkey CASCADE;
98 DROP INDEX IF EXISTS copyright_pkey;
99 DROP SEQUENCE IF EXISTS public.copyright_pk_seq CASCADE;
100 ";
101  $dbManager->queryOnce($sql);
102  $sql = "
103 CREATE SEQUENCE public.copyright_pk_seq;
104 SELECT setval('public.copyright_pk_seq',(SELECT greatest(1,max($copyrightColumn)) val FROM copyright));
105 ALTER TABLE public.copyright ALTER COLUMN $copyrightColumn SET DEFAULT nextval('copyright_pk_seq'::regclass);
106 ALTER SEQUENCE public.copyright_pk_seq RESTART WITH 1;
107 UPDATE public.copyright SET $copyrightColumn=nextval('public.copyright_pk_seq');
108 
109 ALTER TABLE ONLY public.copyright
110  ADD CONSTRAINT copyright_pkey PRIMARY KEY ($copyrightColumn);
111 ALTER TABLE ONLY public.copyright
112  ADD CONSTRAINT copyright_agent_fk_fkey FOREIGN KEY (agent_fk) REFERENCES public.agent(agent_pk) ON DELETE CASCADE;
113 
114 COMMIT;
115 ";
116  $dbManager->queryOnce($sql);
117 }
118 
119 
126 function CleanAuthorTable($dbManager, $authorColumn)
127 {
128  if($dbManager == NULL){
129  echo "No connection object passed!\n";
130  return false;
131  }
132 
133  $sql = "
134 BEGIN;
135 DELETE FROM public.author
136 USING public.author AS a LEFT OUTER JOIN public.pfile AS p ON p.pfile_pk = a.pfile_fk
137 WHERE public.author.$authorColumn = a.$authorColumn AND p.pfile_pk IS NULL;
138 
139 DELETE FROM public.author
140 USING public.author AS au LEFT OUTER JOIN public.agent AS ag ON au.agent_fk = ag.agent_pk
141 WHERE public.author.$authorColumn = au.$authorColumn AND ag.agent_pk IS NULL;
142 
143 DELETE FROM public.author
144 WHERE $authorColumn IN (SELECT $authorColumn
145 FROM (SELECT $authorColumn,
146  ROW_NUMBER() OVER (PARTITION BY hash, pfile_fk, agent_fk, copy_startbyte, copy_endbyte, type
147  ORDER BY $authorColumn) AS rnum
148  FROM public.author) a
149  WHERE a.rnum > 1);
150 COMMIT;
151 ";
152  $dbManager->queryOnce($sql);
153 }
154 
161 function CleanCopyrightTable($dbManager, $copyrightColumn)
162 {
163  if($dbManager == NULL){
164  echo "No connection object passed!\n";
165  return false;
166  }
167 
168  $sql = "
169 BEGIN;
170 
171 DELETE FROM public.copyright
172 USING public.copyright AS cp LEFT OUTER JOIN public.agent AS ag ON cp.agent_fk = ag.agent_pk
173 WHERE public.copyright.$copyrightColumn = cp.$copyrightColumn AND ag.agent_pk IS NULL;
174 
175 COMMIT;
176 ";
177  $dbManager->queryOnce($sql);
178 }
179 
180 $result = $dbManager->getSingleRow("SELECT count(*) FROM pg_class WHERE relname = 'author_pk_seq';",
181  array(), 'checkAuthorCtPkSequence');
182 if($result['count'] == 0)
183 {
184  $DatabaseName = $GLOBALS["SysConf"]["DBCONF"]["dbname"];
185  $authorColumn = DB_ColExists("author", "ct_pk", $DatabaseName) == 1 ? "ct_pk" : "author_pk";
186  $copyrightColumn = DB_ColExists("copyright", "ct_pk", $DatabaseName) == 1 ? "ct_pk" : "copyright_pk";
187 
188  try {
189  echo "*** Cleaning author table ***\n";
190  CleanAuthorTable($dbManager, $authorColumn);
191  echo "*** Cleaning copyright table ***\n";
192  CleanCopyrightTable($dbManager, $copyrightColumn);
193  echo "*** Resequencing author table ***\n";
194  ResequenceAuthorTablePKey($dbManager, $authorColumn);
195  echo "*** Resequencing copyright table ***\n";
196  ResequenceCopyrightTablePKey($dbManager, $copyrightColumn);
197  } catch (Exception $e) {
198  echo "Something went wrong. Try running postinstall again!\n";
199  $dbManager->queryOnce("ROLLBACK;");
200  }
201 }
202 
ResequenceCopyrightTablePKey($dbManager, $copyrightColumn)
Drop primary key constrains and resequence the copyright table and build them again.
ResequenceAuthorTablePKey($dbManager, $authorColumn)
Drop all sequence and constrains and resequence the author table and build them again.
CleanAuthorTable($dbManager, $authorColumn)
Remove redundant entries from author table.
CleanCopyrightTable($dbManager, $copyrightColumn)
Remove invalid entries from copyright table.
DB_ColExists($tableName, $colName, $DBName='fossology')
Check if a column exists.
Definition: common-db.php:251