Files
relspecgo/pkg/writers/mssql
warkanum 96281c9f03
Release / test (push) Failing after 2m20s
Release / release (push) Skipped
Release / pkg-aur (push) Skipped
Release / pkg-deb (push) Skipped
Release / pkg-rpm (push) Skipped
chore(ci): add govulncheck, staticcheck, go vet, gofumpt gates
* Add lint/format checks to the Gitea release workflow and Makefile
  (targets: vet, fmt, fmt-check, staticcheck, govulncheck, check)
* Switch .golangci.json formatter from gofmt to gofumpt (extra.group-params)
* Bump golang.org/x/text 0.37.0 -> 0.39.0 for GO-2026-5970; re-vendor
* Fix staticcheck S1011 in pkg/diff; drop unused pgsql writer helpers
* Fix gocritic unnamedResult (pkg/diff) and rangeValCopy (pkg/pgsql)
* Apply gofumpt + goimports formatting across the tree
2026-09-03 21:17:03 +02:00
..

MSSQL Writer

Generates Microsoft SQL Server DDL (Data Definition Language) from database schema models.

Features

  • DDL Generation: Generates complete SQL scripts for creating MSSQL schema
  • Schema Support: Creates multiple schemas with proper naming
  • Bracket Notation: Uses [schema].[table] bracket notation for identifiers
  • Identity Columns: Generates IDENTITY(1,1) for auto-increment columns
  • Constraints: Generates primary keys, foreign keys, unique, and check constraints
  • Indexes: Creates indexes with unique support
  • Extended Properties: Uses sp_addextendedproperty for comments
  • Direct Execution: Can directly execute DDL on MSSQL database
  • Schema Flattening: Optional schema flattening for compatibility

Features by Phase

  1. Phase 1: Create schemas
  2. Phase 2: Create tables with columns, identity, and defaults
  3. Phase 3: Add primary key constraints
  4. Phase 4: Create indexes
  5. Phase 5: Add unique constraints
  6. Phase 6: Add check constraints
  7. Phase 7: Add foreign key constraints
  8. Phase 8: Add extended properties (comments)

Type Mappings

Canonical Type MSSQL Type
int INT
int64 BIGINT
int16 SMALLINT
int8 TINYINT
bool BIT
float32 REAL
float64 FLOAT
decimal NUMERIC
string NVARCHAR(255)
text NVARCHAR(MAX)
timestamp DATETIME2
timestamptz DATETIMEOFFSET
uuid UNIQUEIDENTIFIER
bytea VARBINARY(MAX)
date DATE
time TIME

Usage

Generate SQL File

import "git.warky.dev/wdevs/relspecgo/pkg/writers/mssql"
import "git.warky.dev/wdevs/relspecgo/pkg/writers"

writer := mssql.NewWriter(&writers.WriterOptions{
    OutputPath: "schema.sql",
    FlattenSchema: false,
})

err := writer.WriteDatabase(db)
if err != nil {
    panic(err)
}

Direct Database Execution

writer := mssql.NewWriter(&writers.WriterOptions{
    OutputPath: "",
    Metadata: map[string]interface{}{
        "connection_string": "sqlserver://sa:password@localhost/newdb",
    },
})

err := writer.WriteDatabase(db)
if err != nil {
    panic(err)
}

CLI Usage

Generate SQL file:

relspec convert --from json --from-path schema.json \
                --to mssql --to-path schema.sql

Execute directly to database:

relspec convert --from json --from-path schema.json \
                --to mssql \
                --metadata '{"connection_string":"sqlserver://sa:password@localhost/mydb"}'

Default Values

The writer supports several default value patterns:

  • Functions: GETDATE(), CURRENT_TIMESTAMP()
  • Literals: strings wrapped in quotes, numbers, booleans (0/1 for BIT)
  • CAST expressions

Comments/Extended Properties

Table and column descriptions are stored as MS_Description extended properties:

EXEC sp_addextendedproperty
  @name = 'MS_Description',
  @value = 'Table description here',
  @level0type = 'SCHEMA', @level0name = 'dbo',
  @level1type = 'TABLE', @level1name = 'my_table';

Testing

Run tests with:

go test ./pkg/writers/mssql/...

Limitations

  • Views are not currently supported in the writer
  • Sequences are not supported (MSSQL uses IDENTITY instead)
  • Partitioning and advanced features are not supported
  • Generated DDL assumes no triggers or computed columns