Log statements during migrations, if desired

This commit is contained in:
Simon Binder 2019-04-19 17:59:30 +02:00
parent 83f8fdd105
commit f15e06fbb0
No known key found for this signature in database
GPG Key ID: B807FDF954BA00CF
1 changed files with 13 additions and 3 deletions

View File

@ -21,7 +21,7 @@ abstract class _DatabaseOwner extends QueryExecutor {
final bool logStatements;
void _log(String sql, List args) {
void _log(String sql, [List args]) {
if (logStatements == true) {
final formattedArgs = (args?.isEmpty ?? true) ? ' no variables' : args;
print('moor: $sql with $formattedArgs');
@ -94,12 +94,12 @@ class FlutterQueryExecutor extends _DatabaseOwner {
onCreate: (db, version) {
_hadMigration = true;
return databaseInfo.handleDatabaseCreation(
executor: (sql) => db.execute(sql),
executor: _migrationExecutor(db),
);
}, onUpgrade: (db, from, to) {
_hadMigration = true;
return databaseInfo.handleDatabaseVersionChange(
executor: (sql) => db.execute(sql), from: from, to: to);
executor: _migrationExecutor(db), from: from, to: to);
}, onOpen: (db) async {
db = db;
// the openDatabase future will resolve later, so we can get an instance
@ -113,6 +113,16 @@ class FlutterQueryExecutor extends _DatabaseOwner {
return true;
}
SqlExecutor _migrationExecutor(s.Database db) {
return (sql) {
if (logStatements) {
_log(sql);
}
db.execute(sql);
};
}
@override
TransactionExecutor beginTransaction() {
return _SqfliteTransactionExecutor.startFromDb(this);