Skip to content

mssql: schema objects and DDL - #5

Open
EliSauder wants to merge 9 commits into
mainfrom
mssql/ddl
Open

EliSauder wants to merge 9 commits into
mainfrom
mssql/ddl

Conversation

@EliSauder

@EliSauder EliSauder commented Nov 13, 2026 •

Copy link
Copy Markdown
Owner

Schema objects and DDL that SQL Server schemas use, applied to the catalog: views, computed columns (with their length and precision), ALTER TABLE ALTER COLUMN, sp_rename, ALTER SCHEMA … TRANSFER, schema changes inside IF/BEGIN … END blocks, sequences (NEXT VALUE FOR) and synonyms.

Commits

  • 07566c853 feat(mssql): type computed columns by their expression
  • 6ae8fb1d5 feat(mssql): support views
  • e49d9b22a fix(mssql): keep the length and precision of a computed column
  • 0995c0a85 feat(mssql): apply ALTER TABLE ALTER COLUMN
  • 4a2d38bde feat(mssql): apply sp_rename to tables and columns
  • 4bbce3455 feat(mssql): apply the schema changes in IF and BEGIN ... END blocks
  • bbda21d2b feat(mssql): type NEXT VALUE FOR a sequence
  • 475e15b20 feat(mssql): select from a synonym the schema creates
  • 586e335d6 feat(mssql): apply ALTER SCHEMA ... TRANSFER

Test changes for independence

  • analyze_computed_columns: the CONCAT and ISNULL computed columns are left out, since those are typed by the expression typing PR's seeds.

Independence. This is one of five PRs split from #1, each off main with no dependency on the others: it builds, passes the base suite and keeps the core context at or above main's, alone. Its goldens are generated on this branch, and they hold unchanged with all five merged, whose code then equals the original branch. Parallel PRs add lines next to each other in convert.go, functions.jsonl and the dialect settings, so whichever merges later may need a rebase that keeps both sides.

Verification. Behavior was checked against SQL Server 2025 in #1 (goldeneye or sp_describe_*). go test ./internal/endtoend -run TestReplay/base/ and the touched packages' tests pass; generated Go goldens pass go vet.

EliSauder and others added 9 commits November 12, 2026 18:44
A computed column declares no type, so the converter left it with an
empty type name and the whole schema failed to load. The converter now
carries the expression as a generated-column constraint, and the core
types it against the table's other columns, falling back to an untyped
column rather than failing. SQL Server describes most computed columns
as nullable, even PERSISTED NOT NULL ones, so the converter marks only
the expressions it keeps NOT NULL.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
CREATE VIEW was ignored, so a query reading a view failed as an unknown
relation. CREATE VIEW, CREATE OR ALTER VIEW and ALTER VIEW now convert
to the view statement the core already records, with the columns its
query selects or its column list names, and DROP VIEW drops it.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
A computed column took the type its expression was analyzed as, but the
analysis reports the family's row for a type with arguments no column
has interned, so CAST(price AS nvarchar(37)) was stored as nvarchar and
read back as the nvarchar(1) a bare declaration means. A type with
arguments is now resolved as the whole expression.

Addresses review comment r4557025274 on #1.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
ALTER COLUMN was ignored, so a column kept the type and nullability it
was created with. It now converts to the column type and NOT NULL
changes the core already applies: a new type with NOT NULL keeps the
column NOT NULL, and one without makes it nullable, as SQL Server does.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
SQL Server renames a table or column with EXEC sp_rename, which was
ignored as a procedure call, so a schema kept the old names. A call of
sp_rename, its arguments by position or by name, now converts to the
table or column rename the core already applies. Any other procedure
call is still no statement sqlc has a use for.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
A migration script guards its changes, as IF OBJECT_ID('t') IS NULL
CREATE TABLE t ... does, and a statement inside an IF or BEGIN ... END
block was skipped with the block, so such a schema was empty. The schema
changes in a block's THEN branch are now applied, each tolerating the
object being there or not, since the guard decides that: a create of
one that exists, and a drop, alter or rename of one that does not, are
skipped. Queries and data changes inside a block are still left out.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
CREATE SEQUENCE was ignored, so NEXT VALUE FOR could not be typed and
the query failed. A sequence is now recorded as a function returning its
type, bigint unless AS names another, which NEXT VALUE FOR calls.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
CREATE SYNONYM was ignored, so a query naming a synonym failed. A
synonym is now recorded as a view of everything in what it names. A
synonym may as well name a procedure, or something not yet created,
which leaves nothing to select from it, so it is then left out.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
ALTER SCHEMA s TRANSFER t, which moves a table or view to schema s, was
ignored, leaving t where it was. The core schema now moves a relation
for ALTER TABLE ... SET SCHEMA, which PostgreSQL's converter already
produces, and SQL Server's TRANSFER converts to it.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant