mirror of https://github.com/AMT-Cheif/drift.git
Finish custom constraints implementation
This commit is contained in:
parent
f3a0f5f230
commit
9490cd22fe
|
@ -27,8 +27,15 @@ abstract class GeneratedColumn<T, S extends SqlType<T>> extends Column<T, S> {
|
|||
/// [here](https://www.sqlite.org/syntax/column-def.html), into the given
|
||||
/// buffer.
|
||||
void writeColumnDefinition(StringBuffer into) {
|
||||
into..write('${$name} $typeName ')..write($nullable ? 'NULL' : 'NOT NULL');
|
||||
writeCustomConstraints(into);
|
||||
into.write('${$name} $typeName ');
|
||||
|
||||
if ($customConstraints == null) {
|
||||
into.write($nullable ? 'NULL' : 'NOT NULL');
|
||||
// these custom constraints refer to builtin constraints from moor
|
||||
writeCustomConstraints(into);
|
||||
} else {
|
||||
into.write($customConstraints);
|
||||
}
|
||||
}
|
||||
|
||||
@visibleForOverriding
|
||||
|
|
|
@ -26,7 +26,8 @@ class Users extends Table {
|
|||
@DataClassName('Category')
|
||||
class Categories extends Table {
|
||||
IntColumn get id => integer().autoIncrement()();
|
||||
TextColumn get description => text().named('desc')();
|
||||
TextColumn get description =>
|
||||
text().named('desc').customConstraint('NOT NULL UNIQUE')();
|
||||
}
|
||||
|
||||
class SharedTodos extends Table {
|
||||
|
|
|
@ -217,10 +217,9 @@ class $CategoriesTable extends Categories
|
|||
_id ??= GeneratedIntColumn('id', false, hasAutoIncrement: true);
|
||||
GeneratedTextColumn _description;
|
||||
@override
|
||||
GeneratedTextColumn get description => _description ??= GeneratedTextColumn(
|
||||
'`desc`',
|
||||
false,
|
||||
);
|
||||
GeneratedTextColumn get description =>
|
||||
_description ??= GeneratedTextColumn('`desc`', false,
|
||||
$customConstraints: 'NOT NULL UNIQUE');
|
||||
@override
|
||||
List<GeneratedColumn> get $columns => [id, description];
|
||||
@override
|
||||
|
|
|
@ -24,7 +24,7 @@ void main() {
|
|||
'category INTEGER NULL);'));
|
||||
|
||||
verify(mockQueryExecutor.call('CREATE TABLE IF NOT EXISTS categories '
|
||||
'(id INTEGER PRIMARY KEY AUTOINCREMENT, `desc` VARCHAR NOT NULL);'));
|
||||
'(id INTEGER PRIMARY KEY AUTOINCREMENT, `desc` VARCHAR NOT NULL UNIQUE);'));
|
||||
|
||||
verify(mockQueryExecutor.call('CREATE TABLE IF NOT EXISTS users '
|
||||
'(id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR NOT NULL, '
|
||||
|
|
|
@ -98,6 +98,11 @@ class TableWriter {
|
|||
}
|
||||
}
|
||||
|
||||
if (column.customConstraints != null) {
|
||||
// todo should we worry about escaping this string?
|
||||
additionalParams['\$customConstraints'] = "'${column.customConstraints}'";
|
||||
}
|
||||
|
||||
// GeneratedIntColumn('sql_name', isNullable, additionalField: true)
|
||||
final expressionBuffer = StringBuffer()
|
||||
..write(column.implColumnTypeName)
|
||||
|
|
Loading…
Reference in New Issue