Files
relspecgo/test_data/mssql/TESTING.md
Hein 466d657ea7
All checks were successful
CI / Test (1.24) (push) Successful in -23m27s
CI / Test (1.25) (push) Successful in -23m4s
CI / Lint (push) Successful in -24m57s
CI / Build (push) Successful in -25m15s
Integration Tests / Integration Tests (push) Successful in -25m42s
feat(mssql): add MSSQL writer for generating DDL from database schema
- Implement MSSQL writer to generate SQL scripts for creating schemas, tables, and constraints.
- Support for identity columns, indexes, and extended properties.
- Add tests for column definitions, table creation, primary keys, foreign keys, and comments.
- Include testing guide and sample schema for integration tests.
2026-02-07 16:09:27 +02:00

5.9 KiB

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

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

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:

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

Verify output:

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:

./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:

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

Inspect generated SQL:

head -50 test_output.sql

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

Test bidirectional conversion:

# 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:

# 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:

{
  "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:

./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:

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:

docker-compose logs mssql

Connection refused errors

Wait for container to be healthy:

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:

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:

# 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