26 require_once(__DIR__ .
'/../../vendor/autoload.php');
71 $this->dbman = $dbManager;
80 $this->dbman->setDriver($dbDriver);
95 return $this->dbman->queryOnce($sql, $stmt);
108 function applySchema($filename = NULL,
$debug =
false, $catalog =
'fossology', $migrateColumns = array())
113 $sql_statement =
"select lanname from pg_language where lanname = 'plpgsql'";
115 $result = pg_query($PG_CONN, $sql_statement);
117 throw new Exception(
"Could not check the database for plpgsql language");
120 $plpgsql_already_installed =
false;
121 if ( pg_fetch_row($result) ) {
122 $plpgsql_already_installed =
true;
126 if ($plpgsql_already_installed ==
false) {
127 $sql_statement =
"CREATE LANGUAGE plpgsql";
128 $result = pg_query($PG_CONN, $sql_statement);
130 throw new Exception(
"Could not create plpgsql language in the database");
135 if (!file_exists($filename)) {
136 $errMsg =
"$filename does not exist.";
141 $this->schema = $Schema;
144 if ((count($this->schema[
'TABLE']) < 5) || (count($this->schema[
'SEQUENCE']) < 5)
145 || (count($this->schema[
'INDEX']) < 5) || (count($this->schema[
'CONSTRAINT']) < 5)
147 $errMsg =
"Schema from '$filename' appears invalid.";
152 $result = $this->dbman->getSingleRow(
"show statement_timeout", array(), $stmt = __METHOD__ .
'.getTimeout');
153 $statementTimeout = $result[
'statement_timeout'];
154 $this->dbman->queryOnce(
"SET statement_timeout = 0", $stmt = __METHOD__ .
'.setTimeout');
159 $errlev = error_reporting(E_ERROR | E_WARNING | E_PARSE);
172 error_reporting($errlev);
178 foreach ($this->currSchema[
'TABLE'] as $table => $columns) {
179 $skipColumns = array_key_exists($table, $migrateColumns) ? $migrateColumns[$table] : array();
180 $dropColumns = array_diff(array_keys($columns), $skipColumns);
187 $this->dbman->getSingleRow(
"SET statement_timeout = $statementTimeout", array(), $stmt = __METHOD__ .
'.resetTimeout');
188 print
"DB schema has been updated for $catalog.\n";
190 print
"These queries could update DB schema for $catalog.\n";
203 if (empty($this->schema[
'SEQUENCE'])) {
206 foreach ($this->schema[
'SEQUENCE'] as $name => $import) {
211 if (!array_key_exists(
'SEQUENCE', $this->currSchema)
212 || !array_key_exists($name, $this->currSchema[
'SEQUENCE'])) {
213 $createSql = is_string($import) ? $import : $import[
'CREATE'];
214 $this->
applyOrEchoOnce($createSql, $stmt = __METHOD__ .
"." . $name .
".CREATE");
226 if (empty($this->schema[
'CLUSTER'])) {
229 foreach ($this->schema[
'CLUSTER'] as $name => $sql) {
234 if (!array_key_exists(
'CLUSTER', $this->currSchema)
235 || !array_key_exists($name, $this->currSchema[
'CLUSTER'])) {
236 $this->
applyOrEchoOnce($sql, $stmt = __METHOD__ .
"." . $name .
".CREATE");
250 if (empty($this->schema[
'SEQUENCE'])) {
253 foreach ($this->schema[
'SEQUENCE'] as $name => $import) {
258 if (is_array($import) && array_key_exists(
'UPDATE', $import)) {
259 $this->
applyOrEchoOnce($import[
'UPDATE'], $stmt = __METHOD__ .
"." . $name);
272 if (empty($this->schema[
'TABLE'])) {
275 foreach ($this->schema[
'TABLE'] as $table => $columns) {
276 if (empty($table) || $inherits^array_key_exists($table,$this->schema[
'INHERITS']) ) {
280 $sql =
"CREATE TABLE IF NOT EXISTS \"$table\" ()";
283 foreach ($columns as $column => $modification) {
284 if ($this->currSchema[
'TABLE'][$table][$column][
'ADD'] != $modification[
'ADD']) {
289 $rename = $column .
'_old';
290 $sql =
"ALTER TABLE \"$table\" RENAME COLUMN \"$column\" TO \"$rename\"";
294 $sql = $modification[
'ADD'];
299 $this->dbman->queryOnce($sql);
301 if (!empty($rename)) {
303 $this->
applyOrEchoOnce($sql =
"UPDATE \"$table\" SET \"$column\" = \"$rename\"");
304 $this->
applyOrEchoOnce($sql =
"ALTER TABLE \"$table\" DROP COLUMN \"$rename\"");
307 if ($this->currSchema[
'TABLE'][$table][$column][
'ALTER'] != $modification[
'ALTER'] && isset($modification[
'ALTER'])) {
308 $sql = $modification[
'ALTER'];
311 }
else if (!empty ($sql)) {
312 $this->dbman->queryOnce($sql);
315 if ($this->currSchema[
'TABLE'][$table][$column][
'DESC'] != $modification[
'DESC']) {
316 $sql = empty($modification[
'DESC']) ?
"COMMENT ON COLUMN \"$table\".\"$column\" IS ''" : $modification[
'DESC'];
317 $this->
applyOrEchoOnce($sql, $stmt = __METHOD__ .
"$table.$column.comment");
328 if (empty($this->schema[
'VIEW'])) {
331 foreach ($this->schema[
'VIEW'] as $name => $sql) {
332 if (empty($name) || $this->currSchema[
'VIEW'][$name] == $sql) {
335 if (!empty($this->currSchema[
'VIEW'][$name])) {
336 $sqlDropView =
"DROP VIEW IF EXISTS $name";
350 if (empty($this->currSchema[
'CONSTRAINT'])) {
353 foreach ($this->currSchema[
'CONSTRAINT'] as $name => $sql) {
355 if (empty($name) || ($this->schema[
'CONSTRAINT'][$name] == $sql)
361 $table = preg_replace(
"/^ALTER TABLE \"(.*)\" ADD CONSTRAINT.*/",
'${1}', $sql);
362 $TableFk = preg_replace(
"/^.*FOREIGN KEY .* REFERENCES \"(.*)\" \(.*/",
'${1}', $sql);
363 if ($TableFk == $sql) {
367 if (empty($this->schema[
'TABLE'][$table]) && empty($this->schema[
'TABLE'][$TableFk])) {
370 $sql =
"ALTER TABLE \"$table\" DROP CONSTRAINT \"$name\" CASCADE";
380 if (empty($this->currSchema[
'INDEX'])) {
383 foreach ($this->currSchema[
'INDEX'] as $table => $IndexInfo) {
384 if (empty($table) || (empty($this->schema[
'TABLE'][$table]) && empty($this->schema[
'INHERITS'][$table]))) {
387 foreach ($IndexInfo as $name => $sql) {
388 if (empty($name) || $this->schema[
'INDEX'][$table][$name] == $sql) {
391 $sql =
"DROP INDEX \"$name\"";
402 if (empty($this->schema[
'INDEX'])) {
405 foreach ($this->schema[
'INDEX'] as $table => $indexInfo) {
409 if (!array_key_exists($table, $this->schema[
"TABLE"]) && !array_key_exists($table, $this->schema[
'INHERITS'])) {
410 echo
"skipping orphan table: $table\n";
413 foreach ($indexInfo as $name => $sql) {
414 if (empty($name) || $this->currSchema[
'INDEX'][$table][$name] == $sql) {
418 $sql =
"REINDEX INDEX \"$name\"";
430 if (empty($this->schema[
'CONSTRAINT'])) {
434 $orderedConstraints = array(
'primary' => array(),
'unique' => array(),
'foreign' => array(),
'other' => array());
435 foreach ($this->schema[
'CONSTRAINT'] as $Name => $sql) {
436 if (empty($Name) || $this->currSchema[
'CONSTRAINT'][$Name] == $sql) {
439 if (preg_match(
"/PRIMARY KEY/", $sql)) {
440 $orderedConstraints[
'primary'][] = $sql;
441 } elseif (preg_match(
"/UNIQUE/", $sql)) {
442 $orderedConstraints[
'unique'][] = $sql;
443 } elseif (preg_match(
"/FOREIGN KEY/", $sql)) {
444 $orderedConstraints[
'foreign'][] = $sql;
446 $orderedConstraints[
'other'][] = $sql;
449 foreach ($orderedConstraints as $type => $constraints) {
450 foreach ($constraints as $sql) {
451 $this->
applyOrEchoOnce($sql, $stmt = __METHOD__ .
".constraint.$type");
467 $sql =
"SELECT view_name,table_name,column_name 468 FROM information_schema.view_column_usage 469 WHERE table_catalog='$catalog' 470 ORDER BY view_name,table_name,column_name";
472 $this->dbman->prepare($stmt, $sql);
473 $result = $this->dbman->execute($stmt);
474 while ($row = $this->dbman->fetchArray($result)) {
475 $View = $row[
'view_name'];
476 $table = $row[
'table_name'];
477 if (empty($this->schema[
'TABLE'][$table])) {
480 $column = $row[
'column_name'];
481 if (empty($this->schema[
'TABLE'][$table][$column])) {
482 $sql =
"DROP VIEW \"$View\";";
486 $result = $this->dbman->freeResult($result);
496 if (empty($table) || empty($this->schema[
'TABLE'][$table])) {
499 foreach ($columns as $column) {
500 if (empty($column)) {
503 if (empty($this->schema[
'TABLE'][$table][$column])) {
504 $sql =
"ALTER TABLE \"$table\" DROP COLUMN \"$column\";";
517 $this->currSchema = array();
519 $referencedSequencesInTableColumns = $this->
addTables();
520 $this->
addViews($viewowner = $SysConf[
'DBCONF'][
'user']);
521 $this->
addSequences($referencedSequencesInTableColumns);
524 unset($this->currSchema[
'TABLEID']);
533 $sql =
"SELECT class.relname AS table, daddy.relname AS inherits_from 534 FROM pg_class AS class 535 INNER JOIN pg_catalog.pg_inherits ON pg_inherits.inhrelid = class.oid 536 INNER JOIN pg_class daddy ON pg_inherits.inhparent = daddy.oid";
537 $this->dbman->prepare($stmt=__METHOD__, $sql);
538 $res = $this->dbman->execute($stmt);
539 $relations = array();
540 while ($row=$this->dbman->fetchArray($res)) {
541 $relations[$row[
'table']] = $row[
'inherits_from'];
543 $this->dbman->freeResult($res);
544 $this->currSchema[
'INHERITS'] = $relations;
552 $referencedSequencesInTableColumns = array();
555 table_name AS table, ordinal_position AS ordinal, column_name, 556 udt_name AS type, character_maximum_length AS modifier, 557 CASE is_nullable WHEN 'YES' THEN false WHEN 'NO' THEN true END AS notnull, 558 column_default AS default, 559 col_description(table_name::regclass, ordinal_position) AS description 560 FROM information_schema.columns 561 WHERE table_schema = 'public' 562 ORDER BY table_name, ordinal_position;";
564 $this->dbman->prepare($stmt, $sql);
565 $result = $this->dbman->execute($stmt);
566 while ($R = $this->dbman->fetchArray($result)) {
567 $Table = $R[
'table'];
568 $Column = $R[
'column_name'];
569 if (array_key_exists($Table, $this->currSchema[
'INHERITS'])) {
570 $this->currSchema[
'TABLEID'][$Table][$R[
'ordinal']] = $Column;
574 if ($Type ==
'bpchar') {
577 if ($R[
'modifier'] > 0) {
578 $Type .=
'(' . $R[
'modifier'] .
')';
580 $Desc = str_replace(
"'",
"''", $R[
'description']);
581 $this->currSchema[
'TABLEID'][$Table][$R[
'ordinal']] = $Column;
583 $this->currSchema[
'TABLE'][$Table][$Column][
'DESC'] =
"COMMENT ON COLUMN \"$Table\".\"$Column\" IS '$Desc'";
585 $this->currSchema[
'TABLE'][$Table][$Column][
'DESC'] =
"";
587 $this->currSchema[
'TABLE'][$Table][$Column][
'ADD'] =
"ALTER TABLE \"$Table\" ADD COLUMN \"$Column\" $Type";
588 $this->currSchema[
'TABLE'][$Table][$Column][
'ALTER'] =
"ALTER TABLE \"$Table\"";
589 $Alter =
"ALTER COLUMN \"$Column\"";
590 if ($R[
'notnull'] ==
't') {
591 $this->currSchema[
'TABLE'][$Table][$Column][
'ALTER'] .=
" $Alter SET NOT NULL";
593 $this->currSchema[
'TABLE'][$Table][$Column][
'ALTER'] .=
" $Alter DROP NOT NULL";
595 if ($R[
'default'] !=
'') {
596 $R[
'default'] = preg_replace(
"/::bpchar/",
"::char", $R[
'default']);
597 $R[
'default'] = str_replace(
"public.",
"", $R[
'default']);
598 $this->currSchema[
'TABLE'][$Table][$Column][
'ALTER'] .=
", $Alter SET DEFAULT " . $R[
'default'];
599 $this->currSchema[
'TABLE'][$Table][$Column][
'ADD'] .=
" DEFAULT " . $R[
'default'];
601 $rgx =
"/nextval\('([a-z_]*)'.*\)/";
603 if (preg_match($rgx, $R[
'default'], $matches)) {
604 $sequence = $matches[1];
605 $referencedSequencesInTableColumns[$sequence] = array(
"table" => $Table,
"column" => $Column);
609 $this->dbman->freeResult($result);
611 return $referencedSequencesInTableColumns;
620 $sql =
"SELECT viewname,definition FROM pg_views WHERE viewowner = $1";
622 $this->dbman->prepare($stmt, $sql);
623 $result = $this->dbman->execute($stmt, array($viewowner));
624 while ($row = $this->dbman->fetchArray($result)) {
625 $sql =
"CREATE VIEW \"" . $row[
'viewname'] .
"\" AS " . $row[
'definition'];
626 $this->currSchema[
'VIEW'][$row[
'viewname']] = $sql;
628 $this->dbman->freeResult($result);
637 $sql =
"SELECT relname 640 AND relnamespace IN ( 641 SELECT oid FROM pg_namespace WHERE nspname NOT LIKE 'pg_%' AND nspname != 'information_schema' 645 $this->dbman->prepare($stmt, $sql);
646 $result = $this->dbman->execute($stmt);
648 while ($row = $this->dbman->fetchArray($result)) {
649 $sequence = $row[
'relname'];
650 if (empty($sequence)) {
654 $sqlCreate =
"CREATE SEQUENCE \"" . $sequence .
"\"";
655 $this->currSchema[
'SEQUENCE'][$sequence][
'CREATE'] = $sqlCreate;
657 if (array_key_exists($sequence, $referencedSequencesInTableColumns)) {
658 $table = $referencedSequencesInTableColumns[$sequence][
'table'];
659 $column = $referencedSequencesInTableColumns[$sequence][
'column'];
661 $sqlUpdate =
"SELECT setval('$sequence',(SELECT greatest(1,max($column)) val FROM $table))";
662 $this->currSchema[
'SEQUENCE'][$sequence][
'UPDATE'] = $sqlUpdate;
666 $this->dbman->freeResult($result);
674 $sql =
"SELECT c.conname AS constraint_name, 676 WHEN 'c' THEN 'CHECK' 677 WHEN 'f' THEN 'FOREIGN KEY' 678 WHEN 'p' THEN 'PRIMARY KEY' 679 WHEN 'u' THEN 'UNIQUE' 681 CASE WHEN c.condeferrable = 'f' THEN 0 ELSE 1 END AS is_deferrable, 682 CASE WHEN c.condeferred = 'f' THEN 0 ELSE 1 END AS is_deferred, 683 t.relname AS table_name, array_to_string(c.conkey, ' ') AS constraint_key, 685 WHEN 'a' THEN 'NO ACTION' 686 WHEN 'r' THEN 'RESTRICT' 687 WHEN 'c' THEN 'CASCADE' 688 WHEN 'n' THEN 'SET NULL' 689 WHEN 'd' THEN 'SET DEFAULT' 692 WHEN 'a' THEN 'NO ACTION' 693 WHEN 'r' THEN 'RESTRICT' 694 WHEN 'c' THEN 'CASCADE' 695 WHEN 'n' THEN 'SET NULL' 696 WHEN 'd' THEN 'SET DEFAULT' END AS on_delete, 698 WHEN 'u' THEN 'UNSPECIFIED' 700 WHEN 'p' THEN 'PARTIAL' 702 t2.relname AS references_table, 703 array_to_string(c.confkey, ' ') AS fk_constraint_key 704 FROM pg_constraint AS c 705 LEFT JOIN pg_class AS t ON c.conrelid = t.oid 706 INNER JOIN information_schema.tables AS tab ON t.relname = tab.table_name 707 LEFT JOIN pg_class AS t2 ON c.confrelid = t2.oid 708 ORDER BY constraint_name,table_name 711 $this->dbman->prepare($stmt, $sql);
712 $result = $this->dbman->execute($stmt);
713 $Results = $this->dbman->fetchAll($result);
714 $this->dbman->freeResult($result);
716 for ($i = 0; !empty($Results[$i][
'constraint_name']); $i++) {
718 $Keys = explode(
" ", $Results[$i][
'constraint_key']);
719 foreach ($Keys as $K) {
726 $Key .=
'"' . $this->currSchema[
'TABLEID'][$Results[$i][
'table_name']][$K] .
'"';
728 $Results[$i][
'constraint_key'] = $Key;
730 $Keys = explode(
" ", $Results[$i][
'fk_constraint_key']);
731 foreach ($Keys as $K) {
738 $Key .=
'"' . $this->currSchema[
'TABLEID'][$Results[$i][
'references_table']][$K] .
'"';
740 $Results[$i][
'fk_constraint_key'] = $Key;
745 for ($i = 0; !empty($Results[$i][
'constraint_name']); $i++) {
746 if ($Results[$i][
'type'] !=
'PRIMARY KEY') {
749 $sql =
"ALTER TABLE \"" . $Results[$i][
'table_name'] .
"\"";
750 $sql .=
" ADD CONSTRAINT \"" . $Results[$i][
'constraint_name'] .
'"';
751 $sql .=
" " . $Results[$i][
'type'];
752 $sql .=
" (" . $Results[$i][
'constraint_key'] .
")";
753 if (!empty($Results[$i][
'references_table'])) {
754 $sql .=
" REFERENCES \"" . $Results[$i][
'references_table'] .
"\"";
755 $sql .=
" (" . $Results[$i][
'fk_constraint_key'] .
")";
758 $this->currSchema[
'CONSTRAINT'][$Results[$i][
'constraint_name']] = $sql;
759 $Results[$i][
'processed'] = 1;
762 for ($i = 0; !empty($Results[$i][
'constraint_name']); $i++) {
763 if ($Results[$i][
'type'] !=
'UNIQUE') {
766 $sql =
"ALTER TABLE \"" . $Results[$i][
'table_name'] .
"\"";
767 $sql .=
" ADD CONSTRAINT \"" . $Results[$i][
'constraint_name'] .
'"';
768 $sql .=
" " . $Results[$i][
'type'];
769 $sql .=
" (" . $Results[$i][
'constraint_key'] .
")";
770 if (!empty($Results[$i][
'references_table'])) {
771 $sql .=
" REFERENCES \"" . $Results[$i][
'references_table'] .
"\"";
772 $sql .=
" (" . $Results[$i][
'fk_constraint_key'] .
")";
775 $this->currSchema[
'CONSTRAINT'][$Results[$i][
'constraint_name']] = $sql;
776 $Results[$i][
'processed'] = 1;
780 for ($i = 0; !empty($Results[$i][
'constraint_name']); $i++) {
781 if ($Results[$i][
'type'] !=
'FOREIGN KEY') {
784 $sql =
"ALTER TABLE \"" . $Results[$i][
'table_name'] .
"\"";
785 $sql .=
" ADD CONSTRAINT \"" . $Results[$i][
'constraint_name'] .
'"';
786 $sql .=
" " . $Results[$i][
'type'];
787 $sql .=
" (" . $Results[$i][
'constraint_key'] .
")";
788 if (!empty($Results[$i][
'references_table'])) {
789 $sql .=
" REFERENCES \"" . $Results[$i][
'references_table'] .
"\"";
790 $sql .=
" (" . $Results[$i][
'fk_constraint_key'] .
")";
793 if (!empty($Results[$i][
'on_update'])) {
794 $sql .=
" ON UPDATE " . $Results[$i][
'on_update'];
796 if (!empty($Results[$i][
'on_delete'])) {
797 $sql .=
" ON DELETE " . $Results[$i][
'on_delete'];
801 $this->currSchema[
'CONSTRAINT'][$Results[$i][
'constraint_name']] = $sql;
802 $Results[$i][
'processed'] = 1;
806 for ($i = 0; !empty($Results[$i][
'constraint_name']); $i++) {
807 if (!empty($Results[$i][
'processed']) && $Results[$i][
'processed'] == 1) {
811 $sql =
"ALTER TABLE \"" . $Results[$i][
'table_name'] .
"\"";
812 $sql .=
" ADD CONSTRAINT \"" . $Results[$i][
'constraint_name'] .
'"';
813 $sql .=
" " . $Results[$i][
'type'];
814 $sql .=
" (" . $Results[$i][
'constraint_key'] .
")";
815 if (!empty($Results[$i][
'references_table'])) {
816 $sql .=
" REFERENCES \"" . $Results[$i][
'references_table'] .
"\"";
817 $sql .=
" (" . $Results[$i][
'fk_constraint_key'] .
")";
820 $this->currSchema[
'CONSTRAINT'][$Results[$i][
'constraint_name']] = $sql;
821 $Results[$i][
'processed'] = 1;
830 $sql =
"SELECT tablename AS table, indexname AS index, indexdef AS define 832 INNER JOIN information_schema.tables ON table_name = tablename 833 AND table_type = 'BASE TABLE' 834 AND table_schema = 'public' 835 AND schemaname = 'public' 836 ORDER BY tablename,indexname; 839 $this->dbman->prepare($stmt, $sql);
840 $result = $this->dbman->execute($stmt);
841 while ($row = $this->dbman->fetchArray($result)) {
843 if (empty($this->currSchema[
'CONSTRAINT'][$row[
'index']])) {
844 $this->currSchema[
'INDEX'][$row[
'table']][$row[
'index']] = str_replace(
"public.",
"", $row[
'define']) .
";";
847 $this->dbman->freeResult($result);
859 $sql =
"SELECT proname AS name, 860 pronargs AS input_num, 861 proargnames AS input_names, 862 proargtypes AS input_type, 863 proargmodes AS input_modes, 865 prorettype AS output_type 867 INNER JOIN pg_language AS lang ON proc.prolang = lang.oid 868 WHERE lang.lanname = 'plpgsql' 871 $this->dbman->prepare($stmt, $sql);
872 $result = $this->dbman->execute($stmt);
873 while ($row = $this->dbman->fetchArray($result)) {
874 $sql =
"CREATE or REPLACE function " . $row[
'proname'] .
"()";
875 $sql .=
' RETURNS ' .
"TBD" .
' AS $$';
876 $sql .=
" " . $row[
'prosrc'];
877 $schema[
'FUNCTION'][$row[
'proname']] = $sql;
879 $this->dbman->freeResult($result);
892 $varname .=
'["' . str_replace(
'"',
'\"', $key) .
'"]';
893 if (!is_array($value)) {
894 $value = str_replace(
'"',
'\"', $value);
895 fwrite($fout,
"$varname = \"$value\";\n");
898 foreach ($value as $k => $v) {
917 $dbDriver = $this->dbman->getDriver();
918 if (empty($dbDriver)) {
919 $this->dbman->setDriver(
new Postgres($PG_CONN));
922 if (empty($filename)) {
926 $fout = fopen($filename,
"w");
928 return (
"Failed to write to $filename\n");
931 fwrite($fout,
"<?php\n");
932 fwrite($fout,
"/* This file is generated by " . $Name .
" */\n");
933 fwrite($fout,
"/* Do not manually edit this file */\n\n");
934 fwrite($fout,
' $Schema=array();' .
"\n\n");
935 foreach ($Schema as $K1 => $V1) {
948 print
" Applying database functions\n";
954 $sql =
'drop function if exists uploadtree2path(integer);';
955 $this->
applyOrEchoOnce($sql, $stmt = __METHOD__ .
'.uploadtree2path.drop');
958 CREATE function uploadtree2path(uploadtree_pk_in int) returns setof uploadtree as $$ 964 UTpk := uploadtree_pk_in; 966 sql := ' .
"'" .
'select * from uploadtree where uploadtree_pk=' .
"'" .
' || UTpk; 967 execute sql into UTrec; 968 IF ((UTrec.ufile_mode & (1<<28)) = 0) THEN RETURN NEXT UTrec; END IF; 969 UTpk := UTrec.parent; 976 $this->
applyOrEchoOnce($sql, $stmt = __METHOD__ .
'.uploadtree2path.create');
982 $sql =
'drop function if exists getItemParent(integer);';
983 $this->
applyOrEchoOnce($sql, $stmt = __METHOD__ .
'.getItemParent.drop');
986 CREATE OR REPLACE FUNCTION getItemParent(itemId Integer) RETURNS Integer AS $$ 987 WITH RECURSIVE file_tree(uploadtree_pk, parent, jump, path, cycle) AS ( 988 SELECT ut.uploadtree_pk, ut.parent, 990 ARRAY[ut.uploadtree_pk], 993 WHERE ut.uploadtree_pk = $1 995 SELECT ut.uploadtree_pk, ut.parent, 996 ut.ufile_mode & (1<<28) != 0, 997 path || ut.uploadtree_pk, 998 ut.uploadtree_pk = ANY(path) 999 FROM uploadtree ut, file_tree ft 1000 WHERE ut.uploadtree_pk = ft.parent AND jump AND NOT cycle 1002 SELECT uploadtree_pk from file_tree ft WHERE NOT jump 1006 RETURNS NULL ON NULL INPUT 1008 $this->
applyOrEchoOnce($sql, $stmt = __METHOD__ .
'.getItemParent.create');
1017 if (empty($this->schema[
'INHERITS'])) {
1020 foreach ($this->schema[
'INHERITS'] as $table => $fromTable) {
1021 if (empty($table)) {
1024 if (!$this->dbman->existsTable($table) && $this->dbman->existsTable($fromTable)) {
1025 $sql =
"CREATE TABLE \"$table\" () INHERITS (\"$fromTable\")";
1034 if (empty($dbManager) || !($dbManager instanceof
DbManager)) {
1035 $logLevel = Logger::INFO;
1036 $logger =
new Logger(__FILE__);
1037 $logger->pushHandler(
new ErrorLogHandler(ErrorLogHandler::OPERATING_SYSTEM, $logLevel));
1040 $dbManager->setDriver($pgDriver);
1051 function ApplySchema($Filename = NULL, $Debug =
false, $Catalog =
'fossology')
1054 return $libschema->applySchema($Filename, $Debug, $Catalog);
1063 return $libschema->getCurrSchema();
1075 return $libschema->exportSchema($filename);
1084 return $libschema->makeFunctions($Debug);
Class to handle database schema.
ApplySchema($Filename=NULL, $Debug=false, $Catalog= 'fossology')
Make schema match $Filename. This is a single transaction.
dropConstraints()
Delete constraints.
applyClusters()
Add clusters.
dropColumnsFromTable($columns, $table)
makeFunctions()
Create any required DB functions.
exportSchema($filename=NULL)
Export the schema of the connected ($PG_CONN) database to a file in the format readable by GetSchema(...
applyInheritedRelations()
ExportSchema($filename=NULL)
Export the schema of the connected ($PG_CONN) database to a file in the format readable by GetSchema(...
updateSequences()
Add sequences.
applyTables($inherits=false)
Add tables/columns (dependent on sequences for default values)
DB_TableExists($tableName)
Check if table exists.
MakeFunctions($Debug)
Create any required DB functions.
applySequences()
Add sequences to the database.
GetSchema()
Load the schema from the db into an array.
DB_ConstraintExists($ConstraintName, $DBName='fossology')
Check if a constraint exists.
DB_ColExists($tableName, $colName, $DBName='fossology')
Check if a column exists.
ReportCachePurgeAll()
Purge all records from the report cache.
__construct(DbManager &$dbManager)
addSequences($referencedSequencesInTableColumns)
setDriver(Driver &$dbDriver)
writeArrayEntries($fout, $key, $value, $varname)
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN
getCurrSchema()
Load the schema from the db into an array.
dropViews($catalog)
Delete views.
applyOrEchoOnce($sql, $stmt= '')
applySchema($filename=NULL, $debug=false, $catalog= 'fossology', $migrateColumns=array())
Make schema match $Filename. This is a single transaction.