Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.

Commit 2ae574d

Browse files
author
Michael Grauer
committed
ENH: refs #0283. Added script to run at configure time to setup the db.
This script will run during the cmake configure step, will drop all tables from the test database, load the latest version of the midas core tables, run the upgrades for midas core, create a Default assetstore for testing, install and upgrade all modules.
1 parent fc979cf commit 2ae574d

File tree

3 files changed

+289
-1
lines changed

3 files changed

+289
-1
lines changed

CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,21 @@ if(NOT PHP)
2020
message(FATAL_ERROR "Please set the PHP executable")
2121
endif()
2222

23+
#-----------------------------------------------------------------------------
24+
# Drop all tables from the testing DB
25+
# Then re-install the DB, set a Default assetstore, and install all modules
26+
27+
execute_process(
28+
COMMAND ${PHP} ${CMAKE_CURRENT_SOURCE_DIR}/tests/DatabaseSetup.php
29+
RESULT_VARIABLE databaseSetup_RESULT
30+
OUTPUT_VARIABLE databaseSetup_OUT
31+
ERROR_VARIABLE databaseSetup_ERR)
32+
33+
if(${databaseSetup_RESULT})
34+
message(STATUS "DatabaseSetup output: ${databaseSetup_OUT}")
35+
message(FATAL_ERROR "DatabaseSetup error: ${databaseSetup_ERR}")
36+
endif()
37+
2338

2439
#-----------------------------------------------------------------------------
2540
# This should be at the top level for warning suppression

modules/helloworld/models/base/HelloModelBase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
1010
PURPOSE. See the above copyright notices for more information.
1111
=========================================================================*/
12-
12+
require_once BASE_PATH . '/modules/helloworld/models/AppModel.php';
1313
/** demo base model*/
1414
class Helloworld_HelloModelBase extends Helloworld_AppModel
1515
{

tests/DatabaseSetup.php

Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
<?php
2+
3+
4+
5+
function getSqlDbTypes($testConfigDir)
6+
{
7+
// setup testing for whichever db config testing files exist
8+
$d = dir($testConfigDir);
9+
$dbTypes = array();
10+
while(false !== ($entry = $d->read()))
11+
{
12+
if($entry === 'mysql.ini')
13+
{
14+
$dbTypes[] = 'mysql';
15+
}
16+
else if($entry === 'pgsql.ini')
17+
{
18+
$dbTypes[] = 'pgsql';
19+
}
20+
}
21+
$d->close();
22+
return $dbTypes;
23+
}
24+
25+
26+
function loadDbAdapter($testConfigDir, $dbType)
27+
{
28+
29+
// create the lockfile for this dbType
30+
$dbConfigFile = $testConfigDir . '/' . $dbType . '.ini';
31+
$lockFile = $testConfigDir . '/lock.' . $dbType . '.ini';
32+
copy($dbConfigFile, $lockFile);
33+
34+
// TODO what are these lockfiles for even
35+
// they don't seem to do anything
36+
// load the lockfile as the test dbConfig
37+
if(file_exists($lockFile))
38+
{
39+
$configDatabase = new Zend_Config_Ini($lockFile, 'testing');
40+
}
41+
else
42+
{
43+
throw new Zend_Exception("Error, cannot load lockfile: ".$lockFile);
44+
}
45+
46+
// currently only support pdo
47+
if ($configDatabase->database->type == 'pdo')
48+
{
49+
$db = Zend_Db::factory($configDatabase->database->adapter, array(
50+
'host' => $configDatabase->database->params->host,
51+
'username' => $configDatabase->database->params->username,
52+
'password' => $configDatabase->database->params->password,
53+
'dbname' => $configDatabase->database->params->dbname,
54+
'port' =>$configDatabase->database->params->port,));
55+
if($configDatabase->database->profiler == '1')
56+
{
57+
$db->getProfiler()->setEnabled(true);
58+
}
59+
Zend_Db_Table::setDefaultAdapter($db);
60+
Zend_Registry::set('dbAdapter', $db);
61+
Zend_Registry::set('configDatabase', $configDatabase);
62+
return $db;
63+
}
64+
else
65+
{
66+
throw new Zend_Exception("Database type Error. Expecting pdo but have ".$configDatabase->database->type);
67+
}
68+
}
69+
70+
71+
function dropTables($db)
72+
{
73+
$tables = $db->listTables();
74+
foreach($tables as $table)
75+
{
76+
$sql = "drop table `".$table."`";
77+
$db->query($sql);
78+
}
79+
}
80+
81+
82+
function installCore($db, $dbType, $utilityComponent)
83+
{
84+
require_once BASE_PATH.'/core/controllers/components/UpgradeComponent.php';
85+
$upgradeComponent=new UpgradeComponent();
86+
$upgradeComponent->dir = BASE_PATH."/core/database/".$dbType;
87+
$upgradeComponent->init = true;
88+
89+
$newestVersion = $upgradeComponent->getNewestVersion(true);
90+
91+
$sqlFile = BASE_PATH."/core/database/{$dbType}/" . $newestVersion . ".sql";
92+
if(!isset($sqlFile) || !file_exists($sqlFile))
93+
{
94+
throw new Zend_Exception("Unable to find sql file: ".$sqlFile);
95+
}
96+
97+
$databaseConfig = $db->getConfig();
98+
switch($dbType)
99+
{
100+
case 'mysql':
101+
$utilityComponent->run_mysql_from_file($sqlFile, $databaseConfig['host'], $databaseConfig['username'], $databaseConfig['password'], $databaseConfig['dbname'], $databaseConfig['port']);
102+
break;
103+
case 'pgsql':
104+
$utilityComponent->run_pgsql_from_file($sqlFile, $databaseConfig['host'], $databaseConfig['username'], $databaseConfig['password'], $databaseConfig['dbname'], $databaseConfig['port']);
105+
break;
106+
default:
107+
throw new Zend_Exception("Unknown db type: ".$dbType);
108+
break;
109+
}
110+
111+
$upgradeComponent->upgrade(str_replace('.sql', '', basename($sqlFile)), true /* true for testing */);
112+
}
113+
114+
115+
// want to create a default assetstore that is separate from application's
116+
function createDefaultAssetstore()
117+
{
118+
Zend_Registry::set('models', array());
119+
$modelLoader = new MIDAS_ModelLoader();
120+
$modelLoader->loadModel('Assetstore');
121+
122+
// path munging
123+
$testAssetstoreBase = BASE_PATH.'/tmp/misc/test';
124+
$testAssetstoreBase = str_replace('tests/../', '', $testAssetstoreBase);
125+
$testAssetstoreBase = str_replace('//', '/', $testAssetstoreBase);
126+
127+
// create assetstore directory
128+
if(!is_dir($testAssetstoreBase))
129+
{
130+
mkdir($testAssetstoreBase);
131+
}
132+
$testAssetstore = $testAssetstoreBase . '/assetstore';
133+
if(!is_dir($testAssetstore))
134+
{
135+
mkdir($testAssetstore);
136+
}
137+
138+
//create default assetstore in db
139+
require_once BASE_PATH.'/core/models/dao/AssetstoreDao.php';
140+
$assetstoreDao = new AssetstoreDao();
141+
$assetstoreDao->setName('Default');
142+
$assetstoreDao->setPath($testAssetstore);
143+
$assetstoreDao->setType(MIDAS_ASSETSTORE_LOCAL);
144+
$assetstore = new AssetstoreModel();
145+
$assetstore->save($assetstoreDao);
146+
}
147+
148+
// install and perform upgrades on modules
149+
//
150+
// What to do about module config files, these should be copied into
151+
// core/configs when a module is installed, but we can't do that
152+
// as there are already module files there, and we may not want
153+
// all the module files
154+
// we could copy the existing ones somewhere, then copy them back at the end
155+
// but i don't like that idea
156+
// for now do nothing
157+
function installModules($utilityComponent)
158+
{
159+
$modules = $utilityComponent->getAllModules();
160+
foreach($modules as $moduleName => $module)
161+
{
162+
$utilityComponent->installModule($moduleName);
163+
}
164+
}
165+
166+
167+
168+
169+
170+
171+
172+
173+
174+
175+
// general setup
176+
177+
error_reporting(E_ALL | E_STRICT);
178+
ini_set('display_startup_errors', 1);
179+
ini_set('display_errors', 1);
180+
181+
define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
182+
define('APPLICATION_ENV', 'testing');
183+
define('LIBRARY_PATH', realpath(dirname(__FILE__) . '/../library'));
184+
define('TESTS_PATH', realpath(dirname(__FILE__)));
185+
define('BASE_PATH', realpath(dirname(__FILE__)) . "/../");
186+
187+
188+
$includePaths = array(LIBRARY_PATH, get_include_path());
189+
set_include_path(implode(PATH_SEPARATOR, $includePaths));
190+
191+
require_once dirname(__FILE__)."/../library/Zend/Loader/Autoloader.php";
192+
$loader = Zend_Loader_Autoloader::getInstance();
193+
$loader->setFallbackAutoloader(true);
194+
$loader->suppressNotFoundWarnings(false);
195+
196+
require_once(BASE_PATH . "/core/include.php");
197+
define('START_TIME',microtime(true));
198+
199+
Zend_Session::$_unitTestEnabled = true;
200+
Zend_Session::start();
201+
202+
$logger = Zend_Log::factory(array(
203+
array(
204+
'writerName' => 'Stream',
205+
'writerParams' => array(
206+
'stream' => BASE_PATH.'/tests/log/testing.log',
207+
),
208+
'filterName' => 'Priority',
209+
'filterParams' => array(
210+
'priority' => Zend_Log::INFO,
211+
),
212+
),
213+
array(
214+
'writerName' => 'Firebug',
215+
'filterName' => 'Priority',
216+
'filterParams' => array(
217+
'priority' => Zend_Log::INFO,
218+
),
219+
),
220+
));
221+
222+
Zend_Registry::set('logger', $logger);
223+
224+
225+
226+
227+
228+
// get the config properties
229+
230+
$configGlobal = new Zend_Config_Ini(APPLICATION_CONFIG, 'global', true);
231+
$configGlobal->environment = 'testing';
232+
Zend_Registry::set('configGlobal', $configGlobal);
233+
234+
$config = new Zend_Config_Ini(APPLICATION_CONFIG, 'testing');
235+
Zend_Registry::set('config', $config);
236+
237+
238+
239+
240+
241+
242+
243+
244+
// get DB type
245+
// for now only supporting pgsql and mysql
246+
// get the DB type from the existing config files
247+
248+
$testConfigDir = BASE_PATH.'/tests/configs/';
249+
$dbTypes = getSqlDbTypes($testConfigDir);
250+
251+
252+
foreach($dbTypes as $dbType)
253+
{
254+
try
255+
{
256+
$dbAdapter = loadDbAdapter($testConfigDir, $dbType);
257+
dropTables($dbAdapter);
258+
259+
require_once BASE_PATH.'/core/controllers/components/UtilityComponent.php';
260+
$utilityComponent = new UtilityComponent();
261+
262+
installCore($dbAdapter, $dbType, $utilityComponent);
263+
createDefaultAssetstore();
264+
installModules($utilityComponent);
265+
}
266+
catch(Zend_Exception $ze)
267+
{
268+
echo $ze->getMessage();
269+
exit(1);
270+
}
271+
}
272+
273+
exit(0);

0 commit comments

Comments
 (0)