Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/Exception/ClickhouseConfigException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,4 @@

use Exception;

final class ClickhouseConfigException extends Exception
{
}
final class ClickhouseConfigException extends Exception {}
12 changes: 4 additions & 8 deletions src/Factory/ClickhouseClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,9 @@

final class ClickhouseClientFactory
{
private array $defaultConfig;

public function __construct(
array $defaultConfig
) {
$this->defaultConfig = $defaultConfig;
}
private array $defaultConfig,
) {}

/**
* Creating a new instance of ClickHouse Client.
Expand All @@ -35,7 +31,7 @@ public function __construct(
* @throws ClickhouseConfigException
*/
public function create(
array $config = []
array $config = [],
): Client {
if (count($config) === 0) {
$config = $this->defaultConfig;
Expand Down Expand Up @@ -65,7 +61,7 @@ public function create(
*/
private function resolveOptionMutatorMethod(
Client $client,
string $option
string $option,
): string {
if (method_exists($client, $option)) {
return $option;
Expand Down
2 changes: 1 addition & 1 deletion src/Migration/AbstractClickhouseMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ abstract class AbstractClickhouseMigration

public function __construct(
?Client $clickhouseClient = null,
?string $databaseName = null
?string $databaseName = null,
) {
$this->clickhouseClient = $clickhouseClient ?? app(Client::class);
$this->databaseName = $databaseName ?? config('clickhouse.connection.options.database');
Expand Down
16 changes: 5 additions & 11 deletions src/Migration/MigrationCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,14 @@

class MigrationCreator
{
private Filesystem $filesystem;
private ?string $migrationStubFilePath;

public function __construct(
Filesystem $filesystem,
?string $migrationStubFilePath
) {
$this->filesystem = $filesystem;
$this->migrationStubFilePath = $migrationStubFilePath;
}
private Filesystem $filesystem,
private ?string $migrationStubFilePath,
) {}

public function create(
string $fileName,
string $migrationsDirectoryPath
string $migrationsDirectoryPath,
): ?string {
$stubFileContent = $this->getStubFileContent();

Expand All @@ -45,7 +39,7 @@ public function create(

private function generateMigrationFilePath(
string $name,
string $migrationsDirectoryPath
string $migrationsDirectoryPath,
): string {
return $migrationsDirectoryPath . '/' . $this->getDatePrefix() . '_' . $name . '.php';
}
Expand Down
72 changes: 33 additions & 39 deletions src/Migration/MigrationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,10 @@

final class MigrationRepository
{
private Client $client;
private string $table;

public function __construct(
Client $client,
string $table
) {
$this->client = $client;
$this->table = $table;
}
private Client $client,
private string $table,
) {}

/**
* Creating a new table to store migrations.
Expand All @@ -36,14 +30,14 @@ public function createMigrationRegistryTable(): Statement
{
return $this->client->write(
<<<SQL
CREATE TABLE IF NOT EXISTS {table} (
migration String,
batch UInt32,
applied_at DateTime DEFAULT NOW()
)
ENGINE = ReplacingMergeTree()
ORDER BY migration
SQL,
CREATE TABLE IF NOT EXISTS {table} (
migration String,
batch UInt32,
applied_at DateTime DEFAULT NOW()
)
ENGINE = ReplacingMergeTree()
ORDER BY migration
SQL,
[
'table' => $this->table,
],
Expand All @@ -57,9 +51,9 @@ public function all(): array
{
$rows = $this->client->select(
<<<SQL
SELECT migration
FROM {table}
SQL,
SELECT migration
FROM {table}
SQL,
[
'table' => $this->table,
],
Expand All @@ -77,10 +71,10 @@ public function latest(): array
{
$rows = $this->client->select(
<<<SQL
SELECT migration
FROM {table}
ORDER BY batch DESC, migration DESC
SQL,
SELECT migration
FROM {table}
ORDER BY batch DESC, migration DESC
SQL,
[
'table' => $this->table,
],
Expand All @@ -99,9 +93,9 @@ public function getLastBatchNumber(): int
return $this->client
->select(
<<<SQL
SELECT MAX(batch) AS batch
FROM {table}
SQL,
SELECT MAX(batch) AS batch
FROM {table}
SQL,
[
'table' => $this->table,
],
Expand All @@ -111,7 +105,7 @@ public function getLastBatchNumber(): int

public function add(
string $migration,
int $batch
int $batch,
): Statement {
return $this->client->insert(
$this->table,
Expand All @@ -124,9 +118,9 @@ public function total(): int
{
return (int)$this->client->select(
<<<SQL
SELECT COUNT(*) AS count
FROM {table}
SQL,
SELECT COUNT(*) AS count
FROM {table}
SQL,
[
'table' => $this->table,
],
Expand All @@ -137,8 +131,8 @@ public function exists(): bool
{
return (bool)$this->client->select(
<<<SQL
EXISTS TABLE {table}
SQL,
EXISTS TABLE {table}
SQL,
[
'table' => $this->table,
],
Expand All @@ -150,15 +144,15 @@ public function exists(): bool
* @return array|null
*/
public function find(
string $migration
string $migration,
): ?array {
return $this->client->select(
<<<SQL
SELECT *
FROM {table}
WHERE migration = :migration
LIMIT 1
SQL,
SELECT *
FROM {table}
WHERE migration = :migration
LIMIT 1
SQL,
[
'table' => $this->table,
'migration' => $migration,
Expand Down
40 changes: 15 additions & 25 deletions src/Migration/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,30 @@
use ClickHouseDB\Client;
use DomainException;
use Generator;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Support\Str;
use Illuminate\Console\OutputStyle;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Str;
use ReflectionClass;
use Symfony\Component\Finder\SplFileInfo;

use function in_array;

final class Migrator
{
private Client $client;
private MigrationRepository $repository;
private Filesystem $filesystem;

public function __construct(
Client $client,
MigrationRepository $repository,
Filesystem $files
) {
$this->client = $client;
$this->filesystem = $files;
$this->repository = $repository;
}
private Client $client,
private MigrationRepository $repository,
private Filesystem $filesystem,
) {}

/**
* @throws FileNotFoundException
*/
public function runUp(
string $migrationsDirectoryPath,
OutputStyle $output,
int $step
int $step,
): void {
$migrations = $this->getMigrationsUp($migrationsDirectoryPath);

Expand Down Expand Up @@ -93,7 +85,7 @@ public function ensureTableExists(): self
}

private function getMigrationsUp(
string $migrationsDirectoryPath
string $migrationsDirectoryPath,
): Generator {
$migrationFiles = $this->getUnAppliedMigrationFiles($migrationsDirectoryPath);

Expand All @@ -103,7 +95,7 @@ private function getMigrationsUp(
}

private function getMigrationName(
string $migrationFilePath
string $migrationFilePath,
): string {
return str_replace('.php', '', basename($migrationFilePath));
}
Expand All @@ -112,23 +104,21 @@ private function getMigrationName(
* @return list<SplFileInfo>
*/
private function getUnAppliedMigrationFiles(
string $migrationsDirectoryPath
string $migrationsDirectoryPath,
): array {
$migrationFiles = $this->filesystem->files($migrationsDirectoryPath);

return collect($migrationFiles)
->reject(
fn(SplFileInfo $migrationFile) => $this->isAppliedMigration(
$migrationFile->getFilename(),
),
fn(SplFileInfo $migrationFile) => $this->isAppliedMigration($migrationFile->getFilename()),
)->all();
}

/**
* @throws FileNotFoundException
*/
private function resolveMigrationInstance(
string $path
string $path,
): object {
$class = $this->generateMigrationClassName($this->getMigrationName($path));

Expand All @@ -142,15 +132,15 @@ private function resolveMigrationInstance(
}

private function generateMigrationClassName(
string $migrationName
string $migrationName,
): string {
return Str::studly(
implode('_', array_slice(explode('_', $migrationName), 4)),
);
}

private function resolveMigrationNameFromInstance(
object $migration
object $migration,
): string {
$reflectionClass = new ReflectionClass($migration);

Expand All @@ -162,7 +152,7 @@ private function resolveMigrationNameFromInstance(
}

private function isAppliedMigration(
string $fileName
string $fileName,
): bool {
return in_array(
$this->getMigrationName($fileName),
Expand Down
4 changes: 2 additions & 2 deletions stubs/clickhouse-migration.stub
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ return new class extends AbstractClickhouseMigration
public function up(): void
{
$this->clickhouseClient->write(
<<<SQL
<<<'SQL'

SQL
SQL
);
}
};
2 changes: 1 addition & 1 deletion tests/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ abstract class AbstractTestCase extends OrchestraTestCase
* {@inheritdoc}
*/
protected function getPackageProviders(
$app
$app,
): array {
return [
ClickhouseServiceProvider::class,
Expand Down
4 changes: 2 additions & 2 deletions tests/Factory/ClickhouseClientFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function testInitializationWithMainConfig(): void
'timeout' => 150,
'connectTimeOut' => 151,
],
]
],
);

$client = $clickhouse->create();
Expand Down Expand Up @@ -61,7 +61,7 @@ public function testInitializationWithNonExistsOption(): void
'connectTimeOut' => 151,
'nonExistsOption' => 'value',
],
]
],
);

try {
Expand Down