22 require_once(dirname(dirname(dirname(dirname(__FILE__)))) .
"/vendor/autoload.php");
23 require_once(__DIR__ .
"/../../../testing/db/TestDbFactory.php");
41 function __construct($dbName = null, $sysConf = null)
43 $dbName = strtolower($dbName);
44 $testDbFactory = new \TestDbFactory();
45 $this->sys_conf = $sysConf;
46 if (empty($this->sys_conf)) {
47 $this->sys_conf = $testDbFactory->setupTestDb($dbName);
48 $dbName = $testDbFactory->getDbName($this->sys_conf);
50 $this->dbName = $dbName;
52 require_once (dirname(dirname(__FILE__)).
'/common-db.php');
53 $this->connection =
DBconnect($this->sys_conf);
55 require (dirname(dirname(__FILE__)).
'/common-container.php');
57 $logger =
new Logger(
'default');
58 $this->logFileName = dirname(dirname(dirname(dirname(dirname(__FILE__))))) .
'db.pg.log';
59 $logger->pushHandler(
new StreamHandler($this->logFileName, Logger::DEBUG));
61 $this->
dbManager = $container->get(
'db.manager');
62 $postgres =
new Postgres($this->connection);
64 $this->
dbManager->queryOnce(
"DEALLOCATE ALL");
65 $this->dropAllTables();
66 $this->dropAllSequences();
69 public function getFossSysConf()
71 return $this->sys_conf;
74 private function dropAllTables()
76 $this->
dbManager->prepare(__METHOD__.
'.get',
"SELECT table_name FROM information_schema.tables WHERE table_schema=$1 AND table_type=$2");
77 $res = $this->
dbManager->execute(__METHOD__.
'.get',array(
'public',
'BASE TABLE'));
78 $tableNames = $this->
dbManager->fetchAll($res);
80 foreach ($tableNames as $row) {
81 $name = $row[
'table_name'];
82 $this->
dbManager->queryOnce(
"DROP TABLE IF EXISTS $name CASCADE",$sqlLog=__METHOD__.
".$name");
86 private function dropAllSequences()
88 $this->
dbManager->prepare($stmt=__METHOD__.
'.get',
89 "SELECT sequence_name FROM information_schema.sequences WHERE sequence_schema=$1");
90 $res = $this->
dbManager->execute($stmt,array(
'public'));
91 $tableNames = $this->
dbManager->fetchAll($res);
93 foreach ($tableNames as $row) {
94 $name = $row[
'sequence_name'];
95 $this->
dbManager->queryOnce(
"DROP SEQUENCE $name CASCADE",$sqlLog=__METHOD__.
".$name");
102 $this->connection = null;
105 function fullDestruct()
107 pg_close($this->connection);
108 $GLOBALS[
'PG_CONN'] =
false;
109 $testDbFactory = new \TestDbFactory();
110 $testDbFactory->purgeTestDb($this->sys_conf);
114 function isInFossyGroup()
116 $gid_array = posix_getgroups();
117 foreach ($gid_array as $gid) {
118 $gid_info = posix_getgrgid($gid);
119 if ($gid_info[
'name'] ===
'fossy') {
123 $uid = posix_getuid();
124 $uid_info = posix_getpwuid($uid);
125 return ($uid_info[
'name'] !==
'root');
134 $coreSchemaFile = $this->dirnameRec(__FILE__, 4) .
'/www/ui/core-schema.dat';
136 require($coreSchemaFile);
137 foreach ($Schema[
'TABLE'] as $tableName=>$tableCols) {
138 if ($invert^!in_array($tableName, $tableList) || array_key_exists($tableName, $Schema[
'INHERITS'])) {
141 $this->
dbManager->queryOnce(
"CREATE TABLE \"$tableName\" ()");
142 $sqlAddArray = array();
143 foreach ($tableCols as $attributes) {
144 $sqlAdd = preg_replace(
'/ DEFAULT .*/',
'',$attributes[
"ADD"]);
145 $sqlAddArray[] = $sqlAdd;
147 $this->
dbManager->queryOnce(implode(
";\n",$sqlAddArray));
151 public function resetSequenceAsMaxOf($sequenceName, $tableName, $columnName)
153 $this->
dbManager->queryOnce(
"SELECT setval('$sequenceName', (SELECT MAX($columnName) FROM $tableName))");
179 $table =
'license_candidate';
180 if ((empty($tableList) || in_array($table, $tableList)) && !$this->
dbManager->existsTable($table)) {
181 $this->
dbManager->queryOnce(
"CREATE TABLE $table (group_fk integer) INHERITS (license_ref)");
183 $coreSchemaFile = $this->dirnameRec(__FILE__, 4) .
'/www/ui/core-schema.dat';
185 require($coreSchemaFile);
186 foreach ($Schema[
'INHERITS'] as $table=>$fromTable) {
187 if ($fromTable==
'master_ars' || !empty($tableList) && !in_array($table, $tableList) ) {
190 if (!$this->
dbManager->existsTable($table) && $this->
dbManager->existsTable($fromTable)) {
191 $this->
dbManager->queryOnce(
"CREATE TABLE \"$table\" () INHERITS (\"$fromTable\")");
196 public function createInheritedArsTables($agents)
198 foreach ($agents as $agent) {
199 if (!$this->
dbManager->existsTable($agent .
'_ars')) {
200 $this->
dbManager->queryOnce(
"create table " . $agent .
"_ars() inherits(ars_master)");
DBconnect($sysconfdir, $options="", $exitOnFail=true)
Connect to database engine. This is a no-op if $PG_CONN already has a value.
createPlainTables($tableList, $invert=false)
applySchema($type, $elementList, $invert=false)
createSequences($seqList, $invert=FALSE)
createConstraints($cList, $invert=FALSE)
createInheritedTables($tableList=array())
fo_dbManager * dbManager
fo_dbManager object