Include `NULL` constraints in node->sql

This commit is contained in:
Simon Binder 2023-12-12 21:46:58 +01:00
parent 7964782c2d
commit 08ceb939bc
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
8 changed files with 31 additions and 4 deletions

View File

@ -115,7 +115,7 @@ class WithDefaults extends Table with TableInfo<WithDefaults, WithDefault> {
'b', aliasedName, true,
type: DriftSqlType.int,
requiredDuringInsert: false,
$customConstraints: 'UNIQUE');
$customConstraints: 'UNIQUE NULL');
@override
List<GeneratedColumn> get $columns => [a, b];
@override

View File

@ -7,7 +7,7 @@ CREATE TABLE no_ids (
CREATE TABLE with_defaults (
a `const CustomTextType()` JSON KEY customJsonName DEFAULT 'something',
b INT UNIQUE
b INT UNIQUE NULL
);
CREATE TABLE with_constraints (

View File

@ -11,7 +11,7 @@ const _createNoIds =
'WITHOUT ROWID;';
const _createWithDefaults = 'CREATE TABLE IF NOT EXISTS "with_defaults" ('
"\"a\" MY_TEXT DEFAULT 'something', \"b\" INTEGER UNIQUE);";
"\"a\" MY_TEXT DEFAULT 'something', \"b\" INTEGER UNIQUE NULL);";
const _createWithConstraints = 'CREATE TABLE IF NOT EXISTS "with_constraints" ('
'"a" TEXT, "b" INTEGER NOT NULL, "c" REAL, '

View File

@ -1,3 +1,14 @@
## 2.15.0-dev
- Potentially __breaking change__: Fix a bug causing `NULL` column constraints
not to show up in generated table definitions.
This is a breaking change drift's migration tooling is used, since they will
not expect the `NULL` column constraint to be there.
After upgrading, you can either drop the `NULL` constraint from your drift
files to make it consistent with the schema (since `NULL` has no effect in
sqlite3), or increment the schema version and run a `TableMigration` on
affected tables to make column constraints consistent.
## 2.14.1
- Fix inconsistencies when generating `Variable` instances for columns with

View File

@ -4,7 +4,7 @@ import 'tables.dart';
CREATE TABLE "groups" (
id INTEGER NOT NULL,
title TEXT NOT NULL,
deleted BOOLEAN DEFAULT FALSE,
deleted BOOLEAN NULL DEFAULT FALSE,
owner INTEGER NOT NULL REFERENCES users (id),
PRIMARY KEY (id)

View File

@ -1,3 +1,8 @@
## 0.33.1
- Fix explicit `NULL` column constraints being dropped when converting nodes
to SQL.
## 0.33.0
- Support the [column-name-list](https://sqlite.org/syntax/column-name-list.html)

View File

@ -204,6 +204,9 @@ class NodeSqlBuilder extends AstVisitor<void, void> {
keyword(TokenType.$null);
_conflictClause(notNull.onConflict);
},
nullable: (nullable) {
keyword(TokenType.$null);
},
unique: (unique) {
keyword(TokenType.unique);
_conflictClause(unique.onConflict);

View File

@ -165,6 +165,14 @@ CREATE TABLE foo (bar INTEGER NOT NULL PRIMARY KEY) With FooData.myConstructor;
test('virtual', () {
testFormat('CREATE VIRTUAL TABLE foo USING bar(a, b, c);');
});
test('not null', () {
testFormat('CREATE TABLE foo (bar TEXT NOT NULL);');
});
test('null constraint', () {
testFormat('CREATE TABLE foo (bar TEXT NULL);');
});
});
test('index', () {