Metadata-Version: 2.4
Name: sqliac-mssql
Version: 0.1.1
Summary: Microsoft SQL Server adapter for SQLIaC — a Terraform alternative for SQL databases without a state file.
Requires-Python: >=3.12
Description-Content-Type: text/markdown
Requires-Dist: sqliac>=0.2.1
Requires-Dist: mssql-python>=1.10

# sqliac-mssql

Microsoft SQL Server adapter for [SQLIaC](https://pypi.org/project/sqliac/) — a Terraform alternative for SQL databases that uses no state file.

This package provides the MSSQL database connector and credential handling so SQLIaC can query SQL Server's live state and execute DDL to reconcile your declared resource definitions.

## Installation

```bash
pip install sqliac-mssql
```

This also installs `sqliac` (the CLI and core engine) and `mssql-python` (Microsoft's official SQL Server driver for Python).

## Quick Start

### Credentials

SQLIaC looks for MSSQL credentials in two places, in order of precedence:

**1. Environment variables** (recommended)

```bash
export MSSQL_HOST="localhost"
export MSSQL_USER="my_user"
export MSSQL_PASSWORD="my_password"
export MSSQL_DATABASE="my_db"
# Optional:
export MSSQL_PORT="1433"
export MSSQL_ENCRYPT="no"
export MSSQL_TRUST_SERVER_CERTIFICATE="yes"
```

`mssql-python` defaults to `Encrypt=yes` (mandatory TLS). For on-prem servers
without a trusted certificate, set `MSSQL_ENCRYPT=no` or
`MSSQL_TRUST_SERVER_CERTIFICATE=yes`, or connections will fail at handshake.

**2. Configuration file** `.sqliac/provider/credentials.toml`

```toml
[mssql]
host = "localhost"
user = "my_user"
password = "my_password"
database = "my_db"
```

Environment variables take precedence. Run `sqliac init` to scaffold this file.

Authentication is SQL login (user/password). Named instances can be given as `host = "server\\INSTANCE"` or via `MSSQL_PORT`.

### Using SQLIaC with MSSQL

```bash
# Scaffold a project
sqliac init

# Configure your credentials and edit definitions/
# then preview and apply:

sqliac apply --dry-run
sqliac apply
```

### Provider config

The adapter is registered via the `sqliac.adapters` entry point group. Your `.sqliac/provider/config.toml` should start with a top-level `[mssql]` section:

```toml
[mssql.schema.ddl_command]
create = "CREATE"
alter  = "ALTER"
drop   = "DROP"

[mssql.schema.ddl_context]
object_name = "analytics"
owner = "analytics_owner"
wait_time = 0

[mssql.schema.ddl_context.depends_on]
```

## API

The adapter exposes three classes, all available from the top-level package:

```python
from sqliac_mssql import MssqlAdapter, MssqlCredentials, MssqlConnectionManager
```

| Class | Role |
|-------|------|
| `MssqlAdapter` | Concrete `BaseAdapter` — executes queries and manages connections |
| `MssqlCredentials` | Frozen dataclass — validates and stores `host`, `user`, `password`, `database`, etc. |
| `MssqlConnectionManager` | Thread-safe per-thread connection pool (autocommit, so DDL runs outside transaction blocks) |

All three are registered via the entry point `sqliac.adapters / mssql` and resolved automatically by the SQLIaC CLI. You don't need to import them directly unless building custom tooling.

### Supported credential fields

| Field | Required | Env var |
|-------|----------|---------|
| `host` | Yes | `MSSQL_HOST` |
| `user` | Yes | `MSSQL_USER` |
| `password` | Yes | `MSSQL_PASSWORD` |
| `database` | Yes | `MSSQL_DATABASE` |
| `port` | No (default 1433) | `MSSQL_PORT` |
| `encrypt` | No (driver default `yes`) | `MSSQL_ENCRYPT` |
| `trust_server_certificate` | No | `MSSQL_TRUST_SERVER_CERTIFICATE` |

## License

MIT — see the [sqliac](https://pypi.org/project/sqliac/) package for details.
