- WIP on Alter table
This commit is contained in:
parent
f9229b890d
commit
963598764a
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -65,7 +65,7 @@
|
|||
{% php $alter[] = $entity %}
|
||||
|
||||
<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>
|
||||
{% break %}
|
||||
{% endswitch %}
|
||||
|
@ -78,6 +78,12 @@
|
|||
<button name="createAll" value="{{ implode(',', $createAll) }}" class="btn-red">🗸 {% _ 'database.table.createAll' %}</button>
|
||||
</div>
|
||||
{% 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 %}
|
||||
</div>
|
||||
</article>
|
||||
|
|
Loading…
Reference in New Issue