Skip to content

Commit

Permalink
Initial Release
Browse files Browse the repository at this point in the history
  • Loading branch information
davedevelopment committed Oct 22, 2011
0 parents commit 6b1cef8
Show file tree
Hide file tree
Showing 22 changed files with 2,169 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitmodules
@@ -0,0 +1,9 @@
[submodule "vendor/Symfony/Component/Console"]
path = vendor/Symfony/Component/Console
url = https://github.com/symfony/Console.git
[submodule "vendor/Symfony/Component/ClassLoader"]
path = vendor/Symfony/Component/ClassLoader
url = https://github.com/symfony/ClassLoader
[submodule "vendor/Symfony/Component/Config"]
path = vendor/Symfony/Component/Config
url = https://github.com/symfony/Config.git
21 changes: 21 additions & 0 deletions LICENCE
@@ -0,0 +1,21 @@
Pimple: Copyright (c) 2009,2010,2011 Fabien Potencier

Phpmig: Copyright (C) 2011 Dave Marshall <dave.marshall@atstsolutions.co.uk>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
163 changes: 163 additions & 0 deletions README.md
@@ -0,0 +1,163 @@
Phpmig
======

What is it?
-----------

Phpmig is a (database) migration tool for php, that should be adaptable for use with most PHP 5.3+ projects. It's kind of like [doctrine migrations][doctrinemigrations], without the [doctrine][doctrine]. Although you can use doctrine if you want. And ironically, I use doctrine in my examples.

How does it work?
-----------------

$ phpmig migrate

Phpmig aims to be vendor/framework independant, and in doing so, requires you to do a little bit of work up front to use it.

Phpmig requires a bootstrap file, that must return an object that implements the ArrayAccess interface with several predefined keys. We recommend returning an instance of [Pimple][pimple], a simple dependency injection container (there's a version bundled at \Phpmig\Pimple\Pimple). This is also an ideal opportunity to expose your own services to the migrations themselves, which have access to the container.

Getting Started
---------------

The best way to install phpmig is using pear

$ sudo pear channel-discover pear.atstsolutions.co.uk
$ sudo pear install atst/phpmig-alpha

Phpmig can do a little configuring for you to get started, go to the root of your project and:

$ phpmig init
+d ./migrations Place your migration files in here
+f ./phpmig.php Create services in here
$

It can generate migrations, but you have to tell it where. Phpmig gets you to supply it with a list of migrations, so it doesn't know where to put them. Migration files should be named versionnumber_name.php, where version number is made up of 0-9 and name is CamelCase or snake\_case. Each migration file should contain a class with the same name as the file in CamelCase.

$ phpmig generate AddRatingToLolCats ./migrations
+f ./migrations/20111018171411_AddRatingToLolCats.php
$ phpmig status

Status Migration ID Migration Name
-----------------------------------------
down 20111018171929 AddRatingToLolCats


Use the migrate command to run migrations

$ phpmig migrate
== 20111018171411 AddRatingToLolCats migrating
== 20111018171411 AddRatingToLolCats migrated 0.0005s
$ phpmig status

Status Migration ID Migration Name
-----------------------------------------
up 20111018171929 AddRatingToLolCats

$

Better Persistence
------------------

The init command creates a bootstrap file that specifies a flat file to use to
track which migrations have been run, which isn't great. You can use the
provided adapters to store this information in your database. For example, to
use Doctrine's DBAL:

``` php
<?php

# phpmig.php

// do some autoloading of Doctrine here

use \Phpmig\Adapter,
\Phpmig\Pimple\Pimple,
\Doctrine\DBAL\DriverManager;

$container = new Pimple();

$container['db'] = $container->share(function() {
return DriverManager::getConnection(array(
'driver' => 'pdo_sqlite',
'path' => __DIR__ . DIRECTORY_SEPARATOR . 'db.sqlite',
));
});

$container['phpmig.adapter'] = $container->share(function() use ($container) {
return new Adapter\Doctrine\DBAL($container['db'], 'migrations');
});

$container['phpmig.migrations'] = function() {
return glob(__DIR__ . DIRECTORY_SEPARATOR . 'migrations/*.php');
};

return $container;
```

Writing Migrations
------------------

The migrations should extend the Phpmig\Migration\Migration class, and have
access to the container. For example, assuming you've rewritten your bootstrap
file like above:

``` php
<?php

use Phpmig\Migration\Migration;

class AddRatingToLolCats extends Migration
{
/**
* Do the migration
*/
public function up()
{
$sql = "ALTER TABLE `lol_cats` ADD COLUMN `rating` INT(10) UNSIGNED NULL";
$container = $this->getContainer();
$container['db']->query($sql);
}

/**
* Undo the migration
*/
public function down()
{
$sql = "ALTER TABLE `lol_cats` DROP COLUMN `rating`";
$container = $this->getContainer();
$container['db']->query($sql);
}
}
```

Todo
----

* Some sort of migration manager, that will take some of the logic out of the commands for calculating which migrations have been run, which need running etc
* Adapters for Zend\_Db and/or Zend\_Db\_Table and others?
* Redo and rollback commands
* Tests!
* Configuration?
* Someway of protecting against class definition clashes with regard to the symfony dependencies and the user supplied bootstrap?

Contributing
------------

Feel free to fork and send me pull requests, but I don't have a 1.0 release yet, so I may change the API quite frequently. If you want to implement somthing that I might easily break, please drop me an email

Inspiration
-----------

I basically started copying [ActiveRecord::Migrations][activerecordmigrations] in terms of the migration features, the bootstrapping was my own idea, the layout of the code was inspired by [Symfony][symfony] and [Behat][behat]

Copyright
---------

[Pimple][pimple] is copyright Fabien Potencier. Everything I haven't copied from anyone else is Copyright (c) 2011 Dave Marshall. See LICENCE for further details


[pimple]:https://github.com/fabpot/Pimple
[doctrinemigrations]:https://github.com/doctrine/migrations
[doctrine]:https://github.com/doctrine
[behat]:http://behat.org/
[symfony]:http://symfony.com/
[activerecordmigrations]:http://api.rubyonrails.org/classes/ActiveRecord/Migration.html
22 changes: 22 additions & 0 deletions bin/phpmig
@@ -0,0 +1,22 @@
#!/usr/bin/env php
<?php

/**
* This file is part of phpmig
*
* Copyright (c) 2011 Dave Marshall <dave.marshall@atstsolutuions.co.uk>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

define('PHPMIG_VERSION', 'dev');

if (is_dir(__DIR__ . '/../src/Phpmig')) {
require_once __DIR__ . "/../src/Phpmig/autoload.php.dist";
} else {
require_once "Phpmig/autoload.php.dist";
}

$app = new Phpmig\Console\PhpmigApplication(PHPMIG_VERSION);
$app->run();
51 changes: 51 additions & 0 deletions bin/release
@@ -0,0 +1,51 @@
#!/usr/bin/env php
<?php

if (!isset($argv[1])) {
throw new RuntimeException('You must provide version.');
}
$version = $argv[1];

if (!isset($argv[2])) {
throw new RuntimeException('You must provide stability status (alpha/beta/stable).');
}
$stability = $argv[2];

$package = __DIR__ . '/../package.xml';
$oldPackageContents = file_get_contents($package);

$replace = array(
'@@DATE@@' => date('Y-m-d'),
'@@TIME@@' => date('H:i:s'),
'@@VERSION@@' => $version,
'@@STABILITY@@' => $stability,
);

$contents = array();
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__ . '/../src/'));

while($it->valid()) {

if (!$it->isDot() && !$it->isDir()) {
$name = str_replace(__DIR__ . '/../', '', $it->key());
$installAs = $it->getSubPathName();
$contents[] = "<file role=\"php\" install-as=\"$installAs\" name=\"$name\" />";
}

$it->next();
}

$replace['@@CONTENTS@@'] = implode("\n", $contents);

$newPackageContents = $oldPackageContents;

foreach($replace as $s => $r) {
$newPackageContents = str_replace($s, $r, $newPackageContents);
}

file_put_contents($package, $newPackageContents);

system('pear package');

file_put_contents($package, $oldPackageContents);
exit(0);
90 changes: 90 additions & 0 deletions package.xml
@@ -0,0 +1,90 @@
<?xml version="1.0" encoding="UTF-8"?>
<package xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" packagerversion="1.9.0" version="2.0" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.0 http://pear.php.net/dtd/package-2.0.xsd">
<name>phpmig</name>
<channel>pear.atstsolutions.co.uk</channel>
<summary>A simple migrations tool for php.</summary>
<description>A simple migrations tool for php.</description>
<lead>
<name>Dave Marshall</name>
<user>davedevelopment</user>
<email>dave.marshall@atstsolutions.co.uk</email>
<active>yes</active>
</lead>
<date>@@DATE@@</date>
<time>@@TIME@@</time>
<version>
<release>@@VERSION@@</release>
<api>@@VERSION@@</api>
</version>
<stability>
<release>@@STABILITY@@</release>
<api>@@STABILITY@@</api>
</stability>
<license uri="https://github.com/davedevelopment/phpmig/blob/develop/LICENCE">MIT Licence</license>
<notes>
-
</notes>
<contents>
<dir name="/">
@@CONTENTS@@
<file role="php" install-as="Phpmig/autoload.php.dist" name="src/Phpmig/autoload.php.dist"><tasks:replace from="@@PHP_DIR@@" to="php_dir" type="pear-config"/></file>
<file role="script" name="bin/phpmig"><tasks:replace from="/usr/bin/env php" to="php_bin" type="pear-config"/><tasks:replace from="dev" to="version" type="package-info"/></file>
<file role="doc" name="README.md"/>
<file role="doc" name="LICENCE"/>
</dir>
</contents>
<dependencies>
<required>
<php>
<min>5.3.0</min>
</php>
<pearinstaller>
<min>1.6.0</min>
</pearinstaller>
<package>
<name>ClassLoader</name>
<channel>pear.symfony.com</channel>
<min>2.0.0</min>
<max>3.999.9999</max>
</package>
<package>
<name>Config</name>
<channel>pear.symfony.com</channel>
<min>2.0.0</min>
<max>3.999.9999</max>
</package>
<package>
<name>Console</name>
<channel>pear.symfony.com</channel>
<min>2.0.0</min>
<max>3.999.9999</max>
</package>
</required>
</dependencies>
<phprelease>
<filelist>
<install as="phpmig" name="bin/phpmig"/>
</filelist>
</phprelease>
<changelog>
<release>
<version>
<release>@@VERSION@@</release>
<api>@@VERSION@@</api>
</version>
<stability>
<release>@@STABILITY@@</release>
<api>@@STABILITY@@</api>
</stability>
<date>@@DATE@@</date>
<license>MIT Licence</license>
<notes>
</notes>
</release>
</changelog>
<phprelease>
<filelist>
<install as="phpmig" name="bin/phpmig"/>
</filelist>
</phprelease>
</package>

0 comments on commit 6b1cef8

Please sign in to comment.