FOSSology  4.8.0-rc1
Open Source License Compliance by Open Source Software
libschema.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2008-2014 Hewlett-Packard Development Company, L.P.
4  SPDX-FileCopyrightText: © 2014-2015, 2018 Siemens AG
5 
6  SPDX-License-Identifier: LGPL-2.1-only
7 */
8 
15 require_once(__DIR__ . '/../../vendor/autoload.php');
16 
21 use Monolog\Handler\ErrorLogHandler;
22 use Monolog\Logger;
23 
29 {
34  public $debug = false;
35 
40  private $dbman;
41 
46  private $schema = array();
47 
52  private $currSchema = array();
53 
58  function __construct(DbManager &$dbManager)
59  {
60  $this->dbman = $dbManager;
61  }
62 
67  function setDriver(Driver &$dbDriver)
68  {
69  $this->dbman->setDriver($dbDriver);
70  }
71 
76  private function ensureDriver()
77  {
78  // If dbman already has a working driver, nothing to do
79  $driver = $this->dbman->getDriver();
80  if ($driver !== null && $driver instanceof Driver && $driver->isConnected()) {
81  return;
82  }
83  global $PG_CONN;
84  if (!empty($PG_CONN)) {
85  $pgDriver = new Postgres($PG_CONN);
86  $this->dbman->setDriver($pgDriver);
87  return;
88  }
89  throw new \Exception(
90  "No database connection available: \$PG_CONN is not set and no driver " .
91  "was injected into \$dbManager before calling this function."
92  );
93  }
94 
100  function applyOrEchoOnce($sql, $stmt = '')
101  {
102  $this->ensureDriver();
103  if ($this->debug) {
104  print ("$sql\n");
105  } else {
106  $this->dbman->queryOnce($sql, $stmt);
107  }
108  }
109 
110 
119  function applySchema($filename = NULL, $debug = false, $catalog = 'fossology', $migrateColumns = array())
120  {
121  global $PG_CONN;
122 
123  // first check to make sure we don't already have the plpgsql language installed
124  $result = $this->dbman->getSingleRow(
125  "SELECT lanname FROM pg_language WHERE lanname = 'plpgsql'",
126  array(),
127  __METHOD__ . '.checkPlpgsql'
128  );
129 
130  // then create language plpgsql if not already created
131  if (empty($result)) {
132  $this->dbman->queryOnce("CREATE LANGUAGE plpgsql", __METHOD__ . '.createPlpgsql');
133  }
134 
135  $result = $this->dbman->getSingleRow(
136  "SELECT extname FROM pg_extension WHERE extname = 'uuid-ossp'",
137  array(),
138  __METHOD__ . '.checkUuid'
139  );
140 
141  // then create extension uuid-ossp if not already created
142  if (empty($result)) {
143  $this->dbman->queryOnce('CREATE EXTENSION "uuid-ossp"', __METHOD__ . '.createUuid');
144  }
145 
146  $this->debug = $debug;
147  if (!file_exists($filename)) {
148  return "$filename does not exist.";
149  }
150  $Schema = array(); /* will be filled in next line */
151  require($filename); /* this cause Fatal Error if the file does not exist. */
152  $this->schema = $Schema;
153 
154  /* Very basic sanity check (so we don't delete everything!) */
155  if ((count($this->schema['TABLE']) < 5) || (count($this->schema['SEQUENCE']) < 5)
156  || (count($this->schema['INDEX']) < 5) || (count($this->schema['CONSTRAINT']) < 5)
157  ) {
158  return "Schema from '$filename' appears invalid.";
159  }
160 
161  if (!$debug) {
162  $result = $this->dbman->getSingleRow("show statement_timeout", array(), $stmt = __METHOD__ . '.getTimeout');
163  $statementTimeout = $result['statement_timeout'];
164  $this->dbman->queryOnce("SET statement_timeout = 0", $stmt = __METHOD__ . '.setTimeout');
165  }
166 
167  $this->applyOrEchoOnce('BEGIN');
168  $this->getCurrSchema();
169  $errlev = error_reporting(E_ERROR | E_WARNING | E_PARSE);
170  $this->dropViews($catalog);
171  $this->dropConstraints();
172  $this->applySequences();
173  $this->applyTables();
174  $this->applyInheritedRelations();
175  $this->getCurrSchema(); /* New tables created, recheck */
176  $this->applyTables(true);
177  $this->updateSequences();
178  $this->applyViews();
179  $this->dropConstraints();
180  /* Reload current since the CASCADE may have changed things */
181  $this->getCurrSchema(); /* constraints and indexes are linked, recheck */
182  $this->dropIndexes();
183  $this->applyIndexes();
184  $this->applyConstraints();
185  error_reporting($errlev); /* return to previous error reporting level */
186  $this->makeFunctions();
187  $this->applyClusters();
188  /* Reload current since CASCADE during migration may have changed things */
189  $this->getCurrSchema();
190  $this->dropViews($catalog);
191  foreach ($this->currSchema['TABLE'] as $table => $columns) {
192  $skipColumns = array_key_exists($table, $migrateColumns) ? $migrateColumns[$table] : array();
193  $dropColumns = array_diff(array_keys($columns), $skipColumns);
194  $this->dropColumnsFromTable($dropColumns, $table);
195  }
196  $this->applyOrEchoOnce('COMMIT');
197  flush();
199  if (!$debug) {
200  $this->dbman->getSingleRow("SET statement_timeout = $statementTimeout", array(), $stmt = __METHOD__ . '.resetTimeout');
201  print "DB schema has been updated for $catalog.\n";
202  } else {
203  print "These queries could update DB schema for $catalog.\n";
204  }
205  return false;
206  }
207 
214  function applySequences()
215  {
216  if (empty($this->schema['SEQUENCE'])) {
217  return;
218  }
219  foreach ($this->schema['SEQUENCE'] as $name => $import) {
220  if (empty($name)) {
221  continue;
222  }
223 
224  if (!array_key_exists('SEQUENCE', $this->currSchema)
225  || !array_key_exists($name, $this->currSchema['SEQUENCE'])) {
226  $createSql = is_string($import) ? $import : $import['CREATE'];
227  $this->applyOrEchoOnce($createSql, $stmt = __METHOD__ . "." . $name . ".CREATE");
228  }
229  }
230  }
237  function applyClusters()
238  {
239  if (empty($this->schema['CLUSTER'])) {
240  return;
241  }
242  foreach ($this->schema['CLUSTER'] as $name => $sql) {
243  if (empty($name)) {
244  continue;
245  }
246 
247  if (!array_key_exists('CLUSTER', $this->currSchema)
248  || !array_key_exists($name, $this->currSchema['CLUSTER'])) {
249  $this->applyOrEchoOnce($sql, $stmt = __METHOD__ . "." . $name . ".CREATE");
250  }
251  }
252  }
253 
261  function updateSequences()
262  {
263  if (empty($this->schema['SEQUENCE']) ||
264  !(array_key_exists('SEQUENCE', $this->currSchema))) {
265  return;
266  }
267  foreach ($this->schema['SEQUENCE'] as $name => $import) {
268  if (empty($name)) {
269  continue;
270  }
271 
272  if (is_array($import) && array_key_exists('UPDATE', $import)) {
273  $this->applyOrEchoOnce($import['UPDATE'], $stmt = __METHOD__ . "." . $name);
274  }
275  }
276  }
277 
284  function applyTables($inherits=false)
285  {
286  if (empty($this->schema['TABLE'])) {
287  return;
288  }
289  foreach ($this->schema['TABLE'] as $table => $columns) {
290  if (empty($table) || $inherits^array_key_exists($table,$this->schema['INHERITS']) ) {
291  continue;
292  }
293  $newTable = false;
294  if (!DB_TableExists($table)) {
295  $sql = "CREATE TABLE IF NOT EXISTS \"$table\" ()";
296  $this->applyOrEchoOnce($sql, $stmt = __METHOD__ . $table);
297  $newTable = true;
298  } elseif (!array_key_exists($table, $this->currSchema['TABLE'])) {
299  $newTable = true;
300  }
301  /* Drop any leftover _old columns from previous incomplete migrations. */
302  $this->applyOrEchoOnce(
303  "DO \$cleanup\$ DECLARE r RECORD; BEGIN
304  FOR r IN
305  SELECT column_name FROM information_schema.columns
306  WHERE table_name = '$table' AND column_name LIKE '%_old'
307  LOOP
308  EXECUTE 'ALTER TABLE \"$table\" DROP COLUMN IF EXISTS \"' || r.column_name || '\"';
309  END LOOP;
310  END \$cleanup\$;",
311  $stmt = __METHOD__ . ".$table.purge_old_cols"
312  );
313 
314  foreach ($columns as $column => $modification) {
315  if (!$newTable && !array_key_exists($column, $this->currSchema['TABLE'][$table])) {
316  $colNewTable = true;
317  } else {
318  $colNewTable = $newTable;
319  }
320  if ($colNewTable ||
321  $this->currSchema['TABLE'][$table][$column]['ADD'] != $modification['ADD']) {
322  $rename = "";
323  if (DB_ColExists($table, $column)) {
324  /* The column exists, but it looks different!
325  Solution: Delete the column! */
326  $rename = $column . '_old';
327  $sql = "ALTER TABLE \"$table\" RENAME COLUMN \"$column\" TO \"$rename\"";
328  $this->applyOrEchoOnce($sql);
329  // Check constraints on the renamed column
330  $constraints_on_column = DB_ColumnConstraints($table, $rename);
331  if (!empty($constraints_on_column)) {
332  // Drop constraints on the renamed column
333  foreach ($constraints_on_column as $conname) {
334  $sql = "ALTER TABLE \"$table\" DROP CONSTRAINT \"$conname\"";
335  $this->applyOrEchoOnce($sql);
336  }
337  }
338  }
339 
340  $sql = $modification['ADD'];
341  if ($this->debug) {
342  print "$sql\n";
343  } else {
344  // Add the new column which sets the default value
345  $this->dbman->queryOnce($sql);
346  }
347  if (!empty($rename)) {
348  /* copy over the old data */
349  $this->applyOrEchoOnce($sql = "UPDATE \"$table\" SET \"$column\" = \"$rename\"");
350  $this->applyOrEchoOnce($sql = "ALTER TABLE \"$table\" DROP COLUMN \"$rename\"");
351  }
352  }
353  if ($colNewTable ||
354  $this->currSchema['TABLE'][$table][$column]['ALTER'] != $modification['ALTER'] && isset($modification['ALTER'])) {
355  $sql = $modification['ALTER'];
356  if ($this->debug) {
357  print "$sql\n";
358  } else if (!empty ($sql)) {
359  $this->dbman->queryOnce($sql);
360  }
361  }
362  if ($colNewTable ||
363  $this->currSchema['TABLE'][$table][$column]['DESC'] != $modification['DESC']) {
364  $sql = empty($modification['DESC']) ? "COMMENT ON COLUMN \"$table\".\"$column\" IS ''" : $modification['DESC'];
365  $this->applyOrEchoOnce($sql, $stmt = __METHOD__ . "$table.$column.comment");
366  }
367  }
368  }
369  }
370 
374  function applyViews()
375  {
376  if (empty($this->schema['VIEW'])) {
377  return;
378  }
379  $newViews = !array_key_exists('VIEW', $this->currSchema);
380  foreach ($this->schema['VIEW'] as $name => $sql) {
381  if (empty($name) || (!$newViews &&
382  array_key_exists($name, $this->currSchema['VIEW']) &&
383  $this->currSchema['VIEW'][$name] == $sql)) {
384  continue;
385  }
386  if (!$newViews && !empty($this->currSchema['VIEW'][$name])) {
387  $sqlDropView = "DROP VIEW IF EXISTS $name";
388  $this->applyOrEchoOnce($sqlDropView);
389  }
390  $this->applyOrEchoOnce($sql, $stmt = __METHOD__ . $name);
391  }
392  }
393 
399  function dropConstraints()
400  {
401  if (!array_key_exists('CONSTRAINT', $this->currSchema) || // Empty DB
402  empty($this->currSchema['CONSTRAINT'])) {
403  return;
404  }
405  foreach ($this->currSchema['CONSTRAINT'] as $name => $sql) {
406  // skip if constraint name is empty or does not exist
407  if (empty($name) || !array_key_exists($name, $this->schema['CONSTRAINT'])
408  || ($this->schema['CONSTRAINT'][$name] == $sql)
409  || !DB_ConstraintExists($name)) {
410  continue;
411  }
412 
413  /* Only process tables that I know about */
414  $table = preg_replace("/^ALTER TABLE \"(.*)\" ADD CONSTRAINT.*/", '${1}', $sql);
415  $TableFk = preg_replace("/^.*FOREIGN KEY .* REFERENCES \"(.*)\" \‍(.*/", '${1}', $sql);
416  if ($TableFk == $sql) {
417  $TableFk = $table;
418  }
419  /* If I don't know the primary or foreign table... */
420  if (empty($this->schema['TABLE'][$table]) && empty($this->schema['TABLE'][$TableFk])) {
421  continue;
422  }
423  $sql = "ALTER TABLE \"$table\" DROP CONSTRAINT \"$name\" CASCADE";
424  $this->applyOrEchoOnce($sql);
425  }
426  }
427 
431  function dropIndexes()
432  {
433  if (!array_key_exists('INDEX', $this->currSchema) ||
434  empty($this->currSchema['INDEX'])) {
435  return;
436  }
437  foreach ($this->currSchema['INDEX'] as $table => $IndexInfo) {
438  if (empty($table) || (empty($this->schema['TABLE'][$table]) && empty($this->schema['INHERITS'][$table]))) {
439  continue;
440  }
441  foreach ($IndexInfo as $name => $sql) {
442  if (empty($name) || $this->schema['INDEX'][$table][$name] == $sql) {
443  continue;
444  }
445  $sql = "DROP INDEX \"$name\"";
446  $this->applyOrEchoOnce($sql);
447  }
448  }
449  }
450 
454  function applyIndexes()
455  {
456  if (empty($this->schema['INDEX'])) {
457  return;
458  }
459  foreach ($this->schema['INDEX'] as $table => $indexInfo) {
460  if (empty($table)) {
461  continue;
462  }
463  if (!array_key_exists($table, $this->schema["TABLE"]) && !array_key_exists($table, $this->schema['INHERITS'])) {
464  echo "skipping orphan table: $table\n";
465  continue;
466  }
467  $newIndexes = false;
468  if (!array_key_exists('INDEX', $this->currSchema) ||
469  !array_key_exists($table, $this->currSchema['INDEX'])) {
470  $newIndexes = true;
471  }
472  foreach ($indexInfo as $name => $sql) {
473  if (empty($name) || (!$newIndexes &&
474  array_key_exists($name, $this->currSchema['INDEX'][$table]) &&
475  $this->currSchema['INDEX'][$table][$name] == $sql)) {
476  continue;
477  }
478  $this->applyOrEchoOnce($sql);
479  $sql = "REINDEX INDEX \"$name\"";
480  $this->applyOrEchoOnce($sql);
481  }
482  }
483  }
484 
488  function applyConstraints()
489  {
490  $this->currSchema = $this->getCurrSchema(); /* constraints and indexes are linked, recheck */
491  if (empty($this->schema['CONSTRAINT'])) {
492  return;
493  }
494  /* Constraints must be added in the correct order! */
495  $orderedConstraints = array('primary' => array(), 'unique' => array(), 'foreign' => array(), 'other' => array());
496  foreach ($this->schema['CONSTRAINT'] as $Name => $sql) {
497  $newConstraint = false;
498  if (!array_key_exists('CONSTRAINT', $this->currSchema) ||
499  !array_key_exists($Name, $this->currSchema['CONSTRAINT'])) {
500  $newConstraint = true;
501  }
502  if (empty($Name) || (!$newConstraint &&
503  $this->currSchema['CONSTRAINT'][$Name] == $sql)) {
504  continue;
505  }
506  if (preg_match("/PRIMARY KEY/", $sql)) {
507  $orderedConstraints['primary'][] = $sql;
508  } elseif (preg_match("/UNIQUE/", $sql)) {
509  $orderedConstraints['unique'][] = $sql;
510  } elseif (preg_match("/FOREIGN KEY/", $sql)) {
511  $orderedConstraints['foreign'][] = $sql;
512  } else {
513  $orderedConstraints['other'][] = $sql;
514  }
515  }
516  foreach ($orderedConstraints as $type => $constraints) {
517  foreach ($constraints as $sql) {
518  $this->applyOrEchoOnce($sql, $stmt = __METHOD__ . ".constraint.$type");
519  }
520  }
521  }
522 
532  function dropViews($catalog)
533  {
534  $sql = "SELECT view_name,vcs.table_name,column_name
535  FROM information_schema.view_column_usage AS vcs
536  INNER JOIN information_schema.views AS v
537  ON vcs.view_name = v.table_name
538  WHERE vcs.table_catalog='$catalog'
539  AND v.table_schema = 'public'
540  ORDER BY view_name,vcs.table_name,column_name;";
541  $stmt = __METHOD__;
542  $this->dbman->prepare($stmt, $sql);
543  $result = $this->dbman->execute($stmt);
544  while ($row = $this->dbman->fetchArray($result)) {
545  $View = $row['view_name'];
546  $table = $row['table_name'];
547  $column = $row['column_name'];
548  if (empty($this->schema['TABLE'][$table]) || empty($this->schema['TABLE'][$table][$column])) {
549  $sql = "DROP VIEW IF EXISTS \"$View\";";
550  $this->applyOrEchoOnce($sql);
551  }
552  }
553  $result = $this->dbman->freeResult($result);
554  }
555 
561  function dropColumnsFromTable($columns, $table)
562  {
563  if (empty($table) || empty($this->schema['TABLE'][$table])) {
564  return;
565  }
566  foreach ($columns as $column) {
567  if (empty($column)) {
568  continue;
569  }
570  if (empty($this->schema['TABLE'][$table][$column])) {
571  $sql = "ALTER TABLE \"$table\" DROP COLUMN \"$column\";";
572  $this->applyOrEchoOnce($sql);
573  }
574  }
575  }
576 
577 
581  function getCurrSchema()
582  {
583  $this->ensureDriver();
584  global $SysConf, $PG_CONN;
585  $this->currSchema = array();
586  $this->addInheritedRelations();
587  $referencedSequencesInTableColumns = $this->addTables();
588  if (!empty($SysConf['DBCONF']['user'])) {
589  $viewowner = $SysConf['DBCONF']['user'];
590  } elseif (!empty($PG_CONN)) {
591  $viewowner = pg_parameter_status($PG_CONN, 'session_authorization');
592  }
593  if (empty($viewowner)) {
594  throw new \Exception(
595  "Unable to load schema views: could not determine the view owner. " .
596  "Ensure \$SysConf['DBCONF']['user'] is set or a valid \$PG_CONN is available."
597  );
598  }
599  $this->addViews($viewowner);
600  $this->addSequences($referencedSequencesInTableColumns);
601  $this->addConstraints();
602  $this->addIndexes();
603  unset($this->currSchema['TABLEID']);
604  return $this->currSchema;
605  }
606 
611  {
612  $sql = "SELECT class.relname AS \"table\", daddy.relname AS inherits_from
613  FROM pg_class AS class
614  INNER JOIN pg_catalog.pg_inherits ON pg_inherits.inhrelid = class.oid
615  INNER JOIN pg_class daddy ON pg_inherits.inhparent = daddy.oid";
616  $this->dbman->prepare($stmt=__METHOD__, $sql);
617  $res = $this->dbman->execute($stmt);
618  $relations = array();
619  while ($row=$this->dbman->fetchArray($res)) {
620  $relations[$row['table']] = $row['inherits_from'];
621  }
622  $this->dbman->freeResult($res);
623  $this->currSchema['INHERITS'] = $relations;
624  }
625 
629  function addTables()
630  {
631  $referencedSequencesInTableColumns = array();
632 
633  $sql = "SELECT
634  table_name AS \"table\", ordinal_position AS ordinal, column_name,
635  udt_name AS type, character_maximum_length AS modifier,
636  CASE is_nullable WHEN 'YES' THEN false WHEN 'NO' THEN true END AS \"notnull\",
637  column_default AS \"default\",
638  col_description(table_name::regclass, ordinal_position) AS description,
639  (SELECT attinhcount FROM pg_attribute
640  WHERE attrelid = table_name::regclass AND attname = column_name) AS inhcount
641  FROM information_schema.columns
642  WHERE table_schema = 'public'
643  ORDER BY table_name, ordinal_position;";
644  $stmt = __METHOD__;
645  $this->dbman->prepare($stmt, $sql);
646  $result = $this->dbman->execute($stmt);
647  while ($R = $this->dbman->fetchArray($result)) {
648  $Table = $R['table'];
649  $Column = $R['column_name'];
650  /* Skip only columns inherited from the parent. Local columns of a child
651  * table must be known, else applyTables() rebuilds them on every run. */
652  if (array_key_exists($Table, $this->currSchema['INHERITS']) && $R['inhcount'] > 0) {
653  $this->currSchema['TABLEID'][$Table][$R['ordinal']] = $Column;
654  continue;
655  }
656  $Type = $R['type'];
657  if ($Type == 'bpchar') {
658  $Type = "char";
659  }
660  if ($R['modifier'] > 0) {
661  $Type .= '(' . $R['modifier'] . ')';
662  }
663  if (!empty($R['description'])) {
664  $Desc = str_replace("'", "''", $R['description']);
665  } else {
666  $Desc = "";
667  }
668  $this->currSchema['TABLEID'][$Table][$R['ordinal']] = $Column;
669  if (!empty($Desc)) {
670  $this->currSchema['TABLE'][$Table][$Column]['DESC'] = "COMMENT ON COLUMN \"$Table\".\"$Column\" IS '$Desc'";
671  } else {
672  $this->currSchema['TABLE'][$Table][$Column]['DESC'] = "";
673  }
674  $this->currSchema['TABLE'][$Table][$Column]['ADD'] = "ALTER TABLE \"$Table\" ADD COLUMN \"$Column\" $Type";
675  $this->currSchema['TABLE'][$Table][$Column]['ALTER'] = "ALTER TABLE \"$Table\"";
676  $Alter = "ALTER COLUMN \"$Column\"";
677  if ($R['notnull'] == 't') {
678  $this->currSchema['TABLE'][$Table][$Column]['ALTER'] .= " $Alter SET NOT NULL";
679  } else {
680  $this->currSchema['TABLE'][$Table][$Column]['ALTER'] .= " $Alter DROP NOT NULL";
681  }
682  if ($R['default'] != '') {
683  $R['default'] = preg_replace("/::bpchar/", "::char", $R['default']);
684  $R['default'] = str_replace("public.", "", $R['default']);
685  $this->currSchema['TABLE'][$Table][$Column]['ALTER'] .= ", $Alter SET DEFAULT " . $R['default'];
686  $this->currSchema['TABLE'][$Table][$Column]['ADD'] .= " DEFAULT " . $R['default'];
687 
688  $rgx = "/nextval\‍('([a-z_]*)'.*\‍)/";
689  $matches = array();
690  if (preg_match($rgx, $R['default'], $matches)) {
691  $sequence = $matches[1];
692  $referencedSequencesInTableColumns[$sequence] = array("table" => $Table, "column" => $Column);
693  }
694  }
695  }
696  $this->dbman->freeResult($result);
697 
698  return $referencedSequencesInTableColumns;
699  }
700 
705  function addViews($viewowner)
706  {
707  $sql = "SELECT viewname,definition FROM pg_views WHERE viewowner = $1";
708  $stmt = __METHOD__;
709  $this->dbman->prepare($stmt, $sql);
710  $result = $this->dbman->execute($stmt, array($viewowner));
711  while ($row = $this->dbman->fetchArray($result)) {
712  $sql = "CREATE VIEW \"" . $row['viewname'] . "\" AS " . $row['definition'];
713  $this->currSchema['VIEW'][$row['viewname']] = $sql;
714  }
715  $this->dbman->freeResult($result);
716  }
717 
722  function addSequences($referencedSequencesInTableColumns)
723  {
724  $sql = "SELECT relname
725  FROM pg_class
726  WHERE relkind = 'S'
727  AND relnamespace IN (
728  SELECT oid FROM pg_namespace WHERE nspname NOT LIKE 'pg_%' AND nspname != 'information_schema'
729  )";
730 
731  $stmt = __METHOD__;
732  $this->dbman->prepare($stmt, $sql);
733  $result = $this->dbman->execute($stmt);
734 
735  while ($row = $this->dbman->fetchArray($result)) {
736  $sequence = $row['relname'];
737  if (empty($sequence)) {
738  continue;
739  }
740 
741  $sqlCreate = "CREATE SEQUENCE \"" . $sequence . "\"";
742  $this->currSchema['SEQUENCE'][$sequence]['CREATE'] = $sqlCreate;
743 
744  if (array_key_exists($sequence, $referencedSequencesInTableColumns)) {
745  $table = $referencedSequencesInTableColumns[$sequence]['table'];
746  $column = $referencedSequencesInTableColumns[$sequence]['column'];
747 
748  $sqlUpdate = "SELECT setval('$sequence',(SELECT greatest(1,max($column)) val FROM $table))";
749  $this->currSchema['SEQUENCE'][$sequence]['UPDATE'] = $sqlUpdate;
750  }
751  }
752 
753  $this->dbman->freeResult($result);
754  }
755 
759  function addConstraints()
760  {
761  $sql = "SELECT c.conname AS constraint_name,
762  CASE c.contype
763  WHEN 'c' THEN 'CHECK'
764  WHEN 'f' THEN 'FOREIGN KEY'
765  WHEN 'p' THEN 'PRIMARY KEY'
766  WHEN 'u' THEN 'UNIQUE'
767  END AS type,
768  CASE WHEN c.condeferrable = 'f' THEN 0 ELSE 1 END AS is_deferrable,
769  CASE WHEN c.condeferred = 'f' THEN 0 ELSE 1 END AS is_deferred,
770  t.relname AS table_name, array_to_string(c.conkey, ' ') AS constraint_key,
771  CASE confupdtype
772  WHEN 'a' THEN 'NO ACTION'
773  WHEN 'r' THEN 'RESTRICT'
774  WHEN 'c' THEN 'CASCADE'
775  WHEN 'n' THEN 'SET NULL'
776  WHEN 'd' THEN 'SET DEFAULT'
777  END AS on_update,
778  CASE confdeltype
779  WHEN 'a' THEN 'NO ACTION'
780  WHEN 'r' THEN 'RESTRICT'
781  WHEN 'c' THEN 'CASCADE'
782  WHEN 'n' THEN 'SET NULL'
783  WHEN 'd' THEN 'SET DEFAULT' END AS on_delete,
784  CASE confmatchtype
785  WHEN 'u' THEN 'UNSPECIFIED'
786  WHEN 'f' THEN 'FULL'
787  WHEN 'p' THEN 'PARTIAL'
788  END AS match_type,
789  t2.relname AS references_table,
790  array_to_string(c.confkey, ' ') AS fk_constraint_key
791  FROM pg_constraint AS c
792  LEFT JOIN pg_class AS t ON c.conrelid = t.oid
793  INNER JOIN information_schema.tables AS tab ON t.relname = tab.table_name
794  LEFT JOIN pg_class AS t2 ON c.confrelid = t2.oid
795  ORDER BY constraint_name,table_name
796  ";
797  $stmt = __METHOD__;
798  $this->dbman->prepare($stmt, $sql);
799  $result = $this->dbman->execute($stmt);
800  $Results = $this->dbman->fetchAll($result);
801  $this->dbman->freeResult($result);
802  /* Constraints use indexes into columns. Covert those to column names. */
803  for ($i = 0; !empty($Results[$i]['constraint_name']); $i++) {
804  $Key = "";
805  $Keys = explode(" ", $Results[$i]['constraint_key']);
806  foreach ($Keys as $K) {
807  if (empty($K)) {
808  continue;
809  }
810  if (!empty($Key)) {
811  $Key .= ",";
812  }
813  if (!empty($this->currSchema['TABLEID'][$Results[$i]['table_name']][$K])) {
814  $Key .= '"' . $this->currSchema['TABLEID'][$Results[$i]['table_name']][$K] . '"';
815  }
816  }
817  $Results[$i]['constraint_key'] = $Key;
818  $Key = "";
819  if (!empty($Results[$i]['fk_constraint_key'])) {
820  $Keys = explode(" ", $Results[$i]['fk_constraint_key']);
821  } else {
822  $Keys = [];
823  }
824  foreach ($Keys as $K) {
825  if (empty($K)) {
826  continue;
827  }
828  if (!empty($Key)) {
829  $Key .= ",";
830  }
831  $Key .= '"' . $this->currSchema['TABLEID'][$Results[$i]['references_table']][$K] . '"';
832  }
833  $Results[$i]['fk_constraint_key'] = $Key;
834  }
835  /* Save the constraint */
836  /* There are different types of constraints that must be stored in order */
837  /* CONSTRAINT: PRIMARY KEY */
838  for ($i = 0; !empty($Results[$i]['constraint_name']); $i++) {
839  if ($Results[$i]['type'] != 'PRIMARY KEY') {
840  continue;
841  }
842  $sql = "ALTER TABLE \"" . $Results[$i]['table_name'] . "\"";
843  $sql .= " ADD CONSTRAINT \"" . $Results[$i]['constraint_name'] . '"';
844  $sql .= " " . $Results[$i]['type'];
845  $sql .= " (" . $Results[$i]['constraint_key'] . ")";
846  if (!empty($Results[$i]['references_table'])) {
847  $sql .= " REFERENCES \"" . $Results[$i]['references_table'] . "\"";
848  $sql .= " (" . $Results[$i]['fk_constraint_key'] . ")";
849  }
850  $sql .= ";";
851  $this->currSchema['CONSTRAINT'][$Results[$i]['constraint_name']] = $sql;
852  $Results[$i]['processed'] = 1;
853  }
854  /* CONSTRAINT: UNIQUE */
855  for ($i = 0; !empty($Results[$i]['constraint_name']); $i++) {
856  if ($Results[$i]['type'] != 'UNIQUE') {
857  continue;
858  }
859  $sql = "ALTER TABLE \"" . $Results[$i]['table_name'] . "\"";
860  $sql .= " ADD CONSTRAINT \"" . $Results[$i]['constraint_name'] . '"';
861  $sql .= " " . $Results[$i]['type'];
862  $sql .= " (" . $Results[$i]['constraint_key'] . ")";
863  if (!empty($Results[$i]['references_table'])) {
864  $sql .= " REFERENCES \"" . $Results[$i]['references_table'] . "\"";
865  $sql .= " (" . $Results[$i]['fk_constraint_key'] . ")";
866  }
867  $sql .= ";";
868  $this->currSchema['CONSTRAINT'][$Results[$i]['constraint_name']] = $sql;
869  $Results[$i]['processed'] = 1;
870  }
871 
872  /* CONSTRAINT: FOREIGN KEY */
873  for ($i = 0; !empty($Results[$i]['constraint_name']); $i++) {
874  if ($Results[$i]['type'] != 'FOREIGN KEY') {
875  continue;
876  }
877  $sql = "ALTER TABLE \"" . $Results[$i]['table_name'] . "\"";
878  $sql .= " ADD CONSTRAINT \"" . $Results[$i]['constraint_name'] . '"';
879  $sql .= " " . $Results[$i]['type'];
880  $sql .= " (" . $Results[$i]['constraint_key'] . ")";
881  if (!empty($Results[$i]['references_table'])) {
882  $sql .= " REFERENCES \"" . $Results[$i]['references_table'] . "\"";
883  $sql .= " (" . $Results[$i]['fk_constraint_key'] . ")";
884  }
885 
886  if (!empty($Results[$i]['on_update'])) {
887  $sql .= " ON UPDATE " . $Results[$i]['on_update'];
888  }
889  if (!empty($Results[$i]['on_delete'])) {
890  $sql .= " ON DELETE " . $Results[$i]['on_delete'];
891  }
892 
893  $sql .= ";";
894  $this->currSchema['CONSTRAINT'][$Results[$i]['constraint_name']] = $sql;
895  $Results[$i]['processed'] = 1;
896  }
897 
898  /* CONSTRAINT: ALL OTHERS */
899  for ($i = 0; !empty($Results[$i]['constraint_name']); $i++) {
900  if (!empty($Results[$i]['processed']) && $Results[$i]['processed'] == 1) {
901  continue;
902  }
903 
904  $sql = "ALTER TABLE \"" . $Results[$i]['table_name'] . "\"";
905  $sql .= " ADD CONSTRAINT \"" . $Results[$i]['constraint_name'] . '"';
906  $sql .= " " . $Results[$i]['type'];
907  $sql .= " (" . $Results[$i]['constraint_key'] . ")";
908  if (!empty($Results[$i]['references_table'])) {
909  $sql .= " REFERENCES \"" . $Results[$i]['references_table'] . "\"";
910  $sql .= " (" . $Results[$i]['fk_constraint_key'] . ")";
911  }
912  $sql .= ";";
913  $this->currSchema['CONSTRAINT'][$Results[$i]['constraint_name']] = $sql;
914  $Results[$i]['processed'] = 1;
915  }
916  }
917 
921  function addIndexes()
922  {
923  $sql = "SELECT tablename AS \"table\", indexname AS index, indexdef AS define
924  FROM pg_indexes
925  INNER JOIN information_schema.tables ON table_name = tablename
926  AND table_type = 'BASE TABLE'
927  AND table_schema = 'public'
928  AND schemaname = 'public'
929  ORDER BY tablename,indexname;
930  ";
931  $stmt = __METHOD__;
932  $this->dbman->prepare($stmt, $sql);
933  $result = $this->dbman->execute($stmt);
934  while ($row = $this->dbman->fetchArray($result)) {
935  /* UNIQUE constraints also include indexes. */
936  if (empty($this->currSchema['CONSTRAINT'][$row['index']])) {
937  $this->currSchema['INDEX'][$row['table']][$row['index']] = str_replace("public.", "", $row['define']) . ";";
938  }
939  }
940  $this->dbman->freeResult($result);
941  }
942 
949  {
950  // prosrc
951  // proretset == setof
952  $sql = "SELECT proname AS name,
953  pronargs AS input_num,
954  proargnames AS input_names,
955  proargtypes AS input_type,
956  proargmodes AS input_modes,
957  proretset AS setof,
958  prorettype AS output_type
959  FROM pg_proc AS proc
960  INNER JOIN pg_language AS lang ON proc.prolang = lang.oid
961  WHERE lang.lanname = 'plpgsql'
962  ORDER BY proname;";
963  $stmt = __METHOD__;
964  $this->dbman->prepare($stmt, $sql);
965  $result = $this->dbman->execute($stmt);
966  while ($row = $this->dbman->fetchArray($result)) {
967  $sql = "CREATE or REPLACE function " . $row['proname'] . "()";
968  $sql .= ' RETURNS ' . "TBD" . ' AS $$';
969  $sql .= " " . $row['prosrc'];
970  $schema['FUNCTION'][$row['proname']] = $sql;
971  }
972  $this->dbman->freeResult($result);
973  return $schema;
974  }
975 
983  function writeArrayEntries($fout, $key, $value, $varname)
984  {
985  $varname .= '["' . str_replace('"', '\"', $key) . '"]';
986  if (!is_array($value)) {
987  $value = str_replace('"', '\"', $value);
988  fwrite($fout, "$varname = \"$value\";\n");
989  return;
990  }
991  foreach ($value as $k => $v) {
992  $this->writeArrayEntries($fout, $k, $v, $varname);
993  }
994  fwrite($fout, "\n");
995  }
996 
1005  function exportSchema($filename = NULL)
1006  {
1007  if (empty($filename)) {
1008  $filename = 'php://stdout';
1009  }
1010  $Schema = $this->getCurrSchema();
1011  $fout = fopen($filename, "w");
1012  if (!$fout) {
1013  return ("Failed to write to $filename\n");
1014  }
1015  global $Name;
1016  fwrite($fout, "<?php\n");
1017  fwrite($fout, "/* This file is generated by " . $Name . " */\n");
1018  fwrite($fout, "/* Do not manually edit this file */\n\n");
1019  fwrite($fout, ' $Schema=array();' . "\n\n");
1020  foreach ($Schema as $K1 => $V1) {
1021  $this->writeArrayEntries($fout, $K1, $V1, ' $Schema');
1022  }
1023  fclose($fout);
1024  return false;
1025  }
1026 
1030  function makeFunctions()
1031  {
1032  print " Applying database functions\n";
1033  flush();
1034  /* *******************************************
1035  * uploadtree2path is a DB function that returns the non-artifact parents of an uploadtree_pk.
1036  * drop and recreate to change the return type.
1037  */
1038  $sql = 'drop function if exists uploadtree2path(integer);';
1039  $this->applyOrEchoOnce($sql, $stmt = __METHOD__ . '.uploadtree2path.drop');
1040 
1041  $sql = '
1042  CREATE function uploadtree2path(uploadtree_pk_in int) returns setof uploadtree as $$
1043  DECLARE
1044  UTrec uploadtree;
1045  UTpk integer;
1046  sql varchar;
1047  BEGIN
1048  UTpk := uploadtree_pk_in;
1049  WHILE UTpk > 0 LOOP
1050  sql := ' . "'" . 'select * from uploadtree where uploadtree_pk=' . "'" . ' || UTpk;
1051  execute sql into UTrec;
1052  IF ((UTrec.ufile_mode & (1<<28)) = 0) THEN RETURN NEXT UTrec; END IF;
1053  UTpk := UTrec.parent;
1054  END LOOP;
1055  RETURN;
1056  END;
1057  $$
1058  LANGUAGE plpgsql;
1059  ';
1060  $this->applyOrEchoOnce($sql, $stmt = __METHOD__ . '.uploadtree2path.create');
1061 
1062  /*
1063  * getItemParent is a DB function that returns the non-artifact parent of an uploadtree_pk.
1064  * drop and recreate to change the return type.
1065  */
1066  $sql = 'drop function if exists getItemParent(integer);';
1067  $this->applyOrEchoOnce($sql, $stmt = __METHOD__ . '.getItemParent.drop');
1068 
1069  $sql = '
1070  CREATE OR REPLACE FUNCTION getItemParent(itemId Integer) RETURNS Integer AS $$
1071  WITH RECURSIVE file_tree(uploadtree_pk, parent, jump, path, cycle) AS (
1072  SELECT ut.uploadtree_pk, ut.parent,
1073  true,
1074  ARRAY[ut.uploadtree_pk],
1075  false
1076  FROM uploadtree ut
1077  WHERE ut.uploadtree_pk = $1
1078  UNION ALL
1079  SELECT ut.uploadtree_pk, ut.parent,
1080  ut.ufile_mode & (1<<28) != 0,
1081  path || ut.uploadtree_pk,
1082  ut.uploadtree_pk = ANY(path)
1083  FROM uploadtree ut, file_tree ft
1084  WHERE ut.uploadtree_pk = ft.parent AND jump AND NOT cycle
1085  )
1086  SELECT uploadtree_pk from file_tree ft WHERE NOT jump
1087  $$
1088  LANGUAGE SQL
1089  STABLE
1090  RETURNS NULL ON NULL INPUT
1091  ';
1092  $this->applyOrEchoOnce($sql, $stmt = __METHOD__ . '.getItemParent.create');
1093  return;
1094  }
1095 
1100  {
1101  if (empty($this->schema['INHERITS'])) {
1102  return;
1103  }
1104  foreach ($this->schema['INHERITS'] as $table => $fromTable) {
1105  if (empty($table)) {
1106  continue;
1107  }
1108  if (!$this->dbman->existsTable($table) && $this->dbman->existsTable($fromTable)) {
1109  $sql = "CREATE TABLE \"$table\" () INHERITS (\"$fromTable\")";
1110  $this->applyOrEchoOnce($sql, $stmt = __METHOD__ . $table);
1111  }
1112  }
1113  }
1114 
1115  // MakeFunctions()
1116 }
1117 
1118 if (empty($dbManager) || !($dbManager instanceof DbManager)) {
1119  $logLevel = Logger::INFO;
1120  $logger = new Logger(__FILE__);
1121  $logger->pushHandler(new ErrorLogHandler(ErrorLogHandler::OPERATING_SYSTEM, $logLevel));
1122  $dbManager = new ModernDbManager($logger);
1123 }
1124 global $PG_CONN;
1125 if (empty($PG_CONN)) {
1126  $sysconfdir = getenv('SYSCONFDIR');
1127  if (empty($sysconfdir)) {
1128  $sysconfdir = "/usr/local/etc/fossology";
1129  }
1130  $foConf = $sysconfdir . "/fossology.conf";
1131  if (file_exists($foConf)) {
1132  require_once(__DIR__ . "/common-db.php");
1133  $PG_CONN = DBconnect($sysconfdir);
1134  $GLOBALS['PG_CONN'] = $PG_CONN;
1135  if (!isset($GLOBALS['SysConf']) && isset($SysConf)) {
1136  $GLOBALS['SysConf'] = $SysConf;
1137  }
1138  }
1139 }
1140 /* simulate the old functions*/
1141 $libschema = new fo_libschema($dbManager);
1149 function ApplySchema($Filename = NULL, $Debug = false, $Catalog = 'fossology')
1150 {
1151  global $libschema;
1152  return $libschema->applySchema($Filename, $Debug, $Catalog);
1153 }
1154 
1158 function GetSchema()
1159 {
1160  global $libschema;
1161  return $libschema->getCurrSchema();
1162 }
1163 
1170 function ExportSchema($filename = NULL)
1171 {
1172  global $libschema;
1173  return $libschema->exportSchema($filename);
1174 }
1175 
1179 function MakeFunctions($Debug)
1180 {
1181  global $libschema;
1182  $libschema->makeFunctions($Debug);
1183 }
int debug
Definition: buckets.c:57
Class to handle database schema.
Definition: libschema.php:29
writeArrayEntries($fout, $key, $value, $varname)
Definition: libschema.php:983
applyTables($inherits=false)
Add tables/columns (dependent on sequences for default values)
Definition: libschema.php:284
addFunctions($schema)
Definition: libschema.php:948
applySchema($filename=NULL, $debug=false, $catalog='fossology', $migrateColumns=array())
Make schema match $Filename. This is a single transaction.
Definition: libschema.php:119
exportSchema($filename=NULL)
Export the schema of the connected database to a file in the format readable by GetSchema().
Definition: libschema.php:1005
__construct(DbManager &$dbManager)
Definition: libschema.php:58
addViews($viewowner)
Definition: libschema.php:705
addInheritedRelations()
Definition: libschema.php:610
applySequences()
Add sequences to the database.
Definition: libschema.php:214
applyClusters()
Add clusters.
Definition: libschema.php:237
applyOrEchoOnce($sql, $stmt='')
Definition: libschema.php:100
dropColumnsFromTable($columns, $table)
Definition: libschema.php:561
dropViews($catalog)
Delete views.
Definition: libschema.php:532
updateSequences()
Add sequences.
Definition: libschema.php:261
addSequences($referencedSequencesInTableColumns)
Definition: libschema.php:722
dropConstraints()
Delete constraints.
Definition: libschema.php:399
makeFunctions()
Create any required DB functions.
Definition: libschema.php:1030
getCurrSchema()
Load the schema from the db into an array.
Definition: libschema.php:581
setDriver(Driver &$dbDriver)
Definition: libschema.php:67
applyInheritedRelations()
Definition: libschema.php:1099
ReportCachePurgeAll()
Purge all records from the report cache.
DB_ColumnConstraints($table, $column)
Get constraints on a specific column.
Definition: common-db.php:316
DBconnect($sysconfdir, $options="", $exitOnFail=true)
Connect to database engine. This is a no-op if $PG_CONN already has a value.
Definition: common-db.php:33
DB_ColExists($tableName, $colName, $DBName='fossology')
Check if a column exists.
Definition: common-db.php:242
DB_ConstraintExists($ConstraintName, $DBName='fossology')
Check if a constraint exists.
Definition: common-db.php:266
DB_TableExists($tableName)
Check if table exists.
Definition: common-db.php:216
ExportSchema($filename=NULL)
Export the schema of the connected database to a file in the format readable by GetSchema().
Definition: libschema.php:1170
GetSchema()
Load the schema from the db into an array.
Definition: libschema.php:1158
MakeFunctions($Debug)
Create any required DB functions.
Definition: libschema.php:1179
ApplySchema($Filename=NULL, $Debug=false, $Catalog='fossology')
Make schema match $Filename. This is a single transaction.
Definition: libschema.php:1149
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN