- WIP on Alter table

This commit is contained in:
Dave M. 2023-03-30 15:25:27 +00:00
parent f9229b890d
commit 963598764a
3 changed files with 46 additions and 23 deletions

View File

@ -18,7 +18,9 @@
"table-fields" : "Fields in table", "table-fields" : "Fields in table",
"create": "Create table", "create": "Create table",
"createAll": "Create all tables", "createAll": "Create all tables",
"query": "SQL Query" "query": "SQL Query",
"alter": "Alter table",
"alterAll": "Alter all tables"
} }
} }

View File

@ -15,6 +15,8 @@ class DatabaseMigration implements FormInterface
{ {
protected ? Lib\DatabaseMigrations $migration; protected ? Lib\DatabaseMigrations $migration;
protected array $alter = [];
public function __construct(Lib\DatabaseMigrations $migration) public function __construct(Lib\DatabaseMigrations $migration)
{ {
$this->migration = $migration; $this->migration = $migration;
@ -25,6 +27,8 @@ class DatabaseMigration implements FormInterface
$context->tableExist = []; $context->tableExist = [];
foreach($this->migration->entities as $entity => $table) { foreach($this->migration->entities as $entity => $table) {
$previous = null;
$this->alter[$entity] = [];
$connection = $entity::resolveEntity()->sqlAdapter(); $connection = $entity::resolveEntity()->sqlAdapter();
$adapter = $connection->adapter(); $adapter = $connection->adapter();
@ -35,21 +39,31 @@ class DatabaseMigration implements FormInterface
continue; continue;
} }
# Query current table's columns
$tableEntity = $adapter->schemaTable($connection, $databaseName, $tableName); $tableEntity = $adapter->schemaTable($connection, $databaseName, $tableName);
if ( $tableEntity ) { if ( $tableEntity ) {
#foreach($tableEntity->columns as $field => $definition) { foreach($table->fieldList(EntityResolver::KEY_COLUMN_NAME, true) as $field => $definition) {
# if ( $table->columns->filtersCollection(fn($e) => $e->name === $field)->count() === 0 ) { if ( $tableEntity->columns->filtersCollection(fn($e) => $e->name === $field)->count() === 0 ) {
# $alter[$field] = $definition; $this->alter[$entity][$field] = [
# 'previous' => $previous,
#} 'action' => 'add',
'definition' => $definition,
$alter =false; ];
}
if ( $alter ) { $previous = [
'field' => $field,
'definition' => $definition
];
}
if ( $this->alter[$entity] ) {
$context->status[$entity] = [ $context->status[$entity] = [
'msg' => "alter", 'msg' => "altering",
"query" => $entity::repository()->alterSqlQuery($alter)->getSqlQuery(true), "query" => $entity::repository()
->alterSqlQuery($this->alter[$entity])
->getSqlQuery(true),
]; ];
} }
else { else {
@ -63,9 +77,10 @@ class DatabaseMigration implements FormInterface
else { else {
$context->status[$entity] = [ $context->status[$entity] = [
'msg' => "unexisting", '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 public function execute(FormContextInterface $context) : void
{ {
if ( $context->create ?? false ) { $create = $context->createAll ? explode(',', $context->createAll) : [ $context->create ];
$context->create::repository()->createTable();
}
elseif ( $context->createAll ?? false ) {
foreach(explode(',', $context->createAll) as $entity) {
$entity::repository()->createTable();
}
}
elseif ( $context->alter ?? false ) {
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); $this->initialize($context);

View File

@ -65,7 +65,7 @@
{% php $alter[] = $entity %} {% php $alter[] = $entity %}
<div class="text-right"> <div class="text-right">
<button name="create" value="{{$entity}}" class="btn-blue">🗸 {% _ 'database.table.alter' %}</button> <button name="alter" value="{{$entity}}" class="btn-blue">🗸 {% _ 'database.table.alter' %}</button>
</div> </div>
{% break %} {% break %}
{% endswitch %} {% endswitch %}
@ -78,6 +78,12 @@
<button name="createAll" value="{{ implode(',', $createAll) }}" class="btn-red">🗸 {% _ 'database.table.createAll' %}</button> <button name="createAll" value="{{ implode(',', $createAll) }}" class="btn-red">🗸 {% _ 'database.table.createAll' %}</button>
</div> </div>
{% endif %} {% endif %}
{% if ($alterAll ?? 0) > 1 %}
<div class="text-right" style="padding:0.5rem 0">
<button name="createAll" value="{{ implode(',', $alterAll) }}" class="btn-red">🗸 {% _ 'database.table.alterAll' %}</button>
</div>
{% endif %}
{% ui.endform %} {% ui.endform %}
</div> </div>
</article> </article>