]> source.dussan.org Git - nextcloud-server.git/commitdiff
Don't allow Notnull for boolean columns
authorJoas Schilling <coding@schilljs.com>
Wed, 11 Nov 2020 13:34:24 +0000 (14:34 +0100)
committerJoas Schilling <coding@schilljs.com>
Wed, 31 Mar 2021 08:21:17 +0000 (10:21 +0200)
Signed-off-by: Joas Schilling <coding@schilljs.com>
lib/private/DB/MigrationService.php
tests/lib/DB/MigrationsTest.php

index 852ee8b701f0e7aea37691800360f48440cb0e07..4957706bb1d95a0c1f6aa2b78d4226d9fa0a98fc 100644 (file)
@@ -562,6 +562,7 @@ class MigrationService {
         * Data constraints:
         * - Columns with "NotNull" can not have empty string as default value
         * - Columns with "NotNull" can not have number 0 as default value
+        * - Columns with type "bool" (which is in fact integer of length 1) can not be "NotNull" as it can not store 0/false
         *
         * @param Schema $sourceSchema
         * @param Schema $targetSchema
@@ -590,6 +591,10 @@ class MigrationService {
                                        && $sourceTable instanceof Table && !$sourceTable->hasColumn($thing->getName())) {
                                        throw new \InvalidArgumentException('Column "' . $table->getName() . '"."' . $thing->getName() . '" is NotNull, but has empty string or null as default.');
                                }
+
+                               if ($thing->getNotnull() && $thing->getType()->getName() === Types::BOOLEAN) {
+                                       throw new \InvalidArgumentException('Column "' . $table->getName() . '"."' . $thing->getName() . '" is type Bool and also NotNull, so it can not store "false".');
+                               }
                        }
 
                        foreach ($table->getIndexes() as $thing) {
index 9b6dec006d69a01c5ef8540b2b344573055da157..c8b1af3e08e0aa4384388b3bf21479da36df0b4a 100644 (file)
@@ -18,6 +18,7 @@ use Doctrine\DBAL\Schema\Schema;
 use Doctrine\DBAL\Schema\SchemaException;
 use Doctrine\DBAL\Schema\Sequence;
 use Doctrine\DBAL\Schema\Table;
+use Doctrine\DBAL\Types\Type;
 use OC\DB\Connection;
 use OC\DB\MigrationService;
 use OC\DB\SchemaWrapper;
@@ -632,4 +633,44 @@ class MigrationsTest extends \Test\TestCase {
 
                self::invokePrivate($this->migrationService, 'ensureOracleIdentifierLengthLimit', [$sourceSchema, $schema, 3]);
        }
+
+
+       public function testEnsureOracleIdentifierLengthLimitBooleanNotNull() {
+               $this->expectException(\InvalidArgumentException::class);
+
+               $column = $this->createMock(Column::class);
+               $column->expects($this->any())
+                       ->method('getName')
+                       ->willReturn('aaaa');
+               $column->expects($this->any())
+                       ->method('getType')
+                       ->willReturn(Type::getType('boolean'));
+               $column->expects($this->any())
+                       ->method('getNotnull')
+                       ->willReturn(true);
+
+               $table = $this->createMock(Table::class);
+               $table->expects($this->any())
+                       ->method('getName')
+                       ->willReturn(\str_repeat('a', 30));
+
+               $table->expects($this->once())
+                       ->method('getColumns')
+                       ->willReturn([$column]);
+
+               $schema = $this->createMock(Schema::class);
+               $schema->expects($this->once())
+                       ->method('getTables')
+                       ->willReturn([$table]);
+
+               $sourceSchema = $this->createMock(Schema::class);
+               $sourceSchema->expects($this->any())
+                       ->method('getTable')
+                       ->willThrowException(new SchemaException());
+               $sourceSchema->expects($this->any())
+                       ->method('hasSequence')
+                       ->willReturn(false);
+
+               self::invokePrivate($this->migrationService, 'ensureOracleIdentifierLengthLimit', [$sourceSchema, $schema, 3]);
+       }
 }