Don't generate variables in `CREATE TABLE` (#1936)

This commit is contained in:
Simon Binder 2022-07-20 21:28:47 +02:00
parent 8d840f3eb3
commit 0c2ebb6c4d
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
3 changed files with 53 additions and 3 deletions

View File

@ -102,8 +102,8 @@ class Migrator {
}
}
GenerationContext _createContext() {
return GenerationContext.fromDb(_db);
GenerationContext _createContext({bool supportsVariables = false}) {
return GenerationContext.fromDb(_db, supportsVariables: supportsVariables);
}
/// Creates the given table if it doesn't exist
@ -197,7 +197,7 @@ class Migrator {
await createTable(temporaryTable);
// Step 5: Transfer old content into the new table
final context = _createContext();
final context = _createContext(supportsVariables: true);
final expressionsForSelect = <Expression>[];
context.buffer.write('INSERT INTO $temporaryName (');

View File

@ -187,6 +187,28 @@ void main() {
verify(executor.transactions.runCustom(any, any));
verifyNever(executor.runCustom(any, any));
});
test('removes variables in `CREATE TABLE` statements', () async {
final executor = MockExecutor();
final db = _DefaultDb(executor);
late GeneratedColumn<int> column;
column = GeneratedColumn<int>(
'foo',
'foo',
true,
type: DriftSqlType.int,
check: () => column.isSmallerThan(const Variable(3)),
);
final table = CustomTable('foo', db, [column]);
await db.createMigrator().createTable(table);
await db.close();
// This should not attempt to generate a parameter (`?`)
// https://github.com/simolus3/drift/discussions/1936
verify(executor.runCustom(argThat(contains('CHECK(foo < 3)')), []));
});
}
class _DefaultDb extends GeneratedDatabase {

View File

@ -1,5 +1,33 @@
import 'package:drift/drift.dart';
export 'database_stub.dart'
if (dart.library.ffi) 'database_vm.dart'
if (dart.library.js) 'database_web.dart';
export 'matchers.dart';
export 'mocks.dart';
class CustomTable extends Table with TableInfo<CustomTable, Null> {
@override
final String actualTableName;
@override
final DatabaseConnectionUser attachedDatabase;
final List<GeneratedColumn<Object>> columns;
final String? _alias;
CustomTable(this.actualTableName, this.attachedDatabase, this.columns,
[this._alias]);
@override
List<GeneratedColumn<Object>> get $columns => columns;
@override
String get aliasedName => _alias ?? actualTableName;
@override
CustomTable createAlias(String alias) {
return CustomTable(actualTableName, attachedDatabase, columns, alias);
}
@override
Null map(Map<String, dynamic> data, {String? tablePrefix}) => null;
}