FOSSology  4.4.0
Open Source License Compliance by Open Source Software
TestPgDb.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014-2015 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 namespace Fossology\Lib\Test;
9 
10 // setup autoloading
11 require_once(dirname(dirname(dirname(dirname(__FILE__)))) . "/vendor/autoload.php");
12 require_once(__DIR__ . "/../../../testing/db/TestDbFactory.php");
13 require (dirname(dirname(__FILE__)).'/common-sysconfig.php');
14 
16 use Monolog\Handler\StreamHandler;
17 use Monolog\Logger;
18 
19 
20 class TestPgDb extends TestAbstractDb
21 {
23  private $dbName;
25  private $logFileName;
27  private $connection;
29  private $sys_conf;
30 
31  function __construct($dbName = null, $sysConf = null)
32  {
33  $dbName = strtolower($dbName);
34  $testDbFactory = new \TestDbFactory();
35  $this->sys_conf = $sysConf;
36  if (empty($this->sys_conf)) {
37  $this->sys_conf = $testDbFactory->setupTestDb($dbName);
38  $dbName = $testDbFactory->getDbName($this->sys_conf);
39  }
40  $this->dbName = $dbName;
41 
42  require_once (dirname(dirname(__FILE__)).'/common-db.php');
43  $this->connection = DBconnect($this->sys_conf);
44 
45  require (dirname(dirname(__FILE__)).'/common-container.php');
46  global $container;
47  $logger = new Logger('default'); // $container->get('logger');
48  $this->logFileName = dirname(dirname(dirname(dirname(dirname(__FILE__))))) . 'db.pg.log';
49  $logger->pushHandler(new StreamHandler($this->logFileName, Logger::DEBUG));
50 
51  $this->dbManager = $container->get('db.manager');
52  $postgres = new Postgres($this->connection);
53  $this->dbManager->setDriver($postgres);
54  $this->dbManager->queryOnce("DEALLOCATE ALL");
55  $this->dropAllTables();
56  $this->dropAllSequences();
57  }
58 
59  public function getFossSysConf()
60  {
61  return $this->sys_conf;
62  }
63 
64  private function dropAllTables()
65  {
66  $this->dbManager->prepare(__METHOD__.'.get',"SELECT table_name FROM information_schema.tables WHERE table_schema=$1 AND table_type=$2");
67  $res = $this->dbManager->execute(__METHOD__.'.get',array('public','BASE TABLE'));
68  $tableNames = $this->dbManager->fetchAll($res);
69  $this->dbManager->freeResult($res);
70  foreach ($tableNames as $row) {
71  $name = $row['table_name'];
72  $this->dbManager->queryOnce("DROP TABLE IF EXISTS $name CASCADE",$sqlLog=__METHOD__.".$name");
73  }
74  }
75 
76  private function dropAllSequences()
77  {
78  $this->dbManager->prepare($stmt=__METHOD__.'.get',
79  "SELECT sequence_name FROM information_schema.sequences WHERE sequence_schema=$1");
80  $res = $this->dbManager->execute($stmt,array('public'));
81  $tableNames = $this->dbManager->fetchAll($res);
82  $this->dbManager->freeResult($res);
83  foreach ($tableNames as $row) {
84  $name = $row['sequence_name'];
85  $this->dbManager->queryOnce("DROP SEQUENCE $name CASCADE",$sqlLog=__METHOD__.".$name");
86  }
87  }
88 
89  function __destruct()
90  {
91  $this->dbManager = null;
92  $this->connection = null;
93  }
94 
95  function fullDestruct()
96  {
97  pg_close($this->connection);
98  $GLOBALS['PG_CONN'] = false;
99  $testDbFactory = new \TestDbFactory();
100  $testDbFactory->purgeTestDb($this->sys_conf);
101  $this->dbManager = null;
102  }
103 
104  function isInFossyGroup()
105  {
106  $gid_array = posix_getgroups();
107  foreach ($gid_array as $gid) {
108  $gid_info = posix_getgrgid($gid);
109  if ($gid_info['name'] === 'fossy') {
110  return true;
111  }
112  }
113  $uid = posix_getuid();
114  $uid_info = posix_getpwuid($uid);
115  return ($uid_info['name'] !== 'root');
116  }
117 
122  public function createPlainTables($tableList, $invert=false)
123  {
124  $coreSchemaFile = $this->dirnameRec(__FILE__, 4) . '/www/ui/core-schema.dat';
125  $Schema = array();
126  require($coreSchemaFile);
127  foreach ($Schema['TABLE'] as $tableName=>$tableCols) {
128  if ($invert^!in_array($tableName, $tableList) || array_key_exists($tableName, $Schema['INHERITS'])) {
129  continue;
130  }
131  $this->dbManager->queryOnce("CREATE TABLE \"$tableName\" ()");
132  $sqlAddArray = array();
133  foreach ($tableCols as $attributes) {
134  $sqlAdd = preg_replace('/ DEFAULT .*/','',$attributes["ADD"]);
135  $sqlAddArray[] = $sqlAdd;
136  }
137  $this->dbManager->queryOnce(implode(";\n",$sqlAddArray));
138  }
139  }
140 
141  public function resetSequenceAsMaxOf($sequenceName, $tableName, $columnName)
142  {
143  $this->dbManager->queryOnce("SELECT setval('$sequenceName', (SELECT MAX($columnName) FROM $tableName))");
144  }
145 
150  public function createSequences($seqList, $invert=FALSE)
151  {
152  $this->applySchema('SEQUENCE', $seqList, $invert);
153  }
154 
159  public function createConstraints($cList, $invert=FALSE)
160  {
161  $this->applySchema('CONSTRAINT', $cList, $invert);
162  }
163 
167  public function createInheritedTables($tableList=array())
168  {
169  $table = 'license_candidate';
170  if ((empty($tableList) || in_array($table, $tableList)) && !$this->dbManager->existsTable($table)) {
171  $this->dbManager->queryOnce("CREATE TABLE $table (group_fk integer,rf_creationdate timestamptz,rf_lastmodified timestamptz,rf_user_fk_created integer,rf_user_fk_modified integer) INHERITS (license_ref)");
172  }
173  $coreSchemaFile = $this->dirnameRec(__FILE__, 4) . '/www/ui/core-schema.dat';
174  $Schema = array();
175  require($coreSchemaFile);
176  foreach ($Schema['INHERITS'] as $table=>$fromTable) {
177  if ($fromTable=='master_ars' || !empty($tableList) && !in_array($table, $tableList) ) {
178  continue;
179  }
180  if (!$this->dbManager->existsTable($table) && $this->dbManager->existsTable($fromTable)) {
181  $this->dbManager->queryOnce("CREATE TABLE \"$table\" () INHERITS (\"$fromTable\")");
182  }
183  }
184  }
185 
186  public function createInheritedArsTables($agents)
187  {
188  foreach ($agents as $agent) {
189  if (!$this->dbManager->existsTable($agent . '_ars')) {
190  $this->dbManager->queryOnce("create table " . $agent . "_ars() inherits(ars_master)");
191  }
192  }
193  }
194 
198  public function setupSysconfig()
199  {
200  $this->createPlainTables(['sysconfig'], false);
201  $this->createSequences(['sysconfig_sysconfig_pk_seq'], false);
203  }
204 }
applySchema($type, $elementList, $invert=false)
createPlainTables($tableList, $invert=false)
Definition: TestPgDb.php:122
createSequences($seqList, $invert=FALSE)
Definition: TestPgDb.php:150
createConstraints($cList, $invert=FALSE)
Definition: TestPgDb.php:159
createInheritedTables($tableList=array())
Definition: TestPgDb.php:167
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
Populate_sysconfig()
Populate the sysconfig table with core variables.
int Test
Definition: util.c:20
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16