diff --git a/meta/i18n/en/lean.storage.json b/meta/i18n/en/lean.storage.json index 3459fd2..5c1d124 100644 --- a/meta/i18n/en/lean.storage.json +++ b/meta/i18n/en/lean.storage.json @@ -18,7 +18,9 @@ "table-fields" : "Fields in table", "create": "Create table", "createAll": "Create all tables", - "query": "SQL Query" + "query": "SQL Query", + "alter": "Alter table", + "alterAll": "Alter all tables" } } diff --git a/src/Form/Storage/DatabaseMigration.php b/src/Form/Storage/DatabaseMigration.php index e70158c..1361100 100644 --- a/src/Form/Storage/DatabaseMigration.php +++ b/src/Form/Storage/DatabaseMigration.php @@ -15,6 +15,8 @@ class DatabaseMigration implements FormInterface { protected ? Lib\DatabaseMigrations $migration; + protected array $alter = []; + public function __construct(Lib\DatabaseMigrations $migration) { $this->migration = $migration; @@ -25,6 +27,8 @@ class DatabaseMigration implements FormInterface $context->tableExist = []; foreach($this->migration->entities as $entity => $table) { + $previous = null; + $this->alter[$entity] = []; $connection = $entity::resolveEntity()->sqlAdapter(); $adapter = $connection->adapter(); @@ -35,21 +39,31 @@ class DatabaseMigration implements FormInterface continue; } + # Query current table's columns $tableEntity = $adapter->schemaTable($connection, $databaseName, $tableName); if ( $tableEntity ) { - #foreach($tableEntity->columns as $field => $definition) { - # if ( $table->columns->filtersCollection(fn($e) => $e->name === $field)->count() === 0 ) { - # $alter[$field] = $definition; - # - #} - - $alter =false; + foreach($table->fieldList(EntityResolver::KEY_COLUMN_NAME, true) as $field => $definition) { + if ( $tableEntity->columns->filtersCollection(fn($e) => $e->name === $field)->count() === 0 ) { + $this->alter[$entity][$field] = [ + 'previous' => $previous, + 'action' => 'add', + 'definition' => $definition, + ]; + } - if ( $alter ) { + $previous = [ + 'field' => $field, + 'definition' => $definition + ]; + } + + if ( $this->alter[$entity] ) { $context->status[$entity] = [ - 'msg' => "alter", - "query" => $entity::repository()->alterSqlQuery($alter)->getSqlQuery(true), + 'msg' => "altering", + "query" => $entity::repository() + ->alterSqlQuery($this->alter[$entity]) + ->getSqlQuery(true), ]; } else { @@ -63,9 +77,10 @@ class DatabaseMigration implements FormInterface else { $context->status[$entity] = [ 'msg' => "unexisting", - 'query' => $entity::repository()->createSqlQuery()->getSqlQuery(true), + 'query' => $entity::repository() + ->createSqlQuery() + ->getSqlQuery(true), ]; - } } @@ -81,16 +96,16 @@ class DatabaseMigration implements FormInterface public function execute(FormContextInterface $context) : void { - if ( $context->create ?? false ) { - $context->create::repository()->createTable(); - } - elseif ( $context->createAll ?? false ) { - foreach(explode(',', $context->createAll) as $entity) { - $entity::repository()->createTable(); - } - } - elseif ( $context->alter ?? false ) { + $create = $context->createAll ? explode(',', $context->createAll) : [ $context->create ]; + foreach(array_filter($create) as $entity) { + $entity::repository()->createTable(); + } + + $alter = $context->alterAll ? explode(',', $context->alterAll) : [ $context->alter ]; + + foreach(array_filter($alter) as $entity) { + $entity::repository()->alterTable($this->alter[$entity]); } $this->initialize($context); diff --git a/view/lean-console/page/storage/database.phtml b/view/lean-console/page/storage/database.phtml index 01305ee..61dc0bf 100644 --- a/view/lean-console/page/storage/database.phtml +++ b/view/lean-console/page/storage/database.phtml @@ -65,7 +65,7 @@ {% php $alter[] = $entity %}