Skip to content

Custom builder for migrations #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: new-builder
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/ci-mssql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
extensions: pdo, pdo_sqlsrv
mssql: 'server:2019-latest'
- php: '8.1'
extensions: pdo, pdo_sqlsrv-5.10.0beta2
extensions: pdo, pdo_sqlsrv
mssql: 'server:2019-latest'

services:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
php-version: ${{ matrix.php-versions }}
coverage: pcov
tools: pecl
extensions: mbstring, pdo, pdo_sqlite, pdo_pgsql, pdo_sqlsrv-5.10.0beta2, pdo_mysql
extensions: mbstring, pdo, pdo_sqlite, pdo_pgsql, pdo_sqlsrv, pdo_mysql
- name: Get Composer Cache Directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "MIT",
"require": {
"php": ">=8.0",
"cycle/database": "^2.0",
"cycle/database": "^2.3",
"spiral/core": "^2.7",
"spiral/files": "^2.7",
"spiral/tokenizer": "^2.7",
Expand Down
10 changes: 3 additions & 7 deletions src/FileRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function getMigrations(): array
$migrations[] = $migration->withState(new State($f['name'], $f['created']));
}

\array_multisort($timestamps, $chunks, SORT_ASC | SORT_NATURAL, $migrations);
\array_multisort($timestamps, $chunks, SORT_NATURAL, $migrations);

return $migrations;
}
Expand Down Expand Up @@ -136,7 +136,7 @@ private function getFiles(string $directory): \Generator
{
foreach ($this->files->getFiles($directory, '*.php') as $filename) {
$reflection = new ReflectionFile($filename);
$definition = \explode('_', \basename($filename));
$definition = \explode('_', \basename($filename, '.php'), 3);

if (\count($definition) < 3) {
throw new RepositoryException("Invalid migration filename '{$filename}'");
Expand All @@ -152,11 +152,7 @@ private function getFiles(string $directory): \Generator
'class' => $reflection->getClasses()[0],
'created' => $created,
'chunk' => $definition[1],
'name' => \str_replace(
'.php',
'',
\implode('_', \array_slice($definition, 2))
),
'name' => $definition[2],
];
}
}
Expand Down
14 changes: 10 additions & 4 deletions src/Operation/Column/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,27 @@ protected function declareColumn(AbstractTable $schema): AbstractColumn
//Type configuring
if (method_exists($column, $this->type)) {
$arguments = [];
$variadic = false;

$method = new \ReflectionMethod($column, $this->type);
foreach ($method->getParameters() as $parameter) {
if ($this->hasOption($parameter->getName())) {
$arguments[] = $this->getOption($parameter->getName());
$arguments[$parameter->getName()] = $this->getOption($parameter->getName());
} elseif (!$parameter->isOptional()) {
throw new ColumnException(
"Option '{$parameter->getName()}' are required to define column with type '{$this->type}'"
);
} else {
$arguments[] = $parameter->getDefaultValue();
} elseif ($parameter->isDefaultValueAvailable()) {
$arguments[$parameter->getName()] = $parameter->getDefaultValue();
} elseif ($parameter->isVariadic()) {
$variadic = true;
}
}

call_user_func_array([$column, $this->type], $arguments);
\call_user_func_array(
[$column, $this->type],
$variadic ? $arguments + $this->options + $column->getAttributes() : $arguments,
);
} else {
$column->type($this->type);
}
Expand Down
32 changes: 32 additions & 0 deletions src/Operation/Table/Truncate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Cycle\Migrations\Operation\Table;

use Cycle\Migrations\CapsuleInterface;
use Cycle\Migrations\Exception\Operation\TableException;
use Cycle\Migrations\Operation\AbstractOperation;

final class Truncate extends AbstractOperation
{
public function __construct(string $table, private string $strategy)
{
parent::__construct($table);
}

/**
* {@inheritdoc}
*/
public function execute(CapsuleInterface $capsule): void
{
$schema = $capsule->getSchema($this->getTable());
$database = $this->database ?? '[default]';

if (!$schema->exists()) {
throw new TableException(
"Unable to truncate table '{$database}'.'{$this->getTable()}', table does not exists"
);
}

$capsule->getDatabase()->execute(sprintf('TRUNCATE "%s" %s', $this->getTable(), $this->strategy));
}
}
62 changes: 62 additions & 0 deletions src/V2/Column.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace Cycle\Migrations\V2;

class Column
{
protected string $type;
protected ?int $length = null;
protected bool $isUnique = false;
protected ?string $default = null;
protected bool $isNotNull = false;
protected ?string $check = null;
protected ?string $comment = null;

public function __construct(string $type, ?int $length = null)
{
$this->type = $type;
$this->length = $length;
}

public function notNull(): self
{
$this->isNotNull = true;
return $this;
}

public function null(): self
{
$this->isNotNull = false;
return $this;
}

public function unique(): self
{
$this->isUnique = true;
return $this;
}

public function check($check): self
{
$this->check = $check;
return $this;
}

public function defaultValue(?string $default): self
{
if ($default === null) {
$this->null();
}

$this->default = $default;
return $this;
}

public function comment(?string $comment): self
{
$this->comment = $comment;
return $this;
}
}
63 changes: 63 additions & 0 deletions src/V2/ColumnParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace Cycle\Migrations\V2;

class ColumnParser extends Column
{
private Column $column;

public function __construct(Column $column)
{
$this->column = $column;
}

public function getType(): string
{
return $this->column->type;
}

public function getLength(): ?int
{
return $this->column->length;
}

public function isUnique(): bool
{
return $this->column->isUnique;
}

public function getDefault(): ?string
{
return $this->column->default;
}

public function isNotNull(): bool
{
return $this->column->isNotNull;
}

public function getCheck(): ?string
{
return $this->column->check;
}

public function getComment(): ?string
{
return $this->column->comment;
}

public function getOptions(): array
{
$options = [];
$options['unique'] = $this->isUnique();
$options['nullable'] = !$this->isNotNull();

if ($this->getDefault() !== null) {
$options['default'] = $this->getDefault();
}

return $options;
}
}
79 changes: 79 additions & 0 deletions src/V2/ColumnTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

namespace Cycle\Migrations\V2;

trait ColumnTrait
{
protected function primaryKey($length = null): Column
{
$column = new Column(ColumnType::TYPE_PK, $length);
$column->notNull();

return $column;
}

protected function bigPrimaryKey($length = null): Column
{
$column = new Column(ColumnType::TYPE_BIGPK, $length);
$column->notNull();

return $column;
}

protected function string($length = null): Column
{
return new Column(ColumnType::TYPE_STRING, $length);
}

protected function text(): Column
{
return new Column(ColumnType::TYPE_TEXT);
}

protected function integer($length = null): Column
{
return new Column(ColumnType::TYPE_INTEGER, $length);
}

protected function bigInteger($length = null): Column
{
return new Column(ColumnType::TYPE_BIGINT, $length);
}

protected function numeric($precision = null): Column
{
return new Column(ColumnType::TYPE_NUMERIC, $precision);
}

protected function dateTime(): Column
{
return new Column(ColumnType::TYPE_DATETIME);
}

protected function boolean(): Column
{
return new Column(ColumnType::TYPE_BOOLEAN);
}

protected function money(): Column
{
return new Column(ColumnType::TYPE_MONEY);
}

protected function json(): Column
{
return new Column(ColumnType::TYPE_JSON);
}

protected function point(): Column
{
return new Column(ColumnType::TYPE_POINT);
}

protected function customType(string $type): Column
{
return new Column($type);
}
}
31 changes: 31 additions & 0 deletions src/V2/ColumnType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Cycle\Migrations\V2;

class ColumnType
{
public const TYPE_PK = 'primary';
public const TYPE_BIGPK = 'bigPrimary';
public const TYPE_CHAR = 'char';
public const TYPE_STRING = 'string';
public const TYPE_TEXT = 'text';
public const TYPE_TINYINT = 'tinyint';
public const TYPE_SMALLINT = 'smallint';
public const TYPE_INTEGER = 'integer';
public const TYPE_BIGINT = 'bigint';
public const TYPE_FLOAT = 'float';
public const TYPE_DOUBLE = 'double';
public const TYPE_DECIMAL = 'decimal';
public const TYPE_NUMERIC = 'numeric';
public const TYPE_DATETIME = 'datetime';
public const TYPE_TIMESTAMP = 'timestamp';
public const TYPE_TIME = 'time';
public const TYPE_DATE = 'date';
public const TYPE_BINARY = 'binary';
public const TYPE_BOOLEAN = 'boolean';
public const TYPE_MONEY = 'money';
public const TYPE_POINT = 'point';
public const TYPE_JSON = 'jsonb';
}
Loading