/* ===================================================================================================================== ShipLink 2026 - SQLite History Import : bookkeeping tables in ShipLinkCentralDB --------------------------------------------------------------------------------------------------------------------- Run once against ShipLinkCentralDB (any login with CREATE TABLE / ALTER TABLE rights). Idempotent: safe to run again, and upgrades tables created by the first version of the utility. ShipLink runs this same script (embedded resource) before every import, so running it by hand is only required when the ShipLink SQL login is not allowed to create tables. dbo.SLHistoryImport one row per execution (manual from Support & Tools > Data Utilities, or automatic on startup). It is also the run-once indicator: a PC (PCId) is "done" when it has a row with Status IN ('Completed', 'NoSource', 'Undoing', 'Undone'). dbo.SLHistoryImportJournal every row inserted and every ShipRequest_Id changed by an execution; used by Undo and to skip SQLite ShipRequests that were already imported for the same station (AID). This script contains no GO separators so ShipLink can execute it as a single batch. ===================================================================================================================== */ IF OBJECT_ID(N'dbo.SLHistoryImport', N'U') IS NULL BEGIN CREATE TABLE dbo.SLHistoryImport ( Id bigint IDENTITY(1,1) NOT NULL CONSTRAINT PK_SLHistoryImport PRIMARY KEY, AID varchar(20) NOT NULL, -- 2023 Station ID (= SLCShipmentInformation.StationId) PCId int NULL, -- License CustomerPC.Id; run-once key PCName nvarchar(200) NULL, MachineName nvarchar(200) NULL, UserId int NULL, TriggeredBy varchar(20) NOT NULL CONSTRAINT DF_SLHistoryImport_TriggeredBy DEFAULT ('Manual'), -- Manual | Startup SourceFile nvarchar(1000) NULL, SourceHash varchar(64) NULL, StartedAt datetime NOT NULL, CompletedAt datetime NULL, Status varchar(20) NOT NULL, -- Running | Completed | Failed | Cancelled | NoSource | Undoing | Undone ShipRequestCount int NOT NULL CONSTRAINT DF_SLHistoryImport_ShipRequestCount DEFAULT (0), SLCFixedCount int NOT NULL CONSTRAINT DF_SLHistoryImport_SLCFixedCount DEFAULT (0), ConflictCount int NOT NULL CONSTRAINT DF_SLHistoryImport_ConflictCount DEFAULT (0), NotCompletedCount int NOT NULL CONSTRAINT DF_SLHistoryImport_NotCompletedCount DEFAULT (0), Counts nvarchar(max) NULL, -- JSON: rows inserted / re-linked per table Error nvarchar(max) NULL ); END -- Upgrade tables created by the first version of the utility. IF COL_LENGTH(N'dbo.SLHistoryImport', N'TriggeredBy') IS NULL ALTER TABLE dbo.SLHistoryImport ADD TriggeredBy varchar(20) NOT NULL CONSTRAINT DF_SLHistoryImport_TriggeredBy DEFAULT ('Manual'); IF COL_LENGTH(N'dbo.SLHistoryImport', N'ConflictCount') IS NULL ALTER TABLE dbo.SLHistoryImport ADD ConflictCount int NOT NULL CONSTRAINT DF_SLHistoryImport_ConflictCount DEFAULT (0); IF COL_LENGTH(N'dbo.SLHistoryImport', N'NotCompletedCount') IS NULL ALTER TABLE dbo.SLHistoryImport ADD NotCompletedCount int NOT NULL CONSTRAINT DF_SLHistoryImport_NotCompletedCount DEFAULT (0); IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name = N'IX_SLHistoryImport_PC' AND object_id = OBJECT_ID(N'dbo.SLHistoryImport')) EXEC (N'CREATE INDEX IX_SLHistoryImport_PC ON dbo.SLHistoryImport (PCId, Status)'); IF OBJECT_ID(N'dbo.SLHistoryImportJournal', N'U') IS NULL BEGIN CREATE TABLE dbo.SLHistoryImportJournal ( Id bigint IDENTITY(1,1) NOT NULL CONSTRAINT PK_SLHistoryImportJournal PRIMARY KEY, Import_Id bigint NOT NULL, -- dbo.SLHistoryImport.Id TableName varchar(128) NOT NULL, KeyColumn varchar(128) NOT NULL, RowId bigint NOT NULL, Action varchar(10) NOT NULL, -- Inserted | Updated ColumnName varchar(128) NULL, -- Updated only: always ShipRequest_Id OldValue bigint NULL, -- Updated only: 2023 per-PC ShipRequest_Id NewValue bigint NULL, -- Updated only: new ShipLinkCentralDB ShipRequest.Id SourceShipRequestId bigint NULL, -- SQLite ShipRequest.Id the row came from IsUndone bit NOT NULL CONSTRAINT DF_SLHistoryImportJournal_IsUndone DEFAULT (0) ); CREATE INDEX IX_SLHistoryImportJournal_Import ON dbo.SLHistoryImportJournal (Import_Id, IsUndone); CREATE INDEX IX_SLHistoryImportJournal_Source ON dbo.SLHistoryImportJournal (SourceShipRequestId, Import_Id) WHERE TableName = 'ShipRequest'; END /* --------------------------------------------------------------------------------------------------------------------- The first version also created dbo.SLHistoryImportSnapshot (table-snapshot backup) and the column dbo.SLHistoryImport.BackupFile. Both are no longer used; they are left in place so existing data is not lost. To remove them after reviewing: DROP TABLE dbo.SLHistoryImportSnapshot; ALTER TABLE dbo.SLHistoryImport DROP COLUMN BackupFile; Useful queries (not executed): -- Every execution, newest first SELECT Id, PCName, AID, TriggeredBy, Status, StartedAt, CompletedAt, ShipRequestCount, SLCFixedCount, Error FROM dbo.SLHistoryImport ORDER BY Id DESC; -- Run-once indicator per PC SELECT PCId, PCName, MAX(Id) AS LastImportId FROM dbo.SLHistoryImport WHERE Status IN ('Completed', 'NoSource', 'Undoing', 'Undone') GROUP BY PCId, PCName; -- Let the automatic startup import run again for one PC UPDATE dbo.SLHistoryImport SET Status = 'Reset' WHERE PCId = AND Status IN ('Completed', 'NoSource', 'Undone'); --------------------------------------------------------------------------------------------------------------------- */