Skip to content

Commit

Permalink
key can not be used as collum name in mysql
Browse files Browse the repository at this point in the history
  • Loading branch information
antedebaas committed Oct 13, 2024
1 parent 5952353 commit 1b8303d
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 20 deletions.
35 changes: 35 additions & 0 deletions migrations/mysql/Version20241013152145.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20241013152145 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('DROP INDEX `primary` ON config');
$this->addSql('ALTER TABLE config CHANGE `key` name VARCHAR(255) NOT NULL');
$this->addSql('ALTER TABLE config ADD PRIMARY KEY (name)');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('DROP INDEX `PRIMARY` ON config');
$this->addSql('ALTER TABLE config CHANGE name `key` VARCHAR(255) NOT NULL');
$this->addSql('ALTER TABLE config ADD PRIMARY KEY (`key`)');
}
}
35 changes: 35 additions & 0 deletions migrations/postgresql/Version20241013152649.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20241013152649 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE config DROP CONSTRAINT config_pkey');
$this->addSql('ALTER TABLE config RENAME COLUMN key TO name');
$this->addSql('ALTER TABLE config ADD PRIMARY KEY (name)');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('DROP INDEX config_pkey');
$this->addSql('ALTER TABLE config RENAME COLUMN name TO key');
$this->addSql('ALTER TABLE config ADD PRIMARY KEY (key)');
}
}
39 changes: 39 additions & 0 deletions migrations/sqlite/Version20241013152748.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20241013152748 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE TEMPORARY TABLE __temp__config AS SELECT "key", value, type FROM config');
$this->addSql('DROP TABLE config');
$this->addSql('CREATE TABLE config (name VARCHAR(255) NOT NULL, value VARCHAR(255) NOT NULL, type VARCHAR(16) NOT NULL, PRIMARY KEY(name))');
$this->addSql('INSERT INTO config (name, value, type) SELECT "key", value, type FROM __temp__config');
$this->addSql('DROP TABLE __temp__config');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE TEMPORARY TABLE __temp__config AS SELECT name, value, type FROM config');
$this->addSql('DROP TABLE config');
$this->addSql('CREATE TABLE config ("key" VARCHAR(255) NOT NULL, value VARCHAR(255) NOT NULL, type VARCHAR(16) NOT NULL, PRIMARY KEY("key"))');
$this->addSql('INSERT INTO config ("key", value, type) SELECT name, value, type FROM __temp__config');
$this->addSql('DROP TABLE __temp__config');
}
}
6 changes: 3 additions & 3 deletions src/Command/GetReportsFromMailboxCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,18 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$io = new SymfonyStyle($input, $output);

$repository = $this->em->getRepository(Config::class);
$lock = $repository->findOneBy(array('key' => 'check_mailbox_lock'));
$lock = $repository->findOneBy(array('name' => 'check_mailbox_lock'));
if(!$lock) {
$lock = new Config();
$lock->setKey('check_mailbox_lock');
$lock->setName('check_mailbox_lock');
$lock->setValue('0');
$lock->setType('boolean');
$this->em->persist($lock);
$this->em->flush();
}

try {
if($lock->getValue() == 'true') {
if($lock->getValue() == '1') {
$log = new Logs();
$log->setTime(new \DateTime());
$log->setState(StateType::Warn);
Expand Down
2 changes: 1 addition & 1 deletion src/Command/RemoveMailLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$io = new SymfonyStyle($input, $output);

$repository = $this->em->getRepository(Config::class);
$lock = $repository->findOneBy(array('key' => 'check_mailbox_lock'));
$lock = $repository->findOneBy(array('name' => 'check_mailbox_lock'));
if($lock){
$lock->setValue('0');
$this->em->persist($lock);
Expand Down
6 changes: 3 additions & 3 deletions src/Controller/ConfigurationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ public function index(Request $request): Response
if ($form->isSubmitted() && $form->isValid()) {
$formdata = $form->getData();

foreach ($formdata as $key => $value) {
if($key == "entries") {
foreach ($formdata as $name => $value) {
if($name == "entries") {
continue;
}
$entry = $repository->findOneBy(['key' => $key]);
$entry = $repository->findOneBy(['name' => $name]);
if($entry == null) {
continue;
}
Expand Down
10 changes: 5 additions & 5 deletions src/Entity/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ class Config
{
#[ORM\Id]
#[ORM\Column(length: 255)]
private ?string $key = null;
private ?string $name = null;

#[ORM\Column(length: 255)]
private ?string $value = null;

#[ORM\Column(length: 16)]
private ?string $Type = null;

public function getKey(): ?string
public function getName(): ?string
{
return $this->key;
return $this->name;
}

public function setKey(string $key): static
public function setName(string $name): static
{
$this->key = $key;
$this->name = $name;

return $this;
}
Expand Down
11 changes: 5 additions & 6 deletions src/Form/ConfigurationFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Length;
//use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;

Expand All @@ -18,23 +17,23 @@ class ConfigurationFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
foreach($options['data']['entries'] as $key => $item) {
foreach($options['data']['entries'] as $name => $item) {
switch ($item->getType()) {
case 'text':
$builder->add($item->getKey(), TextType::class, [
$builder->add($item->getName(), TextType::class, [
'data' => $item->getValue(),
'label' => $item->getKey(),
'label' => $item->getName(),
'required' => false,
]);
break;
case 'boolean':
$builder->add($item->getKey(), ChoiceType ::class, [
$builder->add($item->getName(), ChoiceType ::class, [
'choices' => [
'True' => true,
'False' => false,
],
'data' => $item->getValue() == '1' ?? true :: false,
'label' => $item->getKey(),
'label' => $item->getName(),
'required' => true,
]);
break;
Expand Down
2 changes: 1 addition & 1 deletion src/Repository/ConfigRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function __construct(ManagerRegistry $registry)
public function getTotalRows(): int
{
$qb = $this->createQueryBuilder('r')
->select('count(r.key)');
->select('count(r.name)');

return $qb->getQuery()
->getOneOrNullResult()[1]
Expand Down
2 changes: 1 addition & 1 deletion templates/configuration/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<table class="table card-table table-vcenter text-nowrap datatable">
<thead>
<tr>
<th class="w-3">{{ "Key"|trans }}</th>
<th class="w-3">{{ "Name"|trans }}</th>
<th>{{ "Value"|trans }}</th>
<th></th>
</tr>
Expand Down

0 comments on commit 1b8303d

Please sign in to comment.