Skip to content

Commit cc75853

Browse files
author
Alexandre GUIDET
committed
gestion de la table changelog
1 parent 0eed4ae commit cc75853

4 files changed

Lines changed: 48 additions & 17 deletions

File tree

Migrator.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ class Migrate {
183183
private $options = null;
184184
private $action = null;
185185
private $version = "";
186+
private $changelogTableName;
186187

187188
/**
188189
* configuration from ini
@@ -191,6 +192,20 @@ class Migrate {
191192
*/
192193
private $config;
193194

195+
/**
196+
* @return mixed
197+
*/
198+
public function getChangelogTableName() {
199+
return $this->changelogTableName;
200+
}
201+
202+
/**
203+
* @param mixed $changelogTableName
204+
*/
205+
public function setChangelogTableName($changelogTableName) {
206+
$this->changelogTableName = $changelogTableName;
207+
}
208+
194209
/**
195210
* @param string $environmentPath
196211
* @return Migrate
@@ -434,6 +449,7 @@ public function loadConfiguration() {
434449

435450
$config = parse_ini_file($this->getEnvironementPath() . "/" . $env . ".ini");
436451
$this->setConfig($config);
452+
$this->setChangelogTableName($config['changelog']);
437453
}
438454

439455
/**
@@ -546,7 +562,7 @@ public function getLocaleList() {
546562
*
547563
*/
548564
public function getDbList() {
549-
$sqlResult = $this->getDb()->query('SELECT * FROM ' .$config['changelog'] .' ORDER BY id');
565+
$sqlResult = $this->getDb()->query('SELECT * FROM ' . $this->getChangelogTableName() . ' ORDER BY id');
550566

551567
$migrationList = array();
552568
foreach ($sqlResult as $row) {
@@ -644,7 +660,7 @@ private function up(Migration $migration) {
644660

645661
// insert into changelog
646662
$this->getDb()->exec(
647-
"INSERT INTO " .$config['changelog'] ." (id, applied_at, description, version) VALUES ("
663+
"INSERT INTO " . $this->getChangelogTableName() . " (id, applied_at, description, version) VALUES ("
648664
. $migration->getId() . ", '"
649665
. $date . "', '"
650666
. $migration->getDescription() . "', '"
@@ -694,7 +710,7 @@ private function down(Migration $migration) {
694710

695711
// insert into changelog
696712
$this->getDb()->exec(
697-
"DELETE FROM " .$config['changelog'] ." WHERE id = " . $migration->getId()
713+
"DELETE FROM " . $this->getChangelogTableName() . " WHERE id = " . $migration->getId()
698714
);
699715

700716
if ($sqlReturnCode != '0') {

templates/changelog-table.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- generate the changelog table
2+
create table {TABLE_NAME} (
3+
id numeric(20,0),
4+
applied_at character varying(25),
5+
version character varying(25),
6+
description character varying(255)
7+
)

tests/MigrateTest.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,24 @@ class MigrateTest extends PHPUnit_Framework_TestCase
99
private static $db;
1010
private static $dbName;
1111
private static $migrationPath;
12-
private static $changelog;
12+
private static $changelog = 'versionning';
1313

1414
public static function setUpBeforeClass() {
1515
self::$dbName = dirname(__FILE__) . '/test.sqlite';
1616
unlink(self::$dbName);
17-
18-
self::$changelog = 'mychangelog';
1917

2018
self::$db = new PDO('sqlite:' . self::$dbName, '', '');
2119
self::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
22-
self::$db->exec("create table " .self::$changelog ."(
20+
self::$db->exec("create table " . self::$changelog . " (
2321
id numeric(20,0),
2422
applied_at character varying(25),
2523
version character varying(25),
2624
description character varying(255)
2725
)");
2826

2927
self::$migrationPath = realpath(dirname(__FILE__)) . "/../migrations";
28+
29+
exec('rm ' . self::$migrationPath . '/*');
3030
}
3131

3232
public function setUp() {
@@ -59,9 +59,9 @@ public function testGetLocaleList() {
5959
* @Test
6060
*/
6161
public function testGetDbList() {
62-
self::$db->query("INSERT INTO " .self::$changelog ." (id, applied_at, description) VALUES (20100101, '2010-01-01', 'description 1')");
63-
self::$db->query("INSERT INTO " .self::$changelog ." (id, applied_at, description) VALUES (20100102, '2010-01-02', 'description 2')");
64-
self::$db->query("INSERT INTO " .self::$changelog ." (id, applied_at, description) VALUES (20100103, '2010-01-03', 'description 3')");
62+
self::$db->query("INSERT INTO " . self::$changelog . " (id, applied_at, description) VALUES (20100101, '2010-01-01', 'description 1')");
63+
self::$db->query("INSERT INTO " . self::$changelog . " (id, applied_at, description) VALUES (20100102, '2010-01-02', 'description 2')");
64+
self::$db->query("INSERT INTO " . self::$changelog . " (id, applied_at, description) VALUES (20100103, '2010-01-03', 'description 3')");
6565

6666
$expected = array(
6767
'20100101' => new Migration('20100101', '2010-01-01', 'description 1', '20100101_description_1.sql', true),
@@ -74,7 +74,7 @@ public function testGetDbList() {
7474

7575
$migrationList = $migrator->getDbList();
7676

77-
self::$db->query("DELETE FROM " .self::$changelog );
77+
self::$db->query("DELETE FROM " . self::$changelog);
7878

7979
$this->assertEquals($expected, $migrationList);
8080

@@ -93,10 +93,10 @@ public function testGetMigrationList() {
9393
touch(self::$migrationPath . "/20100104_description_2.sql");
9494
touch(self::$migrationPath . "/20100105_description_3.sql");
9595

96-
self::$db->query("INSERT INTO " .self::$changelog ." (id, applied_at, description) VALUES (20100100, '2009-12-30', 'description 0')");
97-
self::$db->query("INSERT INTO " .self::$changelog ." (id, applied_at, description) VALUES (20100103, '2010-01-01', 'description 1')");
98-
self::$db->query("INSERT INTO " .self::$changelog ." (id, applied_at, description) VALUES (20100104, '2010-01-02', 'description 2')");
99-
self::$db->query("INSERT INTO " .self::$changelog ." (id, applied_at, description) VALUES (20100105, '2010-01-03', 'description 3')");
96+
self::$db->query("INSERT INTO " . self::$changelog . " (id, applied_at, description) VALUES (20100100, '2009-12-30', 'description 0')");
97+
self::$db->query("INSERT INTO " . self::$changelog . " (id, applied_at, description) VALUES (20100103, '2010-01-01', 'description 1')");
98+
self::$db->query("INSERT INTO " . self::$changelog . " (id, applied_at, description) VALUES (20100104, '2010-01-02', 'description 2')");
99+
self::$db->query("INSERT INTO " . self::$changelog . " (id, applied_at, description) VALUES (20100105, '2010-01-03', 'description 3')");
100100

101101
$expected = array(
102102
'20100100' => new Migration('20100100', '2009-12-30', 'description 0', '20100100_description_0.sql', true),
@@ -111,7 +111,7 @@ public function testGetMigrationList() {
111111
$migrator->setDb(self::$db);
112112
$migrationList = $migrator->getMigrationList();
113113

114-
self::$db->query("DELETE FROM " .self::$changelog );
114+
self::$db->query("DELETE FROM " . self::$changelog);
115115

116116
unlink(self::$migrationPath . "/20100101_add_table_1.sql");
117117
unlink(self::$migrationPath . "/20100102_add_table_2.sql");
@@ -172,6 +172,7 @@ public function testDoUpThenDown() {
172172
SQL;
173173

174174
file_put_contents(self::$migrationPath . "/" . $aMigration->getSqlFile(), $sqlUp1);
175+
175176
}
176177

177178

@@ -500,7 +501,7 @@ public function testDoStatus() {
500501
$migrator->doUpForce($migrationList[4]->getId());
501502

502503
$status = $migrator->doStatus();
503-
print_r($status);
504+
// print_r($status);
504505

505506
foreach ($migrationList as $aMigration) {
506507
/* @var $aMigration Migration */

tests/MigratorTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
require_once dirname(__FILE__) . '/../Migrator.php';
3+
4+
class MigratorTest extends PHPUnit_Framework_TestCase
5+
{
6+
7+
}

0 commit comments

Comments
 (0)