# MSSQL Reader and Writer Testing Guide

## Prerequisites

- Docker and Docker Compose installed
- RelSpec binary built (`make build`)
- jq (optional, for JSON processing)

## Quick Start

### 1. Start SQL Server Express

```bash
docker-compose up -d mssql

# Wait for container to be healthy
docker-compose ps

# Monitor startup logs
docker-compose logs -f mssql
```

### 2. Verify Database Creation

```bash
docker exec -it $(docker-compose ps -q mssql) \
  /opt/mssql-tools/bin/sqlcmd \
  -S localhost \
  -U sa \
  -P 'StrongPassword123!' \
  -Q "SELECT name FROM sys.databases WHERE name = 'RelSpecTest'"
```

## Testing Scenarios

### Scenario 1: Read MSSQL Database to JSON

Read the test schema from MSSQL and export to JSON:

```bash
./build/relspec convert \
  --from mssql \
  --from-conn "sqlserver://sa:StrongPassword123!@localhost:1433/RelSpecTest" \
  --to json \
  --to-path test_output.json
```

Verify output:
```bash
jq '.Schemas[0].Tables | length' test_output.json
jq '.Schemas[0].Tables[0]' test_output.json
```

### Scenario 2: Read MSSQL Database to DBML

Convert MSSQL schema to DBML format:

```bash
./build/relspec convert \
  --from mssql \
  --from-conn "sqlserver://sa:StrongPassword123!@localhost:1433/RelSpecTest" \
  --to dbml \
  --to-path test_output.dbml
```

### Scenario 3: Generate SQL Script (No Direct Execution)

Generate SQL script without executing:

```bash
./build/relspec convert \
  --from mssql \
  --from-conn "sqlserver://sa:StrongPassword123!@localhost:1433/RelSpecTest" \
  --to mssql \
  --to-path test_output.sql
```

Inspect generated SQL:
```bash
head -50 test_output.sql
```

### Scenario 4: Round-Trip Conversion (MSSQL → JSON → MSSQL)

Test bidirectional conversion:

```bash
# Step 1: MSSQL → JSON
./build/relspec convert \
  --from mssql \
  --from-conn "sqlserver://sa:StrongPassword123!@localhost:1433/RelSpecTest" \
  --to json \
  --to-path backup.json

# Step 2: JSON → MSSQL SQL
./build/relspec convert \
  --from json \
  --from-path backup.json \
  --to mssql \
  --to-path restore.sql

# Inspect SQL
cat restore.sql | head -50
```

### Scenario 5: Cross-Database Conversion

If you have PostgreSQL running, test conversion:

```bash
# MSSQL → PostgreSQL SQL
./build/relspec convert \
  --from mssql \
  --from-conn "sqlserver://sa:StrongPassword123!@localhost:1433/RelSpecTest" \
  --to pgsql \
  --to-path mssql_to_pg.sql
```

### Scenario 6: Test Type Mappings

Create a JSON file with various types and convert to MSSQL:

```json
{
  "Name": "TypeTest",
  "Schemas": [
    {
      "Name": "dbo",
      "Tables": [
        {
          "Name": "type_samples",
          "Columns": {
            "id": {
              "Name": "id",
              "Type": "int",
              "AutoIncrement": true,
              "NotNull": true,
              "Sequence": 1
            },
            "big_num": {
              "Name": "big_num",
              "Type": "int64",
              "Sequence": 2
            },
            "is_active": {
              "Name": "is_active",
              "Type": "bool",
              "Sequence": 3
            },
            "description": {
              "Name": "description",
              "Type": "text",
              "Sequence": 4
            },
            "created_at": {
              "Name": "created_at",
              "Type": "timestamp",
              "NotNull": true,
              "Default": "GETDATE()",
              "Sequence": 5
            },
            "unique_id": {
              "Name": "unique_id",
              "Type": "uuid",
              "Sequence": 6
            },
            "metadata": {
              "Name": "metadata",
              "Type": "json",
              "Sequence": 7
            },
            "binary_data": {
              "Name": "binary_data",
              "Type": "bytea",
              "Sequence": 8
            }
          },
          "Constraints": {
            "PK_type_samples_id": {
              "Name": "PK_type_samples_id",
              "Type": "PRIMARY_KEY",
              "Columns": ["id"]
            }
          }
        }
      ]
    }
  ]
}
```

Convert to MSSQL:
```bash
./build/relspec convert \
  --from json \
  --from-path type_test.json \
  --to mssql \
  --to-path type_test.sql

cat type_test.sql
```

## Cleanup

Stop and remove the SQL Server container:

```bash
docker-compose down

# Clean up test files
rm -f test_output.* backup.json restore.sql
```

## Troubleshooting

### Container won't start

Check Docker daemon is running and database logs:
```bash
docker-compose logs mssql
```

### Connection refused errors

Wait for container to be healthy:
```bash
docker-compose ps
# Wait until STATUS shows "healthy"

# Or check manually
docker exec -it $(docker-compose ps -q mssql) \
  /opt/mssql-tools/bin/sqlcmd \
  -S localhost \
  -U sa \
  -P 'StrongPassword123!' \
  -Q "SELECT @@VERSION"
```

### Test schema not found

Initialize the test schema:
```bash
docker exec -i $(docker-compose ps -q mssql) \
  /opt/mssql-tools/bin/sqlcmd \
  -S localhost \
  -U sa \
  -P 'StrongPassword123!' \
  < test_data/mssql/test_schema.sql
```

### Connection string format issues

Use the correct format for connection strings:
- Default port: 1433
- Username: `sa`
- Password: `StrongPassword123!`
- Database: `RelSpecTest`

Format: `sqlserver://sa:StrongPassword123!@localhost:1433/RelSpecTest`

## Performance Notes

- Initial reader setup may take a few seconds
- Type mapping queries are cached within a single read operation
- Direct execution mode is atomic per table/constraint
- Large schemas (100+ tables) should complete in under 5 seconds

## Unit Test Verification

Run the MSSQL-specific tests:

```bash
# Type mapping tests
go test ./pkg/mssql/... -v

# Reader tests
go test ./pkg/readers/mssql/... -v

# Writer tests
go test ./pkg/writers/mssql/... -v

# All together
go test ./pkg/mssql/... ./pkg/readers/mssql/... ./pkg/writers/mssql/... -v
```

Expected output: All tests should PASS
