Skip to content

Commit 23acb7a

Browse files
committed
fix: enable autocommit for MSSQL connections
1 parent dee3140 commit 23acb7a

2 files changed

Lines changed: 62 additions & 1 deletion

File tree

sqlit/domains/connections/providers/mssql/adapter.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,10 @@ def connect(self, config: ConnectionConfig) -> Any:
180180
)
181181

182182
conn_str = self._build_connection_string(config)
183-
return mssql_python.connect(conn_str)
183+
conn = mssql_python.connect(conn_str)
184+
# Enable autocommit to allow DDL statements like CREATE DATABASE
185+
conn.autocommit = True
186+
return conn
184187

185188
def get_databases(self, conn: Any) -> list[str]:
186189
"""Get list of databases from SQL Server."""
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""Unit tests for MSSQL autocommit behavior.
2+
3+
Regression test for GitHub issue #107:
4+
CREATE DATABASE was failing with "CREATE DATABASE statement not allowed within
5+
multi-statement transaction" because the MSSQL adapter didn't set autocommit=True.
6+
"""
7+
8+
from __future__ import annotations
9+
10+
from unittest.mock import MagicMock, patch
11+
12+
import pytest
13+
14+
15+
class TestMSSQLAutocommitBehavior:
16+
"""Test that MSSQL adapter sets autocommit correctly."""
17+
18+
@pytest.fixture
19+
def mock_mssql_python(self):
20+
"""Create a mock mssql_python module."""
21+
mock_module = MagicMock()
22+
mock_conn = MagicMock()
23+
mock_conn.autocommit = False # Default behavior
24+
mock_module.connect.return_value = mock_conn
25+
with patch.dict("sys.modules", {"mssql_python": mock_module}):
26+
yield mock_module, mock_conn
27+
28+
def test_mssql_adapter_sets_autocommit(self, mock_mssql_python):
29+
"""Regression test: MSSQL adapter must set autocommit=True.
30+
31+
This prevents 'CREATE DATABASE statement not allowed within
32+
multi-statement transaction' errors (GitHub issue #107).
33+
"""
34+
from sqlit.domains.connections.domain.config import ConnectionConfig, TcpEndpoint
35+
from sqlit.domains.connections.providers.mssql.adapter import SQLServerAdapter
36+
37+
_, mock_conn = mock_mssql_python
38+
39+
adapter = SQLServerAdapter()
40+
config = ConnectionConfig(
41+
name="test_mssql",
42+
db_type="mssql",
43+
endpoint=TcpEndpoint(
44+
host="localhost",
45+
port="1433",
46+
database="master",
47+
username="sa",
48+
password="password",
49+
),
50+
options={"auth_type": "sql"},
51+
)
52+
53+
adapter.connect(config)
54+
55+
assert mock_conn.autocommit is True, (
56+
"MSSQL adapter must set autocommit=True after connecting "
57+
"to allow DDL statements like CREATE DATABASE"
58+
)

0 commit comments

Comments
 (0)