-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the SqliteCommand and examples for migrate using Sqlite;
- Loading branch information
Showing
20 changed files
with
254 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
require "../../vendor/autoload.php"; | ||
|
||
/** | ||
* Make sure you have a database with the user 'migrateuser' and password 'migratepwd' | ||
* | ||
* This user need to have grant for DDL commands; | ||
*/ | ||
|
||
$connection = new \ByJG\AnyDataset\ConnectionManagement('mysql://migrateuser:migratepwd@localhost/migratedatabase'); | ||
|
||
$migration = new \ByJG\DbMigration\Migration($connection, '.'); | ||
|
||
$migration->prepareEnvironment(); | ||
|
||
$migration->reset(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
-- -------------------------------------------------------- | ||
-- THIS IS THE BASE FILE . The version '0' | ||
-- -------------------------------------------------------- | ||
|
||
-- Create the demo table USERS and populate it | ||
|
||
create table users ( | ||
|
||
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT , | ||
name varchar(50) NOT NULL, | ||
createdate VARCHAR(8) | ||
|
||
); | ||
|
||
insert into users (name, createdate) values ('John Doe 2', '20160110'); | ||
insert into users (name, createdate) values ('Jane Doe 2', '20151230'); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
|
||
-- -------------------------------------------------------- | ||
-- This is the script for migrate DOWN | ||
-- from version '1' to version '0' | ||
-- | ||
-- This is the reverse operation of the script up/00001 | ||
-- -------------------------------------------------------- | ||
|
||
ALTER TABLE `users` | ||
ADD COLUMN `createdate_old` VARCHAR(8) NULL AFTER `createdate`; | ||
|
||
update users | ||
set createdate_old = DATE_FORMAT(createdate,'%Y%m%d'); | ||
|
||
ALTER TABLE `users` | ||
DROP COLUMN `createdate`; | ||
|
||
ALTER TABLE `users` | ||
CHANGE COLUMN `createdate_old` `createdate` VARCHAR(8) NOT NULL ; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
-- -------------------------------------------------------- | ||
-- This is the script for migrate DOWN | ||
-- from version '2' to version '1' | ||
-- | ||
-- This is the reverse operation of the script up/00002 | ||
-- -------------------------------------------------------- | ||
|
||
drop table roles; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
-- -------------------------------------------------------- | ||
-- This is the script for migrate up | ||
-- from version '0' to version '1' | ||
-- -------------------------------------------------------- | ||
|
||
|
||
-- Create a temp table; | ||
CREATE table users_backup ( | ||
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT , | ||
name varchar(50) NOT NULL, | ||
createdate date not NULL | ||
); | ||
|
||
INSERT INTO users_backup | ||
SELECT id, name, substr(createdate, 1, 4) || '-' || substr(createdate, 5, 2) || '-' || substr(createdate, 7, 2) | ||
FROM users; | ||
|
||
DROP TABLE users; | ||
|
||
ALTER TABLE users_backup RENAME TO users; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
|
||
-- -------------------------------------------------------- | ||
-- This is the script for migrate up | ||
-- from version '1' to version '2' | ||
-- -------------------------------------------------------- | ||
|
||
create table roles | ||
( | ||
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, | ||
rolename char(1) NOT NULL, | ||
userid int not null | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
require "../../vendor/autoload.php"; | ||
|
||
$connection = new \ByJG\AnyDataset\ConnectionManagement('sqlite:///tmp/teste.sqlite'); | ||
|
||
$migration = new \ByJG\DbMigration\Migration($connection, '.'); | ||
|
||
$migration->reset(); | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace ByJG\DbMigration\Commands; | ||
|
||
use ByJG\AnyDataset\Repository\DBDataset; | ||
|
||
abstract class AbstractCommand implements CommandInterface | ||
{ | ||
/** | ||
* @var DBDataset | ||
*/ | ||
private $_dbDataset; | ||
|
||
/** | ||
* Command constructor. | ||
* | ||
* @param DBDataset $_dbDataset | ||
*/ | ||
public function __construct(DBDataset $_dbDataset) | ||
{ | ||
$this->_dbDataset = $_dbDataset; | ||
} | ||
|
||
/** | ||
* @return DBDataset | ||
*/ | ||
public function getDbDataset() | ||
{ | ||
return $this->_dbDataset; | ||
} | ||
|
||
public function getVersion() | ||
{ | ||
return $this->getDbDataset()->getScalar('SELECT version FROM migration_version'); | ||
} | ||
|
||
public function setVersion($version) | ||
{ | ||
$this->getDbDataset()->execSQL('UPDATE migration_version SET version = :version', ['version' => $version]); | ||
} | ||
|
||
protected function checkExistsVersion() | ||
{ | ||
// Get the version to check if exists | ||
$version = $this->getVersion(); | ||
if ($version === false) { | ||
$this->getDbDataset()->execSQL('insert into migration_version values(0)'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace ByJG\DbMigration\Commands; | ||
|
||
use ByJG\AnyDataset\ConnectionManagement; | ||
|
||
class SqliteCommand extends AbstractCommand | ||
{ | ||
|
||
public static function prepareEnvironment(ConnectionManagement $connection) | ||
{ | ||
} | ||
|
||
public function createDatabase() | ||
{ | ||
} | ||
|
||
public function dropDatabase() | ||
{ | ||
$iterator = $this->getDbDataset()->getIterator(" | ||
select | ||
'drop table ' || name || ';' as command | ||
from sqlite_master | ||
where type = 'table' | ||
and name <> 'sqlite_sequence'; | ||
"); | ||
|
||
foreach ($iterator as $row) { | ||
$this->getDbDataset()->execSQL($row->getField('command')); | ||
} | ||
} | ||
|
||
public function createVersion() | ||
{ | ||
$this->getDbDataset()->execSQL('CREATE TABLE IF NOT EXISTS migration_version (version int)'); | ||
$this->checkExistsVersion(); | ||
} | ||
|
||
public function executeSql($sql) | ||
{ | ||
$statements = explode(";", $sql); | ||
|
||
foreach ($statements as $sql) { | ||
$this->executeSqlInternal($sql); | ||
} | ||
} | ||
|
||
protected function executeSqlInternal($sql) | ||
{ | ||
$this->getDbDataset()->execSQL($sql); | ||
} | ||
} |
Oops, something went wrong.