Skip to content

Commit

Permalink
Merge pull request #21 from PeeHaa/validate-label-lengths
Browse files Browse the repository at this point in the history
Implemented label length validation
  • Loading branch information
PeeHaa committed Aug 7, 2019
2 parents b906998 + dfec708 commit f0832e9
Show file tree
Hide file tree
Showing 72 changed files with 671 additions and 445 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

*None*
- Label lengths are validated (63 bytes max)

### Changed

Expand Down
5 changes: 3 additions & 2 deletions src/Action/AddCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

use PeeHaa\Migres\Constraint\Check;
use PeeHaa\Migres\Migration\Queries;
use PeeHaa\Migres\Specification\Label;

final class AddCheck extends TableAction implements Action
{
private Check $check;

public function __construct(string $tableName, Check $check)
public function __construct(Label $tableName, Check $check)
{
parent::__construct($tableName);

Expand All @@ -23,6 +24,6 @@ public function getCheck(): Check

public function toQueries(): Queries
{
return new Queries(sprintf('ALTER TABLE "%s" ADD %s', $this->tableName, $this->check->toSql()));
return new Queries(sprintf('ALTER TABLE "%s" ADD %s', $this->tableName->toString(), $this->check->toSql()));
}
}
7 changes: 5 additions & 2 deletions src/Action/AddColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

use PeeHaa\Migres\Migration\Queries;
use PeeHaa\Migres\Specification\Column;
use PeeHaa\Migres\Specification\Label;

final class AddColumn extends TableAction implements Action
{
private Column $column;

public function __construct(string $tableName, Column $column)
public function __construct(Label $tableName, Column $column)
{
parent::__construct($tableName);

Expand All @@ -23,6 +24,8 @@ public function getColumn(): Column

public function toQueries(): Queries
{
return new Queries(sprintf('ALTER TABLE "%s" ADD COLUMN %s', $this->tableName, $this->column->toSql()));
return new Queries(
sprintf('ALTER TABLE "%s" ADD COLUMN %s', $this->tableName->toString(), $this->column->toSql()),
);
}
}
3 changes: 2 additions & 1 deletion src/Action/AddForeignByQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
namespace PeeHaa\Migres\Action;

use PeeHaa\Migres\Migration\Queries;
use PeeHaa\Migres\Specification\Label;

final class AddForeignByQuery extends TableAction implements Action
{
private string $query;

public function __construct(string $tableName, string $query)
public function __construct(Label $tableName, string $query)
{
parent::__construct($tableName);

Expand Down
5 changes: 3 additions & 2 deletions src/Action/AddForeignKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

use PeeHaa\Migres\Constraint\ForeignKey;
use PeeHaa\Migres\Migration\Queries;
use PeeHaa\Migres\Specification\Label;

final class AddForeignKey extends TableAction implements Action
{
private ForeignKey $foreignKey;

public function __construct(string $tableName, ForeignKey $foreignKey)
public function __construct(Label $tableName, ForeignKey $foreignKey)
{
parent::__construct($tableName);

Expand All @@ -23,6 +24,6 @@ public function getForeignKey(): ForeignKey

public function toQueries(): Queries
{
return new Queries(sprintf('ALTER TABLE "%s" ADD %s', $this->tableName, $this->foreignKey->toSql()));
return new Queries(sprintf('ALTER TABLE "%s" ADD %s', $this->tableName->toString(), $this->foreignKey->toSql()));
}
}
3 changes: 2 additions & 1 deletion src/Action/AddIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

use PeeHaa\Migres\Constraint\Index;
use PeeHaa\Migres\Migration\Queries;
use PeeHaa\Migres\Specification\Label;

final class AddIndex extends TableAction implements Action
{
private Index $index;

public function __construct(string $tableName, Index $index)
public function __construct(Label $tableName, Index $index)
{
parent::__construct($tableName);

Expand Down
3 changes: 2 additions & 1 deletion src/Action/AddIndexByQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
namespace PeeHaa\Migres\Action;

use PeeHaa\Migres\Migration\Queries;
use PeeHaa\Migres\Specification\Label;

final class AddIndexByQuery extends TableAction implements Action
{
private string $query;

public function __construct(string $tableName, string $query)
public function __construct(Label $tableName, string $query)
{
parent::__construct($tableName);

Expand Down
5 changes: 3 additions & 2 deletions src/Action/AddPrimaryKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

use PeeHaa\Migres\Constraint\PrimaryKey;
use PeeHaa\Migres\Migration\Queries;
use PeeHaa\Migres\Specification\Label;

final class AddPrimaryKey extends TableAction implements Action
{
private PrimaryKey $primaryKey;

public function __construct(string $tableName, PrimaryKey $primaryKey)
public function __construct(Label $tableName, PrimaryKey $primaryKey)
{
parent::__construct($tableName);

Expand All @@ -23,6 +24,6 @@ public function getPrimaryKey(): PrimaryKey

public function toQueries(): Queries
{
return new Queries(sprintf('ALTER TABLE "%s" ADD %s', $this->tableName, $this->primaryKey->toSql()));
return new Queries(sprintf('ALTER TABLE "%s" ADD %s', $this->tableName->toString(), $this->primaryKey->toSql()));
}
}
5 changes: 3 additions & 2 deletions src/Action/AddUniqueConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

use PeeHaa\Migres\Constraint\Unique;
use PeeHaa\Migres\Migration\Queries;
use PeeHaa\Migres\Specification\Label;

final class AddUniqueConstraint extends TableAction implements Action
{
private Unique $constraint;

public function __construct(string $tableName, Unique $constraint)
public function __construct(Label $tableName, Unique $constraint)
{
parent::__construct($tableName);

Expand All @@ -23,6 +24,6 @@ public function getConstraint(): Unique

public function toQueries(): Queries
{
return new Queries(sprintf('ALTER TABLE "%s" ADD %s', $this->tableName, $this->constraint->toSql()));
return new Queries(sprintf('ALTER TABLE "%s" ADD %s', $this->tableName->toString(), $this->constraint->toSql()));
}
}
31 changes: 22 additions & 9 deletions src/Action/ChangeColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@

use PeeHaa\Migres\Migration\Queries;
use PeeHaa\Migres\Specification\Column;
use PeeHaa\Migres\Specification\Label;

final class ChangeColumn extends TableAction implements Action
{
private Column $column;

public function __construct(string $tableName, Column $column)
public function __construct(Label $tableName, Column $column)
{
parent::__construct($tableName);

$this->column = $column;
}

public function getName(): string
public function getName(): Label
{
return $this->column->getName();
}
Expand All @@ -27,8 +28,8 @@ public function toQueries(): Queries

$queries[] = sprintf(
'ALTER TABLE "%s" ALTER COLUMN "%s" TYPE %s',
$this->tableName,
$this->column->getName(),
$this->tableName->toString(),
$this->column->getName()->toString(),
$this->column->getType()->toSql(),
);

Expand All @@ -43,13 +44,17 @@ private function getSetDefaultQuery(): string
$options = $this->column->getOptions();

if (!$options->hasDefault()) {
return sprintf('ALTER TABLE "%s" ALTER COLUMN "%s" DROP DEFAULT', $this->tableName, $this->column->getName());
return sprintf(
'ALTER TABLE "%s" ALTER COLUMN "%s" DROP DEFAULT',
$this->tableName->toString(),
$this->column->getName()->toString(),
);
}

return sprintf(
'ALTER TABLE "%s" ALTER COLUMN "%s" SET DEFAULT %s',
$this->tableName,
$this->column->getName(),
$this->tableName->toString(),
$this->column->getName()->toString(),
$options->getDefaultValue($this->column),
);
}
Expand All @@ -59,9 +64,17 @@ public function getSetNullQuery(): string
$options = $this->column->getOptions();

if ($options->isNullable()) {
return sprintf('ALTER TABLE "%s" ALTER COLUMN "%s" DROP NOT NULL', $this->tableName, $this->column->getName());
return sprintf(
'ALTER TABLE "%s" ALTER COLUMN "%s" DROP NOT NULL',
$this->tableName->toString(),
$this->column->getName()->toString(),
);
}

return sprintf('ALTER TABLE "%s" ALTER COLUMN "%s" SET NOT NULL', $this->tableName, $this->column->getName());
return sprintf(
'ALTER TABLE "%s" ALTER COLUMN "%s" SET NOT NULL',
$this->tableName->toString(),
$this->column->getName()->toString(),
);
}
}
2 changes: 1 addition & 1 deletion src/Action/CreateTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ final class CreateTable extends TableAction implements Action
{
public function toQueries(): Queries
{
return new Queries(sprintf('CREATE TABLE "%s" ()', $this->tableName));
return new Queries(sprintf('CREATE TABLE "%s" ()', $this->tableName->toString()));
}
}
11 changes: 7 additions & 4 deletions src/Action/DropCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,28 @@
namespace PeeHaa\Migres\Action;

use PeeHaa\Migres\Migration\Queries;
use PeeHaa\Migres\Specification\Label;

final class DropCheck extends TableAction implements Action
{
private string $name;
private Label $name;

public function __construct(string $tableName, string $name)
public function __construct(Label $tableName, Label $name)
{
parent::__construct($tableName);

$this->name = $name;
}

public function getName(): string
public function getName(): Label
{
return $this->name;
}

public function toQueries(): Queries
{
return new Queries(sprintf('ALTER TABLE "%s" DROP CONSTRAINT "%s"', $this->tableName, $this->name));
return new Queries(
sprintf('ALTER TABLE "%s" DROP CONSTRAINT "%s"', $this->tableName->toString(), $this->name->toString()),
);
}
}
11 changes: 7 additions & 4 deletions src/Action/DropColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,28 @@
namespace PeeHaa\Migres\Action;

use PeeHaa\Migres\Migration\Queries;
use PeeHaa\Migres\Specification\Label;

final class DropColumn extends TableAction implements Action
{
private string $name;
private Label $name;

public function __construct(string $tableName, string $name)
public function __construct(Label $tableName, Label $name)
{
parent::__construct($tableName);

$this->name = $name;
}

public function getName(): string
public function getName(): Label
{
return $this->name;
}

public function toQueries(): Queries
{
return new Queries(sprintf('ALTER TABLE "%s" DROP COLUMN "%s"', $this->tableName, $this->name));
return new Queries(
sprintf('ALTER TABLE "%s" DROP COLUMN "%s"', $this->tableName->toString(), $this->name->toString()),
);
}
}
11 changes: 7 additions & 4 deletions src/Action/DropForeignKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,28 @@
namespace PeeHaa\Migres\Action;

use PeeHaa\Migres\Migration\Queries;
use PeeHaa\Migres\Specification\Label;

final class DropForeignKey extends TableAction implements Action
{
private string $name;
private Label $name;

public function __construct(string $tableName, string $name)
public function __construct(Label $tableName, Label $name)
{
parent::__construct($tableName);

$this->name = $name;
}

public function getName(): string
public function getName(): Label
{
return $this->name;
}

public function toQueries(): Queries
{
return new Queries(sprintf('ALTER TABLE "%s" DROP CONSTRAINT "%s"', $this->tableName, $this->name));
return new Queries(
sprintf('ALTER TABLE "%s" DROP CONSTRAINT "%s"', $this->tableName->toString(), $this->name->toString()),
);
}
}
9 changes: 5 additions & 4 deletions src/Action/DropIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,26 @@
namespace PeeHaa\Migres\Action;

use PeeHaa\Migres\Migration\Queries;
use PeeHaa\Migres\Specification\Label;

final class DropIndex extends TableAction implements Action
{
private string $name;
private Label $name;

public function __construct(string $tableName, string $name)
public function __construct(Label $tableName, Label $name)
{
parent::__construct($tableName);

$this->name = $name;
}

public function getName(): string
public function getName(): Label
{
return $this->name;
}

public function toQueries(): Queries
{
return new Queries(sprintf('DROP INDEX "%s"', $this->name));
return new Queries(sprintf('DROP INDEX "%s"', $this->name->toString()));
}
}
Loading

0 comments on commit f0832e9

Please sign in to comment.