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

Commit 4d8355a

Browse files
authored
Ignore readonly connections (#28)
1 parent aa7235c commit 4d8355a

File tree

3 files changed

+126
-6
lines changed

3 files changed

+126
-6
lines changed

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
],
1212
"require": {
1313
"php": ">=7.2",
14-
"spiral/core": "^2.7",
15-
"spiral/database": "^2.7",
16-
"spiral/files": "^2.7",
17-
"spiral/tokenizer": "^2.7",
18-
"spiral/reactor": "^2.7"
14+
"spiral/core": "^2.8",
15+
"spiral/database": "^2.9",
16+
"spiral/files": "^2.8",
17+
"spiral/tokenizer": "^2.8",
18+
"spiral/reactor": "^2.8"
1919
},
2020
"autoload": {
2121
"psr-4": {

src/Migrator.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Spiral\Migrations;
1313

1414
use Spiral\Database\Database;
15+
use Spiral\Database\DatabaseInterface;
1516
use Spiral\Database\DatabaseManager;
1617
use Spiral\Database\Table;
1718
use Spiral\Migrations\Config\MigrationConfig;
@@ -112,14 +113,26 @@ private function getDatabases(): array
112113
foreach ($this->repository->getMigrations() as $migration) {
113114
$database = $this->dbal->database($migration->getDatabase());
114115

115-
if (! isset($result[$database->getName()])) {
116+
if (!$this->isReadonly($database) && !isset($result[$database->getName()])) {
116117
$result[$database->getName()] = $database;
117118
}
118119
}
119120

120121
return $result;
121122
}
122123

124+
/**
125+
* Returns {@see true} in case that connection is readonly
126+
* or {@see false} instead.
127+
*
128+
* @param DatabaseInterface $db
129+
* @return bool
130+
*/
131+
private function isReadonly(DatabaseInterface $db): bool
132+
{
133+
return $db->getDriver()->isReadonly();
134+
}
135+
123136
/**
124137
* Create migration table inside given database
125138
*
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
/**
4+
* Spiral Framework.
5+
*
6+
* @license MIT
7+
* @author Anton Titov (Wolfy-J)
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace Spiral\Migrations\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Spiral\Core\Container;
16+
use Spiral\Database\Config\DatabaseConfig;
17+
use Spiral\Database\DatabaseManager;
18+
use Spiral\Database\Driver\SQLite\SQLiteDriver;
19+
use Spiral\Files\Files;
20+
use Spiral\Migrations\Config\MigrationConfig;
21+
use Spiral\Migrations\FileRepository;
22+
use Spiral\Migrations\Migrator;
23+
24+
class MigrationsDatabaseTest extends TestCase
25+
{
26+
public const DRIVER = 'sqlite';
27+
28+
public function testSkipReadonly(): void
29+
{
30+
$this->generateMigration('20200909.024119_333_333_migration_1.php', 'A3');
31+
32+
// Standard behavior
33+
$this->assertFalse($this->migrator(false)->isConfigured());
34+
35+
// Ignore created migrations in case the connection is readonly
36+
$this->assertTrue($this->migrator(true)->isConfigured());
37+
}
38+
39+
private function generateMigration(string $file, string $class): string
40+
{
41+
$out = __DIR__ . '/../files/' . $file;
42+
43+
file_put_contents($out, sprintf(file_get_contents(__DIR__ . '/../files/migration.stub'), $class));
44+
45+
return $out;
46+
}
47+
48+
/**
49+
* @param bool $readonly
50+
* @return Migrator
51+
*/
52+
private function migrator(bool $readonly): Migrator
53+
{
54+
$config = new MigrationConfig([
55+
'directory' => __DIR__ . '/../files/',
56+
'table' => 'migrations',
57+
'safe' => true,
58+
]);
59+
60+
return new Migrator(
61+
$config,
62+
$this->dbal($readonly),
63+
new FileRepository($config, new Container())
64+
);
65+
}
66+
67+
/**
68+
* @param bool $readonly
69+
* @return DatabaseManager
70+
*/
71+
private function dbal(bool $readonly): DatabaseManager
72+
{
73+
return new DatabaseManager(
74+
new DatabaseConfig([
75+
'default' => 'default',
76+
'databases' => [
77+
'default' => ['driver' => 'test'],
78+
],
79+
'drivers' => [
80+
'test' => [
81+
'driver' => SQLiteDriver::class,
82+
'options' => [
83+
'connection' => 'sqlite::memory:',
84+
'readonly' => $readonly,
85+
'username' => 'sqlite',
86+
'password' => '',
87+
],
88+
],
89+
],
90+
])
91+
);
92+
}
93+
94+
/**
95+
* @return void
96+
*/
97+
public function tearDown(): void
98+
{
99+
$files = new Files();
100+
foreach ($files->getFiles(__DIR__ . '/../files/', '*.php') as $file) {
101+
$files->delete($file);
102+
clearstatcache(true, $file);
103+
}
104+
105+
parent::tearDown();
106+
}
107+
}

0 commit comments

Comments
 (0)