FOSSology  4.4.0
Open Source License Compliance by Open Source Software
resequence_author_table.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2018 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
20 function ResequenceAuthorTablePKey($dbManager, $authorColumn)
21 {
22  if($dbManager == NULL){
23  echo "No connection object passed!\n";
24  return false;
25  }
26 
27  $sql = "
28 BEGIN;
29 DROP SEQUENCE IF EXISTS public.author_pk_seq CASCADE;
30 
31 ALTER TABLE ONLY public.author
32  DROP CONSTRAINT IF EXISTS author_agent_fk_fkey CASCADE;
33 ALTER TABLE ONLY public.author
34  DROP CONSTRAINT IF EXISTS author_pfile_fk_fkey CASCADE;
35 ALTER TABLE ONLY public.author
36  DROP CONSTRAINT IF EXISTS author_pkey CASCADE;
37 ";
38  $dbManager->queryOnce($sql);
39  $sql = "
40 CREATE SEQUENCE public.author_pk_seq;
41 SELECT setval('public.author_pk_seq',(SELECT greatest(1,max($authorColumn)) val FROM author));
42 ALTER TABLE public.author ALTER COLUMN $authorColumn SET DEFAULT nextval('author_pk_seq'::regclass);
43 
44 DROP INDEX IF EXISTS author_agent_fk_idx;
45 DROP INDEX IF EXISTS author_pfile_fk_index;
46 DROP INDEX IF EXISTS author_pfile_hash_idx;
47 DROP INDEX IF EXISTS author_pkey;
48 
49 ALTER SEQUENCE public.author_pk_seq RESTART WITH 1;
50 UPDATE public.author SET $authorColumn=nextval('public.author_pk_seq');
51 
52 ALTER TABLE ONLY public.author
53  ADD CONSTRAINT author_pkey PRIMARY KEY ($authorColumn);
54 
55 CREATE INDEX author_agent_fk_idx ON public.author USING btree (agent_fk);
56 CREATE INDEX author_pfile_fk_index ON public.author USING btree (pfile_fk);
57 CREATE INDEX author_pfile_hash_idx ON public.author USING btree (hash, pfile_fk);
58 ALTER TABLE ONLY public.author
59  ADD CONSTRAINT author_agent_fk_fkey FOREIGN KEY (agent_fk) REFERENCES public.agent(agent_pk) ON DELETE CASCADE;
60 ALTER TABLE ONLY public.author
61  ADD CONSTRAINT author_pfile_fk_fkey FOREIGN KEY (pfile_fk) REFERENCES public.pfile(pfile_pk) ON DELETE CASCADE;
62 
63 COMMIT;
64 ";
65  $dbManager->queryOnce($sql);
66 }
67 
74 function ResequenceCopyrightTablePKey($dbManager, $copyrightColumn)
75 {
76  if($dbManager == NULL){
77  echo "No connection object passed!\n";
78  return false;
79  }
80 
81  $sql = "
82 BEGIN;
83 ALTER TABLE ONLY public.copyright
84  DROP CONSTRAINT IF EXISTS copyright_pkey CASCADE;
85 ALTER TABLE ONLY public.copyright
86  DROP CONSTRAINT IF EXISTS copyright_agent_fk_fkey CASCADE;
87 DROP INDEX IF EXISTS copyright_pkey;
88 DROP SEQUENCE IF EXISTS public.copyright_pk_seq CASCADE;
89 ";
90  $dbManager->queryOnce($sql);
91  $sql = "
92 CREATE SEQUENCE public.copyright_pk_seq;
93 SELECT setval('public.copyright_pk_seq',(SELECT greatest(1,max($copyrightColumn)) val FROM copyright));
94 ALTER TABLE public.copyright ALTER COLUMN $copyrightColumn SET DEFAULT nextval('copyright_pk_seq'::regclass);
95 ALTER SEQUENCE public.copyright_pk_seq RESTART WITH 1;
96 UPDATE public.copyright SET $copyrightColumn=nextval('public.copyright_pk_seq');
97 
98 ALTER TABLE ONLY public.copyright
99  ADD CONSTRAINT copyright_pkey PRIMARY KEY ($copyrightColumn);
100 ALTER TABLE ONLY public.copyright
101  ADD CONSTRAINT copyright_agent_fk_fkey FOREIGN KEY (agent_fk) REFERENCES public.agent(agent_pk) ON DELETE CASCADE;
102 
103 COMMIT;
104 ";
105  $dbManager->queryOnce($sql);
106 }
107 
108 
115 function CleanAuthorTable($dbManager, $authorColumn)
116 {
117  if($dbManager == NULL){
118  echo "No connection object passed!\n";
119  return false;
120  }
121 
122  $sql = "
123 BEGIN;
124 DELETE FROM public.author
125 USING public.author AS a LEFT OUTER JOIN public.pfile AS p ON p.pfile_pk = a.pfile_fk
126 WHERE public.author.$authorColumn = a.$authorColumn AND p.pfile_pk IS NULL;
127 
128 DELETE FROM public.author
129 USING public.author AS au LEFT OUTER JOIN public.agent AS ag ON au.agent_fk = ag.agent_pk
130 WHERE public.author.$authorColumn = au.$authorColumn AND ag.agent_pk IS NULL;
131 
132 DELETE FROM public.author
133 WHERE $authorColumn IN (SELECT $authorColumn
134 FROM (SELECT $authorColumn,
135  ROW_NUMBER() OVER (PARTITION BY hash, pfile_fk, agent_fk, copy_startbyte, copy_endbyte, type
136  ORDER BY $authorColumn) AS rnum
137  FROM public.author) a
138  WHERE a.rnum > 1);
139 COMMIT;
140 ";
141  $dbManager->queryOnce($sql);
142 }
143 
150 function CleanCopyrightTable($dbManager, $copyrightColumn)
151 {
152  if($dbManager == NULL){
153  echo "No connection object passed!\n";
154  return false;
155  }
156 
157  $sql = "
158 BEGIN;
159 
160 DELETE FROM public.copyright
161 USING public.copyright AS cp LEFT OUTER JOIN public.agent AS ag ON cp.agent_fk = ag.agent_pk
162 WHERE public.copyright.$copyrightColumn = cp.$copyrightColumn AND ag.agent_pk IS NULL;
163 
164 COMMIT;
165 ";
166  $dbManager->queryOnce($sql);
167 }
168 
169 $result = $dbManager->getSingleRow("SELECT count(*) FROM pg_class WHERE relname = 'author_pk_seq';",
170  array(), 'checkAuthorCtPkSequence');
171 if($result['count'] == 0)
172 {
173  $DatabaseName = $GLOBALS["SysConf"]["DBCONF"]["dbname"];
174  $authorColumn = DB_ColExists("author", "ct_pk", $DatabaseName) == 1 ? "ct_pk" : "author_pk";
175  $copyrightColumn = DB_ColExists("copyright", "ct_pk", $DatabaseName) == 1 ? "ct_pk" : "copyright_pk";
176 
177  try {
178  echo "*** Cleaning author table ***\n";
179  CleanAuthorTable($dbManager, $authorColumn);
180  echo "*** Cleaning copyright table ***\n";
181  CleanCopyrightTable($dbManager, $copyrightColumn);
182  echo "*** Resequencing author table ***\n";
183  ResequenceAuthorTablePKey($dbManager, $authorColumn);
184  echo "*** Resequencing copyright table ***\n";
185  ResequenceCopyrightTablePKey($dbManager, $copyrightColumn);
186  } catch (Exception $e) {
187  echo "Something went wrong. Try running postinstall again!\n";
188  $dbManager->queryOnce("ROLLBACK;");
189  }
190 }
191 
DB_ColExists($tableName, $colName, $DBName='fossology')
Check if a column exists.
Definition: common-db.php:240
ResequenceCopyrightTablePKey($dbManager, $copyrightColumn)
Drop primary key constrains and resequence the copyright table and build them again.
CleanCopyrightTable($dbManager, $copyrightColumn)
Remove invalid entries from copyright table.
CleanAuthorTable($dbManager, $authorColumn)
Remove redundant entries from author table.
ResequenceAuthorTablePKey($dbManager, $authorColumn)
Drop all sequence and constrains and resequence the author table and build them again.