-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
17 changed files
with
332 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
library floor; | ||
|
||
export 'package:floor/src/database.dart'; | ||
export 'package:floor/src/migration.dart'; | ||
export 'package:floor_annotation/floor_annotation.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import 'package:sqflite/sqflite.dart' as sqflite; | ||
|
||
/// Base class for a database migration. | ||
/// | ||
/// Each migration can move between 2 versions that are defined by | ||
/// [startVersion] and [endVersion]. | ||
class Migration { | ||
/// The start version of the database. | ||
final int startVersion; | ||
|
||
/// The start version of the database. | ||
final int endVersion; | ||
|
||
/// Function that performs the migration. | ||
final void Function(sqflite.Database database) migrate; | ||
|
||
/// Creates a new migration between [startVersion] and [endVersion]. | ||
/// [migrate] will be called by the database and performs the actual | ||
/// migration. | ||
Migration(this.startVersion, this.endVersion, this.migrate) | ||
: assert(startVersion != null), | ||
assert(endVersion != null), | ||
assert(migrate != null), | ||
assert(startVersion < endVersion); | ||
|
||
@override | ||
bool operator ==(Object other) => | ||
identical(this, other) || | ||
other is Migration && | ||
runtimeType == other.runtimeType && | ||
startVersion == other.startVersion && | ||
endVersion == other.endVersion && | ||
migrate == other.migrate; | ||
|
||
@override | ||
int get hashCode => | ||
startVersion.hashCode ^ endVersion.hashCode ^ migrate.hashCode; | ||
|
||
@override | ||
String toString() { | ||
return 'Migration{startVersion: $startVersion, endVersion: $endVersion, migrate: $migrate}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import 'package:floor/src/database.dart'; | ||
import 'package:floor/src/migration.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:mockito/mockito.dart'; | ||
import 'package:sqflite/sqlite_api.dart'; | ||
|
||
void main() { | ||
final floorDatabase = TestFloorDatabase(); | ||
final mockMigrationDatabase = MockSqfliteDatabase(); | ||
|
||
tearDown(() { | ||
clearInteractions(mockMigrationDatabase); | ||
}); | ||
|
||
test('run single migration', () { | ||
const startVersion = 1; | ||
const endVersion = 2; | ||
const sql = 'FOO BAR'; | ||
final migrations = [ | ||
Migration(1, 2, (database) { | ||
database.execute(sql); | ||
}) | ||
]; | ||
|
||
// ignore: invalid_use_of_protected_member | ||
floorDatabase.runMigrations( | ||
mockMigrationDatabase, | ||
startVersion, | ||
endVersion, | ||
migrations, | ||
); | ||
|
||
verify(mockMigrationDatabase.execute(sql)); | ||
}); | ||
|
||
test('run multiple migrations in order', () { | ||
const startVersion = 1; | ||
const endVersion = 4; | ||
const sql1 = 'first'; | ||
const sql2 = 'second'; | ||
const sql3 = 'third'; | ||
final migrations = [ | ||
Migration(3, 4, (database) { | ||
database.execute(sql3); | ||
}), | ||
Migration(1, 2, (database) { | ||
database.execute(sql1); | ||
}), | ||
Migration(2, 3, (database) { | ||
database.execute(sql2); | ||
}), | ||
]; | ||
|
||
// ignore: invalid_use_of_protected_member | ||
floorDatabase.runMigrations( | ||
mockMigrationDatabase, | ||
startVersion, | ||
endVersion, | ||
migrations, | ||
); | ||
|
||
verifyInOrder([ | ||
mockMigrationDatabase.execute(sql1), | ||
mockMigrationDatabase.execute(sql2), | ||
mockMigrationDatabase.execute(sql3), | ||
]); | ||
}); | ||
|
||
test('exception when no matching start version found', () { | ||
const startVersion = 10; | ||
const endVersion = 20; | ||
const sql = 'FOO BAR'; | ||
final migrations = [ | ||
Migration(1, 2, (database) { | ||
database.execute(sql); | ||
}) | ||
]; | ||
|
||
// ignore: invalid_use_of_protected_member | ||
final actual = () => floorDatabase.runMigrations( | ||
mockMigrationDatabase, | ||
startVersion, | ||
endVersion, | ||
migrations, | ||
); | ||
|
||
expect(actual, throwsStateError); | ||
verifyZeroInteractions(mockMigrationDatabase); | ||
}); | ||
|
||
test('exception when no matching end version found', () { | ||
const startVersion = 1; | ||
const endVersion = 10; | ||
const sql = 'FOO BAR'; | ||
final migrations = [ | ||
Migration(1, 2, (database) { | ||
database.execute(sql); | ||
}) | ||
]; | ||
|
||
// ignore: invalid_use_of_protected_member | ||
final actual = () => floorDatabase.runMigrations( | ||
mockMigrationDatabase, | ||
startVersion, | ||
endVersion, | ||
migrations, | ||
); | ||
|
||
expect(actual, throwsStateError); | ||
verifyZeroInteractions(mockMigrationDatabase); | ||
}); | ||
} | ||
|
||
class TestFloorDatabase extends FloorDatabase { | ||
@override | ||
Future<Database> open(List<Migration> migrations) { | ||
return null; | ||
} | ||
} | ||
|
||
class MockSqfliteDatabase extends Mock implements Database {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
import 'package:meta/meta.dart'; | ||
|
||
/// Marks a class as a FloorDatabase. | ||
class Database { | ||
/// The database version | ||
final int version; | ||
|
||
/// Marks a class as a FloorDatabase. | ||
const Database(); | ||
const Database({@required this.version}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.