Code Monkey home page Code Monkey logo

zfsimplemigrations's Introduction

ZfSimpleMigrations

Simple Migrations for Zend Framework 2. Project originally based on ZendDbMigrations but module author did not response for issues and pull-requests so fork became independent project.

Supported Drivers

The following DB adapter drivers are supported by this module.

  • Pdo_Sqlite
  • Pdo_Mysql
  • Pdo_Pgsql
  • Mysqli only if you configure the driver options with 'buffer_results' => true

Installation

Using composer

php composer.phar require vgarvardt/zf-simple-migrations:dev-master
php composer.phar update

add ZfSimpleMigrations to the modules array in application.config.php

Usage

Available commands

  • migration version [<name>] - show last applied migration (name specifies a configured migration)
  • migration list [<name>] [--all] - list available migrations (all includes applied migrations)
  • migration apply [<name>] [<version>] [--force] [--down] [--fake] - apply or rollback migration
  • migration generate [<name>] - generate migration skeleton class

Migration classes are stored in /path/to/project/migrations/ dir by default.

Generic migration class has name Version<YmdHis> and implement ZfSimpleMigrations\Library\MigrationInterface.

Migration class example

<?php

namespace ZfSimpleMigrations\Migrations;

use ZfSimpleMigrations\Library\AbstractMigration;
use Zend\Db\Metadata\MetadataInterface;

class Version20130403165433 extends AbstractMigration
{
    public static $description = "Migration description";

    public function up(MetadataInterface $schema)
    {
        //$this->addSql(/*Sql instruction*/);
    }

    public function down(MetadataInterface $schema)
    {
        //$this->addSql(/*Sql instruction*/);
    }
}

Multi-statement sql

While this module supports execution of multiple SQL statements it does not have way to detect if any other statement than the first contained an error. It is highly recommended you only provide single SQL statements to addSql at a time. I.e instead of

$this->addSql('SELECT NOW(); SELECT NOW(); SELECT NOW();');

You should use

$this->addSql('SELECT NOW();');
$this->addSql('SELECT NOW();');
$this->addSql('SELECT NOW();');

Accessing ServiceLocator In Migration Class

By implementing the Zend\ServiceManager\ServiceLocatorAwareInterface in your migration class you get access to the ServiceLocator used in the application.

<?php

namespace ZfSimpleMigrations\Migrations;

use ZfSimpleMigrations\Library\AbstractMigration;
use Zend\Db\Metadata\MetadataInterface;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorAwareTrait;

class Version20130403165433 extends AbstractMigration
                            implements ServiceLocatorAwareInterface
{
    use ServiceLocatorAwareTrait;

    public static $description = "Migration description";

    public function up(MetadataInterface $schema)
    {
         //$this->getServiceLocator()->get(/*Get service by alias*/);
         //$this->addSql(/*Sql instruction*/);

    }

    public function down(MetadataInterface $schema)
    {
        //$this->getServiceLocator()->get(/*Get service by alias*/);
        //$this->addSql(/*Sql instruction*/);
    }
}

Accessing Zend Db Adapter In Migration Class

By implementing the Zend\Db\Adapter\AdapterAwareInterface in your migration class you get access to the Db Adapter configured for the migration.

<?php

namespace ZfSimpleMigrations\Migrations;

use Zend\Db\Adapter\AdapterAwareInterface;
use Zend\Db\Adapter\AdapterAwareTrait;
use Zend\Db\Sql\Ddl\Column\Integer;
use Zend\Db\Sql\Ddl\Column\Varchar;
use Zend\Db\Sql\Ddl\Constraint\PrimaryKey;
use Zend\Db\Sql\Ddl\CreateTable;
use Zend\Db\Sql\Ddl\DropTable;
use ZfSimpleMigrations\Library\AbstractMigration;
use Zend\Db\Metadata\MetadataInterface;

class Version20150524162247 extends AbstractMigration implements AdapterAwareInterface
{
    use AdapterAwareTrait;

    public static $description = "Migration description";

    public function up(MetadataInterface $schema)
    {
        $table = new CreateTable('my_table');
        $table->addColumn(new Integer('id', false));
        $table->addConstraint(new PrimaryKey('id'));
        $table->addColumn(new Varchar('my_column', 64));
        $this->addSql($table->getSqlString($this->adapter->getPlatform()));
    }

    public function down(MetadataInterface $schema)
    {
        $drop = new DropTable('my_table');
        $this->addSql($drop->getSqlString($this->adapter->getPlatform()));
    }
}

Configuration

User Configuration

The top-level key used to configure this module is migrations.

Migration Configurations: Migrations Name

Each key under migrations is a migrations configuration, and the value is an array with one or more of the following keys.

Sub-key: dir

The path to the directory where migration files are stored. Defaults to ./migrations in the project root dir.

Sub-key: namespace

The class namespace that migration classes will be generated with. Defaults to ZfSimpleMigrations\Migrations.

Sub-key: show_log (optional)

Flag to log output of the migration. Defaults to true.

Sub-key: adapter (optional)

The service alias that will be used to fetch a Zend\Db\Adapter\Adapter from the service manager.

User configuration example

'migrations' => array(
    'default' => array(
            'dir' => dirname(__FILE__) . '/../../../../migrations-app',
            'namespace' => 'App\Migrations',    
    ),
    'albums' => array(
            'dir' => dirname(__FILE__) . '/../../../../migrations-albums',
            'namespace' => 'Albums\Migrations',
            'adapter' => 'AlbumDb'    
    ),
),

zfsimplemigrations's People

Contributors

codeliner avatar jeremygiberson avatar kondrashin-roman avatar rud5g avatar vgarvardt avatar xiaklizrum avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

zfsimplemigrations's Issues

postgresql issue

Thie module cannot be used with a postgresql database.

The CreateTable instance in the Migration.php starting at line 68 generates an invalid create table statement for postgresql (tested with 9.1).

First, the UniqueKey constraint is invalid. It should be "constraint some_name unique (column_name), but it is "contraint unique (column_name)". If the second param "name" is supplied, it generates "contraint unique some_name (column_name)" (still invalid). =(

Secondly, the id field should be of the type "serial" instead of "integer". Autoincrement does not work on the current way.

Greetings from Bochum
Niklas

(sorry for my english)

How to use?

can u please let us know how to use this module?

where shoud i use that cli command : migration

should i add this module to module list ?

Issue: Deprecated ServiceLocatorAwareInterface

Issue while creating a new migration.

This occurs after updating zendframework/zend-mvc to the latest version: 2.7.1

$ php public/index.php migration generate

PHP Deprecated: ServiceLocatorAwareInterface is deprecated and will be removed in version 3.0, along with the ServiceLocatorAwareInitializer. Please update your class ZfSimpleMigrations\Library\Migration to remove the implementation, and start injecting your dependencies via factory instead. in ./vendor/zendframework/zend-mvc/src/Service/ServiceManagerConfig.php on line 147

Deprecated: ServiceLocatorAwareInterface is deprecated and will be removed in version 3.0, along with the ServiceLocatorAwareInitializer. Please update your class ZfSimpleMigrations\Library\Migration to remove the implementation, and start injecting your dependencies via factory instead. in ./vendor/zendframework/zend-mvc/src/Service/ServiceManagerConfig.php on line 147
Generated skeleton class @ ./migrations/Version20160306221810.php

migration command?

How come this is even working, where there is no bash script or anything, that enables migration command to even work?

Using outside of ZF2 Application

I have a library I have created using components from the ZF2 library while not using a skeleton web application. I have an entry point which uses classes which depend on ZF2 components like Zend\Http\Client, etc so i am not using the application configs or modules. I was wondering if there was a way to use your module without actually being a module in an application.

getting Exception "cannot create source from adapter" when running migration version

[zxurian@ariel ~]$ dev.hub migration version
======================================================================
   The application has thrown an exception!
======================================================================
 Exception
 cannot create source from adapter
----------------------------------------------------------------------
/var/www/dev.hub/vendor/zendframework/zendframework/library/Zend/Db/Metadata/Metadata.php:60
#0 /var/www/dev.hub/vendor/zendframework/zendframework/library/Zend/Db/Metadata/Metadata.php(36): Zend\Db\Metadata\Metadata->createSourceFromAdapter(Object(Zend\Db\Adapter\Adapter))
#1 /var/www/dev.hub/vendor/vgarvardt/zf-simple-migrations/src/ZfSimpleMigrations/Library/Migration.php(48): Zend\Db\Metadata\Metadata->__construct(Object(Zend\Db\Adapter\Adapter))
#2 /var/www/dev.hub/vendor/vgarvardt/zf-simple-migrations/src/ZfSimpleMigrations/Controller/MigrateController.php(132): ZfSimpleMigrations\Library\Migration->__construct(Object(Zend\Db\Adapter\Adapter), Array, Object(ZfSimpleMigrations\Model\MigrationVersionTable), Object(ZfSimpleMigrations\Library\OutputWriter))
#3 /var/www/dev.hub/vendor/vgarvardt/zf-simple-migrations/src/ZfSimpleMigrations/Controller/MigrateController.php(48): ZfSimpleMigrations\Controller\MigrateController->getMigration()
#4 /var/www/dev.hub/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractActionController.php(82): ZfSimpleMigrations\Controller\MigrateController->versionAction()
#5 /var/www/dev.hub/vendor/vgarvardt/zf-simple-migrations/src/ZfSimpleMigrations/Controller/MigrateController.php(28): Zend\Mvc\Controller\AbstractActionController->onDispatch(Object(Zend\Mvc\MvcEvent))
#6 [internal function]: ZfSimpleMigrations\Controller\MigrateController->onDispatch(Object(Zend\Mvc\MvcEvent))
#7 /var/www/dev.hub/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(444): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#8 /var/www/dev.hub/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(205): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#9 /var/www/dev.hub/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractController.php(118): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#10 /var/www/dev.hub/vendor/zendframework/zendframework/library/Zend/Mvc/DispatchListener.php(93): Zend\Mvc\Controller\AbstractController->dispatch(Object(Zend\Console\Request), Object(Zend\Console\Response))
#11 [internal function]: Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent))
#12 /var/www/dev.hub/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(444): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#13 /var/www/dev.hub/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(205): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#14 /var/www/dev.hub/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php(314): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#15 /var/www/dev.hub/html/index.php(17): Zend\Mvc\Application->run()
#16 {main}
======================================================================

Class ZfSimpleMigrations\Library\MigrationAbstractFactory contains 2 abstract methods

I have just installed this library and I am getting this issue. Please check below full details:

 Fatal error: Class ZfSimpleMigrations\Library\MigrationAbstractFactory contains 2 abstract methods and must therefore be declared abstract or implement the remaining methods (Zend\ServiceManager\Factory\AbstractFactoryInterface::canCreate, Zend\ServiceManager\Factory\FactoryInterface::__invoke) in /var/www/clients/vendor/vgarvardt/zf-simple-migrations/src/ZfSimpleMigrations/Library/MigrationAbstractFactory.php on line 12

Any I deas on how I can fix it?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.