Back to changelog
Improvement
•3 minute read

SQL Server: Attaching an Existing History Table

When a system-versioned table names an existing table as its history table, Atlas now changes that table into the shape SQL Server attaches, instead of dropping it after the attach.

When the desired state names an existing table as the history table of a system-versioned table, atlas migrate diff and atlas schema apply now change that table into the shape SQL Server attaches, before SET SYSTEM_VERSIONING = ON. Previously, Atlas turned system versioning on and then dropped the table, and the migration failed.

Attaching an Existing Table

Here, t1_history is a table of its own, with a primary key, a check, a trigger, filtered indexes, and a column t1 does not have:

Current schema
CREATE TABLE [t1] (
[c1] int NOT NULL,
[c2] int NOT NULL,
[c3] datetime2(7) GENERATED ALWAYS AS ROW START NOT NULL,
[c4] datetime2(7) GENERATED ALWAYS AS ROW END NOT NULL,
[c8] varchar(10) NULL,
CONSTRAINT [PK_t1] PRIMARY KEY CLUSTERED ([c1] ASC),
PERIOD FOR SYSTEM_TIME ([c3], [c4])
);
CREATE TABLE [t1_history] (
[c1] int NOT NULL,
[c2] bigint NOT NULL,
[c3] datetime2(7) NOT NULL,
[c4] datetime2(7) NOT NULL,
[c6] int NULL,
[c8] varchar(10) NULL,
CONSTRAINT [PK_t1_history] PRIMARY KEY NONCLUSTERED ([c1] ASC),
CONSTRAINT [CK_t1_history] CHECK ([c1] > 0),
INDEX [IX_t1_history] CLUSTERED ([c4] ASC, [c3] ASC)
);
CREATE INDEX [IX_t1_history_filter] ON [t1_history] ([c1]) WHERE [c6] IS NOT NULL;
CREATE INDEX [IX_t1_history_c8] ON [t1_history] ([c8]) WHERE [c8] IS NOT NULL;
CREATE TRIGGER [TR_t1_history] ON [t1_history] AFTER INSERT AS BEGIN
SET NOCOUNT ON;
END

The desired schema attaches it to t1 as its system_versioned history table, renames c8 to c9 with a renamed_from hint, and adds the NOT NULL column c5:

schema.sql
CREATE TABLE [t1] (
[c1] int NOT NULL,
[c2] int NOT NULL,
[c3] datetime2(7) GENERATED ALWAYS AS ROW START NOT NULL,
[c4] datetime2(7) GENERATED ALWAYS AS ROW END NOT NULL,
-- atlas:renamed_from c8
[c9] varchar(20) NULL,
[c5] int CONSTRAINT [DF_t1_c5] DEFAULT 0 NOT NULL,
CONSTRAINT [PK_t1] PRIMARY KEY CLUSTERED ([c1] ASC),
PERIOD FOR SYSTEM_TIME ([c3], [c4])
)
WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [dbo].[t1_history]));

Atlas prepares t1_history first, and attaches it last:

migrations/2.sql
-- Drop "TR_t1_history" trigger
DROP TRIGGER [TR_t1_history];
-- Drop index "IX_t1_history_filter" from table: "t1_history"
DROP INDEX IF EXISTS [IX_t1_history_filter] ON [t1_history];
-- Drop index "IX_t1_history_c8" from table: "t1_history"
DROP INDEX IF EXISTS [IX_t1_history_c8] ON [t1_history];
-- Rename a column from "c8" to "c9"
EXECUTE sp_rename @objtype = N'COLUMN', @objname = N'[t1_history].[c8]', @newname = N'c9';
-- Modify "t1_history" table
ALTER TABLE [t1_history] DROP CONSTRAINT [CK_t1_history], CONSTRAINT [PK_t1_history], COLUMN [c6];
-- Modify column "c2"
ALTER TABLE [t1_history] ALTER COLUMN [c2] int NOT NULL;
-- Modify column "c9"
ALTER TABLE [t1_history] ALTER COLUMN [c9] varchar(20) NULL;
-- Modify "t1_history" table
ALTER TABLE [t1_history] ADD [c5] int CONSTRAINT [DF_t1_history_c5] DEFAULT 0 NOT NULL;
-- Create index "IX_t1_history_c8" to table: "t1_history"
CREATE NONCLUSTERED INDEX [IX_t1_history_c8] ON [t1_history] ([c9] ASC) WHERE ([c9] IS NOT NULL);
-- Rename a column from "c8" to "c9"
EXECUTE sp_rename @objtype = N'COLUMN', @objname = N'[t1].[c8]', @newname = N'c9';
-- Modify column "c9"
ALTER TABLE [t1] ALTER COLUMN [c9] varchar(20) NULL;
-- Modify "t1" table
ALTER TABLE [t1] ADD [c5] int CONSTRAINT [DF_t1_c5] DEFAULT 0 NOT NULL;
-- Set attribute on "t1" table
ALTER TABLE [t1] SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [dbo].[t1_history], DATA_CONSISTENCY_CHECK = ON));

For the existing table, Atlas:

  • Matches its columns to the system-versioned table at the same positions: drops extra columns, adds missing ones after the existing ones, and changes types, nullability, and collations to match. Names are compared as the database collation compares them.
  • Renames a column the system-versioned table renames, with renamed_from or at an interactive prompt.
  • Drops the primary key, unique indexes, foreign keys, checks, triggers, and full-text index. Rows, defaults, and other indexes stay. A filtered index that reads a dropped or renamed column is dropped first, and created again after a rename.
  • Gives a new NOT NULL column the default of the table column, so existing rows get a value.
  • Drops a default that draws from a sequence the migration drops.

History Tables Named by SQL Server

Without HISTORY_TABLE, SQL Server names the history table MSSQL_TemporalHistoryFor_<object_id>, so the same table gets a different history table name in each environment. Atlas now treats two such names, or a history table left unnamed, as the same table, instead of switching the history table when the names differ. A change to the retention still applies.

Limitations

The diff stops with an error, instead of planning a migration that fails, when only a rebuild could make the table attachable:

  • The existing table has an IDENTITY column.
  • The existing table is system-versioned itself, or has a SYSTEM_TIME period.
  • Its columns can't take the positions of the table columns: they are in another order, or one is missing between existing ones. SQL Server can't reorder columns.
  • The same migration changes columns of the system-versioned table to or from GENERATED ALWAYS or IDENTITY. Make that change in a migration of its own first.
  • A renamed column has both its old and new names in the existing table.
  • The type, nullability, or collation of a computed column can't match.
  • The diff policy skips a change the history table needs, such as drop_column.

A migration can still fail on data:

  • A NOT NULL column without a default is added while the history table has rows.
  • Existing values don't fit a changed type or nullability.
  • Existing history rows fail the data consistency check, such as a period that ends before it starts.

Also:

  • Dropped extra columns lose their data.
  • HISTORY_TABLE must name the schema of the table, as SQL Server requires.
  • When a table switches to another history table, the previous one stays as a regular table.
  • Requires Atlas Pro. Run atlas login to start a free trial.
improvementsqlservermssqltemporal tablesatlas pro