Compare commits
3
Commits
ce2b9399c2
...
v1.0.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d8b208426a | ||
|
|
3a8e56a617 | ||
|
|
39b02507a3 |
No files matched your search
@@ -1,122 +0,0 @@
|
||||
<?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 = [];
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,163 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Lean\Console\Form\Storage;
|
||||
|
||||
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 DatabaseMigration implements FormInterface
|
||||
{
|
||||
public function __construct(
|
||||
protected ? Lib\DatabaseMigrations $migration,
|
||||
protected null|Lib\Database\DefinitionCollection $definition = null,
|
||||
)
|
||||
{
|
||||
$this->definition ??= new Lib\Database\DefinitionCollection();
|
||||
}
|
||||
|
||||
public function initialize(FormContextInterface $context) : void
|
||||
{
|
||||
foreach($this->migration->entities as $entity => $table) {
|
||||
$previous = null;
|
||||
|
||||
$connection = $entity::resolveEntity()->sqlAdapter();
|
||||
$adapter = $connection->adapter();
|
||||
$tableName = $table->tableName();
|
||||
$databaseName = $table->databaseName();
|
||||
|
||||
if ( ! $tableName || ! $databaseName ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
# Query current table's columns
|
||||
$tableEntity = $adapter->schemaTable($connection, $databaseName, $tableName);
|
||||
|
||||
$this->definition[$entity]['table'] = $tableDefinition = new Lib\Database\Definition(
|
||||
entity: $entity,
|
||||
modifier: new Lib\Database\Modifier( type: Lib\Database\ModifierTypeEnum::None, ),
|
||||
);
|
||||
|
||||
if ( $tableEntity ) {
|
||||
$this->definition[$entity]['columns'] = new Lib\Database\DefinitionCollection();
|
||||
|
||||
foreach($table->fieldList(EntityResolver::KEY_COLUMN_NAME, true) as $field => $definition) {
|
||||
$this->definition[$entity]['columns'][$field] = $def = new Lib\Database\Definition(
|
||||
entity: $entity,
|
||||
field: $field,
|
||||
definition: $definition,
|
||||
modifier: new Lib\Database\Modifier( type: Lib\Database\ModifierTypeEnum::None, ),
|
||||
previous: $def ?? null,
|
||||
);
|
||||
|
||||
$column = $tableEntity->columns->filtersOne(fn($e) => strtolower($e->name) === $field);
|
||||
|
||||
# Column do not exists
|
||||
if ( $column === null ) {
|
||||
$def->action = "add";
|
||||
|
||||
$def->modifier = new Lib\Database\Modifier(
|
||||
type: Lib\Database\ModifierTypeEnum::Alter,
|
||||
query: $entity::repository()
|
||||
->alterSqlQuery([ $def->toArray() ])
|
||||
->getSqlQuery(true)
|
||||
);
|
||||
}
|
||||
elseif ( ! $column->matchFieldDefinition($definition) ) {
|
||||
$def->action = "change";
|
||||
|
||||
$def->modifier = new Lib\Database\Modifier(
|
||||
type: Lib\Database\ModifierTypeEnum::Alter,
|
||||
query: $entity::repository()
|
||||
->alterSqlQuery([ $def->toArray() ])
|
||||
->getSqlQuery(true)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
# ALTER TABLE `ajustements` CHANGE `annee` `annee` INT(11) NULL;
|
||||
$alter = $this->definition[$entity]['columns']->filterAlter();
|
||||
|
||||
if ( $alter->count() ) {
|
||||
if ( $adapter->splitAlterQuery() ) {
|
||||
$query = $alter->map(fn(Lib\Database\Definition $map) => $entity::repository()
|
||||
->alterSqlQuery([ $map->toArray() ])
|
||||
->getSqlQuery(true));
|
||||
}
|
||||
else {
|
||||
$query = $entity::repository()
|
||||
->alterSqlQuery($alter->map(fn(Lib\Database\Definition $map) => $map->toArray()))
|
||||
->getSqlQuery(true);
|
||||
}
|
||||
|
||||
$tableDefinition->modifier = new Lib\Database\Modifier(
|
||||
type: Lib\Database\ModifierTypeEnum::Alter,
|
||||
query: $query,
|
||||
);
|
||||
}
|
||||
}
|
||||
else {
|
||||
$tableDefinition->modifier = new Lib\Database\Modifier(
|
||||
type: Lib\Database\ModifierTypeEnum::Create,
|
||||
query: $entity::repository()
|
||||
->createSqlQuery()
|
||||
->getSqlQuery(true)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$context->definition = $this->definition;
|
||||
}
|
||||
|
||||
public function validate(FormContextInterface $context) : bool
|
||||
{
|
||||
return $context->valid();
|
||||
}
|
||||
|
||||
public function execute(FormContextInterface $context) : void
|
||||
{
|
||||
$create = $context->createAll ? explode(',', $context->createAll) : [ $context->create ];
|
||||
|
||||
foreach(array_filter($create) as $entity) {
|
||||
if ($this->definition[$entity]['table']->modifier->type === Lib\Database\ModifierTypeEnum::Create) {
|
||||
$entity::repository()->createTable();
|
||||
}
|
||||
}
|
||||
|
||||
$alter = $context->alterAll ? explode(',', $context->alterAll) : [ $context->alter ];
|
||||
|
||||
foreach(array_filter($alter) as $entity) {
|
||||
if ($this->definition[$entity]['table']->modifier->type === Lib\Database\ModifierTypeEnum::Alter) {
|
||||
$adapter = $entity::resolveEntity()->sqlAdapter()->adapter();
|
||||
|
||||
$def = $this->definition[$entity]['columns']->filterAlter();
|
||||
|
||||
if ( $adapter->splitAlterQuery() ) {
|
||||
$def->walk(fn(Lib\Database\Definition $definition) =>
|
||||
$entity::repository()->alterTable([ $definition->toArray() ])
|
||||
);
|
||||
}
|
||||
else {
|
||||
$entity::repository()->alterTable($def->map( fn(Lib\Database\Definition $e) => $e->toArray() ));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->initialize($context);
|
||||
}
|
||||
|
||||
public static function getContext(ServerRequestInterface $request, ? string $formName = null) : FormContextInterface
|
||||
{
|
||||
return new class($request, $formName) extends FormContext {
|
||||
|
||||
public array $status = [];
|
||||
|
||||
public array $actions = [];
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,7 @@ class DatabaseMigrations
|
||||
foreach($this->folderList as $namespace => $folder) {
|
||||
foreach(static::files($folder) as $file) {
|
||||
$name = $file->getBasename("." . $file->getExtension());
|
||||
$subNs = substr($file->getPath(), strlen($folder));
|
||||
$subNs = str_replace('/', '\\', substr($file->getPath(), strlen($folder)));
|
||||
$entity = rtrim($namespace, "\\") . ( $subNs ? "\\$subNs" : "" ) . "\\{$name}";
|
||||
|
||||
if ( ! method_exists($entity, 'resolveEntity') ) {
|
||||
@@ -34,6 +34,8 @@ class DatabaseMigrations
|
||||
if ( $resolve->sqlAdapter()->adapter() instanceof SqlAdapterInterface) {
|
||||
$this->entities[$entity] = $resolve;
|
||||
}
|
||||
else {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user