sqlalchemy-mssql-bulkcopy 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sqlalchemy_mssql_bulkcopy-0.1.0/.github/workflows/ci.yml +70 -0
- sqlalchemy_mssql_bulkcopy-0.1.0/.github/workflows/publish.yml +61 -0
- sqlalchemy_mssql_bulkcopy-0.1.0/.gitignore +14 -0
- sqlalchemy_mssql_bulkcopy-0.1.0/LICENSE +21 -0
- sqlalchemy_mssql_bulkcopy-0.1.0/PKG-INFO +165 -0
- sqlalchemy_mssql_bulkcopy-0.1.0/README.md +133 -0
- sqlalchemy_mssql_bulkcopy-0.1.0/pyproject.toml +69 -0
- sqlalchemy_mssql_bulkcopy-0.1.0/setup.cfg +4 -0
- sqlalchemy_mssql_bulkcopy-0.1.0/src/sqlalchemy_mssql_bulkcopy/__init__.py +302 -0
- sqlalchemy_mssql_bulkcopy-0.1.0/src/sqlalchemy_mssql_bulkcopy/_version.py +24 -0
- sqlalchemy_mssql_bulkcopy-0.1.0/src/sqlalchemy_mssql_bulkcopy/py.typed +0 -0
- sqlalchemy_mssql_bulkcopy-0.1.0/src/sqlalchemy_mssql_bulkcopy.egg-info/PKG-INFO +165 -0
- sqlalchemy_mssql_bulkcopy-0.1.0/src/sqlalchemy_mssql_bulkcopy.egg-info/SOURCES.txt +16 -0
- sqlalchemy_mssql_bulkcopy-0.1.0/src/sqlalchemy_mssql_bulkcopy.egg-info/dependency_links.txt +1 -0
- sqlalchemy_mssql_bulkcopy-0.1.0/src/sqlalchemy_mssql_bulkcopy.egg-info/requires.txt +8 -0
- sqlalchemy_mssql_bulkcopy-0.1.0/src/sqlalchemy_mssql_bulkcopy.egg-info/top_level.txt +1 -0
- sqlalchemy_mssql_bulkcopy-0.1.0/tests/integration.py +206 -0
- sqlalchemy_mssql_bulkcopy-0.1.0/tests/test_bulkcopy.py +450 -0
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [main]
|
|
8
|
+
|
|
9
|
+
concurrency:
|
|
10
|
+
group: ci-${{ github.ref }}
|
|
11
|
+
cancel-in-progress: true
|
|
12
|
+
|
|
13
|
+
jobs:
|
|
14
|
+
test:
|
|
15
|
+
name: Python ${{ matrix.python-version }}
|
|
16
|
+
runs-on: ubuntu-latest
|
|
17
|
+
strategy:
|
|
18
|
+
fail-fast: false
|
|
19
|
+
matrix:
|
|
20
|
+
python-version: ["3.10", "3.11", "3.12", "3.13"]
|
|
21
|
+
|
|
22
|
+
steps:
|
|
23
|
+
- uses: actions/checkout@v6
|
|
24
|
+
with:
|
|
25
|
+
persist-credentials: false
|
|
26
|
+
fetch-depth: 0
|
|
27
|
+
|
|
28
|
+
- uses: actions/setup-python@v6
|
|
29
|
+
with:
|
|
30
|
+
python-version: ${{ matrix.python-version }}
|
|
31
|
+
cache: pip
|
|
32
|
+
cache-dependency-path: pyproject.toml
|
|
33
|
+
|
|
34
|
+
- run: pip install -e ".[dev]"
|
|
35
|
+
- run: pip install "sqlalchemy @ git+https://github.com/sqlalchemy/sqlalchemy.git@main"
|
|
36
|
+
- run: ruff check src/ tests/
|
|
37
|
+
- run: ruff format --check src/ tests/
|
|
38
|
+
- run: pytest --cov=sqlalchemy_mssql_bulkcopy --cov-report=term-missing
|
|
39
|
+
|
|
40
|
+
integration:
|
|
41
|
+
name: Integration (SQL Server 2022)
|
|
42
|
+
runs-on: ubuntu-latest
|
|
43
|
+
services:
|
|
44
|
+
sqlserver:
|
|
45
|
+
image: mcr.microsoft.com/mssql/server:2022-latest
|
|
46
|
+
env:
|
|
47
|
+
ACCEPT_EULA: Y
|
|
48
|
+
MSSQL_SA_PASSWORD: BenchMark!Pass123
|
|
49
|
+
ports:
|
|
50
|
+
- 1433:1433
|
|
51
|
+
options: >-
|
|
52
|
+
--health-cmd "/opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P 'BenchMark!Pass123' -C -Q 'SELECT 1'"
|
|
53
|
+
--health-interval 10s
|
|
54
|
+
--health-timeout 5s
|
|
55
|
+
--health-retries 10
|
|
56
|
+
--health-start-period 30s
|
|
57
|
+
|
|
58
|
+
steps:
|
|
59
|
+
- uses: actions/checkout@v6
|
|
60
|
+
with:
|
|
61
|
+
persist-credentials: false
|
|
62
|
+
fetch-depth: 0
|
|
63
|
+
|
|
64
|
+
- uses: actions/setup-python@v6
|
|
65
|
+
with:
|
|
66
|
+
python-version: "3.12"
|
|
67
|
+
|
|
68
|
+
- run: pip install -e ".[dev]"
|
|
69
|
+
- run: pip install "sqlalchemy @ git+https://github.com/sqlalchemy/sqlalchemy.git@main"
|
|
70
|
+
- run: python tests/integration.py
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
name: Publish
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
tags: ["v*"]
|
|
6
|
+
|
|
7
|
+
concurrency:
|
|
8
|
+
group: publish-${{ github.ref }}
|
|
9
|
+
cancel-in-progress: false
|
|
10
|
+
|
|
11
|
+
jobs:
|
|
12
|
+
build:
|
|
13
|
+
runs-on: ubuntu-latest
|
|
14
|
+
steps:
|
|
15
|
+
- uses: actions/checkout@v6
|
|
16
|
+
with:
|
|
17
|
+
persist-credentials: false
|
|
18
|
+
fetch-depth: 0
|
|
19
|
+
- uses: actions/setup-python@v6
|
|
20
|
+
with:
|
|
21
|
+
python-version: "3.x"
|
|
22
|
+
- run: pip install build
|
|
23
|
+
- run: python -m build
|
|
24
|
+
- uses: actions/upload-artifact@v5
|
|
25
|
+
with:
|
|
26
|
+
name: dist
|
|
27
|
+
path: dist/
|
|
28
|
+
|
|
29
|
+
publish:
|
|
30
|
+
needs: build
|
|
31
|
+
runs-on: ubuntu-latest
|
|
32
|
+
environment:
|
|
33
|
+
name: pypi
|
|
34
|
+
url: https://pypi.org/p/sqlalchemy-mssql-bulkcopy
|
|
35
|
+
permissions:
|
|
36
|
+
id-token: write
|
|
37
|
+
attestations: write
|
|
38
|
+
contents: read
|
|
39
|
+
steps:
|
|
40
|
+
- uses: actions/download-artifact@v6
|
|
41
|
+
with:
|
|
42
|
+
name: dist
|
|
43
|
+
path: dist/
|
|
44
|
+
- uses: pypa/gh-action-pypi-publish@release/v1
|
|
45
|
+
with:
|
|
46
|
+
verbose: true
|
|
47
|
+
|
|
48
|
+
release:
|
|
49
|
+
needs: publish
|
|
50
|
+
runs-on: ubuntu-latest
|
|
51
|
+
permissions:
|
|
52
|
+
contents: write
|
|
53
|
+
steps:
|
|
54
|
+
- uses: actions/download-artifact@v6
|
|
55
|
+
with:
|
|
56
|
+
name: dist
|
|
57
|
+
path: dist/
|
|
58
|
+
- uses: softprops/action-gh-release@v2
|
|
59
|
+
with:
|
|
60
|
+
files: dist/*
|
|
61
|
+
generate_release_notes: true
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Daniel Caspi
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: sqlalchemy-mssql-bulkcopy
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Bulk copy for SQLAlchemy + mssql-python — 10-50x faster writes via SQL Server's native TDS protocol
|
|
5
|
+
Author: Daniel Caspi
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/dxdc/sqlalchemy-mssql-bulkcopy
|
|
8
|
+
Project-URL: Issues, https://github.com/dxdc/sqlalchemy-mssql-bulkcopy/issues
|
|
9
|
+
Project-URL: Changelog, https://github.com/dxdc/sqlalchemy-mssql-bulkcopy/releases
|
|
10
|
+
Keywords: sqlalchemy,mssql,sql-server,bulk-copy,pandas,mssql-python,bulkcopy,tds,etl
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Topic :: Database
|
|
20
|
+
Classifier: Typing :: Typed
|
|
21
|
+
Requires-Python: >=3.10
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
Requires-Dist: sqlalchemy>=2.1.0b1
|
|
25
|
+
Provides-Extra: dev
|
|
26
|
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
27
|
+
Requires-Dist: pytest-cov>=6.0; extra == "dev"
|
|
28
|
+
Requires-Dist: pandas>=2.0; extra == "dev"
|
|
29
|
+
Requires-Dist: mssql-python>=1.4.0; extra == "dev"
|
|
30
|
+
Requires-Dist: ruff>=0.9; extra == "dev"
|
|
31
|
+
Dynamic: license-file
|
|
32
|
+
|
|
33
|
+
# sqlalchemy-mssql-bulkcopy
|
|
34
|
+
|
|
35
|
+
[](https://github.com/dxdc/sqlalchemy-mssql-bulkcopy/actions/workflows/ci.yml)
|
|
36
|
+
[](https://pypi.org/project/sqlalchemy-mssql-bulkcopy/)
|
|
37
|
+
[](LICENSE)
|
|
38
|
+
|
|
39
|
+
Bulk copy for SQLAlchemy + mssql-python — 10–50× faster writes via
|
|
40
|
+
SQL Server's native TDS bulk load protocol.
|
|
41
|
+
|
|
42
|
+
All `cursor.bulkcopy()` kwargs pass through to the driver. When
|
|
43
|
+
mssql-python adds new options, this package supports them automatically.
|
|
44
|
+
|
|
45
|
+
Based on the [event hook approach recommended by SQLAlchemy's maintainer](https://github.com/sqlalchemy/sqlalchemy/issues/13218).
|
|
46
|
+
|
|
47
|
+
## Install
|
|
48
|
+
|
|
49
|
+
The `mssql+mssqlpython` dialect requires SQLAlchemy ≥ 2.1.0b2,
|
|
50
|
+
which is not yet on PyPI. Install SA from git until then:
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
pip install "sqlalchemy @ git+https://github.com/sqlalchemy/sqlalchemy.git@main"
|
|
54
|
+
pip install sqlalchemy-mssql-bulkcopy
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Once SA 2.1 is stable:
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
pip install sqlalchemy-mssql-bulkcopy
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Requires Python ≥ 3.10, SQLAlchemy ≥ 2.1, mssql-python ≥ 1.4.
|
|
64
|
+
|
|
65
|
+
## Usage
|
|
66
|
+
|
|
67
|
+
### Direct (recommended)
|
|
68
|
+
|
|
69
|
+
No engine configuration needed. Returns the driver result dict.
|
|
70
|
+
|
|
71
|
+
```python
|
|
72
|
+
from sqlalchemy import create_engine
|
|
73
|
+
from sqlalchemy_mssql_bulkcopy import bulkcopy
|
|
74
|
+
|
|
75
|
+
engine = create_engine("mssql+mssqlpython://user:pass@host/db")
|
|
76
|
+
|
|
77
|
+
with engine.begin() as conn:
|
|
78
|
+
result = bulkcopy(conn, df, "dbo.users", batch_size=50_000)
|
|
79
|
+
print(f"{result['rows_copied']} rows in {result['elapsed_time']:.2f}s")
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Accepts DataFrames (columns auto-detected), lists of tuples, generators,
|
|
83
|
+
or any iterable:
|
|
84
|
+
|
|
85
|
+
```python
|
|
86
|
+
# All driver options pass through as kwargs
|
|
87
|
+
result = bulkcopy(
|
|
88
|
+
conn, df, "staging.events",
|
|
89
|
+
batch_size=100_000,
|
|
90
|
+
table_lock=True,
|
|
91
|
+
keep_identity=True,
|
|
92
|
+
check_constraints=True,
|
|
93
|
+
fire_triggers=True,
|
|
94
|
+
keep_nulls=True,
|
|
95
|
+
timeout=300,
|
|
96
|
+
use_internal_transaction=True,
|
|
97
|
+
)
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Event hook (SA Core / ORM)
|
|
101
|
+
|
|
102
|
+
Hooks into SQLAlchemy's INSERT workflow. Per-statement opt-in via
|
|
103
|
+
execution options:
|
|
104
|
+
|
|
105
|
+
```python
|
|
106
|
+
from sqlalchemy_mssql_bulkcopy import register_bulkcopy
|
|
107
|
+
|
|
108
|
+
engine = create_engine(
|
|
109
|
+
"mssql+mssqlpython://user:pass@host/db",
|
|
110
|
+
use_insertmanyvalues=False, # required — see note below
|
|
111
|
+
)
|
|
112
|
+
register_bulkcopy(engine, batch_size=10_000, table_lock=True)
|
|
113
|
+
|
|
114
|
+
with engine.begin() as conn:
|
|
115
|
+
conn.execute(
|
|
116
|
+
users.insert().execution_options(use_bulkcopy=True),
|
|
117
|
+
rows,
|
|
118
|
+
)
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
Per-statement overrides use the `bulkcopy_` prefix:
|
|
122
|
+
|
|
123
|
+
```python
|
|
124
|
+
stmt = users.insert().execution_options(
|
|
125
|
+
use_bulkcopy=True,
|
|
126
|
+
bulkcopy_batch_size=100_000,
|
|
127
|
+
bulkcopy_fire_triggers=True,
|
|
128
|
+
)
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
> **Note:** `register_bulkcopy()` raises `ValueError` if
|
|
132
|
+
> `use_insertmanyvalues` is not disabled. SA 2.x defaults to multi-row
|
|
133
|
+
> `INSERT … VALUES` batching which bypasses `executemany()` entirely —
|
|
134
|
+
> the event hook never fires without this flag. This is a global engine
|
|
135
|
+
> setting: non-bulkcopy inserts will use `executemany()` instead, which
|
|
136
|
+
> is slightly slower for medium-sized batches. If that matters, use
|
|
137
|
+
> `bulkcopy()` directly instead.
|
|
138
|
+
|
|
139
|
+
### Pandas
|
|
140
|
+
|
|
141
|
+
```python
|
|
142
|
+
from sqlalchemy_mssql_bulkcopy import bulkcopy_insert_method
|
|
143
|
+
|
|
144
|
+
df.to_sql("users", engine, method=bulkcopy_insert_method,
|
|
145
|
+
if_exists="append", index=False)
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
## Confirming bulkcopy was used
|
|
149
|
+
|
|
150
|
+
```python
|
|
151
|
+
# Direct — return value
|
|
152
|
+
result = bulkcopy(conn, df, "dbo.users")
|
|
153
|
+
assert result["rows_copied"] == len(df)
|
|
154
|
+
|
|
155
|
+
# Event hook — on_complete callback
|
|
156
|
+
results = []
|
|
157
|
+
register_bulkcopy(engine, on_complete=lambda t, r: results.append(r))
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
Both approaches log at INFO level:
|
|
161
|
+
`bulkcopy: 1000 rows -> dbo.users (0.050s)`
|
|
162
|
+
|
|
163
|
+
## License
|
|
164
|
+
|
|
165
|
+
MIT
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
# sqlalchemy-mssql-bulkcopy
|
|
2
|
+
|
|
3
|
+
[](https://github.com/dxdc/sqlalchemy-mssql-bulkcopy/actions/workflows/ci.yml)
|
|
4
|
+
[](https://pypi.org/project/sqlalchemy-mssql-bulkcopy/)
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
|
|
7
|
+
Bulk copy for SQLAlchemy + mssql-python — 10–50× faster writes via
|
|
8
|
+
SQL Server's native TDS bulk load protocol.
|
|
9
|
+
|
|
10
|
+
All `cursor.bulkcopy()` kwargs pass through to the driver. When
|
|
11
|
+
mssql-python adds new options, this package supports them automatically.
|
|
12
|
+
|
|
13
|
+
Based on the [event hook approach recommended by SQLAlchemy's maintainer](https://github.com/sqlalchemy/sqlalchemy/issues/13218).
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
The `mssql+mssqlpython` dialect requires SQLAlchemy ≥ 2.1.0b2,
|
|
18
|
+
which is not yet on PyPI. Install SA from git until then:
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
pip install "sqlalchemy @ git+https://github.com/sqlalchemy/sqlalchemy.git@main"
|
|
22
|
+
pip install sqlalchemy-mssql-bulkcopy
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Once SA 2.1 is stable:
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pip install sqlalchemy-mssql-bulkcopy
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Requires Python ≥ 3.10, SQLAlchemy ≥ 2.1, mssql-python ≥ 1.4.
|
|
32
|
+
|
|
33
|
+
## Usage
|
|
34
|
+
|
|
35
|
+
### Direct (recommended)
|
|
36
|
+
|
|
37
|
+
No engine configuration needed. Returns the driver result dict.
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
from sqlalchemy import create_engine
|
|
41
|
+
from sqlalchemy_mssql_bulkcopy import bulkcopy
|
|
42
|
+
|
|
43
|
+
engine = create_engine("mssql+mssqlpython://user:pass@host/db")
|
|
44
|
+
|
|
45
|
+
with engine.begin() as conn:
|
|
46
|
+
result = bulkcopy(conn, df, "dbo.users", batch_size=50_000)
|
|
47
|
+
print(f"{result['rows_copied']} rows in {result['elapsed_time']:.2f}s")
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Accepts DataFrames (columns auto-detected), lists of tuples, generators,
|
|
51
|
+
or any iterable:
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
# All driver options pass through as kwargs
|
|
55
|
+
result = bulkcopy(
|
|
56
|
+
conn, df, "staging.events",
|
|
57
|
+
batch_size=100_000,
|
|
58
|
+
table_lock=True,
|
|
59
|
+
keep_identity=True,
|
|
60
|
+
check_constraints=True,
|
|
61
|
+
fire_triggers=True,
|
|
62
|
+
keep_nulls=True,
|
|
63
|
+
timeout=300,
|
|
64
|
+
use_internal_transaction=True,
|
|
65
|
+
)
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Event hook (SA Core / ORM)
|
|
69
|
+
|
|
70
|
+
Hooks into SQLAlchemy's INSERT workflow. Per-statement opt-in via
|
|
71
|
+
execution options:
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
from sqlalchemy_mssql_bulkcopy import register_bulkcopy
|
|
75
|
+
|
|
76
|
+
engine = create_engine(
|
|
77
|
+
"mssql+mssqlpython://user:pass@host/db",
|
|
78
|
+
use_insertmanyvalues=False, # required — see note below
|
|
79
|
+
)
|
|
80
|
+
register_bulkcopy(engine, batch_size=10_000, table_lock=True)
|
|
81
|
+
|
|
82
|
+
with engine.begin() as conn:
|
|
83
|
+
conn.execute(
|
|
84
|
+
users.insert().execution_options(use_bulkcopy=True),
|
|
85
|
+
rows,
|
|
86
|
+
)
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Per-statement overrides use the `bulkcopy_` prefix:
|
|
90
|
+
|
|
91
|
+
```python
|
|
92
|
+
stmt = users.insert().execution_options(
|
|
93
|
+
use_bulkcopy=True,
|
|
94
|
+
bulkcopy_batch_size=100_000,
|
|
95
|
+
bulkcopy_fire_triggers=True,
|
|
96
|
+
)
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
> **Note:** `register_bulkcopy()` raises `ValueError` if
|
|
100
|
+
> `use_insertmanyvalues` is not disabled. SA 2.x defaults to multi-row
|
|
101
|
+
> `INSERT … VALUES` batching which bypasses `executemany()` entirely —
|
|
102
|
+
> the event hook never fires without this flag. This is a global engine
|
|
103
|
+
> setting: non-bulkcopy inserts will use `executemany()` instead, which
|
|
104
|
+
> is slightly slower for medium-sized batches. If that matters, use
|
|
105
|
+
> `bulkcopy()` directly instead.
|
|
106
|
+
|
|
107
|
+
### Pandas
|
|
108
|
+
|
|
109
|
+
```python
|
|
110
|
+
from sqlalchemy_mssql_bulkcopy import bulkcopy_insert_method
|
|
111
|
+
|
|
112
|
+
df.to_sql("users", engine, method=bulkcopy_insert_method,
|
|
113
|
+
if_exists="append", index=False)
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## Confirming bulkcopy was used
|
|
117
|
+
|
|
118
|
+
```python
|
|
119
|
+
# Direct — return value
|
|
120
|
+
result = bulkcopy(conn, df, "dbo.users")
|
|
121
|
+
assert result["rows_copied"] == len(df)
|
|
122
|
+
|
|
123
|
+
# Event hook — on_complete callback
|
|
124
|
+
results = []
|
|
125
|
+
register_bulkcopy(engine, on_complete=lambda t, r: results.append(r))
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
Both approaches log at INFO level:
|
|
129
|
+
`bulkcopy: 1000 rows -> dbo.users (0.050s)`
|
|
130
|
+
|
|
131
|
+
## License
|
|
132
|
+
|
|
133
|
+
MIT
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=75.0", "setuptools-scm>=8.0", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "sqlalchemy-mssql-bulkcopy"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = "Bulk copy for SQLAlchemy + mssql-python — 10-50x faster writes via SQL Server's native TDS protocol"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = { text = "MIT" }
|
|
11
|
+
requires-python = ">=3.10"
|
|
12
|
+
authors = [{ name = "Daniel Caspi" }]
|
|
13
|
+
keywords = [
|
|
14
|
+
"sqlalchemy", "mssql", "sql-server", "bulk-copy", "pandas",
|
|
15
|
+
"mssql-python", "bulkcopy", "tds", "etl",
|
|
16
|
+
]
|
|
17
|
+
classifiers = [
|
|
18
|
+
"Development Status :: 4 - Beta",
|
|
19
|
+
"Intended Audience :: Developers",
|
|
20
|
+
"License :: OSI Approved :: MIT License",
|
|
21
|
+
"Programming Language :: Python :: 3",
|
|
22
|
+
"Programming Language :: Python :: 3.10",
|
|
23
|
+
"Programming Language :: Python :: 3.11",
|
|
24
|
+
"Programming Language :: Python :: 3.12",
|
|
25
|
+
"Programming Language :: Python :: 3.13",
|
|
26
|
+
"Topic :: Database",
|
|
27
|
+
"Typing :: Typed",
|
|
28
|
+
]
|
|
29
|
+
dependencies = ["sqlalchemy>=2.1.0b1"]
|
|
30
|
+
|
|
31
|
+
[project.optional-dependencies]
|
|
32
|
+
dev = [
|
|
33
|
+
"pytest>=8.0",
|
|
34
|
+
"pytest-cov>=6.0",
|
|
35
|
+
"pandas>=2.0",
|
|
36
|
+
"mssql-python>=1.4.0",
|
|
37
|
+
"ruff>=0.9",
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
[project.urls]
|
|
41
|
+
Homepage = "https://github.com/dxdc/sqlalchemy-mssql-bulkcopy"
|
|
42
|
+
Issues = "https://github.com/dxdc/sqlalchemy-mssql-bulkcopy/issues"
|
|
43
|
+
Changelog = "https://github.com/dxdc/sqlalchemy-mssql-bulkcopy/releases"
|
|
44
|
+
|
|
45
|
+
[tool.setuptools.packages.find]
|
|
46
|
+
where = ["src"]
|
|
47
|
+
|
|
48
|
+
[tool.ruff]
|
|
49
|
+
target-version = "py310"
|
|
50
|
+
line-length = 99
|
|
51
|
+
src = ["src", "tests"]
|
|
52
|
+
exclude = ["_version.py"]
|
|
53
|
+
|
|
54
|
+
[tool.ruff.lint]
|
|
55
|
+
select = ["E", "W", "F", "I", "UP", "B", "SIM", "RUF"]
|
|
56
|
+
|
|
57
|
+
[tool.pytest.ini_options]
|
|
58
|
+
testpaths = ["tests"]
|
|
59
|
+
addopts = "-v --tb=short"
|
|
60
|
+
|
|
61
|
+
[tool.coverage.run]
|
|
62
|
+
source = ["sqlalchemy_mssql_bulkcopy"]
|
|
63
|
+
branch = true
|
|
64
|
+
|
|
65
|
+
[tool.coverage.report]
|
|
66
|
+
show_missing = true
|
|
67
|
+
|
|
68
|
+
[tool.setuptools_scm]
|
|
69
|
+
version_file = "src/sqlalchemy_mssql_bulkcopy/_version.py"
|