rwnx / pynonymizer

A universal tool for translating sensitive production database dumps into anonymized copies.
https://pypi.org/project/pynonymizer/
MIT License
101 stars 38 forks source link

Request support for SQLEXPRESS for db-type = mssql #134

Closed sjswerdloff closed 5 months ago

sjswerdloff commented 7 months ago

I have a (MS SQL Server) SQLEXPRESS instance that I use for development purposes. I can't connect to it with pynonymizer. I was able to connect using the pyodbc package with appropriate credentials and server=(local)\SQLEXPRESS.

I would like to be able to connect to a SQLEXPRESS instance. It could be an option to convey that I want to connect to a SQLEXPRESS instance. maybe --mssql-named-instance = SQLEXPRESS

while I'm not that familiar with the details of MS SQL Server, they do have "named instances" that are not SQLEXPRESS.

The alternative approach I took to see if I could "make it work" was to wrap the initial connection attempt (in mssql/init.py, in __db_connection ) with a try/except and in the except, replace the server string with "(local)\SQLEXPRESS". That worked. And that would certainly be good enough for me (it adds a bit of a delay, but in the scheme of things, that's probably not a big deal).

I also tried server=f"{self.db_host}\\SQLEXPRESS,{self.db_port}", and server=f"{self.db_host}\\SQLEXPRESS", but those didn't work.

I did this on Windows 11 with SQL Server 16.0.1000.

rwnx commented 5 months ago

Is this something that could be solved with access to the connection string directly? There is a feature request at #135 that might solve this.

I'm hoping we can kill two birds with one stone here as this is something that I think we can implement. Can you take a look and see if it meets your needs?

sjswerdloff commented 5 months ago

Is this something that could be solved with access to the connection string directly? There is a feature request at #135 that might solve this.

I'm hoping we can kill two birds with one stone here as this is something that I think we can implement. Can you take a look and see if it meets your needs?

I think so. I added the following to an except block after the initial connection code:

                print("Failed initial attempt at SQL Server connection, retrying with SQLExpress")
                server_string = "(local)\\SQLEXPRESS"
                print(server_string)
                self.__db_conn = pyodbc.connect(
                    driver=f"{{{self.__driver}}}",
                    database=self.db_name,
                    server=server_string,
                    uid=self.db_user,
                    pwd=self.db_pass,
                    autocommit=True,
                )

so if "everything" that goes in to the connect function call is extracted from the connection string (or is the connection string), and at least the server key/value pair gets set with that, I'm perfectly happy passing in a complete connection string (with a named instance).

rwnx commented 5 months ago

hi, this change is in the v2 milestone and will be released this week:

You can now pass "(local)\SQLEXPRESS" to the --db-host option and it should override other options. you can also customize the connection stribg using --mssql-connection-string