Skip to content

Use SqlServerContainer with pyodbc driver #244

Open

Description

@gplssm

In fc5f2df#diff-313a71bf8d0b95ccd84a98f198294e10a7c7086c527efb513d9c6619a12086ee the default driver of SqlServerContainer was changed to pymssql.
Currently, I use the pyodbc driver to communicate with MSSQL databases. This is not possible anymore since then. I've tried

test_container = SqlServerContainer(
        image="mcr.microsoft.com/mssql/server:2017-latest",
        dialect="mssql+pyodbc",
    )

which leads to a timeout
testcontainers.core.exceptions.TimeoutException: Wait time (120s) exceeded for _connect(args: (), kwargs {}). Exception: (pyodbc.InterfaceError) ('IM002', '[IM002] [unixODBC][Driver Manager]Data source name not found and no default driver specified (0) (SQLDriverConnect)')

Alternatively, I've tried to specify the driver

test_container = SqlServerContainer(
        image="mcr.microsoft.com/mssql/server:2017-latest",
        dialect="mssql+pyodbc",
        driver = "ODBC Driver 17 for SQL Server"
    )

but this keyword isn't allowed anymore.
> raise create_unexpected_kwargs_error('run', kwargs) E TypeError: run() got an unexpected keyword argument 'driver'

Is it still the goal of testcontainers to support using the pyodbc driver? If so, I might be motivated to support a little bit making this possible again in 3.6.x versions.

Activity

  1. tillahoffmann commented on Sep 13, 2022

    @tillahoffmann
    Contributor

    Based on fc5f2df#diff-313a71bf8d0b95ccd84a98f198294e10a7c7086c527efb513d9c6619a12086eeL45, can you try adding with_env("SQLSERVER_DRIVER", "ODBC Driver 17 for SQL Server") and see whether that does the trick?

  2. gplssm commented on Sep 30, 2022

    @gplssm
    Author

    Sorry for the long pause...

    Yeah, that helps if we use the following as get_connection_url(self)

    def get_connection_url(self):
    standard_url = super()._create_connection_url(dialect="mssql+pyodbc",
                                                  username=self.SQLSERVER_USER,
                                                  password=self.SQLSERVER_PASSWORD,
                                                  db_name=self.SQLSERVER_DBNAME,
                                                  port=self.port_to_expose)
    if "SQLSERVER_DRIVER" in self.env:
        return standard_url + "?driver=" + self.env["SQLSERVER_DRIVER"]
    else:
        return standard_url

    Would you be interested in a PR?

  3. tillahoffmann commented on Jan 6, 2023

    @tillahoffmann
    Contributor

    Maybe we can add a note to the documentation? I'm keen to drop pyodbc support because it requires the additional installation of drivers.

  4. mortenbpost commented on Jan 11, 2023

    @mortenbpost

    Why would you want to get rid of pyodbc support? It's the only way to use an official microsoft driver and pyodbc is actively contributed to by Microsoft. This should be the preferred way. The support to use the driver was already there in the past. We are actually maintaining a fork just to support this. A PR to readd the functionality would be nice.

  5. tillahoffmann commented on Jan 16, 2023

    @tillahoffmann
    Contributor

    How about we add a driver argument as in #156?

  6. gplssm commented on Jan 20, 2023

    @gplssm
    Author

    How about we add a driver argument as in #156?

    I like the idea! And I think we can do it very similar. One difference between pymssql and pyodbc then will be that we have to append an "?driver=ODBC+Driver+17+for+SQL+Server" to the resulting URL for pyodbc.

    I'll see if I can prepare a PR so that can discuss details along the code changes.

  7. added
    ✅ close on merge
    on Feb 16, 2023
  8. meiswjn commented on Nov 14, 2024

    @meiswjn
    Contributor

    Hey :) Any news on this? The PR looks pretty far already

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

    ✅ close on merge📦 package: mssql

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions