Skip to content

Commit

Permalink
Merge pull request #1 from byjg/1.1
Browse files Browse the repository at this point in the history
1.1
  • Loading branch information
byjg authored May 2, 2017
2 parents 56e2708 + f23c89a commit 01e4c86
Show file tree
Hide file tree
Showing 36 changed files with 807 additions and 83 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff:
.idea
.idea/workspace.xml
.idea/tasks.xml
.idea/dictionaries
Expand Down
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
language: php
php:
- "7.1"
- "7.0"
- "5.6"
- "5.5"
- "5.4"

install:
- composer install

script:
- phpunit
- phpunit tests/SqliteCommandTest.php

27 changes: 24 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Database Migrations
[![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)
[![Build Status](https://travis-ci.org/byjg/migration.svg?branch=master)](https://travis-ci.org/byjg/migration)

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

Expand All @@ -18,8 +19,8 @@ The basic usage is
See an example:

```php
$connection = new ConnectionManagement('mysql://migrateuser:migratepwd@localhost/migratedatabase');
$migration = new Migration($connection, '.');
$connectionUri = new \ByJG\Util\Uri('mysql://migrateuser:migratepwd@localhost/migratedatabase');
$migration = new Migration($connectionUri, '.');

// 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;
Expand Down Expand Up @@ -90,9 +91,29 @@ Example:
migrate down --up-to=3 --path=/somepath mysql://root:password@server/database
```

## Suportted databases:

* Sqlite
* Mysql / MariaDB
* Postgres
* SqlServer

## Installing Globally

```bash
composer global require 'byjg/migration=1.0.*'
composer global require 'byjg/migration=1.1.*'
sudo ln -s $HOME/.composer/vendor/bin/migrate /usr/local/bin
```

## Unit Tests

This library has integrated tests and need to be setup for each database you want to test.

Basiclly you have the follow tests:

```
phpunit tests/SqliteCommandTest.php
phpunit tests/MysqlCommandTest.php
phpunit tests/PostgresCommandTest.php
phpunit tests/SqlServerCommandTest.php
```
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
"email": "[email protected]"
}
],
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"byjg/anydataset": "2.1.*",
"byjg/anydataset": "3.0.*",
"byjg/uri": "1.0.*",
"symfony/console": "3.1.*"
},
"autoload": {
Expand Down
6 changes: 3 additions & 3 deletions example/mysql/test_mysql.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?php

require "../../vendor/autoload.php";
require __DIR__ . "/../../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');
$uri = new \ByJG\Util\Uri('mysql://root:password@mysql-container/migratedatabase');

$migration = new \ByJG\DbMigration\Migration($connection, '.');
$migration = new \ByJG\DbMigration\Migration($uri, __DIR__);

$migration->prepareEnvironment();

Expand Down
18 changes: 18 additions & 0 deletions example/postgres/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 SERIAL NOT NULL PRIMARY KEY,
name varchar(50) NOT NULL,
createdate VARCHAR(8)

);

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

20 changes: 20 additions & 0 deletions example/postgres/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;

update users
set createdate_old = TO_CHAR(createdate,'YYYYMMDD');

ALTER TABLE users
DROP COLUMN createdate;

ALTER TABLE users
RENAME COLUMN createdate_old TO createdate;

9 changes: 9 additions & 0 deletions example/postgres/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;
19 changes: 19 additions & 0 deletions example/postgres/migrations/up/00001.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

-- --------------------------------------------------------
-- This is the script for migrate up
-- from version '0' to version '1'
-- --------------------------------------------------------


ALTER TABLE users
ADD COLUMN createdate_new DATE NULL;

update users
set createdate_new = TO_DATE(createdate, 'YYYYMMDD');

ALTER TABLE users
DROP COLUMN createdate;

ALTER TABLE users
RENAME COLUMN createdate_new TO createdate;

12 changes: 12 additions & 0 deletions example/postgres/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 SERIAL NOT NULL PRIMARY KEY,
rolename char(1) NOT NULL,
userid int not null
)
18 changes: 18 additions & 0 deletions example/postgres/test_postgres.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

require __DIR__ . "/../../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;
*/

$uri = new \ByJG\Util\Uri('pgsql://postgres:password@postgres-container/migratedatabase');

$migration = new \ByJG\DbMigration\Migration($uri, __DIR__);

$migration->prepareEnvironment();

$migration->reset();

18 changes: 18 additions & 0 deletions example/sql_server/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 int IDENTITY(1,1) PRIMARY KEY,
name varchar(50) NOT NULL,
createdate VARCHAR(8)

);

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

22 changes: 22 additions & 0 deletions example/sql_server/migrations/down/00000.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

-- --------------------------------------------------------
-- 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 createdate_old VARCHAR(8) NULL ;

update users
set createdate_old = concat(DATEPART(yyyy,createdate),
RIGHT('00' + CONVERT(NVARCHAR(2), DATEPART(MONTH, createdate)), 2),
RIGHT('00' + CONVERT(NVARCHAR(2), DATEPART(DAY, createdate)), 2)
);

ALTER TABLE users
DROP COLUMN createdate;

EXEC sp_RENAME 'users.createdate_old' , 'createdate', 'COLUMN'

9 changes: 9 additions & 0 deletions example/sql_server/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/sql_server/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'
-- --------------------------------------------------------


ALTER TABLE users ADD createdate_new DATE NULL;

update users set createdate_new = concat(
substring(createdate, 1, 4),
'-',
substring(createdate, 5, 2),
'-',
substring(createdate, 7, 2)
);

ALTER TABLE users DROP COLUMN createdate;

EXEC SP_RENAME 'users.createdate_new' , 'createdate', 'COLUMN';


12 changes: 12 additions & 0 deletions example/sql_server/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 int IDENTITY(1,1) PRIMARY KEY,
rolename char(1) NOT NULL,
userid int not null
)
18 changes: 18 additions & 0 deletions example/sql_server/test_sqlserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

require __DIR__ . "/../../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;
*/

$uri = new \ByJG\Util\Uri('dblib://sa:Pa$$word!@mssql-container/migratedatabase');

$migration = new \ByJG\DbMigration\Migration($uri, __DIR__);

$migration->prepareEnvironment();

$migration->reset();

4 changes: 2 additions & 2 deletions example/sqlite/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) values ('John Doe 2', '20160110');
insert into users (name, createdate) values ('Jane Doe 2', '20151230');
insert into users (name, createdate) values ('John Doe', '20160110');
insert into users (name, createdate) values ('Jane Doe', '20151230');

18 changes: 10 additions & 8 deletions example/sqlite/migrations/down/00000.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
-- This is the reverse operation of the script up/00001
-- --------------------------------------------------------

ALTER TABLE `users`
ADD COLUMN `createdate_old` VARCHAR(8) NULL AFTER `createdate`;
CREATE table users_backup (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT ,
name varchar(50) NOT NULL,
createdate varchar(8) not NULL
);

update users
set createdate_old = DATE_FORMAT(createdate,'%Y%m%d');
INSERT INTO users_backup
SELECT id, name, strftime('%Y%m%d', createdate)
FROM users;

ALTER TABLE `users`
DROP COLUMN `createdate`;
DROP TABLE users;

ALTER TABLE `users`
CHANGE COLUMN `createdate_old` `createdate` VARCHAR(8) NOT NULL ;
ALTER TABLE users_backup RENAME TO users;

6 changes: 3 additions & 3 deletions example/sqlite/test_sqlite.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

require "../../vendor/autoload.php";
require __DIR__ . "/../../vendor/autoload.php";

$connection = new \ByJG\AnyDataset\ConnectionManagement('sqlite:///tmp/teste.sqlite');
$uri = new \ByJG\Util\Uri('sqlite:///tmp/teste.sqlite');

$migration = new \ByJG\DbMigration\Migration($connection, '.');
$migration = new \ByJG\DbMigration\Migration($uri, __DIR__);

$migration->reset();

3 changes: 2 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ and open the template in the editor.

<testsuites>
<testsuite name="Test Suite">
<directory>./tests/</directory>
<!--<directory>./tests/</directory>-->
<file>./tests/SqliteCommandTest.php</file>
</testsuite>
</testsuites>

Expand Down
3 changes: 2 additions & 1 deletion scripts/migrate
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ require_once($autoload);

use Symfony\Component\Console\Application;

$application = new Application('Migrate Script by JG', '1.0.2');
$application = new Application('Migrate Script by JG', '1.1.0');
$application->add(new \ByJG\DbMigration\Console\ResetCommand());
$application->add(new \ByJG\DbMigration\Console\UpCommand());
$application->add(new \ByJG\DbMigration\Console\DownCommand());
$application->add(new \ByJG\DbMigration\Console\CreateCommand());
$application->run();
Loading

0 comments on commit 01e4c86

Please sign in to comment.