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

Commit

Permalink
Ignore readonly connections (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
SerafimArts committed Sep 20, 2021
1 parent aa7235c commit 4d8355a
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 6 deletions.
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
],
"require": {
"php": ">=7.2",
"spiral/core": "^2.7",
"spiral/database": "^2.7",
"spiral/files": "^2.7",
"spiral/tokenizer": "^2.7",
"spiral/reactor": "^2.7"
"spiral/core": "^2.8",
"spiral/database": "^2.9",
"spiral/files": "^2.8",
"spiral/tokenizer": "^2.8",
"spiral/reactor": "^2.8"
},
"autoload": {
"psr-4": {
Expand Down
15 changes: 14 additions & 1 deletion src/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Spiral\Migrations;

use Spiral\Database\Database;
use Spiral\Database\DatabaseInterface;
use Spiral\Database\DatabaseManager;
use Spiral\Database\Table;
use Spiral\Migrations\Config\MigrationConfig;
Expand Down Expand Up @@ -112,14 +113,26 @@ private function getDatabases(): array
foreach ($this->repository->getMigrations() as $migration) {
$database = $this->dbal->database($migration->getDatabase());

if (! isset($result[$database->getName()])) {
if (!$this->isReadonly($database) && !isset($result[$database->getName()])) {
$result[$database->getName()] = $database;
}
}

return $result;
}

/**
* Returns {@see true} in case that connection is readonly
* or {@see false} instead.
*
* @param DatabaseInterface $db
* @return bool
*/
private function isReadonly(DatabaseInterface $db): bool
{
return $db->getDriver()->isReadonly();
}

/**
* Create migration table inside given database
*
Expand Down
107 changes: 107 additions & 0 deletions tests/Migrations/MigrationsDatabaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Migrations\Tests;

use PHPUnit\Framework\TestCase;
use Spiral\Core\Container;
use Spiral\Database\Config\DatabaseConfig;
use Spiral\Database\DatabaseManager;
use Spiral\Database\Driver\SQLite\SQLiteDriver;
use Spiral\Files\Files;
use Spiral\Migrations\Config\MigrationConfig;
use Spiral\Migrations\FileRepository;
use Spiral\Migrations\Migrator;

class MigrationsDatabaseTest extends TestCase
{
public const DRIVER = 'sqlite';

public function testSkipReadonly(): void
{
$this->generateMigration('20200909.024119_333_333_migration_1.php', 'A3');

// Standard behavior
$this->assertFalse($this->migrator(false)->isConfigured());

// Ignore created migrations in case the connection is readonly
$this->assertTrue($this->migrator(true)->isConfigured());
}

private function generateMigration(string $file, string $class): string
{
$out = __DIR__ . '/../files/' . $file;

file_put_contents($out, sprintf(file_get_contents(__DIR__ . '/../files/migration.stub'), $class));

return $out;
}

/**
* @param bool $readonly
* @return Migrator
*/
private function migrator(bool $readonly): Migrator
{
$config = new MigrationConfig([
'directory' => __DIR__ . '/../files/',
'table' => 'migrations',
'safe' => true,
]);

return new Migrator(
$config,
$this->dbal($readonly),
new FileRepository($config, new Container())
);
}

/**
* @param bool $readonly
* @return DatabaseManager
*/
private function dbal(bool $readonly): DatabaseManager
{
return new DatabaseManager(
new DatabaseConfig([
'default' => 'default',
'databases' => [
'default' => ['driver' => 'test'],
],
'drivers' => [
'test' => [
'driver' => SQLiteDriver::class,
'options' => [
'connection' => 'sqlite::memory:',
'readonly' => $readonly,
'username' => 'sqlite',
'password' => '',
],
],
],
])
);
}

/**
* @return void
*/
public function tearDown(): void
{
$files = new Files();
foreach ($files->getFiles(__DIR__ . '/../files/', '*.php') as $file) {
$files->delete($file);
clearstatcache(true, $file);
}

parent::tearDown();
}
}

0 comments on commit 4d8355a

Please sign in to comment.