Skip to content

Commit 8015df2

Browse files
author
Dave
committed
fixed bug related to issue 1
1 parent 8b1daae commit 8015df2

File tree

8 files changed

+147
-17
lines changed

8 files changed

+147
-17
lines changed

config/EMPTY

Whitespace-only changes.

config/db_config.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
$db_config = (object) array();
4+
$db_config->host = 'localhost';
5+
$db_config->port = '3306';
6+
$db_config->user = 'root';
7+
$db_config->pass = 'almond';
8+
$db_config->name = 'mpm_test';
9+
$db_config->db_path = '/home/dave/Projects/mysql-php-migrations/db/';
10+
$db_config->method = 2;
11+
$db_config->migrations_table = 'mpm_migrations';
12+
13+
?>

db/schema.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* This file houses the MpmInitialSchema class.
4+
*
5+
* This file may be deleted if you do not wish to use the build command or build on init features.
6+
*
7+
* @package mysql_php_migrations
8+
* @subpackage Classes
9+
* @license http://www.opensource.org/licenses/bsd-license.php The New BSD License
10+
* @link http://code.google.com/p/mysql-php-migrations/
11+
*/
12+
13+
/**
14+
* The MpmInitialSchema class is used to build an initial database structure.
15+
*
16+
* @package mysql_php_migrations
17+
* @subpackage Classes
18+
*/
19+
class MpmInitialSchema extends MpmSchema
20+
{
21+
22+
public function __construct()
23+
{
24+
parent::__construct();
25+
26+
/* If you build your initial schema having already executed a number of migrations,
27+
* you should set the initial migration timestamp.
28+
*
29+
* The initial migration timestamp will be set to active and this migration and all
30+
* previous will be ignored when the build command is used.
31+
*
32+
* EX:
33+
*
34+
* $this->initialMigrationTimestamp = '2009-08-01 15:23:44';
35+
*/
36+
$this->initialMigrationTimestamp = null;
37+
}
38+
39+
public function build()
40+
{
41+
/* Add the queries needed to build the initial structure of your database.
42+
*
43+
* EX:
44+
*
45+
* $this->dbObj->exec('CREATE TABLE `testing` ( `id` INT(11) AUTO_INCREMENT NOT NULL, `vals` INT(11) NOT NULL, PRIMARY KEY ( `id` ))');
46+
*/
47+
$this->dbObj->exec('CREATE TABLE `testing` (`id` INT(11) AUTO_INCREMENT NOT NULL, `vals` INT(11) NOT NULL, PRIMARY KEY ( `id`)) ENGINE=InnoDB');
48+
}
49+
50+
}
51+
52+
?>

db/templates/EMPTY

Whitespace-only changes.

db/test_data.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* This file houses the MpmTestData class.
4+
*
5+
* This file may be deleted if you do not wish to add test data to your database after a build.
6+
*
7+
* @package mysql_php_migrations
8+
* @subpackage Classes
9+
* @license http://www.opensource.org/licenses/bsd-license.php The New BSD License
10+
* @link http://code.google.com/p/mysql-php-migrations/
11+
*/
12+
13+
/**
14+
* The MpmTestData class is used to add test data after successfully building an initial database structure.
15+
*
16+
* @package mysql_php_migrations
17+
* @subpackage Classes
18+
*/
19+
class MpmTestData extends MpmSchema
20+
{
21+
22+
public function build()
23+
{
24+
/* Add the queries needed to insert test data into the initial build of your database.
25+
*
26+
* EX:
27+
*
28+
* $this->dbObj->exec("INSERT INTO `testing` (id, username, password) VALUES (1, 'my_username', 'my_password')");
29+
*/
30+
$this->dbObj->exec("INSERT INTO `testing` (id, vals) VALUES (1, 1234434)");
31+
}
32+
33+
}
34+
35+
?>

lib/controllers/build_controller.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,25 @@ public function build($with_data)
161161
echo "\n", 'Building initial database schema... ';
162162
$obj->build();
163163
echo 'done.', "\n\n", 'Applying migrations... ';
164-
$obj = new MpmLatestController();
165-
$obj->doAction(true);
164+
try
165+
{
166+
$total_migrations = MpmMigrationHelper::getMigrationCount();
167+
if ($total_migrations == 0)
168+
{
169+
echo "no migrations exist.";
170+
}
171+
else
172+
{
173+
$to_id = MpmMigrationHelper::getLatestMigration();
174+
$obj = new MpmUpController('up', array ( $to_id, $forced ));
175+
$obj->doAction($quiet);
176+
}
177+
}
178+
catch (Exception $e)
179+
{
180+
echo "\n\nERROR: " . $e->getMessage() . "\n\n";
181+
exit;
182+
}
166183
if ($with_data)
167184
{
168185
require_once(MPM_DB_PATH . 'test_data.php');
@@ -198,8 +215,10 @@ public function displayHelp()
198215
$obj->addText(' ');
199216
$obj->addText('WARNING: THIS IS A DESTRUCTIVE ACTION!! BEFORE THE DATABASE IS BUILT, ALL TABLES CURRENTLY IN THE DATABASE ARE REMOVED!');
200217
$obj->addText(' ');
201-
$obj->addText('Valid Example:');
202-
$obj->addText('./migrate.php add', 4);
218+
$obj->addText('Valid Examples:');
219+
$obj->addText('./migrate.php build add', 4);
220+
$obj->addText('./migrate.php build with_data', 4);
221+
$obj->addText('./migrate.php build with_data --force', 4);
203222
$obj->write();
204223
}
205224

lib/controllers/latest_controller.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
class MpmLatestController extends MpmController
1818
{
19-
19+
2020
/**
2121
* Determines what action should be performed and takes that action.
2222
*
@@ -26,7 +26,7 @@ class MpmLatestController extends MpmController
2626
* @uses MpmCommandLineWriter::getInstance()
2727
* @uses MpmMigrationHelper::getLatestMigration()
2828
* @uses MpmUpController::doAction()
29-
*
29+
*
3030
* @param bool $quiet supresses certain text when true
3131
*
3232
* @return void
@@ -35,14 +35,14 @@ public function doAction($quiet = false)
3535
{
3636
// make sure we're init'd
3737
MpmDbHelper::test();
38-
38+
3939
// are we forcing this?
4040
$forced = '';
4141
if (isset($this->arguments[0]) && strcasecmp($this->arguments[0], '--force') == 0)
4242
{
4343
$forced = '--force';
4444
}
45-
45+
4646
try
4747
{
4848
$total_migrations = MpmMigrationHelper::getMigrationCount();
@@ -51,26 +51,28 @@ public function doAction($quiet = false)
5151
$clw = MpmCommandLineWriter::getInstance();
5252
$clw->addText('No migrations exist.');
5353
$clw->write();
54-
exit;
5554
}
56-
$to_id = MpmMigrationHelper::getLatestMigration();
57-
$obj = new MpmUpController('up', array ( $to_id, $forced ));
58-
$obj->doAction($quiet);
55+
else
56+
{
57+
$to_id = MpmMigrationHelper::getLatestMigration();
58+
$obj = new MpmUpController('up', array ( $to_id, $forced ));
59+
$obj->doAction($quiet);
60+
}
5961
}
6062
catch (Exception $e)
6163
{
6264
echo "\n\nERROR: " . $e->getMessage() . "\n\n";
6365
exit;
6466
}
6567
}
66-
68+
6769
/**
6870
* Displays the help page for this controller.
69-
*
71+
*
7072
* @uses MpmCommandLineWriter::getInstance()
7173
* @uses MpmCommandLineWriter::addText()
7274
* @uses MpmCommandLineWriter::write()
73-
*
75+
*
7476
* @return void
7577
*/
7678
public function displayHelp()
@@ -87,7 +89,7 @@ public function displayHelp()
8789
$obj->addText('./migrate.php latest --force', 4);
8890
$obj->write();
8991
}
90-
92+
9193
}
9294

9395
?>

lib/helpers/template_helper.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,16 @@ static public function getTemplateAsArrayOfLines($file, $vars = array())
4646
*/
4747
static public function getTemplate($file, $vars = array())
4848
{
49-
$db_config = $GLOBALS['db_config'];
49+
if (isset($GLOBALS['db_config']))
50+
{
51+
$db_config = $GLOBALS['db_config'];
52+
}
53+
else
54+
{
55+
$db_config = new stdClass();
56+
$db_config->db_path = MPM_PATH . '/lib/templates/';
57+
}
58+
5059
// has the file been customized?
5160
if (file_exists($db_config->db_path . $file))
5261
{

0 commit comments

Comments
 (0)