This repository was archived by the owner on Jan 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathinstall.php
More file actions
110 lines (93 loc) · 2.9 KB
/
install.php
File metadata and controls
110 lines (93 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
* Install database.
*
* @author Timo Tijhof
* @since 1.0.0
* @package TestSwarm
*/
define( 'SWARM_ENTRY', 'SCRIPT' );
require_once __DIR__ . '/../inc/init.php';
class DBInstallScript extends MaintenanceScript {
protected function init() {
$this->setDescription(
'Install the TestSwarm database. Can also clear a database if it exists.'
);
$this->registerOption( 'force', 'boolean', 'Skip confirmation for dropping existing tables.' );
}
protected function execute() {
global $swarmInstallDir;
if ( $this->getContext()->dbLock() ) {
$this->error( 'Database is currently locked, please remove ./cache/database.lock first.' );
}
$db = $this->getContext()->getDB();
$this->out( 'Setting database.lock, other requests may not access the database during installation.' );
$this->getContext()->dbLock( true );
$dbTables = array(
// Order matters (due to foreign key restrictions before 1.0)
'projects', // New in 1.0.0
'runresults', // New in 1.0.0
'run_client', // Removed in 1.0.0
'clients',
'run_useragent',
'useragents', // Removed in 1.0.0
'runs',
'jobs',
'users',
);
$tablesExists = false;
foreach ( $dbTables as $dbTable ) {
$exists = $db->tableExists( $dbTable );
if ( $exists ) {
$tablesExists = true;
break;
}
}
if ( $tablesExists ) {
if ( !$this->getOption( 'force' ) ) {
$this->out( 'Database already contains tables. If you continue, all tables will be dropped. (Y/N)' );
$doDrop = $this->cliInput();
if ( $doDrop !== 'Y' ) {
$this->getContext()->dbLock( false );
$this->out( "Installation aborted.\nRemoved database.lock" );
return;
}
}
$this->doDropTables( $dbTables );
}
$this->doInstallDB();
$this->getContext()->dbLock( false );
$this->out( "Removed database.lock.\nInstallation finished!" );
}
protected function doDropTables( Array $dbTables ) {
$db = $this->getContext()->getDB();
foreach( $dbTables as $dbTable ) {
$this->outRaw( "Dropping $dbTable table..." );
$exists = $db->tableExists( $dbTable );
if ( $exists ) {
$dropped = $db->query( 'DROP TABLE ' . $db->addIdentifierQuotes( $dbTable ) );
$this->out( ' ' . ($dropped ? 'OK' : 'FAILED' ) );
} else {
$this->out( 'OK (skipped, didn\'t exist)' );
}
}
}
protected function doInstallDB() {
global $swarmInstallDir;
$db = $this->getContext()->getDB();
// Create new tables
$this->outRaw( 'Creating new tables (this may take a few minutes)...' );
$fullSchemaFile = "$swarmInstallDir/config/tables.sql";
if ( !is_readable( $fullSchemaFile ) ) {
$this->error( 'Can\'t read schema file' );
}
$fullSchemaSql = file_get_contents( $fullSchemaFile );
$executed = $db->batchQueryFromFile( $fullSchemaSql );
if ( !$executed ) {
$this->error( 'Creating new tables failed' );
}
$this->out( 'OK' );
}
}
$script = DBInstallScript::newFromContext( $swarmContext );
$script->run();