Skip to content

Commit

Permalink
#342: add database migration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jorre127 committed Oct 16, 2024
1 parent de0a74d commit 6adcf14
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 1 deletion.
1 change: 1 addition & 0 deletions test/database/drift_schemas/drift_schema_v1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"_meta":{"description":"This file contains a serialized version of schema entities for drift.","version":"1.1.0"},"options":{"store_date_time_values_as_text":false},"entities":[{"id":0,"references":[],"type":"table","data":{"name":"db_todo_table","was_declared_in_moor":false,"columns":[{"name":"id","getter_name":"id","moor_type":"int","nullable":false,"customConstraints":null,"defaultConstraints":"PRIMARY KEY AUTOINCREMENT","default_dart":null,"default_client_dart":null,"dsl_features":["auto-increment"]},{"name":"title","getter_name":"title","moor_type":"string","nullable":false,"customConstraints":null,"default_dart":null,"default_client_dart":null,"dsl_features":[]},{"name":"completed","getter_name":"completed","moor_type":"bool","nullable":false,"customConstraints":null,"defaultConstraints":"CHECK (\"completed\" IN (0, 1))","default_dart":null,"default_client_dart":null,"dsl_features":[]}],"is_virtual":false,"without_rowid":false,"constraints":[]}}]}
2 changes: 1 addition & 1 deletion test/database/flutter_template_database_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:flutter_test/flutter_test.dart';
/// The schema version of the database that is being tested.
/// Update this when a new migration is added.
/// Also run ./tool/save_database_schema.sh to create the latest version schema file.
const schemaVersion = 32;
const schemaVersion = 1;

void main() {
late FlutterTemplateDatabase sut;
Expand Down
1 change: 1 addition & 0 deletions test/database/flutter_template_migration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:flutter_template/database/flutter_template_database.dart';
import 'package:flutter_test/flutter_test.dart';

import 'flutter_template_database_test.dart';
import 'schema_versions/schema.dart';


void main() {
Expand Down
18 changes: 18 additions & 0 deletions test/database/schema_versions/schema.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// GENERATED CODE, DO NOT EDIT BY HAND.
// ignore_for_file: type=lint
//@dart=2.12
import 'package:drift/drift.dart';
import 'package:drift/internal/migrations.dart';
import 'schema_v1.dart' as v1;

class GeneratedHelper implements SchemaInstantiationHelper {
@override
GeneratedDatabase databaseForVersion(QueryExecutor db, int version) {
switch (version) {
case 1:
return v1.DatabaseAtV1(db);
default:
throw MissingSchemaException(version, const {1});
}
}
}
57 changes: 57 additions & 0 deletions test/database/schema_versions/schema_v1.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// GENERATED CODE, DO NOT EDIT BY HAND.
// ignore_for_file: type=lint
//@dart=2.12
import 'package:drift/drift.dart';

class DbTodoTable extends Table with TableInfo {
@override
final GeneratedDatabase attachedDatabase;
final String? _alias;
DbTodoTable(this.attachedDatabase, [this._alias]);
late final GeneratedColumn<int> id = GeneratedColumn<int>(
'id', aliasedName, false,
hasAutoIncrement: true,
type: DriftSqlType.int,
requiredDuringInsert: false,
defaultConstraints:
GeneratedColumn.constraintIsAlways('PRIMARY KEY AUTOINCREMENT'));
late final GeneratedColumn<String> title = GeneratedColumn<String>(
'title', aliasedName, false,
type: DriftSqlType.string, requiredDuringInsert: true);
late final GeneratedColumn<bool> completed = GeneratedColumn<bool>(
'completed', aliasedName, false,
type: DriftSqlType.bool,
requiredDuringInsert: true,
defaultConstraints:
GeneratedColumn.constraintIsAlways('CHECK ("completed" IN (0, 1))'));
@override
List<GeneratedColumn> get $columns => [id, title, completed];
@override
String get aliasedName => _alias ?? actualTableName;
@override
String get actualTableName => $name;
static const String $name = 'db_todo_table';
@override
Set<GeneratedColumn> get $primaryKey => {id};
@override
Never map(Map<String, dynamic> data, {String? tablePrefix}) {
throw UnsupportedError('TableInfo.map in schema verification code');
}

@override
DbTodoTable createAlias(String alias) {
return DbTodoTable(attachedDatabase, alias);
}
}

class DatabaseAtV1 extends GeneratedDatabase {
DatabaseAtV1(QueryExecutor e) : super(e);
late final DbTodoTable dbTodoTable = DbTodoTable(this);
@override
Iterable<TableInfo<Table, Object?>> get allTables =>
allSchemaEntities.whereType<TableInfo<Table, Object?>>();
@override
List<DatabaseSchemaEntity> get allSchemaEntities => [dbTodoTable];
@override
int get schemaVersion => 1;
}

0 comments on commit 6adcf14

Please sign in to comment.