refactor(persistence): moor to drift name change migration.

Signed-off-by: xsahil03x <[email protected]>
This commit is contained in:
Sahil Kumar
2021-10-25 14:01:42 +05:30
committed by xsahil03x
parent 281733cc3c
commit c58ddb1aef
74 changed files with 308 additions and 417 deletions
@@ -2,27 +2,27 @@
import 'dart:io';
import 'dart:isolate';
import 'package:moor/ffi.dart';
import 'package:moor/isolate.dart';
import 'package:moor/moor.dart';
import 'package:drift/drift.dart';
import 'package:drift/isolate.dart';
import 'package:drift/native.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:stream_chat_persistence/src/db/moor_chat_database.dart';
import 'package:stream_chat_persistence/src/db/drift_chat_database.dart';
import 'package:stream_chat_persistence/src/stream_chat_persistence_client.dart';
import 'package:stream_chat_persistence/stream_chat_persistence.dart';
/// A Helper class to construct new instances of [MoorChatDatabase] specifically
/// for native platform applications
/// A Helper class to construct new instances of [DriftChatDatabase]
/// specifically for native platform applications.
class SharedDB {
/// Returns a new instance of [MoorChatDatabase].
static MoorChatDatabase constructDatabase(
/// Returns a new instance of [DriftChatDatabase].
static DriftChatDatabase constructDatabase(
String userId, {
bool logStatements = false,
ConnectionMode connectionMode = ConnectionMode.regular,
}) {
final dbName = 'db_$userId';
if (connectionMode == ConnectionMode.background) {
return MoorChatDatabase.connect(
return DriftChatDatabase.connect(
userId,
DatabaseConnection.delayed(Future(() async {
final isolate = await _createMoorIsolate(
@@ -33,7 +33,7 @@ class SharedDB {
})),
);
}
return MoorChatDatabase(
return DriftChatDatabase(
userId,
LazyDatabase(
() async => _constructDatabase(
@@ -44,7 +44,7 @@ class SharedDB {
);
}
static Future<VmDatabase> _constructDatabase(
static Future<NativeDatabase> _constructDatabase(
String dbName, {
bool logStatements = false,
}) async {
@@ -52,27 +52,27 @@ class SharedDB {
final dir = await getApplicationDocumentsDirectory();
final path = join(dir.path, '$dbName.sqlite');
final file = File(path);
return VmDatabase(file, logStatements: logStatements);
return NativeDatabase(file, logStatements: logStatements);
}
if (Platform.isMacOS || Platform.isLinux) {
final file = File('$dbName.sqlite');
return VmDatabase(file, logStatements: logStatements);
return NativeDatabase(file, logStatements: logStatements);
}
return VmDatabase.memory(logStatements: logStatements);
return NativeDatabase.memory(logStatements: logStatements);
}
static void _startBackground(_IsolateStartRequest request) {
final executor = LazyDatabase(() async => VmDatabase(
final executor = LazyDatabase(() async => NativeDatabase(
File(request.targetPath),
logStatements: request.logStatements,
));
final moorIsolate = MoorIsolate.inCurrent(
final moorIsolate = DriftIsolate.inCurrent(
() => DatabaseConnection.fromExecutor(executor),
);
request.sendMoorIsolate.send(moorIsolate);
}
static Future<MoorIsolate> _createMoorIsolate(
static Future<DriftIsolate> _createMoorIsolate(
String dbName, {
bool logStatements = false,
}) async {
@@ -89,7 +89,7 @@ class SharedDB {
),
);
return await receivePort.first as MoorIsolate;
return await receivePort.first as DriftIsolate;
}
}
@@ -1,11 +1,11 @@
// coverage:ignore-file
import 'package:stream_chat_persistence/src/db/moor_chat_database.dart';
import 'package:stream_chat_persistence/src/db/drift_chat_database.dart';
import 'package:stream_chat_persistence/stream_chat_persistence.dart';
/// A Helper class to construct new instances of [MoorChatDatabase]
/// A Helper class to construct new instances of [DriftChatDatabase]
class SharedDB {
/// Returns a new instance of [MoorChatDatabase].
static MoorChatDatabase constructDatabase(
/// Returns a new instance of [DriftChatDatabase].
static DriftChatDatabase constructDatabase(
String userId, {
bool logStatements = false,
ConnectionMode connectionMode = ConnectionMode.regular,
@@ -1,19 +1,19 @@
// coverage:ignore-file
import 'package:moor/moor_web.dart';
import 'package:stream_chat_persistence/src/db/moor_chat_database.dart';
import 'package:drift/web.dart';
import 'package:stream_chat_persistence/src/db/drift_chat_database.dart';
import 'package:stream_chat_persistence/src/stream_chat_persistence_client.dart';
/// A Helper class to construct new instances of [MoorChatDatabase] specifically
/// for Web applications
/// A Helper class to construct new instances of [DriftChatDatabase]
/// specifically for Web applications.
class SharedDB {
/// Returns a new instance of [MoorChatDatabase].
static MoorChatDatabase constructDatabase(
/// Returns a new instance of [DriftChatDatabase].
static DriftChatDatabase constructDatabase(
String userId, {
bool logStatements = false,
ConnectionMode connectionMode = ConnectionMode.regular, // Ignored on web
}) {
final dbName = 'db_$userId';
final queryExecutor = WebDatabase(dbName, logStatements: logStatements);
return MoorChatDatabase(userId, queryExecutor);
return DriftChatDatabase(userId, queryExecutor);
}
}