diff --git a/src/Form/Database.php b/src/Form/Database.php new file mode 100644 index 0000000..ff1ef14 --- /dev/null +++ b/src/Form/Database.php @@ -0,0 +1,122 @@ +<?php + +namespace Lean\Console\Form; + +use Picea\Ui\Method\{ FormInterface, FormContext, FormContextInterface }; + +use \Lean\Console\Lib; + +use Picea\Compiler\Context; +use Psr\Http\Message\ServerRequestInterface; +use Ulmus\Common\EntityResolver; +use \Ulmus\Entity\InformationSchema\Table; + +class Database implements FormInterface +{ + protected ? Lib\DatabaseMigrations $migration; + + public function __construct(Lib\DatabaseMigrations $migration) + { + $this->migration = $migration; + } + + public function initialize(FormContextInterface $context) : void + { + $context->tableExist = []; + + foreach($this->migration->entities as $entity => $table) { + $adapter = $entity::resolveEntity()->sqlAdapter()->adapter(); + + $tableName = $table->tableName(); + $databaseName = $table->databaseName(); + + if ( ! $tableName || ! $databaseName ) { + continue; + } + +#<<<<<<< HEAD + $tableEntity = $adapter->schemaTable($databaseName, $tableName); + + if ( $tableEntity ) { + #$fields = $entiy::resolveEntity()->fieldList(); + + foreach($tableEntity->columns as $col) { + //dump( $entity::field($col->name) ); +#======= +# $table = Table::repository() +# ->where(Table::field('name'), $table->tableName()) +# ->where(Table::field('schema'), $table->databaseName()) +# ->loadOne(); + +# if ( $table ) { +# $fields = $entity::resolveEntity()->fieldList(EntityResolver::KEY_COLUMN_NAME, true); + +# $alter = []; + +# foreach($fields as $field => $definition) { +# if ( $table->columns->filtersCollection(fn($e) => $e->name === $field)->count() === 0 ) { +# $alter[$field] = $definition; +# } +#>>>>>>> 7194a33bec884d745e838c8ac2693b353e95a3ce + } + + if ( $alter ) { + $context->status[$entity] = [ + 'msg' => "alter", + "query" => $entity::repository()->alterSqlQuery($alter)->getSqlQuery(true), + ]; + } + else { + # if columns are different + $context->status[$entity] = [ + 'msg' => "up-to-date", + 'query' => "", + ]; + } + } + else { + $context->status[$entity] = [ + 'msg' => "unexisting", + 'query' => $entity::repository()->createSqlQuery()->getSqlQuery(true), + ]; + + } + } + + if ( $context->formSent() ) { + + } + } + + public function validate(FormContextInterface $context) : bool + { + return $context->valid(); + } + + 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 ) { + + } + + $this->initialize($context); + } + + public function getContext(ServerRequestInterface $request) : FormContextInterface + { + return new class($request) extends FormContext { + + public array $status = []; + + public array $actions = []; + }; + } +}