doctrine/dbal

SQLite column comment generates invalid alter query

Open

#5,934 opened on Feb 24, 2023

 (2 comments) (0 reactions) (0 assignees)PHP (1,374 forks)batch import
BugHelp wantedSQLiteSchema DefinitionSchema Management

Repository metrics

Stars
 (9,693 stars)
PR merge metrics
 (Avg merge 12h 52m) (46 merged PRs in 30d)

Description

Bug Report

Q A
Version 3.3.8

Summary

Nextcloud got a report that column comments on sqlite leads to an sql error like: General error: 1 incomplete input12

The code to add the column.

$table->addColumn('foo', Types::INTEGER, [
	'notnull' => false,
	'default' => 0,
	'comment' => 'unix-timestamp',
]);

It works to run all migrations on a clean database. It breaks when the migration with the column comment is executed alone.

The generated alter table query is:

ALTER TABLE oc_forms_v2_forms ADD COLUMN last_updated INTEGER DEFAULT 0 --unix-timestamp

Comments with -- are not supported (or broken) according to: https://sqlite.org/forum/forumpost/4bb7806a96e863f0

A possible solution could be to drop comments for alter table statments on sqlite.

Index: src/Platforms/SqlitePlatform.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/Platforms/SqlitePlatform.php b/src/Platforms/SqlitePlatform.php
--- a/src/Platforms/SqlitePlatform.php	(revision 3fc4ea3565d54ff1cc944e5c4e01cc9dc2920df2)
+++ b/src/Platforms/SqlitePlatform.php	(date 1677275353199)
@@ -1263,6 +1263,9 @@
                 $definition['length'] ??= 255;
             }
 
+            // SQLite does not support sql schema comments in alter table statements
+            unset($definition['comment']);
+
             $sql[] = 'ALTER TABLE ' . $table->getQuotedName($this) . ' ADD COLUMN '
                 . $this->getColumnDeclarationSQL($definition['name'], $definition);
         }

Migrating from -- to /* */ should also work, but it seems complicated to make dbal work with the sql style comments and c style comments.

Current behaviour

The generated sql query results in an error

How to reproduce

  • Use sqlite
  • Create a table
  • Add a column with a comment
  • :boom:

Expected behaviour

No sql error ;)

Footnotes

  1. https://github.com/nextcloud/server/pull/36803

  2. https://github.com/nextcloud/forms/pull/1479#issuecomment-1439234202

Contributor guide