Skip to content

Commit

Permalink
Added the SqliteCommand and examples for migrate using Sqlite;
Browse files Browse the repository at this point in the history
  • Loading branch information
byjg committed Jun 10, 2016
1 parent f5a493d commit f6c2331
Show file tree
Hide file tree
Showing 20 changed files with 254 additions and 54 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/byjg/migration/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/byjg/migration/?branch=master)
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/571cb412-7018-4938-a4e5-0f9ce44956d7/mini.png)](https://insight.sensiolabs.com/projects/571cb412-7018-4938-a4e5-0f9ce44956d7)

A micro project in PHP for managing a set of database migrations using pure Sql.
A micro framework in PHP for managing a set of database migrations using pure Sql.

Database Migration is a set of commands for upgrade or downgrade a database.
This library uses only SQL commands.
Expand All @@ -21,7 +21,8 @@ See an example:
$connection = new ConnectionManagement('mysql://migrateuser:migratepwd@localhost/migratedatabase');
$migration = new Migration($connection, '.');

// Create the database from "base.sql" script and run ALL existing scripts for up the database version;
// Restore the database using the "base.sql" script and run ALL existing scripts for up the database version
// and run the up() method to maintain the database updated;
$migration->reset();

// Run ALL existing scripts for up the database version from the current version to the last version;
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "byjg/migration",
"description": "A micro project in PHP for managing a set of database migrations using pure Sql.",
"description": "A micro framework in PHP for managing a set of database migrations using pure Sql.",
"authors": [
{
"name": "jg",
Expand Down
4 changes: 2 additions & 2 deletions example/base.sql → example/mysql/base.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ create table users (

);

insert into users (name, createdate) value ('John Doe', '20160110');
insert into users (name, createdate) value ('Jane Doe', '20151230');
insert into users (name, createdate) values ('John Doe', '20160110');
insert into users (name, createdate) values ('Jane Doe', '20151230');

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
18 changes: 18 additions & 0 deletions example/mysql/test_mysql.php
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();

18 changes: 18 additions & 0 deletions example/sqlite/base.sql
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');

20 changes: 20 additions & 0 deletions example/sqlite/migrations/down/00000.sql
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 ;

9 changes: 9 additions & 0 deletions example/sqlite/migrations/down/00001.sql
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;
22 changes: 22 additions & 0 deletions example/sqlite/migrations/up/00001.sql
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;

12 changes: 12 additions & 0 deletions example/sqlite/migrations/up/00002.sql
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
)
10 changes: 10 additions & 0 deletions example/sqlite/test_sqlite.php
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();

10 changes: 0 additions & 10 deletions example/test.php

This file was deleted.

50 changes: 50 additions & 0 deletions src/Commands/AbstractCommand.php
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)');
}
}
}
6 changes: 6 additions & 0 deletions src/Commands/CommandInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
namespace ByJG\DbMigration\Commands;


use ByJG\AnyDataset\ConnectionManagement;

interface CommandInterface
{
public static function prepareEnvironment(ConnectionManagement $connection);

public function createDatabase();

public function dropDatabase();

public function getVersion();

public function executeSql($sql);

public function setVersion($version);

public function createVersion();
Expand Down
47 changes: 12 additions & 35 deletions src/Commands/MysqlCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,19 @@

namespace ByJG\DbMigration\Commands;

use ByJG\AnyDataset\ConnectionManagement;
use ByJG\AnyDataset\Repository\DBDataset;

class MySqlCommand implements CommandInterface
class MySqlCommand extends AbstractCommand
{
/**
* @var DBDataset
*/
protected $_dbDataset;

/**
* MySqlCommand constructor.
*
* @param DBDataset $_dbDataset
*/
public function __construct(DBDataset $_dbDataset)
public static function prepareEnvironment(ConnectionManagement $connection)
{
$this->_dbDataset = $_dbDataset;
}
$database = $connection->getDatabase();

/**
* @return DBDataset
*/
public function getDbDataset()
{
return $this->_dbDataset;
$newConnection = new ConnectionManagement(str_replace("/$database", "/", $connection->getDbConnectionString()));
$dbDataset = new DBDataset($newConnection->getDbConnectionString());
$dbDataset->execSQL("CREATE SCHEMA IF NOT EXISTS `$database` DEFAULT CHARACTER SET utf8 ;");
}

public function createDatabase()
Expand All @@ -44,25 +32,14 @@ public function dropDatabase()
$this->getDbDataset()->execSQL("drop database `$database`");
}

public function getVersion()
{
return $this->getDbDataset()->getScalar('SELECT version FROM migration_table');
}

public function setVersion($version)
public function createVersion()
{
$this->getDbDataset()->execSQL('UPDATE migration_table SET version = [[version]]', ['version' => $version]);
$this->getDbDataset()->execSQL('CREATE TABLE IF NOT EXISTS migration_version (version int)');
$this->checkExistsVersion();
}

public function createVersion()
public function executeSql($sql)
{
$this->getDbDataset()->execSQL('CREATE TABLE IF NOT EXISTS migration_table (version int)');

// Get the version to check if exists
$version = $this->getVersion();
if ($version === false) {
$this->getDbDataset()->execSQL('insert into migration_table values(0)');
}
$this->getDbDataset()->execSQL($sql);
}

}
52 changes: 52 additions & 0 deletions src/Commands/SqliteCommand.php
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);
}
}
Loading

0 comments on commit f6c2331

Please sign in to comment.