update drift to 2.7.0

Signed-off-by: xsahil03x <[email protected]>
This commit is contained in:
Sahil Kumar
2023-04-20 18:41:51 +05:30
committed by xsahil03x
parent a1a4e55e75
commit fc05a9a4cf
28 changed files with 2021 additions and 1740 deletions
+1 -1
View File
@@ -27,7 +27,7 @@ dependencies:
dev_dependencies:
build_runner: ^2.3.3
dart_code_metrics: ^5.7.0
dart_code_metrics: ^5.7.2
freezed: ^2.3.2
json_serializable: ^6.6.1
mocktail: ^0.3.0
+1 -1
View File
@@ -69,7 +69,7 @@ flutter:
uses-material-design: true
dev_dependencies:
dart_code_metrics: ^5.7.0
dart_code_metrics: ^5.7.2
flutter_test:
sdk: flutter
golden_toolkit: ^0.15.0
@@ -21,7 +21,7 @@ dependencies:
dev_dependencies:
build_runner: ^2.3.3
dart_code_metrics: ^5.7.0
dart_code_metrics: ^5.7.2
fake_async: ^1.2.0
flutter_test:
sdk: flutter
@@ -17,7 +17,7 @@ dependencies:
stream_chat_flutter: ^5.3.0
dev_dependencies:
dart_code_metrics: ^4.16.0
dart_code_metrics: ^5.7.2
flutter_test:
sdk: flutter
@@ -3,7 +3,6 @@ targets:
builders:
drift_dev:
options:
generate_connect_constructor: true
data_class_to_companions: false
apply_converters_on_variables: true
generate_values_in_copy_with: true
@@ -6,7 +6,21 @@ import 'package:drift/drift.dart';
/// by the sqlite backend.
class ListConverter<T> extends TypeConverter<List<T>, String> {
@override
List<T>? mapToDart(String? fromDb) {
List<T> fromSql(String fromDb) {
return List<T>.from(jsonDecode(fromDb) ?? []);
}
@override
String toSql(List<T> value) {
return jsonEncode(value);
}
}
/// Maps a nullable [List] of type [T] into a nullable [String] understood
/// by the sqlite backend.
class NullableListConverter<T> extends TypeConverter<List<T>?, String?> {
@override
List<T>? fromSql(String? fromDb) {
if (fromDb == null) {
return null;
}
@@ -14,7 +28,7 @@ class ListConverter<T> extends TypeConverter<List<T>, String> {
}
@override
String? mapToSql(List<T>? value) {
String? toSql(List<T>? value) {
if (value == null) {
return null;
}
@@ -6,7 +6,21 @@ import 'package:drift/drift.dart';
/// by the sqlite backend.
class MapConverter<T> extends TypeConverter<Map<String, T>, String> {
@override
Map<String, T>? mapToDart(String? fromDb) {
Map<String, T> fromSql(String fromDb) {
return Map<String, T>.from(jsonDecode(fromDb) ?? {});
}
@override
String toSql(Map<String, T> value) {
return jsonEncode(value);
}
}
/// Maps a nullable [Map] of type [String], [T] into a nullable [String]
/// understood by the sqlite backend.
class NullableMapConverter<T> extends TypeConverter<Map<String, T>?, String?> {
@override
Map<String, T>? fromSql(String? fromDb) {
if (fromDb == null) {
return null;
}
@@ -14,7 +28,7 @@ class MapConverter<T> extends TypeConverter<Map<String, T>, String> {
}
@override
String? mapToSql(Map<String, T>? value) {
String? toSql(Map<String, T>? value) {
if (value == null) {
return null;
}
@@ -6,7 +6,7 @@ import 'package:stream_chat/stream_chat.dart';
class MessageSendingStatusConverter
extends TypeConverter<MessageSendingStatus, int> {
@override
MessageSendingStatus? mapToDart(int? fromDb) {
MessageSendingStatus fromSql(int fromDb) {
switch (fromDb) {
case 0:
return MessageSendingStatus.sending;
@@ -22,13 +22,12 @@ class MessageSendingStatusConverter
return MessageSendingStatus.deleting;
case 6:
return MessageSendingStatus.failed_delete;
default:
return null;
}
return MessageSendingStatus.sending;
}
@override
int? mapToSql(MessageSendingStatus? value) {
int toSql(MessageSendingStatus value) {
switch (value) {
case MessageSendingStatus.sending:
return 0;
@@ -44,8 +43,6 @@ class MessageSendingStatusConverter
return 5;
case MessageSendingStatus.failed_delete:
return 6;
default:
return null;
}
}
}
@@ -2,10 +2,7 @@
part of 'channel_dao.dart';
// **************************************************************************
// DaoGenerator
// **************************************************************************
// ignore_for_file: type=lint
mixin _$ChannelDaoMixin on DatabaseAccessor<DriftChatDatabase> {
$ChannelsTable get channels => attachedDatabase.channels;
$UsersTable get users => attachedDatabase.users;
@@ -2,10 +2,7 @@
part of 'channel_query_dao.dart';
// **************************************************************************
// DaoGenerator
// **************************************************************************
// ignore_for_file: type=lint
mixin _$ChannelQueryDaoMixin on DatabaseAccessor<DriftChatDatabase> {
$ChannelQueriesTable get channelQueries => attachedDatabase.channelQueries;
$ChannelsTable get channels => attachedDatabase.channels;
@@ -2,10 +2,7 @@
part of 'connection_event_dao.dart';
// **************************************************************************
// DaoGenerator
// **************************************************************************
// ignore_for_file: type=lint
mixin _$ConnectionEventDaoMixin on DatabaseAccessor<DriftChatDatabase> {
$ConnectionEventsTable get connectionEvents =>
attachedDatabase.connectionEvents;
@@ -2,10 +2,7 @@
part of 'member_dao.dart';
// **************************************************************************
// DaoGenerator
// **************************************************************************
// ignore_for_file: type=lint
mixin _$MemberDaoMixin on DatabaseAccessor<DriftChatDatabase> {
$MembersTable get members => attachedDatabase.members;
$UsersTable get users => attachedDatabase.users;
@@ -2,11 +2,9 @@
part of 'message_dao.dart';
// **************************************************************************
// DaoGenerator
// **************************************************************************
// ignore_for_file: type=lint
mixin _$MessageDaoMixin on DatabaseAccessor<DriftChatDatabase> {
$ChannelsTable get channels => attachedDatabase.channels;
$MessagesTable get messages => attachedDatabase.messages;
$UsersTable get users => attachedDatabase.users;
}
@@ -2,11 +2,9 @@
part of 'pinned_message_dao.dart';
// **************************************************************************
// DaoGenerator
// **************************************************************************
// ignore_for_file: type=lint
mixin _$PinnedMessageDaoMixin on DatabaseAccessor<DriftChatDatabase> {
$ChannelsTable get channels => attachedDatabase.channels;
$PinnedMessagesTable get pinnedMessages => attachedDatabase.pinnedMessages;
$UsersTable get users => attachedDatabase.users;
}
@@ -2,10 +2,7 @@
part of 'pinned_message_reaction_dao.dart';
// **************************************************************************
// DaoGenerator
// **************************************************************************
// ignore_for_file: type=lint
mixin _$PinnedMessageReactionDaoMixin on DatabaseAccessor<DriftChatDatabase> {
$PinnedMessageReactionsTable get pinnedMessageReactions =>
attachedDatabase.pinnedMessageReactions;
@@ -2,10 +2,7 @@
part of 'reaction_dao.dart';
// **************************************************************************
// DaoGenerator
// **************************************************************************
// ignore_for_file: type=lint
mixin _$ReactionDaoMixin on DatabaseAccessor<DriftChatDatabase> {
$ReactionsTable get reactions => attachedDatabase.reactions;
$UsersTable get users => attachedDatabase.users;
@@ -2,10 +2,7 @@
part of 'read_dao.dart';
// **************************************************************************
// DaoGenerator
// **************************************************************************
// ignore_for_file: type=lint
mixin _$ReadDaoMixin on DatabaseAccessor<DriftChatDatabase> {
$ReadsTable get reads => attachedDatabase.reads;
$UsersTable get users => attachedDatabase.users;
@@ -2,10 +2,7 @@
part of 'user_dao.dart';
// **************************************************************************
// DaoGenerator
// **************************************************************************
// ignore_for_file: type=lint
mixin _$UserDaoMixin on DatabaseAccessor<DriftChatDatabase> {
$UsersTable get users => attachedDatabase.users;
}
@@ -1,6 +1,5 @@
import 'package:drift/drift.dart';
import 'package:stream_chat/stream_chat.dart';
import 'package:stream_chat_persistence/src/converter/converter.dart';
import 'package:stream_chat_persistence/src/dao/dao.dart';
import 'package:stream_chat_persistence/src/entity/entity.dart';
@@ -37,18 +36,12 @@ part 'drift_chat_database.g.dart';
],
)
class DriftChatDatabase extends _$DriftChatDatabase {
/// Creates a new moor chat database instance
/// Creates a new drift chat database instance
DriftChatDatabase(
this._userId,
QueryExecutor executor,
) : super(executor);
/// Instantiate a new database instance
DriftChatDatabase.connect(
this._userId,
DatabaseConnection connection,
) : super.connect(connection);
final String _userId;
/// User id to which the database is connected
File diff suppressed because it is too large Load Diff
@@ -23,7 +23,7 @@ class SharedDB {
}) async {
final dbName = 'db_$userId';
if (connectionMode == ConnectionMode.background) {
return DriftChatDatabase.connect(
return DriftChatDatabase(
userId,
DatabaseConnection.delayed(Future(() async {
final isolate = await _createMoorIsolate(
@@ -34,6 +34,7 @@ class SharedDB {
})),
);
}
return DriftChatDatabase(
userId,
LazyDatabase(
@@ -68,7 +69,7 @@ class SharedDB {
logStatements: request.logStatements,
));
final moorIsolate = DriftIsolate.inCurrent(
() => DatabaseConnection.fromExecutor(executor),
() => DatabaseConnection(executor),
);
request.sendMoorIsolate.send(moorIsolate);
}
@@ -4,6 +4,8 @@ import 'package:stream_chat_persistence/src/converter/list_converter.dart';
import 'package:stream_chat_persistence/src/converter/map_converter.dart';
import 'package:stream_chat_persistence/src/converter/message_sending_status_converter.dart';
import 'channels.dart';
/// Represents a [Messages] table in [MoorChatDatabase].
@DataClassName('MessageEntity')
class Messages extends Table {
@@ -78,10 +80,11 @@ class Messages extends Table {
/// The channel cid of which this message is part of
TextColumn get channelCid =>
text().customConstraint('REFERENCES channels(cid) ON DELETE CASCADE')();
text().references(Channels, #cid, onDelete: KeyAction.cascade)();
/// A Map of [messageText] translations.
TextColumn get i18n => text().nullable().map(MapConverter<String>())();
TextColumn get i18n =>
text().nullable().map(NullableMapConverter<String>())();
/// Message custom extraData
TextColumn get extraData => text().nullable().map(MapConverter<Object?>())();
@@ -1,6 +1,5 @@
// coverage:ignore-file
import 'package:drift/drift.dart';
import 'package:stream_chat_persistence/src/entity/messages.dart';
/// Represents a [PinnedMessages] table in [MoorChatDatabase].
@@ -10,7 +10,7 @@ environment:
flutter: ">=1.17.0"
dependencies:
drift: ^1.7.1
drift: ^2.7.0
flutter:
sdk: flutter
logging: ^1.0.1
@@ -23,8 +23,8 @@ dependencies:
dev_dependencies:
build_runner: ^2.3.3
dart_code_metrics: ^4.18.0
drift_dev: ^1.7.1
dart_code_metrics: ^5.7.2
drift_dev: ^2.7.0
flutter_test:
sdk: flutter
mocktail: ^0.3.0
@@ -4,50 +4,89 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:stream_chat_persistence/src/converter/list_converter.dart';
void main() {
group('mapToDart', () {
group('ListConverter', () {
final listConverter = ListConverter<String>();
test('should return null if nothing is provided', () {
final res = listConverter.mapToDart(null);
expect(res, isNull);
group('fromSql', () {
test('should throw type error if the provided json is not a list', () {
final json = {'test_key': 'testData'};
expect(
() => listConverter.fromSql(jsonEncode(json)),
throwsA(isA<TypeError>()),
);
});
test(
'should throw type error if the provided json is not a list of String',
() {
final json = [22, 33, 44];
expect(
() => listConverter.fromSql(jsonEncode(json)),
throwsA(isA<TypeError>()),
);
});
test('should return list of String if json data list is provided', () {
final data = ['data1', 'data2', 'data3'];
final res = listConverter.fromSql(jsonEncode(data));
expect(res.length, data.length);
});
});
test('should throw type error if the provided json is not a list', () {
final json = {'test_key': 'testData'};
expect(
() => listConverter.mapToDart(jsonEncode(json)),
throwsA(isA<TypeError>()),
);
});
test('should throw type error if the provided json is not a list of String',
() {
final json = [22, 33, 44];
expect(
() => listConverter.mapToDart(jsonEncode(json)),
throwsA(isA<TypeError>()),
);
});
test('should return list of String if json data list is provided', () {
final data = ['data1', 'data2', 'data3'];
final res = listConverter.mapToDart(jsonEncode(data));
expect(res!.length, data.length);
group('toSql', () {
test('should return json string if data list is provided', () {
final data = ['data1', 'data2', 'data3'];
final res = listConverter.toSql(data);
expect(res, jsonEncode(data));
});
});
});
group('mapToSql', () {
final listConverter = ListConverter<String>();
group('NullableListConverter', () {
final listConverter = NullableListConverter<String>();
test('should return null if nothing is provided', () {
final res = listConverter.mapToSql(null);
expect(res, isNull);
group('fromSql', () {
test('should return null if nothing is provided', () {
final res = listConverter.fromSql(null);
expect(res, isNull);
});
test('should throw type error if the provided json is not a list', () {
final json = {'test_key': 'testData'};
expect(
() => listConverter.fromSql(jsonEncode(json)),
throwsA(isA<TypeError>()),
);
});
test(
'should throw type error if the provided json is not a list of String',
() {
final json = [22, 33, 44];
expect(
() => listConverter.fromSql(jsonEncode(json)),
throwsA(isA<TypeError>()),
);
});
test('should return list of String if json data list is provided', () {
final data = ['data1', 'data2', 'data3'];
final res = listConverter.fromSql(jsonEncode(data));
expect(res!.length, data.length);
});
});
test('should return json string if data list is provided', () {
final data = ['data1', 'data2', 'data3'];
final res = listConverter.mapToSql(data);
expect(res, jsonEncode(data));
group('toSql', () {
test('should return null if nothing is provided', () {
final res = listConverter.toSql(null);
expect(res, isNull);
});
test('should return json string if data list is provided', () {
final data = ['data1', 'data2', 'data3'];
final res = listConverter.toSql(data);
expect(res, jsonEncode(data));
});
});
});
}
@@ -4,61 +4,109 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:stream_chat_persistence/src/converter/map_converter.dart';
void main() {
group('mapToDart', () {
group('MapConverter', () {
final mapConverter = MapConverter<String>();
test('should return null if nothing is provided', () {
final res = mapConverter.mapToDart(null);
expect(res, isNull);
});
test('should throw type error if the provided json is not a map', () {
const json = ['testData1', 'testData2', 'testData3'];
expect(
() => mapConverter.mapToDart(jsonEncode(json)),
throwsA(isA<TypeError>()),
);
});
test(
'should throw type error if the provided json is not a '
'map of String, String',
() {
const json = {'test_key': 22, 'test_key2': 33, 'test_key3': 44};
group('fromSql', () {
test('should throw type error if the provided json is not a map', () {
const json = ['testData1', 'testData2', 'testData3'];
expect(
() => mapConverter.mapToDart(jsonEncode(json)),
() => mapConverter.fromSql(jsonEncode(json)),
throwsA(isA<TypeError>()),
);
},
);
});
test('should return map of String, String if json data is provided', () {
const data = {
'test_key': 'testValue',
'test_key2': 'testValue2',
'test_key3': 'testValue3',
};
final res = mapConverter.mapToDart(jsonEncode(data));
expect(res, data);
test(
'should throw type error if the provided json is not a '
'map of String, String',
() {
const json = {'test_key': 22, 'test_key2': 33, 'test_key3': 44};
expect(
() => mapConverter.fromSql(jsonEncode(json)),
throwsA(isA<TypeError>()),
);
},
);
test('should return map of String, String if json data is provided', () {
const data = {
'test_key': 'testValue',
'test_key2': 'testValue2',
'test_key3': 'testValue3',
};
final res = mapConverter.fromSql(jsonEncode(data));
expect(res, data);
});
});
group('toSql', () {
test('should return json string if data map is provided', () {
const data = {
'test_key': 'testValue',
'test_key2': 'testValue2',
'test_key3': 'testValue3',
};
final res = mapConverter.toSql(data);
expect(res, jsonEncode(data));
});
});
});
group('mapToSql', () {
final mapConverter = MapConverter<String>();
group('NullableMapConverter', () {
final mapConverter = NullableMapConverter<String>();
test('should return null if nothing is provided', () {
final res = mapConverter.mapToSql(null);
expect(res, isNull);
group('fromSql', () {
test('should return null if nothing is provided', () {
final res = mapConverter.fromSql(null);
expect(res, isNull);
});
test('should throw type error if the provided json is not a map', () {
const json = ['testData1', 'testData2', 'testData3'];
expect(
() => mapConverter.fromSql(jsonEncode(json)),
throwsA(isA<TypeError>()),
);
});
test(
'should throw type error if the provided json is not a '
'map of String, String',
() {
const json = {'test_key': 22, 'test_key2': 33, 'test_key3': 44};
expect(
() => mapConverter.fromSql(jsonEncode(json)),
throwsA(isA<TypeError>()),
);
},
);
test('should return map of String, String if json data is provided', () {
const data = {
'test_key': 'testValue',
'test_key2': 'testValue2',
'test_key3': 'testValue3',
};
final res = mapConverter.fromSql(jsonEncode(data));
expect(res, data);
});
});
test('should return json string if data map is provided', () {
const data = {
'test_key': 'testValue',
'test_key2': 'testValue2',
'test_key3': 'testValue3',
};
final res = mapConverter.mapToSql(data);
expect(res, jsonEncode(data));
group('toSql', () {
test('should return null if nothing is provided', () {
final res = mapConverter.toSql(null);
expect(res, isNull);
});
test('should return json string if data map is provided', () {
const data = {
'test_key': 'testValue',
'test_key2': 'testValue2',
'test_key3': 'testValue3',
};
final res = mapConverter.toSql(data);
expect(res, jsonEncode(data));
});
});
});
}
@@ -3,30 +3,20 @@ import 'package:stream_chat/stream_chat.dart';
import 'package:stream_chat_persistence/src/converter/message_sending_status_converter.dart';
void main() {
group('mapToDart', () {
group('fromSql', () {
final statusConverter = MessageSendingStatusConverter();
test('should return null if nothing is provided', () {
final res = statusConverter.mapToDart(null);
expect(res, isNull);
});
test('should return expected status if status code is provided', () {
final res = statusConverter.mapToDart(3);
final res = statusConverter.fromSql(3);
expect(res, MessageSendingStatus.updating);
});
});
group('mapToSql', () {
group('toSql', () {
final statusConverter = MessageSendingStatusConverter();
test('should return null if nothing is provided', () {
final res = statusConverter.mapToSql(null);
expect(res, isNull);
});
test('should return expected code if the status is provided', () {
final res = statusConverter.mapToSql(MessageSendingStatus.updating);
final res = statusConverter.toSql(MessageSendingStatus.updating);
expect(res, 3);
});
});
@@ -30,7 +30,7 @@ void main() {
final isolate = await DriftIsolate.spawn(_backgroundConnection);
final connection = DatabaseConnection.delayed(isolate.connect());
final database = DriftChatDatabase.connect(userId, connection);
final database = DriftChatDatabase(userId, connection);
expect(database, isNotNull);
expect(database.userId, userId);