Skip to content
This repository has been archived by the owner on Jan 17, 2023. It is now read-only.

Commit

Permalink
Merge pull request #22 from vgarvardt/feature/pg-migrations-table
Browse files Browse the repository at this point in the history
Added support for pg migration table
  • Loading branch information
vgarvardt committed Feb 2, 2022
2 parents 7ecf876 + e747b34 commit 8b0bc34
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 39 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ jobs:
DB_PASSWORD: ''
DB_NAME: '/tmp/db.sqlite'
DB_PORT: ''
# - DB_TYPE: 'Pdo_Pgsql'
# DB_HOST: 'localhost'
# DB_USERNAME: 'test'
# DB_PASSWORD: 'test'
# DB_NAME: 'test'
# DB_PORT: '5432'
- DB_TYPE: 'Pdo_Pgsql'
DB_HOST: 'localhost'
DB_USERNAME: 'test'
DB_PASSWORD: 'test'
DB_NAME: 'test'
DB_PORT: '5432'
- DB_TYPE: 'Pdo_Mysql'
DB_HOST: '127.0.0.1'
DB_USERNAME: 'test'
Expand Down
46 changes: 24 additions & 22 deletions src/ZfSimpleMigrations/Controller/MigrateController.php
Original file line number Diff line number Diff line change
@@ -1,41 +1,40 @@
<?php

namespace ZfSimpleMigrations\Controller;

use ReflectionException;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\Mvc\MvcEvent;
use Zend\Console\Request as ConsoleRequest;
use ZfSimpleMigrations\Library\Migration;
use ZfSimpleMigrations\Library\MigrationException;
use ZfSimpleMigrations\Library\MigrationSkeletonGenerator;
use ZfSimpleMigrations\Library\OutputWriter;

/**
* Migration commands controller
*/
class MigrateController extends AbstractActionController
{
/**
* @var \ZfSimpleMigrations\Library\Migration
*/
/** @var Migration */
protected $migration;
/** @var MigrationSkeletonGenerator */
protected $skeleton_generator;
protected $skeletonGenerator;

/**
* @return MigrationSkeletonGenerator
*/
public function getSkeletonGenerator()
public function getSkeletonGenerator(): MigrationSkeletonGenerator
{
return $this->skeleton_generator;
return $this->skeletonGenerator;
}

/**
* @param MigrationSkeletonGenerator $skeleton_generator
* @param MigrationSkeletonGenerator $skeletonGenerator
* @return self
*/
public function setSkeletonGenerator($skeleton_generator)
public function setSkeletonGenerator(MigrationSkeletonGenerator $skeletonGenerator): self
{
$this->skeleton_generator = $skeleton_generator;
$this->skeletonGenerator = $skeletonGenerator;
return $this;
}

Expand All @@ -53,17 +52,17 @@ public function onDispatch(MvcEvent $e)
*
* @return ConsoleRequest
*/
public function getRequest()
public function getRequest(): ConsoleRequest
{
return parent::getRequest();
}

/**
* Get current migration version
*
* @return int
* @return string
*/
public function versionAction()
public function versionAction(): string
{
return sprintf("Current version %s\n", $this->getMigration()->getCurrentVersion());
}
Expand All @@ -72,8 +71,9 @@ public function versionAction()
* List migrations - not applied by default, all with 'all' flag.
*
* @return string
* @throws ReflectionException
*/
public function listAction()
public function listAction(): string
{
$migrations = $this->getMigration()->getMigrationClasses($this->getRequest()->getParam('all'));
$list = [];
Expand All @@ -85,8 +85,10 @@ public function listAction()

/**
* Apply migration
* @throws ReflectionException
* @throws MigrationException
*/
public function applyAction()
public function applyAction(): string
{
$migrations = $this->getMigration()->getMigrationClasses();
$currentMigrationVersion = $this->getMigration()->getCurrentVersion();
Expand All @@ -95,15 +97,16 @@ public function applyAction()
$force = $this->getRequest()->getParam('force');
$down = $this->getRequest()->getParam('down');
$fake = $this->getRequest()->getParam('fake');
$name = $this->getRequest()->getParam('name');

if (is_null($version) && $force) {
return "Can't force migration apply without migration version explicitly set.";
}
if (is_null($version) && $fake) {
return "Can't fake migration apply without migration version explicitly set.";
}
if (!$force && is_null($version) && $currentMigrationVersion >= $this->getMigration()->getMaxMigrationVersion($migrations)) {

$maxMigrationVersion = $this->getMigration()->getMaxMigrationVersion($migrations);
if (!$force && is_null($version) && $currentMigrationVersion >= $maxMigrationVersion) {
return "No migrations to apply.\n";
}

Expand All @@ -113,8 +116,9 @@ public function applyAction()

/**
* Generate new migration skeleton class
* @throws MigrationException
*/
public function generateSkeletonAction()
public function generateSkeletonAction(): string
{
$classPath = $this->getSkeletonGenerator()->generate();

Expand All @@ -124,7 +128,7 @@ public function generateSkeletonAction()
/**
* @return Migration
*/
public function getMigration()
public function getMigration(): Migration
{
return $this->migration;
}
Expand All @@ -133,11 +137,9 @@ public function getMigration()
* @param Migration $migration
* @return self
*/
public function setMigration(Migration $migration)
public function setMigration(Migration $migration): self
{
$this->migration = $migration;
return $this;
}


}
11 changes: 3 additions & 8 deletions src/ZfSimpleMigrations/Controller/MigrateControllerFactory.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
<?php


namespace ZfSimpleMigrations\Controller;


use Zend\Console\Request;
use Zend\Mvc\Router\Console\RouteMatch;
use Zend\ServiceManager\AbstractPluginManager;
use Zend\ServiceManager\FactoryInterface;
Expand All @@ -14,17 +11,15 @@

class MigrateControllerFactory implements FactoryInterface
{

/**
* Create service
*
* @param ServiceLocatorInterface $serviceLocator
* @return mixed
* @return MigrateController
*/
public function createService(ServiceLocatorInterface $serviceLocator)
public function createService(ServiceLocatorInterface $serviceLocator): MigrateController
{
if($serviceLocator instanceof AbstractPluginManager)
{
if ($serviceLocator instanceof AbstractPluginManager) {
$serviceLocator = $serviceLocator->getServiceLocator();
}

Expand Down
22 changes: 21 additions & 1 deletion src/ZfSimpleMigrations/Library/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ public function __construct(
*/
protected function checkCreateMigrationTable()
{
if ($this->adapter->getPlatform()->getName() === 'PostgreSQL') {
$this->checkCreateMigrationTablePg();
return;
}

$table = new Ddl\CreateTable(MigrationVersion::TABLE_NAME);
$table->addColumn(new Ddl\Column\Integer('id', false, null, ['autoincrement' => true]));
$table->addColumn(new Ddl\Column\BigInteger('version'));
Expand All @@ -109,13 +114,28 @@ protected function checkCreateMigrationTable()
$sql = new Sql($this->adapter);

try {
$this->adapter->query($sql->getSqlStringForSqlObject($table), Adapter::QUERY_MODE_EXECUTE);
$this->adapter->query($sql->buildSqlString($table), Adapter::QUERY_MODE_EXECUTE);
} catch (Exception $e) {
// currently there are no db-independent way to check if table exists
// so we assume that table exists when we catch exception
}
}

/**
* Special case because ZF2 DDL does not support pg serial field
*/
protected function checkCreateMigrationTablePg()
{
$sql = <<<SQL
CREATE TABLE IF NOT EXISTS "%s" (
id SERIAL PRIMARY KEY,
version bigint UNIQUE
);
SQL;

$this->adapter->query(sprintf($sql, MigrationVersion::TABLE_NAME), Adapter::QUERY_MODE_EXECUTE);
}

/**
* @return int
*/
Expand Down
12 changes: 12 additions & 0 deletions src/ZfSimpleMigrations/Model/MigrationVersionTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,22 @@ public function __construct(TableGateway $tableGateway)

public function save($version): int
{
if ($this->tableGateway->getAdapter()->getPlatform()->getName() === 'PostgreSQL') {
return $this->savePg($version);
}

$this->tableGateway->insert(['version' => $version]);
return $this->tableGateway->lastInsertValue;
}

protected function savePg($version): int
{
$sql = sprintf('INSERT INTO "%s" ("version") VALUES (?) RETURNING "id"', $this->tableGateway->getTable());
$stmt = $this->tableGateway->getAdapter()->getDriver()->createStatement($sql);
$result = $stmt->execute([$version]);
return $result->current()["id"];
}

public function delete($version)
{
$this->tableGateway->delete(['version' => $version]);
Expand Down
4 changes: 2 additions & 2 deletions tests/ZfSimpleMigrations/Library/MigrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ protected function setUp()
$metadata = new Metadata($this->adapter);
$tableNames = $metadata->getTableNames();

$drop_if_exists = [
$dropIfExists = [
'test',
MigrationVersion::TABLE_NAME
];
foreach ($drop_if_exists as $table) {
foreach ($dropIfExists as $table) {
if (in_array($table, $tableNames)) {
// ensure db is in expected state
$drop = new DropTable($table);
Expand Down

0 comments on commit 8b0bc34

Please sign in to comment.