All checks were successful
- 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.
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
- Phase 1: Create schemas
- Phase 2: Create tables with columns, identity, and defaults
- Phase 3: Add primary key constraints
- Phase 4: Create indexes
- Phase 5: Add unique constraints
- Phase 6: Add check constraints
- Phase 7: Add foreign key constraints
- 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