datadouble-mssql 0.0.3__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.
- datadouble_mssql-0.0.3/.gitignore +21 -0
- datadouble_mssql-0.0.3/LICENSE +21 -0
- datadouble_mssql-0.0.3/PKG-INFO +77 -0
- datadouble_mssql-0.0.3/README.md +56 -0
- datadouble_mssql-0.0.3/pyproject.toml +38 -0
- datadouble_mssql-0.0.3/src/datadouble_mssql/__init__.py +230 -0
- datadouble_mssql-0.0.3/src/datadouble_mssql/errors.py +78 -0
- datadouble_mssql-0.0.3/src/datadouble_mssql/introspect.py +267 -0
- datadouble_mssql-0.0.3/src/datadouble_mssql/objects.py +257 -0
- datadouble_mssql-0.0.3/src/datadouble_mssql/py.typed +0 -0
- datadouble_mssql-0.0.3/src/datadouble_mssql/render.py +256 -0
- datadouble_mssql-0.0.3/src/datadouble_mssql/script.py +514 -0
- datadouble_mssql-0.0.3/src/datadouble_mssql/server.py +286 -0
- datadouble_mssql-0.0.3/src/datadouble_mssql/source.py +294 -0
- datadouble_mssql-0.0.3/src/datadouble_mssql/working.py +854 -0
- datadouble_mssql-0.0.3/tests/test_mssql.py +491 -0
- datadouble_mssql-0.0.3/tests/test_mssql_conformance.py +230 -0
- datadouble_mssql-0.0.3/tests/test_mssql_units.py +499 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Copyright (c) 2026- Tight Line LLC
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
__pycache__/
|
|
5
|
+
*.py[cod]
|
|
6
|
+
.venv/
|
|
7
|
+
.env
|
|
8
|
+
.coverage
|
|
9
|
+
.coverage.*
|
|
10
|
+
coverage.xml
|
|
11
|
+
.reports/
|
|
12
|
+
.pytest_cache/
|
|
13
|
+
.mypy_cache/
|
|
14
|
+
.ruff_cache/
|
|
15
|
+
dist/
|
|
16
|
+
site/
|
|
17
|
+
.cache/
|
|
18
|
+
|
|
19
|
+
# The default work directory of `datadouble run`.
|
|
20
|
+
.datadouble/
|
|
21
|
+
node_modules/
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026- Tight Line LLC
|
|
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,77 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: datadouble-mssql
|
|
3
|
+
Version: 0.0.3
|
|
4
|
+
Summary: SQL Server engine adapter for Datadouble
|
|
5
|
+
Project-URL: Homepage, https://datadouble.tightlinesoftware.com/
|
|
6
|
+
Project-URL: Source, https://github.com/datadouble/datadouble
|
|
7
|
+
Author: Tight Line LLC
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 2 - Pre-Alpha
|
|
11
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
15
|
+
Classifier: Topic :: Database
|
|
16
|
+
Classifier: Typing :: Typed
|
|
17
|
+
Requires-Python: >=3.13
|
|
18
|
+
Requires-Dist: datadouble
|
|
19
|
+
Requires-Dist: pymssql>=2.3.4
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
|
|
22
|
+
# datadouble-mssql
|
|
23
|
+
|
|
24
|
+
The SQL Server engine adapter for [Datadouble](https://datadouble.tightlinesoftware.com/).
|
|
25
|
+
|
|
26
|
+
It reads one consistent snapshot of a database (snapshot isolation), scripts
|
|
27
|
+
the schema from the catalog views, writes the chosen rows as bulk-load files,
|
|
28
|
+
builds working databases as throwaway SQL Server instances, and packages golden
|
|
29
|
+
images as the database's data and log files (`physical`) or its schema and rows
|
|
30
|
+
(`logical`). Instances run in Microsoft's SQL Server image of the source's
|
|
31
|
+
version, through the spec's container runtime.
|
|
32
|
+
|
|
33
|
+
```yaml
|
|
34
|
+
sources:
|
|
35
|
+
app:
|
|
36
|
+
engine: mssql
|
|
37
|
+
connection:
|
|
38
|
+
url: # mssql://user:password@host:1433/database, by reference
|
|
39
|
+
env: SHOP_MSSQL_URL
|
|
40
|
+
accept_eula: true # you accept the SQL Server licence for working databases
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Objects
|
|
44
|
+
|
|
45
|
+
The catalog lists views, procedures, functions, DML triggers and sequences
|
|
46
|
+
from `sys.objects`, and user-defined alias and table types from
|
|
47
|
+
`sys.types`. Dependencies come from `sys.sql_expression_dependencies`, which
|
|
48
|
+
SQL Server keeps for every module, and are exact. A name SQL Server could
|
|
49
|
+
not bind when the module was created (deferred name resolution) is matched
|
|
50
|
+
to a table or object of that schema and name, and that edge is marked
|
|
51
|
+
inferred; an exact edge to the same target wins. Three-part names in the
|
|
52
|
+
source's own database count.
|
|
53
|
+
|
|
54
|
+
Tables list what they need too: the functions and sequences their default
|
|
55
|
+
and check constraints and computed columns use, from the same view, and the
|
|
56
|
+
alias and table types of their columns. Package planning follows these
|
|
57
|
+
edges to work out what an image needs.
|
|
58
|
+
|
|
59
|
+
A standalone group's database holds exactly its closure: the scripted
|
|
60
|
+
schema runs as for any group, with the pulled-in tables, the rows of the
|
|
61
|
+
group's own extract, and only the closure's sequences, modules and triggers,
|
|
62
|
+
in the same dependency order. Foreign keys into a pulled-in table without
|
|
63
|
+
rows are left out.
|
|
64
|
+
|
|
65
|
+
Reading the catalog needs SQL Server 2017 or later, and the `VIEW
|
|
66
|
+
DEFINITION` permission on the database (or on each schema): SQL Server's
|
|
67
|
+
metadata visibility shows a user only the objects it has some permission
|
|
68
|
+
on, and module text only with `VIEW DEFINITION`. Without it, an object
|
|
69
|
+
looks empty (its definition reads as nothing) or missing (strict mode
|
|
70
|
+
reports it removed), so grant it before locking.
|
|
71
|
+
|
|
72
|
+
## Requirements
|
|
73
|
+
|
|
74
|
+
The adapter reports none. The add-ons a SQL Server database could need in its
|
|
75
|
+
copies are CLR assemblies and Full-Text Search, which Microsoft's Linux image
|
|
76
|
+
leaves out, and the adapter refuses the tables that use them (columns of a CLR
|
|
77
|
+
type, full-text indexes) as not supported yet. Exclude those tables to build.
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# datadouble-mssql
|
|
2
|
+
|
|
3
|
+
The SQL Server engine adapter for [Datadouble](https://datadouble.tightlinesoftware.com/).
|
|
4
|
+
|
|
5
|
+
It reads one consistent snapshot of a database (snapshot isolation), scripts
|
|
6
|
+
the schema from the catalog views, writes the chosen rows as bulk-load files,
|
|
7
|
+
builds working databases as throwaway SQL Server instances, and packages golden
|
|
8
|
+
images as the database's data and log files (`physical`) or its schema and rows
|
|
9
|
+
(`logical`). Instances run in Microsoft's SQL Server image of the source's
|
|
10
|
+
version, through the spec's container runtime.
|
|
11
|
+
|
|
12
|
+
```yaml
|
|
13
|
+
sources:
|
|
14
|
+
app:
|
|
15
|
+
engine: mssql
|
|
16
|
+
connection:
|
|
17
|
+
url: # mssql://user:password@host:1433/database, by reference
|
|
18
|
+
env: SHOP_MSSQL_URL
|
|
19
|
+
accept_eula: true # you accept the SQL Server licence for working databases
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Objects
|
|
23
|
+
|
|
24
|
+
The catalog lists views, procedures, functions, DML triggers and sequences
|
|
25
|
+
from `sys.objects`, and user-defined alias and table types from
|
|
26
|
+
`sys.types`. Dependencies come from `sys.sql_expression_dependencies`, which
|
|
27
|
+
SQL Server keeps for every module, and are exact. A name SQL Server could
|
|
28
|
+
not bind when the module was created (deferred name resolution) is matched
|
|
29
|
+
to a table or object of that schema and name, and that edge is marked
|
|
30
|
+
inferred; an exact edge to the same target wins. Three-part names in the
|
|
31
|
+
source's own database count.
|
|
32
|
+
|
|
33
|
+
Tables list what they need too: the functions and sequences their default
|
|
34
|
+
and check constraints and computed columns use, from the same view, and the
|
|
35
|
+
alias and table types of their columns. Package planning follows these
|
|
36
|
+
edges to work out what an image needs.
|
|
37
|
+
|
|
38
|
+
A standalone group's database holds exactly its closure: the scripted
|
|
39
|
+
schema runs as for any group, with the pulled-in tables, the rows of the
|
|
40
|
+
group's own extract, and only the closure's sequences, modules and triggers,
|
|
41
|
+
in the same dependency order. Foreign keys into a pulled-in table without
|
|
42
|
+
rows are left out.
|
|
43
|
+
|
|
44
|
+
Reading the catalog needs SQL Server 2017 or later, and the `VIEW
|
|
45
|
+
DEFINITION` permission on the database (or on each schema): SQL Server's
|
|
46
|
+
metadata visibility shows a user only the objects it has some permission
|
|
47
|
+
on, and module text only with `VIEW DEFINITION`. Without it, an object
|
|
48
|
+
looks empty (its definition reads as nothing) or missing (strict mode
|
|
49
|
+
reports it removed), so grant it before locking.
|
|
50
|
+
|
|
51
|
+
## Requirements
|
|
52
|
+
|
|
53
|
+
The adapter reports none. The add-ons a SQL Server database could need in its
|
|
54
|
+
copies are CLR assemblies and Full-Text Search, which Microsoft's Linux image
|
|
55
|
+
leaves out, and the adapter refuses the tables that use them (columns of a CLR
|
|
56
|
+
type, full-text indexes) as not supported yet. Exclude those tables to build.
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# Copyright (c) 2026- Tight Line LLC
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
[project]
|
|
5
|
+
name = "datadouble-mssql"
|
|
6
|
+
# All packages share one version until 1.0 (decision 38); scripts/make_tag.py
|
|
7
|
+
# sets it at release time.
|
|
8
|
+
version = "0.0.3"
|
|
9
|
+
description = "SQL Server engine adapter for Datadouble"
|
|
10
|
+
readme = "README.md"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
authors = [{ name = "Tight Line LLC" }]
|
|
14
|
+
requires-python = ">=3.13"
|
|
15
|
+
dependencies = ["datadouble", "pymssql>=2.3.4"]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Development Status :: 2 - Pre-Alpha",
|
|
18
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
19
|
+
"Programming Language :: Python :: 3.13",
|
|
20
|
+
"Programming Language :: Python :: 3.14",
|
|
21
|
+
"Programming Language :: Python :: 3.15",
|
|
22
|
+
"Topic :: Database",
|
|
23
|
+
"Typing :: Typed",
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
[project.urls]
|
|
27
|
+
Homepage = "https://datadouble.tightlinesoftware.com/"
|
|
28
|
+
Source = "https://github.com/datadouble/datadouble"
|
|
29
|
+
|
|
30
|
+
[project.entry-points."datadouble.engines"]
|
|
31
|
+
mssql = "datadouble_mssql:engine"
|
|
32
|
+
|
|
33
|
+
[build-system]
|
|
34
|
+
requires = ["hatchling>=1.33.0"]
|
|
35
|
+
build-backend = "hatchling.build"
|
|
36
|
+
|
|
37
|
+
[tool.hatch.build.targets.wheel]
|
|
38
|
+
packages = ["src/datadouble_mssql"]
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
# Copyright (c) 2026- Tight Line LLC
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
"""The SQL Server engine adapter for Datadouble.
|
|
5
|
+
|
|
6
|
+
Spec settings::
|
|
7
|
+
|
|
8
|
+
sources:
|
|
9
|
+
app:
|
|
10
|
+
engine: mssql
|
|
11
|
+
connection:
|
|
12
|
+
url: {env: SHOP_MSSQL_URL} # mssql://user:password@host:1433/database, by reference
|
|
13
|
+
server:
|
|
14
|
+
accept_eula: true # required: you accept the SQL Server licence
|
|
15
|
+
edition: Developer # the default; any value MSSQL_PID takes
|
|
16
|
+
|
|
17
|
+
The catalog is the URL's database, which must allow snapshot isolation.
|
|
18
|
+
Extract runs no programs in containers. Working databases and restores run in
|
|
19
|
+
Microsoft's SQL Server image of the source's release and cumulative update
|
|
20
|
+
(``mcr.microsoft.com/mssql/server:2022-CU28-ubuntu-22.04``), or in
|
|
21
|
+
``sources.<name>.image``, through the spec's container runtime, as uid and gid 10001,
|
|
22
|
+
which Datadouble must run as too. They run SQL Server under its licence terms,
|
|
23
|
+
which the operator accepts with ``sources.<name>.server.accept_eula``; Developer
|
|
24
|
+
edition is licensed for development and testing, not production.
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
import shlex
|
|
28
|
+
from collections.abc import Generator, Mapping
|
|
29
|
+
from contextlib import contextmanager
|
|
30
|
+
from pathlib import Path
|
|
31
|
+
from typing import Literal
|
|
32
|
+
from urllib.parse import unquote, urlsplit
|
|
33
|
+
|
|
34
|
+
import pymssql
|
|
35
|
+
from pydantic import BaseModel, ConfigDict, JsonValue
|
|
36
|
+
|
|
37
|
+
from datadouble.engine import EngineError, ServerPlan, SourceSession, Tooling, WorkingDatabases
|
|
38
|
+
from datadouble.names import EngineName
|
|
39
|
+
from datadouble.planner import Capabilities
|
|
40
|
+
from datadouble.secrets import resolve_secret
|
|
41
|
+
from datadouble.settings import parse_settings
|
|
42
|
+
from datadouble.spec import ImageFormat, SecretRef
|
|
43
|
+
from datadouble_mssql.errors import database_error
|
|
44
|
+
from datadouble_mssql.introspect import ENGINE
|
|
45
|
+
from datadouble_mssql.render import compose, quote
|
|
46
|
+
from datadouble_mssql.server import (
|
|
47
|
+
DATA,
|
|
48
|
+
Tools,
|
|
49
|
+
configure,
|
|
50
|
+
default_image,
|
|
51
|
+
literal,
|
|
52
|
+
)
|
|
53
|
+
from datadouble_mssql.source import MssqlSession
|
|
54
|
+
from datadouble_mssql.working import DatabaseInfo, MssqlWorking
|
|
55
|
+
|
|
56
|
+
MOUNT = "/var/lib/datadouble"
|
|
57
|
+
"""Where container servers mount a copy."""
|
|
58
|
+
|
|
59
|
+
# A new instance beside the copy's files, with a random sa password; the
|
|
60
|
+
# database is attached and the login datadouble made with the copy's password,
|
|
61
|
+
# which reaches the server through a builtin and a pipe, never a command line.
|
|
62
|
+
_SERVE = """
|
|
63
|
+
set -eu
|
|
64
|
+
instance=@INSTANCE@
|
|
65
|
+
rm -rf "$instance" && mkdir -p "$instance"
|
|
66
|
+
export ACCEPT_EULA=Y MSSQL_PID=@EDITION@ MSSQL_DATA_DIR=@DATA@ MSSQL_LOG_DIR=@DATA@
|
|
67
|
+
export MSSQL_MASTER_DATA_FILE="$instance/master.mdf" MSSQL_MASTER_LOG_FILE="$instance/mastlog.ldf"
|
|
68
|
+
export MSSQL_DUMP_DIR="$instance"
|
|
69
|
+
MSSQL_SA_PASSWORD="Dd-$(od -An -N16 -tx1 /dev/urandom | tr -d ' \\n')"
|
|
70
|
+
export MSSQL_SA_PASSWORD SQLCMDPASSWORD="$MSSQL_SA_PASSWORD"
|
|
71
|
+
sqlcmd() {
|
|
72
|
+
if [ -x /opt/mssql-tools18/bin/sqlcmd ]; then /opt/mssql-tools18/bin/sqlcmd -C "$@"
|
|
73
|
+
else /opt/mssql-tools/bin/sqlcmd "$@"; fi
|
|
74
|
+
}
|
|
75
|
+
/opt/mssql/bin/sqlservr & server=$!
|
|
76
|
+
trap 'kill -TERM "$server"; wait "$server"' TERM INT
|
|
77
|
+
until grep -q 'Recovery is complete' /var/opt/mssql/log/errorlog 2> /dev/null \\
|
|
78
|
+
&& sqlcmd -S localhost -U sa -b -Q "SELECT 1" > /dev/null 2>&1; do sleep 1; done
|
|
79
|
+
password=$(printf %s "$DATADOUBLE_PASSWORD" | sed "s/'/''/g")
|
|
80
|
+
printf '%s\\n' "@ATTACH@" | sqlcmd -S localhost -U sa -b -I > /dev/null
|
|
81
|
+
touch "$instance/ready"
|
|
82
|
+
wait "$server"
|
|
83
|
+
"""
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
class MssqlConnection(BaseModel):
|
|
87
|
+
"""The ``sources.<name>.connection`` settings."""
|
|
88
|
+
|
|
89
|
+
model_config = ConfigDict(strict=True, frozen=True, extra="forbid")
|
|
90
|
+
|
|
91
|
+
url: SecretRef
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
class MssqlServerSettings(BaseModel):
|
|
95
|
+
"""The ``sources.<name>.server`` settings."""
|
|
96
|
+
|
|
97
|
+
model_config = ConfigDict(strict=True, frozen=True, extra="forbid")
|
|
98
|
+
|
|
99
|
+
accept_eula: Literal[True] | None = None
|
|
100
|
+
"""Set by the operator to accept the SQL Server licence; required."""
|
|
101
|
+
edition: str = "Developer"
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def double_quoted(text: str) -> str:
|
|
105
|
+
"""``text`` escaped for a double-quoted shell string, so nothing in it expands."""
|
|
106
|
+
for special in ("\\", '"', "$", "`"):
|
|
107
|
+
text = text.replace(special, "\\" + special)
|
|
108
|
+
return text
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def parse_url(url: str) -> tuple[dict[str, str], str]:
|
|
112
|
+
"""Connection settings (server, port, user, password) and the database, from a URL."""
|
|
113
|
+
parts = urlsplit(url)
|
|
114
|
+
database = unquote(parts.path.lstrip("/"))
|
|
115
|
+
if parts.scheme != "mssql" or not parts.hostname or not database:
|
|
116
|
+
message = "the mssql url must look like mssql://user:password@host:1433/database"
|
|
117
|
+
raise EngineError(message)
|
|
118
|
+
return (
|
|
119
|
+
{
|
|
120
|
+
"server": parts.hostname,
|
|
121
|
+
"port": str(parts.port or 1433),
|
|
122
|
+
"user": unquote(parts.username or ""),
|
|
123
|
+
"password": unquote(parts.password or ""),
|
|
124
|
+
},
|
|
125
|
+
database,
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def server_settings(server: Mapping[str, JsonValue]) -> MssqlServerSettings:
|
|
130
|
+
"""The ``sources.<name>.server`` settings, which must accept the licence."""
|
|
131
|
+
settings = parse_settings(MssqlServerSettings, server, "mssql server")
|
|
132
|
+
if settings.accept_eula is None:
|
|
133
|
+
message = (
|
|
134
|
+
"SQL Server working databases run Microsoft's SQL Server image, whose licence the"
|
|
135
|
+
" operator must accept: set sources.<name>.server.accept_eula to true"
|
|
136
|
+
" (Developer edition, the default, is licensed for development and testing)"
|
|
137
|
+
)
|
|
138
|
+
raise EngineError(message)
|
|
139
|
+
return settings
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
class MssqlEngine:
|
|
143
|
+
"""Implements :class:`datadouble.engine.EngineAdapter` for SQL Server."""
|
|
144
|
+
|
|
145
|
+
@property
|
|
146
|
+
def name(self) -> EngineName:
|
|
147
|
+
"""``mssql``."""
|
|
148
|
+
return ENGINE
|
|
149
|
+
|
|
150
|
+
@property
|
|
151
|
+
def owner(self) -> str:
|
|
152
|
+
"""``10001:10001``: SQL Server in Microsoft's image runs as no other user."""
|
|
153
|
+
return "10001:10001"
|
|
154
|
+
|
|
155
|
+
def capabilities(self) -> Capabilities:
|
|
156
|
+
"""Both image formats, SQL expressions, the nested strategy and hooks."""
|
|
157
|
+
return Capabilities(
|
|
158
|
+
formats=(ImageFormat.PHYSICAL, ImageFormat.LOGICAL),
|
|
159
|
+
expressions=True,
|
|
160
|
+
nested=True,
|
|
161
|
+
standalone=True,
|
|
162
|
+
)
|
|
163
|
+
|
|
164
|
+
@contextmanager
|
|
165
|
+
def connect(self, connection: Mapping[str, JsonValue]) -> Generator[SourceSession]:
|
|
166
|
+
"""Connect to the source."""
|
|
167
|
+
settings = parse_settings(MssqlConnection, connection, "mssql connection")
|
|
168
|
+
options, database = parse_url(resolve_secret(settings.url))
|
|
169
|
+
try:
|
|
170
|
+
opened = configure(
|
|
171
|
+
pymssql.connect(
|
|
172
|
+
server=options["server"],
|
|
173
|
+
port=options["port"],
|
|
174
|
+
user=options["user"],
|
|
175
|
+
password=options["password"],
|
|
176
|
+
database=database,
|
|
177
|
+
autocommit=True,
|
|
178
|
+
)
|
|
179
|
+
)
|
|
180
|
+
except pymssql.Error as error:
|
|
181
|
+
raise database_error(error, "connecting to the source") from None
|
|
182
|
+
try:
|
|
183
|
+
yield MssqlSession(opened, options, database)
|
|
184
|
+
finally:
|
|
185
|
+
opened.close()
|
|
186
|
+
|
|
187
|
+
def working(self, scratch: Path, tooling: Tooling, source_version: str) -> WorkingDatabases:
|
|
188
|
+
"""Instances under ``scratch``, in the image for the source's version."""
|
|
189
|
+
settings = server_settings(tooling.server)
|
|
190
|
+
image = tooling.image or default_image(source_version)
|
|
191
|
+
return MssqlWorking(Tools(tooling.runtime, image, settings.edition), scratch)
|
|
192
|
+
|
|
193
|
+
def server(self, image: Path, server_image: str | None) -> ServerPlan:
|
|
194
|
+
"""Microsoft's image of the image's version, attaching the copy's files.
|
|
195
|
+
|
|
196
|
+
At start a new instance is made in a directory beside the files, with a
|
|
197
|
+
random ``sa`` password; the database is attached, and the login
|
|
198
|
+
``datadouble`` is created with the copy's password.
|
|
199
|
+
"""
|
|
200
|
+
info = DatabaseInfo.model_validate_json((image / "database.json").read_bytes())
|
|
201
|
+
data = f"{MOUNT}/{DATA}"
|
|
202
|
+
attach = compose(
|
|
203
|
+
"CREATE DATABASE {} ON (FILENAME = {}), (FILENAME = {}) FOR ATTACH;"
|
|
204
|
+
" CREATE LOGIN [datadouble] WITH PASSWORD = N'@PASSWORD@', CHECK_POLICY = OFF;"
|
|
205
|
+
" ALTER SERVER ROLE sysadmin ADD MEMBER [datadouble];",
|
|
206
|
+
quote(info.database),
|
|
207
|
+
literal(f"{data}/database.mdf"),
|
|
208
|
+
literal(f"{data}/database_log.ldf"),
|
|
209
|
+
)
|
|
210
|
+
start = (
|
|
211
|
+
_SERVE.replace("@INSTANCE@", f"{MOUNT}/instance")
|
|
212
|
+
.replace("@DATA@", data)
|
|
213
|
+
.replace("@EDITION@", shlex.quote(info.edition))
|
|
214
|
+
.replace("@ATTACH@", double_quoted(attach).replace("@PASSWORD@", "$password"))
|
|
215
|
+
)
|
|
216
|
+
return ServerPlan(
|
|
217
|
+
image=server_image or default_image(info.server_version),
|
|
218
|
+
mount=MOUNT,
|
|
219
|
+
owner=self.owner,
|
|
220
|
+
command=("bash", "-c", start),
|
|
221
|
+
ready=("test", "-f", f"{MOUNT}/instance/ready"),
|
|
222
|
+
port=1433,
|
|
223
|
+
database=info.database,
|
|
224
|
+
user="datadouble",
|
|
225
|
+
)
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
def engine() -> MssqlEngine:
|
|
229
|
+
"""The entry point named in ``datadouble.engines``."""
|
|
230
|
+
return MssqlEngine()
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# Copyright (c) 2026- Tight Line LLC
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
"""Error messages that are safe to show and log.
|
|
5
|
+
|
|
6
|
+
SQL Server messages can quote the data that caused them (``The duplicate key
|
|
7
|
+
value is (x)``, ``converting the varchar value 'x'``). Messages leaving this
|
|
8
|
+
adapter keep the error number and the message with data values hidden;
|
|
9
|
+
quoted object names and identifiers in brackets are kept (decisions 54 and
|
|
10
|
+
139). The driver's own trailer is dropped.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
import pymssql
|
|
14
|
+
|
|
15
|
+
from datadouble.engine import EngineError
|
|
16
|
+
from datadouble.redaction import Redactor, kinds
|
|
17
|
+
from datadouble.runtime import runtime_failure
|
|
18
|
+
|
|
19
|
+
_TRAILER = "DB-Lib error message"
|
|
20
|
+
|
|
21
|
+
# The shapes in which SQL Server reports data values; the ``value`` group is
|
|
22
|
+
# hidden. Message texts are from sys.messages, named by number; a new one is
|
|
23
|
+
# one more line. Any other quoted string that no kind word names is hidden
|
|
24
|
+
# anyway. Truncation without a value (8152), NULL into a column (515) and
|
|
25
|
+
# conflicts with a constraint (547) name only objects.
|
|
26
|
+
_VALUES = (
|
|
27
|
+
r"duplicate key value is \((?P<value>.*)\)", # 2627, 2601
|
|
28
|
+
r"when converting the [\w ]+? value '(?P<value>.*)' to data type", # 245
|
|
29
|
+
r"conversion of the [\w ]+? value '(?P<value>.*)' overflowed", # 248
|
|
30
|
+
r"Truncated value: '(?P<value>.*)'", # 2628
|
|
31
|
+
r"character string '(?P<value>.*)'", # 105, 102: the statement, literals and all
|
|
32
|
+
)
|
|
33
|
+
# Words after which a quoted string names an object: ``column 'email'``,
|
|
34
|
+
# ``Invalid object name 'dbo.orders'``.
|
|
35
|
+
_NAMES = (
|
|
36
|
+
r"\b(?:object|column)\s+name\s+",
|
|
37
|
+
kinds(
|
|
38
|
+
"table column constraint index object database schema login user role"
|
|
39
|
+
" procedure function trigger view type statistics file partition"
|
|
40
|
+
),
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
# A message a user raised (RAISERROR or THROW, numbers from 50000) can quote
|
|
44
|
+
# any value, so every quoted string in it is hidden.
|
|
45
|
+
_USER_ERRORS = 50000
|
|
46
|
+
_RAISED = (r"\bMsg (?:[5-9]\d{4}|\d{6,})\b",)
|
|
47
|
+
|
|
48
|
+
redact = Redactor(values=_VALUES, names=_NAMES, marks="'\"", raised=_RAISED)
|
|
49
|
+
"""Hide the data values in a SQL Server message, keeping the objects it names."""
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def database_error(error: pymssql.Error, action: str) -> EngineError:
|
|
53
|
+
"""An :class:`EngineError` for a failed database operation."""
|
|
54
|
+
arguments: tuple[object, ...] = error.args
|
|
55
|
+
if len(arguments) > 1 and isinstance(arguments[0], int):
|
|
56
|
+
raw = arguments[1]
|
|
57
|
+
detail = raw.decode("utf-8", "replace") if isinstance(raw, bytes) else str(raw)
|
|
58
|
+
detail = detail.split(_TRAILER, 1)[0].strip()
|
|
59
|
+
raised = arguments[0] >= _USER_ERRORS
|
|
60
|
+
return EngineError(f"{action} failed: [{arguments[0]}] {redact(detail, raised=raised)}")
|
|
61
|
+
return EngineError(f"{action} failed: {type(error).__name__}")
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def tool_error(tool: str, status: int, output: str) -> EngineError:
|
|
65
|
+
"""An :class:`EngineError` for a failed SQL Server program in a container.
|
|
66
|
+
|
|
67
|
+
Its output is redacted, unless the status says the container runtime
|
|
68
|
+
failed before the program ran (:func:`~datadouble.runtime.runtime_failure`).
|
|
69
|
+
"""
|
|
70
|
+
keep = runtime_failure(status)
|
|
71
|
+
raised = redact.raises(output)
|
|
72
|
+
lines = [
|
|
73
|
+
line.strip() if keep else redact(line.strip(), raised=raised)
|
|
74
|
+
for line in output.splitlines()
|
|
75
|
+
if line.strip()
|
|
76
|
+
]
|
|
77
|
+
summary = "; ".join(lines[-3:]) or "no output"
|
|
78
|
+
return EngineError(f"{tool} exited with status {status}: {summary}")
|