Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions lib/core/database/mongodb_connection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ class MongoConnection {
Db? _db;
bool _isConnected = false;

/// Effective database name from [database] configuration or connection URI path.
String? get effectiveDatabase {
if (database != null && database!.trim().isNotEmpty) {
return database!.trim();
}
final rawUri = _sessionUri ?? _connectionString;
if (rawUri != null && rawUri.isNotEmpty) {
try {
final parsed = Uri.parse(rawUri);
final path = parsed.path.replaceFirst('/', '').trim();
if (path.isNotEmpty) {
return path;
}
} catch (_) {}
}
return null;
}

/// Scrubs sensitive in-memory credentials once the network handshake completes.
///
/// Getters [password] and [connectionString] become null. The live session
Expand Down Expand Up @@ -298,6 +316,12 @@ class MongoConnection {
.where((name) => name.isNotEmpty)
.toList();
} catch (e) {
final fallback = effectiveDatabase;
if (fallback != null &&
fallback.isNotEmpty &&
fallback.toLowerCase() != 'admin') {
return [fallback];
}
rethrow;
}
}
Expand Down
13 changes: 13 additions & 0 deletions lib/features/mongodb/mongo_databases_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,19 @@ class _MongoDatabasesViewState extends State<MongoDatabasesView> {
});
}
} catch (e) {
final fallback = _connection?.effectiveDatabase;
if (fallback != null &&
fallback.isNotEmpty &&
fallback.toLowerCase() != 'admin') {
if (mounted) {
setState(() {
_databases = [_DatabaseInfo(name: fallback, sizeOnDisk: null)];
_isLoading = false;
_error = null;
});
}
return;
}
if (mounted) {
setState(() {
_error = 'Failed to list databases: $e';
Expand Down
35 changes: 35 additions & 0 deletions test/core/database/mongodb_connection_fallback_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:querya_desktop/core/database/mongodb_connection.dart';

void main() {
group('MongoConnection.effectiveDatabase', () {
test('returns database from property if configured', () {
final conn = MongoConnection(
id: 1,
name: 'test',
host: 'localhost',
database: ' analytics_prod ',
);
expect(conn.effectiveDatabase, 'analytics_prod');
});

test('returns database from connectionString URI path if database property is omitted', () {
final conn = MongoConnection(
id: 1,
name: 'test',
host: 'localhost',
connectionString: 'mongodb://user:secret@mongo.cloud.com:27017/tenant_db?ssl=true',
);
expect(conn.effectiveDatabase, 'tenant_db');
});

test('returns null when neither property nor URI has a database', () {
final conn = MongoConnection(
id: 1,
name: 'test',
host: 'localhost',
);
expect(conn.effectiveDatabase, isNull);
});
});
}
Loading