Reuben (Matatika)
06/17/2024, 4:22 PMtarget-mssql to a newer SDK version, where it is not playing nicely with the SQLConnection refactor back in v0.20.0... I'm getting an Invalid object name error, which suggests to me some issue with the target engine/connection implementation (works fine on v0.19.0, breaks on v0.20.0). Would appreciate some pointers if anyone has some experience here. 🙂
`connector.py`: https://github.com/storebrand/target-mssql/blob/56968e01ab9f7295ef9a0aeeec96459353185d70/target_mssql/connector.py
SDK v0.19.0 <- v0.20.0 diff: https://github.com/meltano/sdk/compare/v0.19.0...v0.20.0?diff=split&w=#diff-db20b40a2eb1ac17938f49d9757779cdb9998129d7fc075a964d20096ddb4b48BuzzCutNorman
06/17/2024, 5:55 PMself.connection.execute which is deprecated and replace it with a variation of this code I grabbed from the SDK's SQL Sink bulk_insert_records.
with self.connector._connect() as conn, conn.begin():
result = conn.execute(insert_sql, new_records)BuzzCutNorman
06/17/2024, 6:55 PMReuben (Matatika)
06/17/2024, 7:10 PMBuzzCutNorman
06/17/2024, 7:56 PM@contextmanager
def _connect(self) -> t.Iterator[sa.engine.Connection]:
with self._engine.connect().execution_options(stream_results=True) as conn:
yield conn
When you call _connect you are dealing with a context manager that I believe will close the connection given once an execution occurs and finishes. Off the top of my head your options are finding a way to have the Sink create and sustain a connection from _engine for the entire merge insert process or move to using permanent tempdb tables that will stay once a connection closes but you will need to track and clean them up.BuzzCutNorman
06/17/2024, 9:05 PM_sink_conn variable during the sink init process
self._sink_conn = self.connector._engine.connect()
You can then use that in the functions called in process_batch. The only other change was to update create_temp_from_table in the mssqlConnector class to take in a connection argument and use it.
def create_temp_table_from_table(self, from_table_name, sink_con = None):
"""Temp table from another table."""
if sink_con is None:
sink_con = self.connection
it works but I bet there is a better way to do it. I hope this at least gives you some helpful information to work with.Reuben (Matatika)
06/18/2024, 2:03 PMconnection property for the time being:
https://github.com/Matatika/target-mssql/blob/9abc4d00df1123a1f239d03ea7f4c8cf223b2faf/target_mssql/connector.py#L400-L402
Bit of an empty victory as I don't think we need the version upgrade after all now, but I suppose it's good just for keeping the plugin updated.