davedevelopment / phpmig

Simple migrations system for php
Other
568 stars 92 forks source link

Add pdo_sqlsrv support #119

Closed kuwa72 closed 7 years ago

kuwa72 commented 7 years ago

I need this function to my job.

Test result.

ubuntu@ubuntu-xenial:/vagrant/sanrio_sys/web$ vendor/bin/phpmig -vvv status

 Status   Migration ID    Migration Name
-----------------------------------------
     up  20161206113249  CraeteTable1

ubuntu@ubuntu-xenial:/vagrant/sanrio_sys/web$ vendor/bin/phpmig -vvv status

 Status   Migration ID    Migration Name
-----------------------------------------
   down  20161206113249  CraeteTable1

ubuntu@ubuntu-xenial:/vagrant/web$ vendor/bin/phpmig -vvv migrate
 == 20161206113249 CraeteTable1 migrating
 == 20161206113249 CraeteTable1 migrated 0.0989s
ubuntu@ubuntu-xenial:/vagrant/web$ vendor/bin/phpmig -vvv status

 Status   Migration ID    Migration Name
-----------------------------------------
     up  20161206113249  CraeteTable1

ubuntu@ubuntu-xenial:/vagrant/web$ vendor/bin/phpmig -vvv rollback
 == 20161206113249 CraeteTable1 reverting
 == 20161206113249 CraeteTable1 reverted 0.0935s
ubuntu@ubuntu-xenial:/vagrant/web$ vendor/bin/phpmig -vvv status

 Status   Migration ID    Migration Name
-----------------------------------------
   down  20161206113249  CraeteTable1

Sample phpmig.php (Pimple version bumped to 3.x)

<?php

# phpmig.php

use \Phpmig\Adapter;
use Pimple\Container;

$container = new Container();

$container['db'] = function() {
    $dbh = new PDO("sqlsrv:server = tcp:foo-bar-baz.database.windows.net,1433; Database = foo-install-test", "accountname", "accountpassword");
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        return $dbh;
};

$container['phpmig.adapter'] = new Adapter\PDO\Sql($container['db'], 'migrations');

$container['phpmig.migrations_path'] = __DIR__ . DIRECTORY_SEPARATOR . 'migrations';
$container['phpmig.migrations_template_path'] = __DIR__ . DIRECTORY_SEPARATOR . 'phpmig_template.php';

return $container;

Sample migration file

<?php
use Phpmig\Migration\Migration;
use Store\Migration\Helper;

class CraeteTable1 extends Migration
{
    /**
     * Do the migration
     */
    public function up()
    {

        $sql = <<<EOS
create table hogehoge (
  id int identity(1,1) not null,
  name nvarchar(1024) not null)
EOS;

        // Get PDO instance from container
        $container = $this->getContainer();
        $con = $container['db'];

        $con->beginTransaction();
        $con->exec($sql);
        $con->commit();
    }

    /**
     * Undo the migration
     */
    public function down()
    {

        $sql = <<<EOS
drop table hogehoge
EOS;

        // Get PDO instance from container
        $container = $this->getContainer();
        $con = $container['db'];

        $con->beginTransaction();
        $con->exec($sql);
        $con->commit();
    }
}
davedevelopment commented 7 years ago

Thanks!