Skip to content

refactor(database): extract duplicate SSH tunnel lifecycle into TunnelableConnectionMixin #1508

Open

Description

@ZhuchkaTriplesix

Summary

Across all database connection implementations:

  • lib/core/database/postgres_connection.dart (lines 121–348)
  • lib/core/database/mysql_connection.dart (lines 126–338)
  • lib/core/database/mongodb_connection.dart (lines 47–291)
  • lib/core/database/redis_connection.dart

The exact same 40–50 lines of SSH tunneling logic are copy-pasted:

  1. Field definitions:
    final SshTunnelConfig? sshConfig;
    final SshTunnelSecrets? sshSecrets;
    SshTunnelHandle? _sshTunnelHandle;
    SshTunnelHandle? get sshTunnelHandle => _sshTunnelHandle;
  2. Opening tunnels:
    _sshTunnelHandle = await SshTunnelManager.instance.openTunnel(
      config: sshConfig!,
      secrets: sec ?? SshTunnelSecrets(),
    );
  3. Host/port overriding (_sshTunnelHandle?.localHost, _sshTunnelHandle?.localPort).
  4. Releasing tunnels in disconnect() and onDispose():
    await _sshTunnelHandle?.release();
    _sshTunnelHandle = null;

This violates DRY and increases the maintenance burden whenever tunnel timeouts, retries, or socket forwarding logic are improved.

Proposed Solution

  1. Create TunnelableConnectionMixin in lib/core/database/tunnelable_connection_mixin.dart:
    • Manages SshTunnelHandle? _sshTunnelHandle.
    • Provides Future<({String host, int port})> openTunnelEndpoint(...).
    • Provides Future<void> releaseTunnel().
  2. Mix TunnelableConnectionMixin into PostgresConnection, MySqlConnection, MongoConnection, and RedisConnection.
  3. Standardize tunnel release semantics across all database drivers.

Acceptance Criteria

  • Common SSH tunneling logic is centralized in a single mixin.
  • No regression in connecting to PostgreSQL, MySQL, MongoDB, or Redis via SSH tunnels.
  • Over 150 lines of duplicate boilerplate are removed across the database drivers.

Activity

  1. added
    coreCore library logic and services
    connectionsDatabase connections, URI parsing, pools
    backendBackend database driver execution and queries
    P2Medium priority / Parity & Refactoring
    on Oct 12, 2026
  2. added this to the 0.5.0 milestone on Oct 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Medium priority / Parity & RefactoringbackendBackend database driver execution and queriesconnectionsDatabase connections, URI parsing, poolscoreCore library logic and services

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions