Skip to content

EspoCRM – Open Source CRM Application

Fixes for PosrgreSQL

Yurii6166caa772e3
diff --git a/application/Espo/Core/Utils/Database/Schema/DiffModifier.php b/application/Espo/Core/Utils/Database/Schema/DiffModifier.php
index 1d06280719..78c8a0982a 100644
--- a/application/Espo/Core/Utils/Database/Schema/DiffModifier.php
+++ b/application/Espo/Core/Utils/Database/Schema/DiffModifier.php
@@ -106,6 +106,14 @@ class DiffModifier
$reRun = true;
}
+
+ if (!$isHard && $secondRun) {
+ $itemTableDiff = $this->moveDroppedNotNullableToChangedNullable($tableDiff, $column);
+
+ if ($itemTableDiff) {
+ $tableDiff = $itemTableDiff;
+ }
+ }
}
if (!$isHard) {
@@ -255,13 +263,21 @@ class DiffModifier
$fromColumn = $columnDiff->getOldColumn();
$column = $columnDiff->getNewColumn();
- if (!$columnDiff->hasAutoIncrementChanged() || $fromColumn->getAutoincrement()) {
+ if (!$columnDiff->hasAutoIncrementChanged() /*|| $fromColumn->getAutoincrement()*/) {
return [$columnDiff, false];
}
+ $notNull = false;
+
+ if ($fromColumn->getAutoincrement()) {
+ // Need to preserve while the sequence is still there.
+ // To be set to false in the second run.
+ $notNull = $fromColumn->getNotnull();
+ }
+
$column
->setAutoincrement(false)
- ->setNotnull(false)
+ ->setNotnull($notNull)
->setDefault(null);
$name = $column->getObjectName()->getIdentifier()->getValue();
@@ -330,6 +346,33 @@ class DiffModifier
return $this->cloneTableDiffWithDroppedIndexes($tableDiff, $droppedIndexes);
}
+ /**
+ * After autoincrement is removed, the column is still not-nullable after the first run.
+ * Otherwise, a conflict with the sequence occurs. Here, it is set to nullable.
+ */
+ private function moveDroppedNotNullableToChangedNullable(TableDiff $tableDiff, Column $column): ?TableDiff
+ {
+ if (!$column->getNotnull()) {
+ return null;
+ }
+
+ $newColumn = clone $column;
+
+ $newColumn
+ ->setNotnull(false);
+
+ $columnDiff = new ColumnDiff(
+ oldColumn: $column,
+ newColumn: $newColumn,
+ );
+
+ $name = $column->getObjectName()->getIdentifier()->getValue();
+
+ $changedColumns = [...$tableDiff->getChangedColumns(), $name => $columnDiff];
+
+ return $this->cloneTableDiffWithChangedColumns($tableDiff, $changedColumns);
+ }
+
/**
* DBAL does not handle autoincrement columns that are not primary keys,
* making them dropped.
diff --git a/tests/integration/Espo/Core/Utils/Database/Fields/TextFieldTest.php b/tests/integration/Espo/Core/Utils/Database/Fields/TextFieldTest.php
index 463b1474c8..947ac68cbe 100644
--- a/tests/integration/Espo/Core/Utils/Database/Fields/TextFieldTest.php
+++ b/tests/integration/Espo/Core/Utils/Database/Fields/TextFieldTest.php
@@ -41,10 +41,10 @@ class TextFieldTest extends Base
$column = $this->getColumn('Test', 'testText');
$this->assertNotNull($column);
- $this->assertInstanceOf(MediumtextType::class, $column->getType());
$this->assertFalse($column->getNotnull());
if ($this->getPlatform() === 'Mysql') {
+ $this->assertInstanceOf(MediumtextType::class, $column->getType());
$this->assertEquals('utf8mb4_unicode_ci', $column->getCollation());
}
@@ -62,10 +62,10 @@ class TextFieldTest extends Base
$column = $this->getColumn('Test', 'testText');
$this->assertNotNull($column);
- $this->assertInstanceOf(LongtextType::class, $column->getType());
$this->assertFalse($column->getNotnull());
if ($this->getPlatform() === 'Mysql') {
+ $this->assertInstanceOf(LongtextType::class, $column->getType());
$this->assertEquals('utf8mb4_unicode_ci', $column->getCollation());
}
}
@@ -85,10 +85,10 @@ class TextFieldTest extends Base
$column = $this->getColumn('Test', 'testText');
$this->assertNotNull($column);
- $this->assertInstanceOf(LongtextType::class, $column->getType());
$this->assertFalse($column->getNotnull());
if ($this->getPlatform() === 'Mysql') {
+ $this->assertInstanceOf(LongtextType::class, $column->getType());
$this->assertEquals('utf8mb4_unicode_ci', $column->getCollation());
}
}
diff --git a/tests/integration/Espo/Core/Utils/Database/RebuildTest.php b/tests/integration/Espo/Core/Utils/Database/RebuildTest.php
index 4a2787bf4d..b9f2aa244f 100644
--- a/tests/integration/Espo/Core/Utils/Database/RebuildTest.php
+++ b/tests/integration/Espo/Core/Utils/Database/RebuildTest.php
@@ -30,11 +30,13 @@
namespace tests\integration\Espo\Core\Utils\Database;
use Doctrine\DBAL\Schema\Column;
+use Espo\Core\ORM\DatabaseParamsFactory;
use Espo\Core\ORM\Type\FieldType;
use Espo\Core\Templates\Entities\Base;
use Espo\Core\Utils\Database\Helper;
use Espo\Core\Utils\Database\Schema\RebuildMode;
use Espo\ORM\Defs\Params\FieldParam;
+use Espo\ORM\Entity;
use Espo\Tools\EntityManager\EntityManager as EntityTypeManager;
use Espo\Tools\FieldManager\FieldManager;
use integration\Core\NoTransaction;
@@ -172,47 +174,29 @@ class RebuildTest extends BaseTestCase
//
+ $em = $this->getEntityManager();
+
$entity = $em->createEntity('C' . self::ENTITY_TYPE_TEST);
$em->refreshEntity($entity);
$this->assertEquals(null, $entity->get('testSecond'));
- // Enable autoincrement back.
-
- $this->getMetadata()->set('entityDefs', 'C' . self::ENTITY_TYPE_TEST, [
- 'fields' => [
- 'testSecond' => [
- 'type' => FieldType::INT,
- 'autoincrement' => true,
- ],
- ],
- ]);
- $this->getMetadata()->save();
-
- $this->reCreateApplication();
- $this->getDataManager()->rebuildDatabase();
-
- $this->assertTrue($this->getColumn($table, 'test_second')?->getAutoincrement());
-
//
+ if ($this->getPlatform() === 'Postgresql') {
+ $entity->set('testSecond', 4);
+ $em->saveEntity($entity);
+ }
- $em->refreshEntity($entity);
- $this->assertEquals(4, $entity->get('testSecond'));
+ // Enable autoincrement back.
- $entity = $em->createEntity('C' . self::ENTITY_TYPE_TEST);
- $em->refreshEntity($entity);
- // Sequence is not preserved in MariaDB for some reason.
- $this->assertIsInt($entity->get('testSecond'));
- // Delete autoincrement column.
+ if ($this->getPlatform() !== 'Postgresql') {
+ $this->runAutoincrementBack($entity, $table);
+ }
- $fm->delete('C' . self::ENTITY_TYPE_TEST, 'testSecond');
- $this->reCreateApplication();
- $this->getDataManager()->rebuildDatabase();
- $this->assertFalse($this->getColumn($table, 'test_second')?->getAutoincrement());
// Delete entity type.
@@ -281,4 +265,56 @@ class RebuildTest extends BaseTestCase
return null;
}
+
+ private function getPlatform(): ?string
+ {
+ $params = $this->getInjectableFactory()->create(DatabaseParamsFactory::class)
+ ->create();
+
+ return $params->getPlatform();
+ }
+
+ /**
+ * @noinspection PhpUnhandledExceptionInspection
+ */
+ private function runAutoincrementBack(Entity $entity, string $table): void
+ {
+ $em = $this->getEntityManager();
+ $fm = $this->getInjectableFactory()->create(FieldManager::class);
+
+ $this->getMetadata()->set('entityDefs', 'C' . self::ENTITY_TYPE_TEST, [
+ 'fields' => [
+ 'testSecond' => [
+ 'type' => FieldType::INT,
+ 'autoincrement' => true,
+ ],
+ ],
+ ]);
+ $this->getMetadata()->save();
+
+ $this->reCreateApplication();
+ $this->getDataManager()->rebuildDatabase();
+
+ $this->assertTrue($this->getColumn($table, 'test_second')?->getAutoincrement());
+
+ //
+
+ $em->refreshEntity($entity);
+ $this->assertEquals(4, $entity->get('testSecond'));
+
+ $entity = $em->createEntity('C' . self::ENTITY_TYPE_TEST);
+ $em->refreshEntity($entity);
+
+ // Sequence is not preserved in MariaDB for some reason.
+ $this->assertIsInt($entity->get('testSecond'));
+
+ // Delete autoincrement column.
+
+ $fm->delete('C' . self::ENTITY_TYPE_TEST, 'testSecond');
+
+ $this->reCreateApplication();
+ $this->getDataManager()->rebuildDatabase();
+
+ $this->assertFalse($this->getColumn($table, 'test_second')?->getAutoincrement());
+ }
}