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');
16 use Monolog\Handler\StreamHandler;
31 function __construct($dbName =
null, $sysConf =
null)
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);
40 $this->dbName = $dbName;
42 require_once (dirname(dirname(__FILE__)).
'/common-db.php');
43 $this->connection =
DBconnect($this->sys_conf);
45 require (dirname(dirname(__FILE__)).
'/common-container.php');
47 $logger =
new Logger(
'default');
48 $this->logFileName = dirname(dirname(dirname(dirname(dirname(__FILE__))))) .
'db.pg.log';
49 $logger->pushHandler(
new StreamHandler($this->logFileName, Logger::DEBUG));
51 $this->
dbManager = $container->get(
'db.manager');
52 $postgres =
new Postgres($this->connection);
54 $this->
dbManager->queryOnce(
"DEALLOCATE ALL");
55 $this->dropAllTables();
56 $this->dropAllSequences();
59 public function getFossSysConf()
61 return $this->sys_conf;
64 private function dropAllTables()
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);
70 foreach ($tableNames as $row) {
71 $name = $row[
'table_name'];
72 $this->
dbManager->queryOnce(
"DROP TABLE IF EXISTS $name CASCADE",$sqlLog=__METHOD__.
".$name");
76 private function dropAllSequences()
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);
83 foreach ($tableNames as $row) {
84 $name = $row[
'sequence_name'];
85 $this->
dbManager->queryOnce(
"DROP SEQUENCE $name CASCADE",$sqlLog=__METHOD__.
".$name");
92 $this->connection =
null;
95 function fullDestruct()
97 pg_close($this->connection);
98 $GLOBALS[
'PG_CONN'] =
false;
99 $testDbFactory = new \TestDbFactory();
100 $testDbFactory->purgeTestDb($this->sys_conf);
104 function isInFossyGroup()
106 $gid_array = posix_getgroups();
107 foreach ($gid_array as $gid) {
108 $gid_info = posix_getgrgid($gid);
109 if ($gid_info[
'name'] ===
'fossy') {
113 $uid = posix_getuid();
114 $uid_info = posix_getpwuid($uid);
115 return ($uid_info[
'name'] !==
'root');
124 $coreSchemaFile = $this->dirnameRec(__FILE__, 4) .
'/www/ui/core-schema.dat';
126 require($coreSchemaFile);
127 foreach ($Schema[
'TABLE'] as $tableName=>$tableCols) {
128 if ($invert^!in_array($tableName, $tableList) || array_key_exists($tableName, $Schema[
'INHERITS'])) {
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;
137 $this->
dbManager->queryOnce(implode(
";\n",$sqlAddArray));
141 public function resetSequenceAsMaxOf($sequenceName, $tableName, $columnName)
143 $this->
dbManager->queryOnce(
"SELECT setval('$sequenceName', (SELECT MAX($columnName) FROM $tableName))");
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)");
173 $coreSchemaFile = $this->dirnameRec(__FILE__, 4) .
'/www/ui/core-schema.dat';
175 require($coreSchemaFile);
176 foreach ($Schema[
'INHERITS'] as $table=>$fromTable) {
177 if ($fromTable==
'master_ars' || !empty($tableList) && !in_array($table, $tableList) ) {
180 if (!$this->
dbManager->existsTable($table) && $this->dbManager->existsTable($fromTable)) {
181 $this->
dbManager->queryOnce(
"CREATE TABLE \"$table\" () INHERITS (\"$fromTable\")");
186 public function createInheritedArsTables($agents)
188 foreach ($agents as $agent) {
189 if (!$this->
dbManager->existsTable($agent .
'_ars')) {
190 $this->
dbManager->queryOnce(
"create table " . $agent .
"_ars() inherits(ars_master)");
applySchema($type, $elementList, $invert=false)
createPlainTables($tableList, $invert=false)
createSequences($seqList, $invert=FALSE)
createConstraints($cList, $invert=FALSE)
createInheritedTables($tableList=array())
DBconnect($sysconfdir, $options="", $exitOnFail=true)
Connect to database engine. This is a no-op if $PG_CONN already has a value.
Populate_sysconfig()
Populate the sysconfig table with core variables.
fo_dbManager * dbManager
fo_dbManager object