Skip to content
Snippets Groups Projects
Commit 5e7648ba authored by Laurent Lecluse's avatar Laurent Lecluse
Browse files

nouvelles commandes et améliorations

parent 99b32c14
No related branches found
No related tags found
No related merge requests found
Pipeline #31556 passed
Showing
with 353 additions and 34 deletions
......@@ -55,6 +55,16 @@ return [
'service_manager' => [
'factories' => [
Bdd::class => BddFactory::class,
Command\UpdateBddCommand::class => Command\UpdateBddCommandFactory::class,
Command\UpdateDdlCommand::class => Command\UpdateDdlCommandFactory::class,
],
],
'laminas-cli' => [
'commands' => [
'bddadmin:update-bdd' => Command\UpdateBddCommand::class,
'bddadmin:update-ddl' => Command\UpdateDdlCommand::class,
],
],
];
\ No newline at end of file
<?php
namespace Unicaen\BddAdmin\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Unicaen\BddAdmin\BddAwareTrait;
use Unicaen\BddAdmin\Ddl\Ddl;
/**
* Description of UpdateBddCommand
*
* @author Laurent Lécluse <laurent.lecluse at unicaen.fr>
*/
class UpdateBddCommand extends Command
{
use BddAwareTrait;
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$bdd = $this->getBdd()->setLogger($io);
$io->title('Mise à jour de la base de données');
$io->info('Mise à jour des définitions de la base de données. Merci de patienter ...');
$ref = $bdd->getRefDdl();
// Construction de la config de DDL pour filtrer
$filters = [];
foreach ($ref as $ddlClass => $objects) {
foreach ($objects as $object => $objectDdl) {
$filters[$ddlClass]['includes'][] = $object;
}
}
$tablesDep = [
Ddl::INDEX,
Ddl::PRIMARY_CONSTRAINT,
Ddl::REF_CONSTRAINT,
Ddl::UNIQUE_CONSTRAINT,
];
foreach ($tablesDep as $tableDep) {
$objects = $bdd->manager($tableDep)->get();
foreach ($objects as $obj) {
if (in_array($obj['table'], $filters['table']['includes'])) {
$filters[$tableDep]['includes'][] = $obj['name'];
}
}
}
// Initialisation et lancement de la pré-migration
//$mm = new MigrationManager($oa, $ref, $filters);
//$mm->migration('before');
// Mise à jour de la BDD (structures)
$bdd->alter($ref, $filters, true);
// Mise à jour des séquences
$bdd->majSequences($ref);
// Mise à jour des données
//$bdd->logBegin('Contrôle et mise à jour des données');
//$bdd->dataUpdater()->run('update');
//$bdd->logEnd('Données à jour');
// Post-migration
//$mm->migration('after');
return Command::SUCCESS;
}
}
\ No newline at end of file
<?php
namespace Unicaen\BddAdmin\Command;
use Psr\Container\ContainerInterface;
use Unicaen\BddAdmin\Bdd;
/**
* Description of UpdateBddCommandFactory
*
* @author Laurent Lécluse <laurent.lecluse at unicaen.fr>
*/
class UpdateBddCommandFactory
{
/**
* @param ContainerInterface $container
* @param string $requestedName
* @param array|null $options
*
* @return UpdateBddCommand
*/
public function __invoke(ContainerInterface $container, $requestedName, $options = null): UpdateBddCommand
{
$command = new UpdateBddCommand;
$command->setBdd($container->get(Bdd::class));
return $command;
}
}
\ No newline at end of file
<?php
namespace Unicaen\BddAdmin\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Unicaen\BddAdmin\BddAwareTrait;
/**
* Description of UpdateDdlCommand
*
* @author Laurent Lécluse <laurent.lecluse at unicaen.fr>
*/
class UpdateDdlCommand extends Command
{
use BddAwareTrait;
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$bdd = $this->getBdd()->setLogger($io);
$filters = [
];
$io->title('Génération de la DDL à partir de la base de données');
$ddl = $bdd->getDdl($filters);
$ddl->saveToDir();
return Command::SUCCESS;
}
}
\ No newline at end of file
<?php
namespace Unicaen\BddAdmin\Command;
use Psr\Container\ContainerInterface;
use Unicaen\BddAdmin\Bdd;
/**
* Description of UpdateDdlCommandFactory
*
* @author Laurent Lécluse <laurent.lecluse at unicaen.fr>
*/
class UpdateDdlCommandFactory
{
/**
* @param ContainerInterface $container
* @param string $requestedName
* @param array|null $options
*
* @return UpdateBddCommand
*/
public function __invoke(ContainerInterface $container, $requestedName, $options = null): UpdateDdlCommand
{
$command = new UpdateDdlCommand;
$command->setBdd($container->get(Bdd::class));
return $command;
}
}
\ No newline at end of file
......@@ -488,8 +488,14 @@ class Ddl implements Iterator, ArrayAccess
if ($name != '.' && $name != '..') {
$def = file_get_contents($dir . $type . '/' . $name);
$name = substr($name, 0, -4);
if (str_contains($name, '.')) {
[$schema, $vmname] = Util::explodedFullObjectName($name);
$this->data[$type][$name] = ['schema' => $schema, 'name' => $vmname, 'definition' => $def];
}else{
$this->data[$type][$name] = ['name' => $name, 'definition' => $def];
}
}
}
}
}
......
......@@ -106,6 +106,12 @@ class FunctionManager extends AbstractManager implements FunctionManagerInteface
public function alter(array $old, array $new): void
{
if (!isset($old['schema'])){
$old['schema'] = 'public';
}
if (!isset($new['schema'])){
$new['schema'] = 'public';
}
if ($old != $new) {
if ($this->sendEvent()->getReturn('no-exec')) return;
......
......@@ -78,7 +78,12 @@ class MaterializedViewManager extends AbstractManager implements MaterializedVie
public function alter(array $old, array $new): void
{
if ($this->sendEvent()->getReturn('no-exec')) return;
if (!isset($old['schema'])){
$old['schema'] = 'public';
}
if (!isset($new['schema'])){
$new['schema'] = 'public';
}
if ($old != $new) {
$this->drop($old);
$this->create($new);
......
......@@ -79,6 +79,12 @@ class ProcedureManager extends AbstractManager implements ProcedureManagerIntefa
public function alter(array $old, array $new): void
{
if (!isset($old['schema'])){
$old['schema'] = 'public';
}
if (!isset($new['schema'])){
$new['schema'] = 'public';
}
if ($old != $new) {
if ($this->sendEvent()->getReturn('no-exec')) return;
......
......@@ -80,8 +80,7 @@ class TableManager extends AbstractManager implements TableManagerInterface
$q = "SELECT
ns.nspname \"schema\",
t.relname \"name\",
'N' \"temporary\",
'NO' logging,
t.relpersistence relpersistence,
c.column_name cname,
c.data_type \"type\",
c.character_maximum_length length,
......@@ -113,8 +112,8 @@ class TableManager extends AbstractManager implements TableManagerInterface
$data[$name] = [
'schema' => $paq['schema'],
'name' => $paq['name'],
'temporary' => $paq['temporary'] == 'YES',
'logging' => $paq['logging'] == 'YES',
'temporary' => $paq['relpersistence'] == 't',
'logging' => $paq['relpersistence'] == 'p',
'commentaire' => $paq['commentaire'],
'sequence' => $paq['sequence'],
'columns' => [],
......@@ -578,9 +577,8 @@ class TableManager extends AbstractManager implements TableManagerInterface
if ($this->sendEvent()->getReturn('no-exec')) return;
if ($old['logging'] !== $new['logging']) {
throw new BddException("Il n'est pas possible actuellement de supprimer le logging d'une table ou de faire l'opération inverse");
//$log = $new['logging'] ? 'LOGGING' : 'NOLOGGING';
//$this->addQuery("ALTER TABLE $name $log", 'Modification du logging de la table ' . $name);
$sql = "ALTER TABLE $name SET ".($new['logging'] ? 'LOGGED' : 'UNLOGGED');
$this->addQuery($sql, 'Modification du logging de la table ' . $name);
}
$newCols = array_diff(array_keys($new['columns']), array_keys($old['columns']));
......
......@@ -153,6 +153,12 @@ class TriggerManager extends AbstractManager implements TriggerManagerInterface
public function alter(array $old, array $new): void
{
if (!isset($old['schema'])){
$old['schema'] = 'public';
}
if (!isset($new['schema'])){
$new['schema'] = 'public';
}
if ($old != $new) {
if ($this->sendEvent()->getReturn('no-exec')) return;
......
......@@ -75,6 +75,12 @@ class ViewManager extends AbstractManager implements ViewManagerInterface
public function alter(array $old, array $new): void
{
if (!isset($old['schema'])){
$old['schema'] = 'public';
}
if (!isset($new['schema'])){
$new['schema'] = 'public';
}
if ($old != $new) {
if ($this->sendEvent()->getReturn('no-exec')) return;
......
......@@ -54,7 +54,7 @@ class DefaultLogger implements LoggerInterface
public function error($e)
public function error(Throwable|string $e)
{
if ($e instanceof \Throwable) {
$e = $e->getMessage();
......
......@@ -2,6 +2,8 @@
namespace Unicaen\BddAdmin\Logger;
use Symfony\Component\Console\Style\SymfonyStyle;
trait LoggerAwareTrait
{
......@@ -24,8 +26,11 @@ trait LoggerAwareTrait
*
* @return self
*/
public function setLogger(?LoggerInterface $logger): self
public function setLogger(null|LoggerInterface|SymfonyStyle $logger): self
{
if ($logger instanceof SymfonyStyle){
$logger = new SymfonyStyleLogger($logger);
}
$this->logger = $logger;
return $this;
......
......@@ -4,7 +4,7 @@ namespace Unicaen\BddAdmin\Logger;
interface LoggerInterface
{
public function error($e);
public function error(Throwable|string $e);
......
<?php
namespace Unicaen\BddAdmin\Logger;
use Symfony\Component\Console\Style\SymfonyStyle;
class SymfonyStyleLogger implements LoggerInterface
{
protected ?string $lastMessage = null;
protected bool $lastRewrite = false;
protected SymfonyStyle $symfonyStyle;
public function __construct(SymfonyStyle $symfonyStyle)
{
$this->symfonyStyle = $symfonyStyle;
}
public function print($text, $color = null, $bgColor = null)
{
$this->symfonyStyle->write($text);
}
public function println($text, $color = null, $bgColor = null)
{
$this->symfonyStyle->writeln($text);
}
public function msg($message, bool $rewrite = false)
{
if ($rewrite) {
if ($this->lastMessage) {
$m = $message . str_pad('', strlen($this->lastMessage) - strlen($message) + 2) . "\r";
} else {
$m = $message . "\r";
}
$this->print($m);
} else {
$this->println($message);
}
$this->lastMessage = $message;
$this->lastRewrite = $rewrite;
}
public function error(Throwable|string $e)
{
if ($e instanceof \Throwable) {
$e = $e->getMessage();
}
if ($this->lastRewrite) $this->println('');
$this->symfonyStyle->error($e);
}
public function begin(string $title)
{
if ($this->lastMessage) {
$title .= str_pad('', strlen($this->lastMessage) - strlen($title) + 2);
}
$this->lastRewrite = false;
$this->lastMessage = null;
$this->symfonyStyle->title($title);
}
public function end(?string $msg = null): void
{
if ($this->lastMessage && $this->lastRewrite) {
$msg .= str_pad('', strlen($this->lastMessage) - strlen($msg ?? '') + 2);
}
if (trim($msg)) {
$this->symfonyStyle->comment($msg);
} else {
$this->println('');
}
}
}
\ No newline at end of file
......@@ -11,7 +11,7 @@ class MigrationManager
{
use OptionsTrait;
const string OPTION_DIR = 'dir';
const OPTION_DIR = 'dir';
protected Ddl $ref;
......@@ -174,7 +174,7 @@ class MigrationManager
protected function getMigrationDir(): ?string
{
$migrationDir = $this->getBdd()->getOption(Bdd::OPTION_MIGRATION_DIR);
$migrationDir = $this->getBdd()->getOption(self::OPTION_DIR);
return $migrationDir;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment