mssql

package
v1.0.39 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 20, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

README

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer implements the Writer interface for MSSQL SQL output

func NewWriter

func NewWriter(options *writers.WriterOptions) *Writer

NewWriter creates a new MSSQL SQL writer

func (*Writer) WriteDatabase

func (w *Writer) WriteDatabase(db *models.Database) error

WriteDatabase writes the entire database schema as SQL

func (*Writer) WriteSchema

func (w *Writer) WriteSchema(schema *models.Schema) error

WriteSchema writes a single schema and all its tables

func (*Writer) WriteTable

func (w *Writer) WriteTable(table *models.Table) error

WriteTable writes a single table with all its elements

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL