Repository: apify/crawlee-python
File: src/crawlee/storage_clients/_sql/_storage_client.py (Python)

Code before:
        return client

    def _get_or_create_engine(self, configuration: Configuration) -> AsyncEngine:
        """Get or create the database engine based on configuration."""
        if self._engine is not None:
            return self._engine

        if self._connection_string is not None:
            connection_string = self._connection_string
        else:
            # Create SQLite database in the storage directory
            storage_dir = Path(configuration.storage_dir)
            if not storage_dir.exists():
                storage_dir.mkdir(parents=True, exist_ok=True)

            db_path = storage_dir / self._DEFAULT_DB_NAME

            # Create connection string with path to default database
            connection_string = f'sqlite+aiosqlite:///{db_path}'

        if (
            ('sqlite' not in connection_string)
            and ('postgresql' not in connection_string)
            and ('mysql' not in connection_string)
            and ('mariadb' not in connection_string)
        ):
            raise ValueError(
                'Unsupported database. Supported: sqlite, postgresql, mysql, mariadb. Consider using a different '
                'database.'
            )

        kwargs: dict[str, Any] = {}
        if 'mysql' in connection_string or 'mariadb' in connection_string:
            connect_args: dict[str, Any] = {'connect_timeout': 30}
            # MySQL/MariaDB require READ COMMITTED isolation level for correct behavior in concurrent environments
            # without deadlocks.
            kwargs['isolation_level'] = 'READ COMMITTED'
        else:
            connect_args = {'timeout': 30}

        self._engine = create_async_engine(
            connection_string,
            future=True,
            pool_size=5,
            max_overflow=10,
            pool_timeout=60,
            pool_recycle=600,
            pool_pre_ping=True,
            echo=False,
            connect_args=connect_args,
            **kwargs,

Review comment (suggestion):
This could be something like  `not any(dialect in connection_string for dialect in self._supported_dialects)`

Code after:

        await self._purge_if_needed(client, configuration)
        return client

    def _get_or_create_engine(self, configuration: Configuration) -> AsyncEngine:
        """Get or create the database engine based on configuration."""
        if self._engine is not None:
            return self._engine

        if self._connection_string is not None:
            connection_string = self._connection_string
        else:
            # Create SQLite database in the storage directory
            storage_dir = Path(configuration.storage_dir)
            if not storage_dir.exists():
                storage_dir.mkdir(parents=True, exist_ok=True)

            db_path = storage_dir / self._DEFAULT_DB_NAME

            # Create connection string with path to default database
            connection_string = f'sqlite+aiosqlite:///{db_path}'

        if not any(connection_string.startswith(dialect) for dialect in self._SUPPORTED_DIALECTS):
            raise ValueError(
                f'Unsupported database. Supported: {", ".join(self._SUPPORTED_DIALECTS)}. Consider using a different '
                'database.'
            )

        kwargs: dict[str, Any] = {}
        if 'mysql' in connection_string or 'mariadb' in connection_string:
            connect_args: dict[str, Any] = {'connect_timeout': 30}
            # MySQL/MariaDB require READ COMMITTED isolation level for correct behavior in concurrent environments
            # without deadlocks.
            kwargs['isolation_level'] = 'READ COMMITTED'
        else:
            connect_args = {'timeout': 30}

        self._engine = create_async_engine(
            connection_string,
            future=True,
            pool_size=5,
            max_overflow=10,
            pool_timeout=60,
            pool_recycle=600,
            pool_pre_ping=True,
            echo=False,
            connect_args=connect_args,
            **kwargs,
        )
        return self._engine