diff --git a/src/Pckg/Migration/Command/ExecuteMigration.php b/src/Pckg/Migration/Command/ExecuteMigration.php
index 9b398e5..5e8a2b5 100644
--- a/src/Pckg/Migration/Command/ExecuteMigration.php
+++ b/src/Pckg/Migration/Command/ExecuteMigration.php
@@ -1,4 +1,6 @@
-migration = $migration;
- }
-
- public function onlyFields()
- {
- $this->fields = true;
- $this->relations = false;
-
- return $this;
- }
-
- public function execute()
- {
- $entity = new Entity();
- $entity->setRepository(context()->get($this->migration->getRepository()));
- $cache = $entity->getRepository()->getCache();
-
- foreach ($this->migration->getTables() as $table) {
- $this->sql = [];
- if ($cache->hasTable($table->getName())) {
- $this->updateTable($cache, $table);
- } else {
- $this->installTable($cache, $table);
- }
- }
-
- if ($this->sqls) {
- $this->applyMigration();
- }
- }
-
- protected function applyMigration()
- {
- $sqls = implode(";\n\n", $this->sqls);
- $installMigrator = context()->getOrDefault(InstallMigrator::class);
-
- if (!$installMigrator) {
- echo $sqls;
-
- return;
- }
-
- $installMigrator->output($sqls);
- $message = 'Should I execute SQL statements on ' . $this->migration->getRepository() . '?';
- if ($installMigrator->askConfirmation($message)) {
- $this->executeInRepository();
- }
- }
-
- protected function executeInRepository()
- {
- $repositoryName = $this->migration->getRepository();
- foreach ($this->sqls as $sql) {
- $repository = context()->get($repositoryName);
-
- $prepare = $repository->getConnection()->prepare($sql);
- $execute = $prepare->execute();
- if (!$execute) {
- throw new Exception(
- 'Cannot execute query! ' . "\n" . $sql . "\n" . 'Error code ' . $prepare->errorCode() . "\n" .
- $prepare->errorInfo()[2]
- );
- }
- }
- }
-
- protected function updateTable(Cache $cache, Table $table)
- {
- foreach ($table->getFields() as $field) {
- if ($cache->tableHasField($table->getName(), $field->getName())) {
- $sql = $this->updateField($cache, $table, $field);
- if ($sql) {
- $this->sql[] = 'CHANGE `' . $field->getName() . '` ' . $sql;
- }
- } else {
- $sql = $this->installField($cache, $table, $field);
- $this->sql[] = 'ADD ' . $sql;
- if (strpos($sql, 'AUTO_INCREMENT')) {
- $this->sql[] = 'ADD PRIMARY KEY(`' . $field->getName() . '`)';
- }
- }
- }
-
- if ($this->sql) {
- $this->sqls[] = 'ALTER TABLE `' . $table->getName() . '` ' . "\n"
- . ' ' . implode(",\n ", $this->sql);
- }
-
- if ($this->relations) {
- foreach ($table->getConstraints() as $constraint) {
- if ($cache->tableHasConstraint($table->getName(), $constraint->getName())) {
- $this->updateConstraint($cache, $table, $constraint);
- } else {
- $this->installConstraint($cache, $table, $constraint);
- }
- }
-
- foreach ($table->getRelations() as $relation) {
- $relationName = $relation->getName();
-
- if (strpos($relationName, 'FOREIGN_') !== 0) {
- continue;
- }
-
- if ($cache->tableHasConstraint($table->getName(), $relation->getName())) {
- $this->updateRelation($cache, $table, $relation);
- } else {
- $this->installRelation($cache, $table, $relation);
- }
- }
- }
- }
-
- public function updateRelation(Cache $cache, Table $table, Relation $relation)
- {
- $cached = $cache->getConstraint($relation->getName(), $table->getName());
-
- if (!isset($cached['primary'])) {
- d($cached, $table->getName(), $relation->getName());
-
- return;
- }
-
- $current = $relation->getSqlByParams(
- $cached['primary'],
- $cached['references'],
- $cached['on'],
- Relation::RESTRICT,
- Relation::CASCADE
- );
-
- if ($current != $relation->getSql()) {
- $this->output('APPLY RELATION MANUALLY: ' . "\n" . $relation->getSql());
- }
- }
-
- public function installRelation(Cache $cache, Table $table, Relation $relation)
- {
- $this->sqls[] = 'SET foreign_key_checks = 0';
- $this->sqls[] = 'ALTER TABLE `' . $table->getName() . '` ADD ' . $relation->getSql();
- $this->sqls[] = 'SET foreign_key_checks = 1';
- }
-
- protected function updateField(Cache $cache, Table $table, Field $field)
- {
- //$this->output('Updating field ' . $table->getName() . '.' . $field->getName());
- $newSql = $field->getSql();
- $oldSql = $this->buildOldFieldSql($cache, $table, $field);
-
- if ($newSql != $oldSql) {
- return '`' . $field->getName() . '` ' . $newSql;
- }
- }
-
- protected function updateConstraint(Cache $cache, Table $table, Constraint $key)
- {
- $newSql = $key->getSql();
- $oldSql = $this->buildOldKeySql($cache, $table, $key);
- }
-
- protected function installTable(Cache $cache, Table $table)
- {
- //$this->output('Installing table ' . $table->getName());
- $primaryKey = null;
- foreach ($table->getFields() as $field) {
- $sql = $this->installField($cache, $table, $field);
- $this->sql[] = $sql;
- if (strpos($sql, 'AUTO_INCREMENT')) {
- $primaryKey = $field->getName();
- }
- }
-
- if ($this->relations) {
- foreach ($table->getConstraints() as $constraint) {
- $this->installNewConstraint($cache, $table, $constraint);
- }
- }
-
- if ($primaryKey && !in_array('PRIMARY KEY(`' . $primaryKey . '`)', $this->sql)) {
- $this->sql[] = 'PRIMARY KEY(`' . $primaryKey . '`)';
- }
-
- if ($this->sql) {
- $this->sqls[] = 'CREATE TABLE IF NOT EXISTS `' . $table->getName() . '` (' . "\n"
- . implode(",\n", $this->sql) . "\n" .
- ') ENGINE=InnoDB DEFAULT CHARSET=utf8';
- }
- }
-
- protected function installField(Cache $cache, Table $table, Field $field)
- {
- //$this->output('Installing field ' . $table->getName() . '.' . $field->getName());
- return '`' . $field->getName() . '` ' . $field->getSql();
- }
-
- protected function installNewConstraint(Cache $cache, Table $table, Constraint $key)
- {
- //$this->output('Installing constraint ' . $table->getName() . '.' . $key->getName());
- $this->sql[] = $key->getSql();
- }
-
- protected function installConstraint(Cache $cache, Table $table, Constraint $key)
- {
- //$this->output('Installing constraint ' . $table->getName() . '.' . $key->getName());
- $this->sql[] = 'ADD ' . $key->getSql();
- }
-
- protected function output($msg = '')
- {
- echo '' . $msg . "\n";
- }
-
- protected function buildOldFieldSql(Cache $cache, Table $table, Field $field)
- {
- $cachedField = $cache->getField($field->getName(), $table->getName());
-
- return strtoupper($cachedField['type'])
- . ($cachedField['limit'] ? '(' . $cachedField['limit'] . ')' : '')
- . ($cachedField['null'] ? ' NULL' : ' NOT NULL')
- . ($cachedField['default']
- ? ' DEFAULT ' . ($cachedField['default'] == 'CURRENT_TIMESTAMP'
- ? $cachedField['default']
- : ("'" . $cachedField['default'] . "'"))
- : ($cachedField['null'] ? ' DEFAULT NULL' : ''))
- . ($cachedField['extra'] ? ' ' . strtoupper($cachedField['extra']) : '');
- }
-
- protected function buildOldKeySql(Cache $cache, Table $table, Constraint $key)
- {
- $cachedConstraint = $cache->getConstraint($key->getName(), $table->getName());
-
- $type = $cachedConstraint['type'] == 'PRIMARY KEY'
- ? 'PRIMARY'
- : ($cachedConstraint['type'] . ' KEY');
-
- return $type . ' ' . $key->getName();
- }
-
+ /**
+ * @var Migration
+ */
+ protected $migration;
+
+ /**
+ * @var array
+ */
+ protected $sql = [];
+
+ /**
+ * @var array
+ */
+ protected $sqls = [];
+
+ /**
+ * @var bool
+ */
+ protected $fields = true;
+
+ /**
+ * @var bool
+ */
+ protected $relations = true;
+
+ /**
+ * ExecuteMigration constructor.
+ *
+ * @param Migration $migration
+ */
+ public function __construct(Migration $migration)
+ {
+ $this->migration = $migration;
+ }
+
+ /**
+ * @return $this
+ */
+ public function onlyFields()
+ {
+ $this->fields = true;
+ $this->relations = false;
+
+ return $this;
+ }
+
+ /**
+ *
+ */
+ public function execute()
+ {
+ $entity = new Entity();
+ $entity->setRepository(context()->get($this->migration->getRepository()));
+ $cache = $entity->getRepository()->getCache();
+
+ foreach ($this->migration->getTables() as $table) {
+ $this->sql = [];
+ if ($cache->hasTable($table->getName())) {
+ $this->updateTable($cache, $table);
+ } else {
+ $this->installTable($cache, $table);
+ }
+ }
+
+ if ($this->sqls) {
+ $this->applyMigration();
+ }
+ }
+
+ /**
+ *
+ */
+ protected function applyMigration()
+ {
+ $sqls = implode(";\n\n", $this->sqls);
+ $installMigrator = context()->getOrDefault(InstallMigrator::class);
+
+ if (!$installMigrator) {
+ echo $sqls;
+
+ return;
+ }
+
+ $installMigrator->output($sqls);
+ $message = 'Should I execute SQL statements on ' . $this->migration->getRepository() . '?';
+ if ($installMigrator->askConfirmation($message)) {
+ $this->executeInRepository();
+ }
+ }
+
+ /**
+ * @throws Exception
+ */
+ protected function executeInRepository()
+ {
+ $repositoryName = $this->migration->getRepository();
+ foreach ($this->sqls as $sql) {
+ $repository = context()->get($repositoryName);
+
+ $prepare = $repository->getConnection()->prepare($sql);
+ $execute = $prepare->execute();
+ if (!$execute) {
+ throw new Exception(
+ 'Cannot execute query! ' . "\n" . $sql . "\n" . 'Error code ' . $prepare->errorCode(
+ ) . "\n" . $prepare->errorInfo()[2]
+ );
+ }
+ }
+ }
+
+ /**
+ * @param Cache $cache
+ * @param Table $table
+ */
+ protected function updateTable(Cache $cache, Table $table)
+ {
+ foreach ($table->getFields() as $field) {
+ if ($cache->tableHasField($table->getName(), $field->getName())) {
+ $sql = $this->updateField($cache, $table, $field);
+ if ($sql) {
+ $this->sql[] = 'CHANGE `' . $field->getName() . '` ' . $sql;
+ }
+ } else {
+ $sql = $this->installField($cache, $table, $field);
+ $this->sql[] = 'ADD ' . $sql;
+ if (strpos($sql, 'AUTO_INCREMENT')) {
+ $this->sql[] = 'ADD PRIMARY KEY(`' . $field->getName() . '`)';
+ }
+ }
+ }
+
+ if ($this->sql) {
+ $this->sqls[] = 'ALTER TABLE `' . $table->getName() . '` ' . "\n" . ' ' . implode(",\n ", $this->sql);
+ }
+
+ if ($this->relations) {
+ foreach ($table->getConstraints() as $constraint) {
+ if ($cache->tableHasConstraint($table->getName(), $constraint->getName())) {
+ $this->updateConstraint($cache, $table, $constraint);
+ } else {
+ $this->installConstraint($cache, $table, $constraint);
+ }
+ }
+
+ foreach ($table->getRelations() as $relation) {
+ $relationName = $relation->getName();
+
+ if (strpos($relationName, 'FOREIGN_') !== 0) {
+ continue;
+ }
+
+ if ($cache->tableHasConstraint($table->getName(), $relation->getName())) {
+ $this->updateRelation($cache, $table, $relation);
+ } else {
+ $this->installRelation($cache, $table, $relation);
+ }
+ }
+ }
+ }
+
+ /**
+ * @param Cache $cache
+ * @param Table $table
+ * @param Relation $relation
+ */
+ public function updateRelation(Cache $cache, Table $table, Relation $relation)
+ {
+ $cached = $cache->getConstraint($relation->getName(), $table->getName());
+
+ if (!isset($cached['primary'])) {
+ d($cached, $table->getName(), $relation->getName());
+
+ return;
+ }
+
+ $current = $relation->getSqlByParams(
+ $cached['primary'],
+ $cached['references'],
+ $cached['on'],
+ Relation::RESTRICT,
+ Relation::CASCADE
+ );
+
+ if ($current != $relation->getSql()) {
+ $this->output('APPLY RELATION MANUALLY: ' . "\n" . $relation->getSql());
+ }
+ }
+
+ /**
+ * @param Cache $cache
+ * @param Table $table
+ * @param Relation $relation
+ */
+ public function installRelation(Cache $cache, Table $table, Relation $relation)
+ {
+ $this->sqls[] = 'SET foreign_key_checks = 0';
+ $this->sqls[] = 'ALTER TABLE `' . $table->getName() . '` ADD ' . $relation->getSql();
+ $this->sqls[] = 'SET foreign_key_checks = 1';
+ }
+
+ /**
+ * @param Cache $cache
+ * @param Table $table
+ * @param Field $field
+ *
+ * @return string
+ */
+ protected function updateField(Cache $cache, Table $table, Field $field)
+ {
+ //$this->output('Updating field ' . $table->getName() . '.' . $field->getName());
+ $newSql = $field->getSql();
+ $oldSql = $this->buildOldFieldSql($cache, $table, $field);
+
+ if ($newSql != $oldSql) {
+ return '`' . $field->getName() . '` ' . $newSql;
+ }
+ }
+
+ /**
+ * @param Cache $cache
+ * @param Table $table
+ * @param Constraint $key
+ */
+ protected function updateConstraint(Cache $cache, Table $table, Constraint $key)
+ {
+ $newSql = $key->getSql();
+ $oldSql = $this->buildOldKeySql($cache, $table, $key);
+ }
+
+ /**
+ * @param Cache $cache
+ * @param Table $table
+ */
+ protected function installTable(Cache $cache, Table $table)
+ {
+ //$this->output('Installing table ' . $table->getName());
+ $primaryKey = null;
+ foreach ($table->getFields() as $field) {
+ $sql = $this->installField($cache, $table, $field);
+ $this->sql[] = $sql;
+ if (strpos($sql, 'AUTO_INCREMENT')) {
+ $primaryKey = $field->getName();
+ }
+ }
+
+ if ($this->relations) {
+ foreach ($table->getConstraints() as $constraint) {
+ $this->installNewConstraint($cache, $table, $constraint);
+ }
+ }
+
+ if ($primaryKey && !in_array('PRIMARY KEY(`' . $primaryKey . '`)', $this->sql)) {
+ $this->sql[] = 'PRIMARY KEY(`' . $primaryKey . '`)';
+ }
+
+ if ($this->sql) {
+ $this->sqls[] = 'CREATE TABLE IF NOT EXISTS `' . $table->getName() . '` (' . "\n" . implode(
+ ",\n",
+ $this->sql
+ ) . "\n" . ') ENGINE=InnoDB DEFAULT CHARSET=utf8';
+ }
+ }
+
+ /**
+ * @param Cache $cache
+ * @param Table $table
+ * @param Field $field
+ *
+ * @return string
+ */
+ protected function installField(Cache $cache, Table $table, Field $field)
+ {
+ //$this->output('Installing field ' . $table->getName() . '.' . $field->getName());
+ return '`' . $field->getName() . '` ' . $field->getSql();
+ }
+
+ /**
+ * @param Cache $cache
+ * @param Table $table
+ * @param Constraint $key
+ */
+ protected function installNewConstraint(Cache $cache, Table $table, Constraint $key)
+ {
+ //$this->output('Installing constraint ' . $table->getName() . '.' . $key->getName());
+ $this->sql[] = $key->getSql();
+ }
+
+ /**
+ * @param Cache $cache
+ * @param Table $table
+ * @param Constraint $key
+ */
+ protected function installConstraint(Cache $cache, Table $table, Constraint $key)
+ {
+ //$this->output('Installing constraint ' . $table->getName() . '.' . $key->getName());
+ $this->sql[] = 'ADD ' . $key->getSql();
+ }
+
+ /**
+ * @param string $msg
+ */
+ protected function output($msg = '')
+ {
+ echo '' . $msg . "\n";
+ }
+
+ /**
+ * @param Cache $cache
+ * @param Table $table
+ * @param Field $field
+ *
+ * @return string
+ */
+ protected function buildOldFieldSql(Cache $cache, Table $table, Field $field)
+ {
+ $cachedField = $cache->getField($field->getName(), $table->getName());
+
+ return strtoupper(
+ $cachedField['type']
+ ) . ($cachedField['limit'] ? '(' . $cachedField['limit'] . ')' : '') . ($cachedField['null'] ? ' NULL' : ' NOT NULL') . ($cachedField['default'] ? ' DEFAULT ' . ($cachedField['default'] == 'CURRENT_TIMESTAMP' ? $cachedField['default'] : ("'" . $cachedField['default'] . "'")) : ($cachedField['null'] ? ' DEFAULT NULL' : '')) . ($cachedField['extra'] ? ' ' . strtoupper(
+ $cachedField['extra']
+ ) : '');
+ }
+
+ /**
+ * @param Cache $cache
+ * @param Table $table
+ * @param Constraint $key
+ *
+ * @return string
+ */
+ protected function buildOldKeySql(Cache $cache, Table $table, Constraint $key)
+ {
+ $cachedConstraint = $cache->getConstraint($key->getName(), $table->getName());
+
+ $type = $cachedConstraint['type'] == 'PRIMARY KEY' ? 'PRIMARY' : ($cachedConstraint['type'] . ' KEY');
+
+ return $type . ' ' . $key->getName();
+ }
}
\ No newline at end of file
diff --git a/src/Pckg/Migration/Console/InstallMigrator.php b/src/Pckg/Migration/Console/InstallMigrator.php
index 39db319..0625b03 100644
--- a/src/Pckg/Migration/Console/InstallMigrator.php
+++ b/src/Pckg/Migration/Console/InstallMigrator.php
@@ -1,4 +1,6 @@
-setName('migrator:install')
- ->setDescription('Install migrations from envirtonment')
- ->addOption('only', null, InputOption::VALUE_OPTIONAL, 'Install only listed migrations')
- ->addOption('fields', null, null, 'Install only fields (no keys)');
- }
-
- /**
- * We
- */
- public function handle()
- {
- $this->app = $this->getApp();
-
- if (!$this->app) {
- throw new Exception('App name is required in migrator');
- }
-
- context()->bind(InstallMigrator::class, $this);
-
- $requestedMigrations = $this->getRequestedMigrations();
- $installedMigrations = (array)$this->getInstalledMigrations();
-
- $installed = 0;
- $updated = 0;
- foreach ($requestedMigrations as $requestedMigration) {
- $migrationClass = is_object($requestedMigration) ? get_class($requestedMigration) : $requestedMigration;
- if ($this->option('only') && strpos($migrationClass, $this->option('only')) === false) {
- continue;
- }
-
- /**
- * @T00D00
- * Implement beforeFirstUp(), beforeUp(), afterUp(), afterFirstUp(), isFirstUp()
- */
- try {
- $this->output('Migration: ' . $requestedMigration, 'info');
- $migration = new $requestedMigration;
- if ($this->option('fields')) {
- $migration->onlyFields();
- }
- foreach ($migration->dependencies() as $dependency) {
- if (is_string($dependency)) {
- $dependency = Reflect::create($dependency);
- }
- if ($this->option('fields')) {
- $dependency->onlyFields();
- }
- $this->output(
- 'Dependency: ' . $dependency->getRepository() . ' : ' . get_class($dependency),
- 'info'
- );
- $dependency->up();
- }
- $migration->up();
- if (!in_array($requestedMigration, $installedMigrations)) {
- $migration->afterFirstUp();
- }
- foreach ($migration->partials() as $partial) {
- if (is_string($partial)) {
- $partial = Reflect::create($partial);
- }
- if ($this->option('fields')) {
- $partial->onlyFields();
- }
- $this->output('Partial: ' . $partial->getRepository() . ' : ' . get_class($partial), 'info');
- $partial->up();
- }
- $this->output($migration->getRepository() . ' : ' . $requestedMigration, 'info');
- $this->output();
- } catch (Throwable $e) {
- dd(exception($e));
- }
-
- if (in_array($requestedMigration, $installedMigrations)) {
- $updated++;
- } else {
- $installedMigrations[] = $requestedMigration;
- $installed++;
- }
- }
-
- $this->output('Updated: ' . $updated, 'comment');
- $this->output('Installed: ' . $installed, 'comment');
- $this->output('Total: ' . count($installedMigrations), 'comment');
-
- $this->putInstalledMigrations($installedMigrations);
- }
-
- private function getRequestedMigrations()
- {
- return require $this->getConfigPath();
- }
-
- private function getInstalledMigrations()
- {
- return is_file($this->getEnvironmentPath())
- ? json_decode(file_get_contents($this->getEnvironmentPath()))
- : [];
- }
-
- private function putInstalledMigrations(array $installedMigrations = [])
- {
- return file_put_contents($this->getEnvironmentPath(), json_encode($installedMigrations));
- }
-
- private function getConfigPath()
- {
- return path('apps') . $this->app . path('ds') . 'config' . path('ds') . 'migrations.php';
- }
-
- private function getSrcPath()
- {
- return path('apps') . $this->app . path('ds') . 'src';
- }
-
- private function getEnvironmentPath()
- {
- return path('root') . 'storage' . path('ds') . 'environment' . path('ds') . 'migrator' . path(
- 'ds'
- ) . $this->app . '.json';
- }
-
+ /**
+ *
+ */
+ protected function configure()
+ {
+ $this->setName('migrator:install')->setDescription('Install migrations from envirtonment')->addOption(
+ 'only',
+ null,
+ InputOption::VALUE_OPTIONAL,
+ 'Install only listed migrations'
+ )->addOption('fields', null, null, 'Install only fields (no keys)')
+ ;
+ }
+
+ /**
+ * We
+ *
+ * @throws Exception
+ */
+ public function handle()
+ {
+ $this->app = $this->getApp();
+
+ if (!$this->app) {
+ throw new Exception('App name is required in migrator');
+ }
+
+ context()->bind(InstallMigrator::class, $this);
+
+ $requestedMigrations = $this->getRequestedMigrations();
+ $installedMigrations = (array) $this->getInstalledMigrations();
+
+ $installed = 0;
+ $updated = 0;
+ foreach ($requestedMigrations as $requestedMigration) {
+ $migrationClass = is_object($requestedMigration) ? get_class($requestedMigration) : $requestedMigration;
+ if ($this->option('only') && strpos($migrationClass, $this->option('only')) === false) {
+ continue;
+ }
+
+ /**
+ * @T00D00
+ * Implement beforeFirstUp(), beforeUp(), afterUp(), afterFirstUp(), isFirstUp()
+ */
+ try {
+ $this->output('Migration: ' . $requestedMigration, 'info');
+ $migration = new $requestedMigration;
+ if ($this->option('fields')) {
+ $migration->onlyFields();
+ }
+ foreach ($migration->dependencies() as $dependency) {
+ if (is_string($dependency)) {
+ $dependency = Reflect::create($dependency);
+ }
+ if ($this->option('fields')) {
+ $dependency->onlyFields();
+ }
+ $this->output(
+ 'Dependency: ' . $dependency->getRepository() . ' : ' . get_class($dependency),
+ 'info'
+ );
+ $dependency->up();
+ }
+ $migration->up();
+ if (!in_array($requestedMigration, $installedMigrations)) {
+ $migration->afterFirstUp();
+ }
+ foreach ($migration->partials() as $partial) {
+ if (is_string($partial)) {
+ $partial = Reflect::create($partial);
+ }
+ if ($this->option('fields')) {
+ $partial->onlyFields();
+ }
+ $this->output('Partial: ' . $partial->getRepository() . ' : ' . get_class($partial), 'info');
+ $partial->up();
+ }
+ $this->output($migration->getRepository() . ' : ' . $requestedMigration, 'info');
+ $this->output();
+ }
+ catch (Throwable $e) {
+ dd(exception($e));
+ }
+
+ if (in_array($requestedMigration, $installedMigrations)) {
+ $updated++;
+ } else {
+ $installedMigrations[] = $requestedMigration;
+ $installed++;
+ }
+ }
+
+ $this->output('Updated: ' . $updated, 'comment');
+ $this->output('Installed: ' . $installed, 'comment');
+ $this->output('Total: ' . count($installedMigrations), 'comment');
+
+ $this->putInstalledMigrations($installedMigrations);
+ }
+
+ /**
+ * @return mixed
+ */
+ private function getRequestedMigrations()
+ {
+ return require $this->getConfigPath();
+ }
+
+ /**
+ * @return array|mixed
+ */
+ private function getInstalledMigrations()
+ {
+ return is_file($this->getEnvironmentPath()) ? json_decode(file_get_contents($this->getEnvironmentPath())) : [];
+ }
+
+ /**
+ * @param array $installedMigrations
+ *
+ * @return bool|int
+ */
+ private function putInstalledMigrations(array $installedMigrations = [])
+ {
+ return file_put_contents($this->getEnvironmentPath(), json_encode($installedMigrations));
+ }
+
+ /**
+ * @return string
+ */
+ private function getConfigPath()
+ {
+ return path('apps') . $this->app . path('ds') . 'config' . path('ds') . 'migrations.php';
+ }
+
+ /**
+ * @return string
+ */
+ private function getSrcPath()
+ {
+ return path('apps') . $this->app . path('ds') . 'src';
+ }
+
+ /**
+ * @return string
+ */
+ private function getEnvironmentPath()
+ {
+ return path('root') . 'storage' . path('ds') . 'environment' . path('ds') . 'migrator' . path(
+ 'ds'
+ ) . $this->app . '.json';
+ }
}
\ No newline at end of file
diff --git a/src/Pckg/Migration/Console/UpdateMigrator.php b/src/Pckg/Migration/Console/UpdateMigrator.php
index 842ac4f..3e4cab1 100644
--- a/src/Pckg/Migration/Console/UpdateMigrator.php
+++ b/src/Pckg/Migration/Console/UpdateMigrator.php
@@ -1,37 +1,46 @@
-setName('migrator:update')->setDescription(
+ 'Install not installed migrations listed in migrations.php in app config'
+ )->addArguments(
+ [
+ 'app' => 'Application name',
+ ],
+ InputArgument::REQUIRED
+ )
+ ;
+ }
- protected function configure()
- {
- $this->setName('migrator:update')
- ->setDescription('Install not installed migrations listed in migrations.php in app config')
- ->addArguments(
- [
- 'app' => 'Application name',
- ],
- InputArgument::REQUIRED
- );
- }
-
- /**
- * We
- */
- public function handle()
- {
- $this->app = $this->argument('app');
- dd("UpdateMigrator::handle", $this->app, $this->getAppMigrationPath());
- }
-
- private function getAppMigrationPath()
- {
- return path('root') . 'storage' . path('ds') . 'environment' . path('ds') . 'migrator' . path(
- 'ds'
- ) . $this->app . path('ds');
- }
+ /**
+ * We
+ */
+ public function handle()
+ {
+ $this->app = $this->argument('app');
+ dd("UpdateMigrator::handle", $this->app, $this->getAppMigrationPath());
+ }
+ private function getAppMigrationPath()
+ {
+ return path('root') . 'storage' . path('ds') . 'environment' . path('ds') . 'migrator' . path(
+ 'ds'
+ ) . $this->app . path('ds');
+ }
}
\ No newline at end of file
diff --git a/src/Pckg/Migration/Constraint.php b/src/Pckg/Migration/Constraint.php
index e40a273..e9b2ee2 100644
--- a/src/Pckg/Migration/Constraint.php
+++ b/src/Pckg/Migration/Constraint.php
@@ -1,32 +1,52 @@
-table = $table;
- $this->fields = $fields;
- }
+ /**
+ * @var array
+ */
+ protected $fields = [];
- public function getSql()
- {
- return $this->type . ' `' . $this->getName() . '` (`' . implode('`,`', $this->fields) . '`)';
- }
+ /**
+ * Constraint constructor.
+ *
+ * @param Table $table
+ * @param array ...$fields
+ */
+ public function __construct(Table $table, ...$fields)
+ {
+ $this->table = $table;
+ $this->fields = $fields;
+ }
- public function getName()
- {
- return str_replace(' KEY', '', $this->type) . '__' . $this->table->getName() . '__' . implode(
- '_',
- $this->fields
- );
- }
+ /**
+ * @return string
+ */
+ public function getSql()
+ {
+ return $this->type . ' `' . $this->getName() . '` (`' . implode('`,`', $this->fields) . '`)';
+ }
+ /**
+ * @return string
+ */
+ public function getName()
+ {
+ return str_replace(' KEY', '', $this->type) . '__' . $this->table->getName() . '__' . implode(
+ '_',
+ $this->fields
+ );
+ }
}
\ No newline at end of file
diff --git a/src/Pckg/Migration/Constraint/Index.php b/src/Pckg/Migration/Constraint/Index.php
index b34f1b7..440ab1a 100644
--- a/src/Pckg/Migration/Constraint/Index.php
+++ b/src/Pckg/Migration/Constraint/Index.php
@@ -1,20 +1,34 @@
-type . '(`' . implode('`,`', $this->fields) . '`)';
- }
-
- public function getName()
- {
- return 'INDEX';
- }
+ /**
+ * @return string
+ */
+ public function getSql()
+ {
+ return $this->type . '(`' . implode('`,`', $this->fields) . '`)';
+ }
+ /**
+ * @return string
+ */
+ public function getName()
+ {
+ return 'INDEX';
+ }
}
\ No newline at end of file
diff --git a/src/Pckg/Migration/Constraint/Primary.php b/src/Pckg/Migration/Constraint/Primary.php
index 38c7158..e55248b 100644
--- a/src/Pckg/Migration/Constraint/Primary.php
+++ b/src/Pckg/Migration/Constraint/Primary.php
@@ -1,20 +1,34 @@
-type . '(`' . implode('`,`', $this->fields) . '`)';
- }
-
- public function getName()
- {
- return 'PRIMARY';
- }
+ /**
+ * @return string
+ */
+ public function getSql()
+ {
+ return $this->type . '(`' . implode('`,`', $this->fields) . '`)';
+ }
+ /**
+ * @return string
+ */
+ public function getName()
+ {
+ return 'PRIMARY';
+ }
}
\ No newline at end of file
diff --git a/src/Pckg/Migration/Constraint/Unique.php b/src/Pckg/Migration/Constraint/Unique.php
index 05f96f0..42bb9d2 100644
--- a/src/Pckg/Migration/Constraint/Unique.php
+++ b/src/Pckg/Migration/Constraint/Unique.php
@@ -1,10 +1,18 @@
-table = $table;
- $this->name = $name;
- }
-
- public function getName()
- {
- return $this->name;
- }
-
- public function getTypeWithLength()
- {
- return $this->type . ($this->length ? '(' . $this->length . ')' : '');
- }
-
- public function getSql()
- {
- $sql = [];
- $sql[] = $this->getTypeWithLength();
- if ($this->isNullable()) {
- $sql[] = 'NULL';
- } else {
- $sql[] = 'NOT NULL';
- }
-
- if ($this->default) {
- $default = '';
- if ($this->default == 'CURRENT_TIMESTAMP') {
- $default = $this->default;
- } else {
- $default = "'" . $this->default . "'";
- }
- $sql[] = 'DEFAULT ' . $default;
-
- } else if ($this->isNullable()) {
- $sql[] = 'DEFAULT NULL';
- }
-
- return implode(' ', $sql);
- }
-
- public function getTable()
- {
- return $this->table;
- }
-
- public function isNullable()
- {
- return $this->nullable;
- }
-
- public function isRequired()
- {
- return !$this->nullable;
- }
-
- public function nullable($nullable = true)
- {
- $this->nullable = $nullable;
-
- return $this;
- }
-
- public function required($required = true)
- {
- $this->nullable = !$required;
-
- return $this;
- }
-
- public function setDefault($default)
- {
- $this->default = $default;
-
- return $this;
- }
-
- public function length($length)
- {
- $this->length = $length;
-
- return $this;
- }
-
- public function index()
- {
- $index = new Index($this, $this->name);
-
- $this->table->addConstraint($index);
-
- return $this;
- }
-
- public function primary()
- {
- $primary = new Primary($this->table, $this->name);
-
- $this->table->addConstraint($primary);
-
- return $this;
- }
-
- public function unique()
- {
- $unique = new Unique($this, $this->name);
-
- $this->table->addConstraint($unique);
-
- return $this;
- }
-
- public function references($table, $on = 'id')
- {
- $relation = new Relation($this, $table, $on);
-
- $this->table->addRelation($relation);
-
- return $this;
- }
-
- public function drop()
- {
- /**
- * @T00D00
- */
- return $this;
- }
-
+ /**
+ * @var
+ */
+ protected $name;
+
+ /**
+ * @var
+ */
+ protected $type;
+
+ /**
+ * @var bool
+ */
+ protected $nullable = true;
+
+ /**
+ * @var null
+ */
+ protected $default = null;
+
+ /**
+ * @var
+ */
+ protected $length;
+
+ /**
+ * @var Table
+ */
+ protected $table;
+
+ /**
+ * Field constructor.
+ *
+ * @param Table $table
+ * @param $name
+ */
+ public function __construct(Table $table, $name)
+ {
+ $this->table = $table;
+ $this->name = $name;
+ }
+
+ /**
+ * @return mixed
+ */
+ public function getName()
+ {
+ return $this->name;
+ }
+
+ /**
+ * @return string
+ */
+ public function getTypeWithLength()
+ {
+ return $this->type . ($this->length ? '(' . $this->length . ')' : '');
+ }
+
+ /**
+ * @return string
+ */
+ public function getSql()
+ {
+ $sql = [];
+ $sql[] = $this->getTypeWithLength();
+ if ($this->isNullable()) {
+ $sql[] = 'NULL';
+ } else {
+ $sql[] = 'NOT NULL';
+ }
+
+ if ($this->default) {
+ $default = '';
+ if ($this->default == 'CURRENT_TIMESTAMP') {
+ $default = $this->default;
+ } else {
+ $default = "'" . $this->default . "'";
+ }
+ $sql[] = 'DEFAULT ' . $default;
+
+ } elseif ($this->isNullable()) {
+ $sql[] = 'DEFAULT NULL';
+ }
+
+ return implode(' ', $sql);
+ }
+
+ /**
+ * @return Table
+ */
+ public function getTable()
+ {
+ return $this->table;
+ }
+
+ /**
+ * @return bool
+ */
+ public function isNullable()
+ {
+ return $this->nullable;
+ }
+
+ /**
+ * @return bool
+ */
+ public function isRequired()
+ {
+ return !$this->nullable;
+ }
+
+ /**
+ * @param bool $nullable
+ *
+ * @return $this
+ */
+ public function nullable($nullable = true)
+ {
+ $this->nullable = $nullable;
+
+ return $this;
+ }
+
+ /**
+ * @param bool $required
+ *
+ * @return $this
+ */
+ public function required($required = true)
+ {
+ $this->nullable = !$required;
+
+ return $this;
+ }
+
+ /**
+ * @param $default
+ *
+ * @return $this
+ */
+ public function setDefault($default)
+ {
+ $this->default = $default;
+
+ return $this;
+ }
+
+ /**
+ * @param $length
+ *
+ * @return $this
+ */
+ public function length($length)
+ {
+ $this->length = $length;
+
+ return $this;
+ }
+
+ /**
+ * @return $this
+ */
+ public function index()
+ {
+ $index = new Index($this, $this->name);
+
+ $this->table->addConstraint($index);
+
+ return $this;
+ }
+
+ /**
+ * @return $this
+ */
+ public function primary()
+ {
+ $primary = new Primary($this->table, $this->name);
+
+ $this->table->addConstraint($primary);
+
+ return $this;
+ }
+
+ /**
+ * @return $this
+ */
+ public function unique()
+ {
+ $unique = new Unique($this, $this->name);
+
+ $this->table->addConstraint($unique);
+
+ return $this;
+ }
+
+ /**
+ * @param $table
+ * @param string $on
+ *
+ * @return $this
+ */
+ public function references($table, $on = 'id')
+ {
+ $relation = new Relation($this, $table, $on);
+
+ $this->table->addRelation($relation);
+
+ return $this;
+ }
+
+ /**
+ * @return $this
+ */
+ public function drop()
+ {
+ /**
+ * @T00D00
+ */
+ return $this;
+ }
}
\ No newline at end of file
diff --git a/src/Pckg/Migration/Field/Boolean.php b/src/Pckg/Migration/Field/Boolean.php
index 4e94d23..cdb5ef5 100644
--- a/src/Pckg/Migration/Field/Boolean.php
+++ b/src/Pckg/Migration/Field/Boolean.php
@@ -1,12 +1,23 @@
-type . ($this->length ? '(' . implode(',', $this->length) . ')' : '');
- }
+ /**
+ * @var array
+ */
+ protected $length = [8, 2];
+ /**
+ * @return string
+ */
+ public function getTypeWithLength()
+ {
+ return $this->type . ($this->length ? '(' . implode(',', $this->length) . ')' : '');
+ }
}
\ No newline at end of file
diff --git a/src/Pckg/Migration/Field/Group/Deletable.php b/src/Pckg/Migration/Field/Group/Deletable.php
index b0f7fa9..da80787 100644
--- a/src/Pckg/Migration/Field/Group/Deletable.php
+++ b/src/Pckg/Migration/Field/Group/Deletable.php
@@ -1,17 +1,30 @@
-table = $table;
-
- $table->datetime('deleted_at');
- }
+ /**
+ * Deletable constructor.
+ *
+ * @param Table $table
+ */
+ public function __construct(Table $table)
+ {
+ $this->table = $table;
+ $table->datetime('deleted_at');
+ }
}
\ No newline at end of file
diff --git a/src/Pckg/Migration/Field/Group/Hidable.php b/src/Pckg/Migration/Field/Group/Hidable.php
index a7008c9..231c1d3 100644
--- a/src/Pckg/Migration/Field/Group/Hidable.php
+++ b/src/Pckg/Migration/Field/Group/Hidable.php
@@ -1,17 +1,30 @@
-table = $table;
-
- $table->boolean('hidden');
- }
+ /**
+ * Hidable constructor.
+ *
+ * @param Table $table
+ */
+ public function __construct(Table $table)
+ {
+ $this->table = $table;
+ $table->boolean('hidden');
+ }
}
\ No newline at end of file
diff --git a/src/Pckg/Migration/Field/Group/Orderable.php b/src/Pckg/Migration/Field/Group/Orderable.php
index bd8e7ce..396739a 100644
--- a/src/Pckg/Migration/Field/Group/Orderable.php
+++ b/src/Pckg/Migration/Field/Group/Orderable.php
@@ -1,17 +1,30 @@
-table = $table;
-
- $table->integer('order');
- }
+ /**
+ * Orderable constructor.
+ *
+ * @param Table $table
+ */
+ public function __construct(Table $table)
+ {
+ $this->table = $table;
+ $table->integer('order');
+ }
}
\ No newline at end of file
diff --git a/src/Pckg/Migration/Field/Group/Timeable.php b/src/Pckg/Migration/Field/Group/Timeable.php
index e6c5f2e..d19720c 100644
--- a/src/Pckg/Migration/Field/Group/Timeable.php
+++ b/src/Pckg/Migration/Field/Group/Timeable.php
@@ -1,19 +1,32 @@
-table = $table;
-
- $table->datetime('created_at')->setDefault('CURRENT_TIMESTAMP');
- $table->datetime('updated_at');
- $table->datetime('deleted_at');
- }
+ /**
+ * Timeable constructor.
+ *
+ * @param Table $table
+ */
+ public function __construct(Table $table)
+ {
+ $this->table = $table;
+ $table->datetime('created_at')->setDefault('CURRENT_TIMESTAMP');
+ $table->datetime('updated_at');
+ $table->datetime('deleted_at');
+ }
}
\ No newline at end of file
diff --git a/src/Pckg/Migration/Field/Id.php b/src/Pckg/Migration/Field/Id.php
index 7c26257..944fe48 100644
--- a/src/Pckg/Migration/Field/Id.php
+++ b/src/Pckg/Migration/Field/Id.php
@@ -1,22 +1,41 @@
-autoincrement = $boolean;
+ /**
+ * @var bool
+ */
+ protected $autoincrement = true;
- return $this;
- }
+ /**
+ * @param $boolean
+ *
+ * @return $this
+ */
+ public function autoincrement($boolean)
+ {
+ $this->autoincrement = $boolean;
- public function getSql()
- {
- return parent::getSql() . ($this->autoincrement ? ' AUTO_INCREMENT' : '');
- }
+ return $this;
+ }
+ /**
+ * @return string
+ */
+ public function getSql()
+ {
+ return parent::getSql() . ($this->autoincrement ? ' AUTO_INCREMENT' : '');
+ }
}
\ No newline at end of file
diff --git a/src/Pckg/Migration/Field/IdString.php b/src/Pckg/Migration/Field/IdString.php
index 65a8760..e48fd36 100644
--- a/src/Pckg/Migration/Field/IdString.php
+++ b/src/Pckg/Migration/Field/IdString.php
@@ -1,10 +1,18 @@
- $table, 'repository' => $repository]);
+ }
+
+ /**
+ * @param $slug
+ *
+ * @return mixed
+ */
+ protected function dynamicFieldType($slug)
+ {
+ return FieldType::getOrFail(['slug' => $slug]);
+ }
+
+ /**
+ * @param Table $table
+ * @param $field
+ * @param $fieldType
+ *
+ * @return mixed
+ */
+ protected function dynamicField(Table $table, $field, $fieldType)
+ {
+ $fieldType = $this->dynamicFieldType($fieldType);
+
+ return Field::getOrCreate(['table_id' => $table->id, 'field' => $field, 'field_type_id' => $fieldType->id]);
+ }
+
+ /**
+ * @param $slug
+ *
+ * @return mixed
+ */
+ protected function dynamicRelationType($slug)
+ {
+ return RelationType::getOrFail(['slug' => $slug]);
+ }
- protected function dynamicTable($table, $repository = null)
- {
- return Table::getOrCreate(['table' => $table, 'repository' => $repository]);
- }
-
- protected function dynamicFieldType($slug)
- {
- return FieldType::getOrFail(['slug' => $slug]);
- }
-
- protected function dynamicField(Table $table, $field, $fieldType)
- {
- $fieldType = $this->dynamicFieldType($fieldType);
-
- return Field::getOrCreate(['table_id' => $table->id, 'field' => $field, 'field_type_id' => $fieldType->id]);
- }
-
- protected function dynamicRelationType($slug)
- {
- return RelationType::getOrFail(['slug' => $slug]);
- }
-
- protected function dynamicRelation(
- Table $onTable,
- Field $onField,
- $relationType,
- Table $showTable,
- Field $showField
- )
- {
- $relationType = $this->dynamicRelationType($relationType);
-
- return Relation::getOrCreate(
- [
- 'on_table_id' => $onTable->id,
- 'on_field_id' => $onField->id,
- 'show_table_id' => $showTable->id,
- 'show_field_id' => $showField->id,
- 'relation_type_id' => $relationType->id,
- ]
- );
- }
+ /**
+ * @param Table $onTable
+ * @param Field $onField
+ * @param $relationType
+ * @param Table $showTable
+ * @param Field $showField
+ *
+ * @return mixed
+ */
+ protected function dynamicRelation(
+ Table $onTable,
+ Field $onField,
+ $relationType,
+ Table $showTable,
+ Field $showField
+ )
+ {
+ $relationType = $this->dynamicRelationType($relationType);
+ return Relation::getOrCreate(
+ [
+ 'on_table_id' => $onTable->id,
+ 'on_field_id' => $onField->id,
+ 'show_table_id' => $showTable->id,
+ 'show_field_id' => $showField->id,
+ 'relation_type_id' => $relationType->id,
+ ]
+ );
+ }
}
diff --git a/src/Pckg/Migration/Helper/Lists.php b/src/Pckg/Migration/Helper/Lists.php
index 57cefb0..d8b157c 100644
--- a/src/Pckg/Migration/Helper/Lists.php
+++ b/src/Pckg/Migration/Helper/Lists.php
@@ -1,4 +1,6 @@
- $id]);
+ }
- /**
- * @param $id
- *
- * @return ListRecord
- */
- protected function list($id)
- {
- return ListRecord::getOrCreate(['id' => $id]);
- }
-
- /**
- * @param $list
- * @param $slug
- *
- * @return ListItemRecord
- */
- protected function listItem($list, $slug)
- {
- $list = $this->list($list);
-
- return ListItemRecord::getOrCreate(['list_id' => $list->id, 'slug' => $slug]);
- }
+ /**
+ * @param $list
+ * @param $slug
+ *
+ * @return ListItemRecord
+ */
+ protected function listItem($list, $slug)
+ {
+ $list = $this->list($list);
+ return ListItemRecord::getOrCreate(['list_id' => $list->id, 'slug' => $slug]);
+ }
}
diff --git a/src/Pckg/Migration/Helper/Translations.php b/src/Pckg/Migration/Helper/Translations.php
index 3d4307d..449e2ab 100644
--- a/src/Pckg/Migration/Helper/Translations.php
+++ b/src/Pckg/Migration/Helper/Translations.php
@@ -1,4 +1,6 @@
- $slug]);
- foreach ($translations as $language => $translation) {
- $t->value = $translation;
- $t->language_id = $language;
- $t->save();
- }
- }
-
+ $t = Translation::getOrCreate(['slug' => $slug]);
+ foreach ($translations as $language => $translation) {
+ $t->value = $translation;
+ $t->language_id = $language;
+ $t->save();
+ }
+ }
}
diff --git a/src/Pckg/Migration/Migration.php b/src/Pckg/Migration/Migration.php
index 000213f..fbe265b 100644
--- a/src/Pckg/Migration/Migration.php
+++ b/src/Pckg/Migration/Migration.php
@@ -5,133 +5,206 @@
use Pckg\Database\Repository;
use Pckg\Migration\Command\ExecuteMigration;
+/**
+ * Class Migration
+ *
+ * @package Pckg\Migration
+ */
class Migration
{
-
- protected $tables = [];
-
- protected $repository = Repository::class;
-
- protected $fields = true;
-
- protected $relations = true;
-
- public function onlyFields()
- {
- $this->fields = true;
- $this->relations = false;
-
- return $this;
- }
-
- public function table($table, $id = true, $primary = true)
- {
- $table = new Table($table);
-
- $this->tables[] = $table;
-
- if ($id) {
- $table->id('id', $primary);
- }
-
- return $table;
- }
-
- public function getTables()
- {
- return $this->tables;
- }
-
- public function getRepository()
- {
- return $this->repository;
- }
-
- public function setRepository($repository)
- {
- $this->repository = $repository;
-
- return $this;
- }
-
- public function up()
- {
- return $this;
- }
-
- public function dependencies()
- {
- return [];
- }
-
- public function partials()
- {
- return [];
- }
-
- public function afterFirstUp()
- {
- return $this;
- }
-
- public function save()
- {
- $executeMigration = (new ExecuteMigration($this));
-
- if (!$this->relations) {
- $executeMigration->onlyFields();
- }
-
- $executeMigration->execute();
-
- $this->tables = [];
- }
-
- public function translatable($table, $suffix = '_i18n')
- {
- $translatable = new Table($table . $suffix);
- $this->tables[] = $translatable;
-
- $translatable->id('id', false)->references($table)->required();
- $translatable->varchar('language_id', 2)->references('languages', 'slug')->required();
-
- $translatable->primary('id', 'language_id');
-
- return $translatable;
- }
-
- public function permissiontable($table, $suffix = '_p17n')
- {
- $permissiontable = new Table($table . $suffix);
- $this->tables[] = $permissiontable;
-
- $permissiontable->id('id', false)->references($table);
- $permissiontable->integer('user_group_id')->references('user_groups');
- $permissiontable->varchar('action', 32)->required();
-
- /**
- * @T00D00 - add double index
- */
-
- return $permissiontable;
- }
-
- public function morphtable($table, $morph, $suffix = '_morphs')
- {
- $morphtable = new Table($table . $suffix);
- $this->tables[] = $morphtable;
-
- $morphtable->id('id');
- $morphtable->integer($morph)->references($table);
- $morphtable->varchar('morph_id');
- $morphtable->varchar('poly_id');
-
- return $morphtable;
- }
-
- public function output($msg)
- {
- echo $msg . "\n";
- }
-
+ /**
+ * @var array
+ */
+ protected $tables = [];
+
+ /**
+ * @var string
+ */
+ protected $repository = Repository::class;
+
+ /**
+ * @var bool
+ */
+ protected $fields = true;
+
+ /**
+ * @var bool
+ */
+ protected $relations = true;
+
+ /**
+ * @return $this
+ */
+ public function onlyFields()
+ {
+ $this->fields = true;
+ $this->relations = false;
+
+ return $this;
+ }
+
+ /**
+ * @param $table
+ * @param bool $id
+ * @param bool $primary
+ *
+ * @return Table
+ */
+ public function table($table, $id = true, $primary = true)
+ {
+ $table = new Table($table);
+
+ $this->tables[] = $table;
+
+ if ($id) {
+ $table->id('id', $primary);
+ }
+
+ return $table;
+ }
+
+ /**
+ * @return array
+ */
+ public function getTables()
+ {
+ return $this->tables;
+ }
+
+ /**
+ * @return string
+ */
+ public function getRepository()
+ {
+ return $this->repository;
+ }
+
+ /**
+ * @param $repository
+ *
+ * @return $this
+ */
+ public function setRepository($repository)
+ {
+ $this->repository = $repository;
+
+ return $this;
+ }
+
+ /**
+ * @return $this
+ */
+ public function up()
+ {
+ return $this;
+ }
+
+ /**
+ * @return array
+ */
+ public function dependencies()
+ {
+ return [];
+ }
+
+ /**
+ * @return array
+ */
+ public function partials()
+ {
+ return [];
+ }
+
+ /**
+ * @return $this
+ */
+ public function afterFirstUp()
+ {
+ return $this;
+ }
+
+ /**
+ *
+ */
+ public function save()
+ {
+ $executeMigration = (new ExecuteMigration($this));
+
+ if (!$this->relations) {
+ $executeMigration->onlyFields();
+ }
+
+ $executeMigration->execute();
+
+ $this->tables = [];
+ }
+
+ /**
+ * @param $table
+ * @param string $suffix
+ *
+ * @return Table
+ */
+ public function translatable($table, $suffix = '_i18n')
+ {
+ $translatable = new Table($table . $suffix);
+ $this->tables[] = $translatable;
+
+ $translatable->id('id', false)->references($table)->required();
+ $translatable->varchar('language_id', 2)->references('languages', 'slug')->required();
+
+ $translatable->primary('id', 'language_id');
+
+ return $translatable;
+ }
+
+ /**
+ * @param $table
+ * @param string $suffix
+ *
+ * @return Table
+ */
+ public function permissiontable($table, $suffix = '_p17n')
+ {
+ $permissiontable = new Table($table . $suffix);
+ $this->tables[] = $permissiontable;
+
+ $permissiontable->id('id', false)->references($table);
+ $permissiontable->integer('user_group_id')->references('user_groups');
+ $permissiontable->varchar('action', 32)->required();
+
+ /**
+ * @T00D00 - add double index
+ */
+
+ return $permissiontable;
+ }
+
+ /**
+ * @param $table
+ * @param $morph
+ * @param string $suffix
+ *
+ * @return Table
+ */
+ public function morphtable($table, $morph, $suffix = '_morphs')
+ {
+ $morphtable = new Table($table . $suffix);
+ $this->tables[] = $morphtable;
+
+ $morphtable->id('id');
+ $morphtable->integer($morph)->references($table);
+ $morphtable->varchar('morph_id');
+ $morphtable->varchar('poly_id');
+
+ return $morphtable;
+ }
+
+ /**
+ * @param $msg
+ */
+ public function output($msg)
+ {
+ echo $msg . "\n";
+ }
}
\ No newline at end of file
diff --git a/src/Pckg/Migration/Provider/Migration.php b/src/Pckg/Migration/Provider/Migration.php
index a396f85..74e629d 100644
--- a/src/Pckg/Migration/Provider/Migration.php
+++ b/src/Pckg/Migration/Provider/Migration.php
@@ -1,18 +1,26 @@
-field = $field;
- $this->references = $references;
- $this->on = $on;
- }
-
- public function getName()
- {
- $name = $this->field->getTable()->getName() . '__' . $this->field->getName();
- if (strlen($name) > 55) {
- $name = substr($name, -55);
- }
-
- return 'FOREIGN__' . $name;
- }
-
- public function getReferences()
- {
- return $this->references;
- }
-
- public function getOn()
- {
- return $this->on;
- }
-
- public function getField()
- {
- return $this->field;
- }
-
- public function getOnDelete()
- {
- return $this->onDelete;
- }
-
- public function getOnUpdate()
- {
- return $this->onUpdate;
- }
-
- public function onDelete($action = self::RESTRICT)
- {
- $this->onDelete = $action;
-
- return $this;
- }
-
- public function onUpdate($action = self::CASCADE)
- {
- $this->onUpdate = $action;
-
- return $this;
- }
-
- public function getSql()
- {
- return 'CONSTRAINT `' . $this->getName()
- . '` FOREIGN KEY (`' . $this->getField()->getName() . '`) ' .
- 'REFERENCES `' . $this->getReferences() . '`(`' . $this->getOn() . '`) ' .
- 'ON DELETE ' . $this->getOnDelete() . ' ' .
- 'ON UPDATE ' . $this->getOnUpdate();
- }
-
- public function getSqlByParams($field, $references, $on, $onDelete, $onUpdate)
- {
- return 'CONSTRAINT `' . $this->getName()
- . '` FOREIGN KEY (`' . $field . '`) ' .
- 'REFERENCES `' . $references . '`(`' . $on . '`) ' .
- 'ON DELETE ' . $onDelete . ' ' .
- 'ON UPDATE ' . $onUpdate;
- }
-
+ const RESTRICT = 'RESTRICT';
+ const CASCADE = 'CASCADE';
+ const SET_NULL = 'SET NULL';
+ const NO_ACTION = 'NO ACTION';
+
+ /**
+ * @var Field
+ */
+ protected $field;
+
+ /**
+ * @var
+ */
+ protected $references;
+
+ /**
+ * @var
+ */
+ protected $on;
+
+ /**
+ * @var string
+ */
+ protected $onDelete = self::RESTRICT;
+
+ /**
+ * @var string
+ */
+ protected $onUpdate = self::CASCADE;
+
+ /**
+ * Relation constructor.
+ *
+ * @param Field $field
+ * @param $references
+ * @param $on
+ */
+ public function __construct(Field $field, $references, $on)
+ {
+ $this->field = $field;
+ $this->references = $references;
+ $this->on = $on;
+ }
+
+ /**
+ * @return string
+ */
+ public function getName()
+ {
+ $name = $this->field->getTable()->getName() . '__' . $this->field->getName();
+ if (strlen($name) > 55) {
+ $name = substr($name, -55);
+ }
+
+ return 'FOREIGN__' . $name;
+ }
+
+ /**
+ * @return mixed
+ */
+ public function getReferences()
+ {
+ return $this->references;
+ }
+
+ /**
+ * @return mixed
+ */
+ public function getOn()
+ {
+ return $this->on;
+ }
+
+ /**
+ * @return Field
+ */
+ public function getField()
+ {
+ return $this->field;
+ }
+
+ /**
+ * @return string
+ */
+ public function getOnDelete()
+ {
+ return $this->onDelete;
+ }
+
+ /**
+ * @return string
+ */
+ public function getOnUpdate()
+ {
+ return $this->onUpdate;
+ }
+
+ /**
+ * @param string $action
+ *
+ * @return $this
+ */
+ public function onDelete($action = self::RESTRICT)
+ {
+ $this->onDelete = $action;
+
+ return $this;
+ }
+
+ /**
+ * @param string $action
+ *
+ * @return $this
+ */
+ public function onUpdate($action = self::CASCADE)
+ {
+ $this->onUpdate = $action;
+
+ return $this;
+ }
+
+ /**
+ * @return string
+ */
+ public function getSql()
+ {
+ return 'CONSTRAINT `' . $this->getName() . '` FOREIGN KEY (`' . $this->getField()->getName(
+ ) . '`) ' . 'REFERENCES `' . $this->getReferences() . '`(`' . $this->getOn(
+ ) . '`) ' . 'ON DELETE ' . $this->getOnDelete() . ' ' . 'ON UPDATE ' . $this->getOnUpdate();
+ }
+
+ /**
+ * @param $field
+ * @param $references
+ * @param $on
+ * @param $onDelete
+ * @param $onUpdate
+ *
+ * @return string
+ */
+ public function getSqlByParams($field, $references, $on, $onDelete, $onUpdate)
+ {
+ return 'CONSTRAINT `' . $this->getName(
+ ) . '` FOREIGN KEY (`' . $field . '`) ' . 'REFERENCES `' . $references . '`(`' . $on . '`) ' . 'ON DELETE ' . $onDelete . ' ' . 'ON UPDATE ' . $onUpdate;
+ }
}
\ No newline at end of file
diff --git a/src/Pckg/Migration/Table.php b/src/Pckg/Migration/Table.php
index 010b859..2116fd3 100644
--- a/src/Pckg/Migration/Table.php
+++ b/src/Pckg/Migration/Table.php
@@ -1,4 +1,6 @@
-name = $name;
- }
-
- public function getName()
- {
- return $this->name;
- }
-
- // getters
-
- public function getFields()
- {
- return $this->fields;
- }
-
- public function getConstraints()
- {
- return $this->constraints;
- }
-
- public function getRelations()
- {
- return $this->relations;
- }
-
- // adders
-
- public function addRelation(Relation $relation)
- {
- $this->relations[] = $relation;
-
- return $this;
- }
-
- public function addConstraint(Constraint $constraint)
- {
- $this->constraints[] = $constraint;
-
- return $this;
- }
-
- public function addField(Field $field)
- {
- $this->fields[] = $field;
-
- return $this;
- }
-
- // fields
-
- public function id($name = 'id', $primary = true)
- {
- $id = new Id($this, $name);
-
- if ($primary) {
- $id->primary();
- } else {
- $id->autoincrement(false);
- }
-
- $this->fields[] = $id;
-
- return $id;
- }
-
- public function idString($name = 'id', $primary = true)
- {
- $id = new IdString($this, $name);
-
- if ($primary) {
- $id->primary();
- }
-
- $this->fields[] = $id;
-
- return $id;
- }
-
- public function varchar($name, $length = 128)
- {
- $varchar = new Varchar($this, $name);
-
- $this->fields[] = $varchar;
-
- $varchar->length($length);
-
- return $varchar;
- }
-
- public function slug($name = 'slug', $length = 128, $unique = true)
- {
- $field = $this->varchar($name, $length);
-
- if ($unique) {
- $this->unique($name);
- }
-
- return $field;
- }
-
- public function title($name = 'title', $length = 128)
- {
- return $this->varchar($name, $length);
- }
-
- public function range($name, $type = 'datetime')
- {
- $this->{$type}($name . '_from')->nullable();
- $this->{$type}($name . '_to')->nullable();
-
- return $this;
- }
-
- public function point($name)
- {
- $point = new Point($this, $name);
-
- $this->fields[] = $point;
-
- return $point;
- }
-
- public function subtitle($name = 'subtitle')
- {
- return $this->text($name);
- }
-
- public function lead($name = 'lead')
- {
- return $this->text($name);
- }
-
- public function content($name = 'content')
- {
- return $this->text($name);
- }
-
- public function boolean($name, $default = null)
- {
- $boolean = new Boolean($this, $name);
-
- $this->fields[] = $boolean;
-
- $boolean->setDefault($default);
-
- return $boolean;
- }
-
- public function text($name)
- {
- $text = new Text($this, $name);
-
- $this->fields[] = $text;
-
- return $text;
- }
-
- public function description($name = 'description')
- {
- return $this->text($name);
- }
-
- public function email($name = 'email')
- {
- return $this->varchar($name);
- }
-
- public function password($name = 'password', $length = 40)
- {
- return $this->varchar($name, $length);
- }
-
- public function integer($name, $length = 11)
- {
- $integer = new Integer($this, $name);
-
- $this->fields[] = $integer;
-
- $integer->length($length);
-
- return $integer;
- }
-
- public function decimal($name, $length = [8, 2])
- {
- $decimal = new Decimal($this, $name);
-
- $this->fields[] = $decimal;
-
- $decimal->length($length);
-
- return $decimal;
- }
-
- public function parent($name = 'parent_id')
- {
- $parent = $this->integer($name);
-
- $parent->references($this->name)->nullable();
-
- return $parent;
- }
-
- public function datetime($name)
- {
- $datetime = new Datetime($this, $name);
-
- $this->fields[] = $datetime;
-
- return $datetime;
- }
-
- public function language()
- {
- $language = $this->varchar('language_id', 2)->references('languages', 'slug');
-
- return $language;
- }
-
- // groups
-
- public function timeable()
- {
- $timeable = new Timeable($this);
-
- return $timeable;
- }
-
- public function orderable()
- {
- $orderable = new Orderable($this);
-
- return $orderable;
- }
-
- public function hideable()
- {
- $hidable = new Hidable($this);
-
- return $hidable;
- }
-
- public function deletable()
- {
- $deletable = new Deletable($this);
-
- return $deletable;
- }
-
- // index, primary, unique
-
- public function primary(...$fields)
- {
- $primary = new Primary($this, ...$fields);
-
- $this->constraints[] = $primary;
-
- return $primary;
- }
-
- public function index(...$fields)
- {
- $index = new Index($this, ...$fields);
-
- $this->constraints[] = $index;
-
- return $index;
- }
-
- public function unique(...$fields)
- {
- $unique = new Unique($this, ...$fields);
-
- $this->constraints[] = $unique;
-
- return $unique;
- }
-
+ /**
+ * @var
+ */
+ protected $name;
+
+ /**
+ * @var array
+ */
+ protected $fields = [];
+
+ /**
+ * @var array
+ */
+ protected $constraints = [];
+
+ /**
+ * @var array
+ */
+ protected $relations = [];
+
+ /**
+ * Table constructor.
+ *
+ * @param $name
+ */
+ public function __construct($name)
+ {
+ $this->name = $name;
+ }
+
+ /**
+ * @return mixed
+ */
+ public function getName()
+ {
+ return $this->name;
+ }
+
+ // getters
+
+ /**
+ * @return array
+ */
+ public function getFields()
+ {
+ return $this->fields;
+ }
+
+ /**
+ * @return array
+ */
+ public function getConstraints()
+ {
+ return $this->constraints;
+ }
+
+ /**
+ * @return array
+ */
+ public function getRelations()
+ {
+ return $this->relations;
+ }
+
+ // adders
+
+ /**
+ * @param Relation $relation
+ *
+ * @return $this
+ */
+ public function addRelation(Relation $relation)
+ {
+ $this->relations[] = $relation;
+
+ return $this;
+ }
+
+ /**
+ * @param Constraint $constraint
+ *
+ * @return $this
+ */
+ public function addConstraint(Constraint $constraint)
+ {
+ $this->constraints[] = $constraint;
+
+ return $this;
+ }
+
+ /**
+ * @param Field $field
+ *
+ * @return $this
+ */
+ public function addField(Field $field)
+ {
+ $this->fields[] = $field;
+
+ return $this;
+ }
+
+ // fields
+
+ /**
+ * @param string $name
+ * @param bool $primary
+ *
+ * @return Id
+ */
+ public function id($name = 'id', $primary = true)
+ {
+ $id = new Id($this, $name);
+
+ if ($primary) {
+ $id->primary();
+ } else {
+ $id->autoincrement(false);
+ }
+
+ $this->fields[] = $id;
+
+ return $id;
+ }
+
+ /**
+ * @param string $name
+ * @param bool $primary
+ *
+ * @return IdString
+ */
+ public function idString($name = 'id', $primary = true)
+ {
+ $id = new IdString($this, $name);
+
+ if ($primary) {
+ $id->primary();
+ }
+
+ $this->fields[] = $id;
+
+ return $id;
+ }
+
+ /**
+ * @param $name
+ * @param int $length
+ *
+ * @return Varchar
+ */
+ public function varchar($name, $length = 128)
+ {
+ $varchar = new Varchar($this, $name);
+
+ $this->fields[] = $varchar;
+
+ $varchar->length($length);
+
+ return $varchar;
+ }
+
+ /**
+ * @param string $name
+ * @param int $length
+ * @param bool $unique
+ *
+ * @return Varchar
+ */
+ public function slug($name = 'slug', $length = 128, $unique = true)
+ {
+ $field = $this->varchar($name, $length);
+
+ if ($unique) {
+ $this->unique($name);
+ }
+
+ return $field;
+ }
+
+ /**
+ * @param string $name
+ * @param int $length
+ *
+ * @return Varchar
+ */
+ public function title($name = 'title', $length = 128)
+ {
+ return $this->varchar($name, $length);
+ }
+
+ /**
+ * @param $name
+ * @param string $type
+ *
+ * @return $this
+ */
+ public function range($name, $type = 'datetime')
+ {
+ $this->{$type}($name . '_from')->nullable();
+ $this->{$type}($name . '_to')->nullable();
+
+ return $this;
+ }
+
+ /**
+ * @param $name
+ *
+ * @return Point
+ */
+ public function point($name)
+ {
+ $point = new Point($this, $name);
+
+ $this->fields[] = $point;
+
+ return $point;
+ }
+
+ /**
+ * @param string $name
+ *
+ * @return Text
+ */
+ public function subtitle($name = 'subtitle')
+ {
+ return $this->text($name);
+ }
+
+ /**
+ * @param string $name
+ *
+ * @return Text
+ */
+ public function lead($name = 'lead')
+ {
+ return $this->text($name);
+ }
+
+ /**
+ * @param string $name
+ *
+ * @return Text
+ */
+ public function content($name = 'content')
+ {
+ return $this->text($name);
+ }
+
+ /**
+ * @param $name
+ * @param null $default
+ *
+ * @return Boolean
+ */
+ public function boolean($name, $default = null)
+ {
+ $boolean = new Boolean($this, $name);
+
+ $this->fields[] = $boolean;
+
+ $boolean->setDefault($default);
+
+ return $boolean;
+ }
+
+ /**
+ * @param $name
+ *
+ * @return Text
+ */
+ public function text($name)
+ {
+ $text = new Text($this, $name);
+
+ $this->fields[] = $text;
+
+ return $text;
+ }
+
+ /**
+ * @param string $name
+ *
+ * @return Text
+ */
+ public function description($name = 'description')
+ {
+ return $this->text($name);
+ }
+
+ /**
+ * @param string $name
+ *
+ * @return Varchar
+ */
+ public function email($name = 'email')
+ {
+ return $this->varchar($name);
+ }
+
+ /**
+ * @param string $name
+ * @param int $length
+ *
+ * @return Varchar
+ */
+ public function password($name = 'password', $length = 40)
+ {
+ return $this->varchar($name, $length);
+ }
+
+ /**
+ * @param $name
+ * @param int $length
+ *
+ * @return Integer
+ */
+ public function integer($name, $length = 11)
+ {
+ $integer = new Integer($this, $name);
+
+ $this->fields[] = $integer;
+
+ $integer->length($length);
+
+ return $integer;
+ }
+
+ /**
+ * @param $name
+ * @param array $length
+ *
+ * @return Decimal
+ */
+ public function decimal($name, $length = [8, 2])
+ {
+ $decimal = new Decimal($this, $name);
+
+ $this->fields[] = $decimal;
+
+ $decimal->length($length);
+
+ return $decimal;
+ }
+
+ /**
+ * @param string $name
+ *
+ * @return int
+ */
+ public function parent($name = 'parent_id')
+ {
+ $parent = $this->integer($name);
+
+ $parent->references($this->name)->nullable();
+
+ return $parent;
+ }
+
+ /**
+ * @param $name
+ *
+ * @return Datetime
+ */
+ public function datetime($name)
+ {
+ $datetime = new Datetime($this, $name);
+
+ $this->fields[] = $datetime;
+
+ return $datetime;
+ }
+
+ /**
+ * @return $this
+ */
+ public function language()
+ {
+ $language = $this->varchar('language_id', 2)->references('languages', 'slug');
+
+ return $language;
+ }
+
+ // groups
+
+ /**
+ * @return Timeable
+ */
+ public function timeable()
+ {
+ $timeable = new Timeable($this);
+
+ return $timeable;
+ }
+
+ /**
+ * @return Orderable
+ */
+ public function orderable()
+ {
+ $orderable = new Orderable($this);
+
+ return $orderable;
+ }
+
+ /**
+ * @return Hidable
+ */
+ public function hideable()
+ {
+ $hidable = new Hidable($this);
+
+ return $hidable;
+ }
+
+ /**
+ * @return Deletable
+ */
+ public function deletable()
+ {
+ $deletable = new Deletable($this);
+
+ return $deletable;
+ }
+
+ // index, primary, unique
+
+ /**
+ * @param array ...$fields
+ *
+ * @return Primary
+ */
+ public function primary(...$fields)
+ {
+ $primary = new Primary($this, ...$fields);
+
+ $this->constraints[] = $primary;
+
+ return $primary;
+ }
+
+ /**
+ * @param array ...$fields
+ *
+ * @return Index
+ */
+ public function index(...$fields)
+ {
+ $index = new Index($this, ...$fields);
+
+ $this->constraints[] = $index;
+
+ return $index;
+ }
+
+ /**
+ * @param array ...$fields
+ *
+ * @return Unique
+ */
+ public function unique(...$fields)
+ {
+ $unique = new Unique($this, ...$fields);
+
+ $this->constraints[] = $unique;
+
+ return $unique;
+ }
}
\ No newline at end of file