From 02eb218276a519c8b85bab47e6f5d1e9780e988a Mon Sep 17 00:00:00 2001 From: Adam Rauch Date: Wed, 10 Jun 2026 16:10:18 -0700 Subject: [PATCH 1/8] Consolidate snprc_scheduler upgrade scripts into 0.000-25.000.sql --- snprc_scheduler/module.properties | 4 +- .../sqlserver/snprc_scheduler-0.00-18.21.sql | 5 - .../snprc_scheduler-0.000-25.000.sql | 803 ++++++++++++++++++ .../sqlserver/snprc_scheduler-18.21-18.22.sql | 276 ------ .../sqlserver/snprc_scheduler-18.22-18.23.sql | 120 --- .../sqlserver/snprc_scheduler-18.23-18.24.sql | 46 - .../sqlserver/snprc_scheduler-18.24-18.25.sql | 13 - .../sqlserver/snprc_scheduler-18.25-18.26.sql | 16 - .../sqlserver/snprc_scheduler-18.26-18.27.sql | 7 - .../sqlserver/snprc_scheduler-18.27-18.28.sql | 77 -- .../sqlserver/snprc_scheduler-18.28-18.29.sql | 201 ----- .../sqlserver/snprc_scheduler-18.29-18.30.sql | 28 - .../sqlserver/snprc_scheduler-18.30-23.00.sql | 3 - .../SNPRC_schedulerModule.java | 2 +- 14 files changed, 806 insertions(+), 795 deletions(-) delete mode 100644 snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-0.00-18.21.sql create mode 100644 snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-0.000-25.000.sql delete mode 100644 snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-18.21-18.22.sql delete mode 100644 snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-18.22-18.23.sql delete mode 100644 snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-18.23-18.24.sql delete mode 100644 snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-18.24-18.25.sql delete mode 100644 snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-18.25-18.26.sql delete mode 100644 snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-18.26-18.27.sql delete mode 100644 snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-18.27-18.28.sql delete mode 100644 snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-18.28-18.29.sql delete mode 100644 snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-18.29-18.30.sql delete mode 100644 snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-18.30-23.00.sql diff --git a/snprc_scheduler/module.properties b/snprc_scheduler/module.properties index 3d5fa7233..1b95bfa9d 100644 --- a/snprc_scheduler/module.properties +++ b/snprc_scheduler/module.properties @@ -1,7 +1,7 @@ ModuleClass: org.labkey.snprc_scheduler.SNPRC_schedulerModule Label: SNPRC EHR Procedure scheduling module Description: The SNPRC scheduler provides timeline and procedure scheduling for snprc_ehr module. -SupportedDatabases: mssql +SupportedDatabases: mssql, pgsql License: Apache 2.0 LicenseURL: http://www.apache.org/licenses/LICENSE-2.0 -ManageVersion: false +ManageVersion: true diff --git a/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-0.00-18.21.sql b/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-0.00-18.21.sql deleted file mode 100644 index 197685052..000000000 --- a/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-0.00-18.21.sql +++ /dev/null @@ -1,5 +0,0 @@ - --- Create schema, tables, indexes, and constraints used for Snprc_scheduler module here --- All SQL VIEW definitions should be created in snprc_scheduler-create.sql and dropped in snprc_scheduler-drop.sql -CREATE SCHEMA snprc_scheduler; -GO \ No newline at end of file diff --git a/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-0.000-25.000.sql b/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-0.000-25.000.sql new file mode 100644 index 000000000..d86da32bd --- /dev/null +++ b/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-0.000-25.000.sql @@ -0,0 +1,803 @@ +-- Create schema, tables, indexes, and constraints used for Snprc_scheduler module here +-- All SQL VIEW definitions should be created in snprc_scheduler-create.sql and dropped in snprc_scheduler-drop.sql +CREATE SCHEMA snprc_scheduler; +GO + +/* 18.xxx SQL scripts */ + +/************************************************************************ +Create schema +Timeline, +TimelineItem, +Schedule, +TimelineAnimalJunction, +TimelinePojectItem + + +Do not have date bound reference from Timeline to TimelineAnimalJunction +Do not have date bound reference TimelineAnimalJunction to studyDataset_..._assignments + + +srr 10.30.18 + +Added qc and revisionNum columns +srr 11.01.2018 + +refactor keys, qcstate only in timeline. +pulled container from all tables + +Changed FK to snd.Projects back to composite. +Dropped ProjectObjectId from Timeline table. +srr 11.07.2018 + +************************************************************************/ + + + +/*==============================================================*/ +/* Table: Timeline */ +/*==============================================================*/ +create table SNPRC_Scheduler.Timeline ( + TimelineId int not null, + RevisionNum int not null, + ProjectObjectId entityId not null, + StartDate date null, + EndDate date null, + Description nvarchar(255) null, + LeadTech nvarchar(50) null, + Notes nvarchar(Max) null, + SchedulerNotes nvarchar(500) null, + QcState int null, + Created datetime null, + CreatedBy int null, + Modified datetime null, + ModifiedBy int null, + ObjectId entityId not null +) +go +execute sp_addextendedproperty 'MS_Description', + 'Data Dict', + 'Schema', 'SNPRC_Scheduler', 'table', 'timeline' +go + +execute sp_addextendedproperty 'MS_Description', + 'Added during 9/17/18 meeting. Size TBD', + 'Schema', 'SNPRC_Scheduler', 'table', 'timeline', 'column', 'Notes' +go + +execute sp_addextendedproperty 'MS_Description', + 'Will handle isActive function', + 'Schema', 'SNPRC_Scheduler', 'table', 'timeline', 'column', 'qcstate' +go + + +alter table SNPRC_Scheduler.Timeline + add constraint PK_Timeline primary key nonclustered (TimelineId, RevisionNum) +go + +alter table SNPRC_Scheduler.Timeline + add constraint AK_TimelineObjectId unique (ObjectId) +go + + + +/*==============================================================*/ +/* Index: IDX_TimelineFK1 */ +/*==============================================================*/ +create clustered index IDX_TimelineFK1 on SNPRC_Scheduler.Timeline ( + ProjectObjectId ASC +) +go + +alter table SNPRC_Scheduler.Timeline + add constraint FK_Timeline_SNDProject foreign key (ProjectObjectId) +references snd.projects (Objectid) +go + + + + + +-- %% 111111111111 %%%%% end timeline + + +/*==============================================================*/ +/* Table: TimelineItem 2 */ +/*==============================================================*/ +create table SNPRC_Scheduler.TimelineItem ( + TimelineItemId int identity, + ProjectitemId int not null, + TimelineObjectId entityId not null, + StudyDay int null, + ScheduleDay int null, + Created datetime null, + CreatedBy int null, + Modified datetime null, + ModifiedBy int null, + ObjectId entityId not null +) + +go +alter table SNPRC_Scheduler.TimelineItem + add constraint PK_TimelineItem primary key nonclustered (TimelineItemId) + +go + +alter table SNPRC_Scheduler.TimelineItem + add constraint AK_TimelineItemObjectId unique (ObjectId) +go + + +/*==============================================================*/ +/* Index: IDXC_TimelineItemFK2 */ +/*==============================================================*/ +create clustered index IDXC_TimelineItemFK2 on SNPRC_Scheduler.TimelineItem ( + ProjectItemId ASC +) + +/*==============================================================*/ +/* Index: IDX_TimelineItemFK1 */ +/*==============================================================*/ +create index IDX_TimelineItemFK1 on SNPRC_Scheduler.TimelineItem ( + TimelineObjectId ASC +) +go + +alter table SNPRC_Scheduler.Timelineitem + add constraint FK_TimelineItem_Timeline foreign key (TimelineObjectId) +references SNPRC_Scheduler.Timeline (ObjectId) +go +alter table SNPRC_Scheduler.Timelineitem + add constraint FK_TimelineItem_SNDProjectItem foreign key (ProjectItemId) +references snd.ProjectItems (ProjectItemId) +go +-- %% 22222222222 %% end timelineItem + +/*==============================================================*/ +/* Table: Schedule 3 */ +/*==============================================================*/ +create table SNPRC_Scheduler.Schedule ( + ScheduleId int identity, + TimelineItemId int not null, + TargetDate datetime not null, + Created datetime null, + CreatedBy int null, + Modified datetime null, + ModifiedBy int null, + ObjectId EntityId not null +) + +go + +alter table SNPRC_Scheduler.Schedule + add constraint PK_Schedule primary key (ScheduleId) +go + + +/*==============================================================*/ +/* Index: IDX_ScheduleFK1 */ +/*==============================================================*/ +create index IDX_ScheduleFK1 on SNPRC_Scheduler.Schedule ( + TimelineItemId ASC +) + + +alter table SNPRC_Scheduler.Schedule + add constraint FK_ScheduleFK1 foreign key (TimelineItemId) +references SNPRC_Scheduler.TimelineItem (TimelineItemId) +go + + +-- %%% 333333 %%% end Schedule + +/*==============================================================*/ +/* Table: TimelineProjectItem 4 */ +/*==============================================================*/ +create table SNPRC_Scheduler.TimelineProjectItem ( + ProjectItemId int not null, + TimelineObjectId entityId not null, + TimelineId int not null, + TimelineRevisionNum int not null, + TimelineFootNotes nvarchar(100) null, + SortOrder int not null +) +go + +alter table SNPRC_Scheduler.TimelineProjectItem + add constraint PK_TimelineProjectItem primary key (TimelineObjectId, ProjectItemId) +go + +/*==============================================================*/ +/* Index: IDX_TimelineProjectItemFK1 */ +/*==============================================================*/ +create index IDX_TimelineProjectItemFK1 on SNPRC_Scheduler.TimelineProjectItem ( + TimelineObjectid ASC +) +go + +/*==============================================================*/ +/* Index: IDX_TimelineItemFK2 */ +/*==============================================================*/ +create index IDX_TimelineProjectItemFK2 on SNPRC_Scheduler.TimelineProjectItem ( + ProjectItemId ASC +) +go + +alter table SNPRC_Scheduler.TimelineProjectItem + add constraint FK_TimelineProjectItem_SndProject foreign key (ProjectItemId) +references snd.ProjectItems (ProjectitemId) +go + +alter table SNPRC_Scheduler.TimelineProjectItem + add constraint FK_TimelineProjectItem foreign key (TimelineObjectId) +references SNPRC_Scheduler.Timeline (ObjectId) +go + + + +-- %%%%%%% 4444444 %%%%%%%%%%%% end TimelineProjectItem + +/*==============================================================*/ +/* Table: timelineanimaljunction 5 */ +/*==============================================================*/ +create table SNPRC_Scheduler.TimelineAnimalJunction ( + RowId int identity, + AnimalId nvarchar(50) not null, + TimelineObjectId entityId null, + StartDate date null, + StopDate date null, + Created datetime null, + CreatedBy int null, + Modified datetime null, + ModifiedBy int null, + ObjectId entityId not null +) + +go + +alter table SNPRC_Scheduler.TimelineAnimalJunction + add constraint PK_TimelineAnimalJunction primary key nonclustered (RowId) +go + + +/*==============================================================*/ +/* Index: IDXC_TimelineAnimalJunctionFK1 */ +/*==============================================================*/ +create clustered index IDXC_TimelineAnimalJunctionFK1 on SNPRC_Scheduler.TimelineAnimalJunction ( + TimelineObjectId ASC +) +go + +/*==============================================================*/ +/* Index: IDX_TimelineAnimalJunctionFK2 */ +/*==============================================================*/ +create index IDX_TimelineAnimalJunctionFK2 on SNPRC_Scheduler.TimelineAnimalJunction ( + AnimalID ASC +) +go + + +alter table SNPRC_Scheduler.TimelineAnimalJunction + add constraint FK_TimelineJunction_Timeline foreign key (TimelineObjectId) +references SNPRC_Scheduler.Timeline (ObjectId) +go + +/****************************************************** + + rename snprc_scheduler.TimelineAnimalJunction.StopDate to EndDate + +srr 11.12.2018 +******************************************************/ +SET QUOTED_IDENTIFIER ON +SET ARITHABORT ON +SET NUMERIC_ROUNDABORT OFF +SET CONCAT_NULL_YIELDS_NULL ON +SET ANSI_NULLS ON +SET ANSI_PADDING ON +SET ANSI_WARNINGS ON + +GO +EXECUTE sp_rename N'snprc_scheduler.TimelineAnimalJunction.StopDate', N'Tmp_EndDate', 'COLUMN' +GO +EXECUTE sp_rename N'snprc_scheduler.TimelineAnimalJunction.Tmp_EndDate', N'EndDate', 'COLUMN' +GO +ALTER TABLE snprc_scheduler.TimelineAnimalJunction + DROP COLUMN StartDate +GO +-- END rename StopDate to EndDate + + + +/*####################################################################### +Change column: + ScheduleDay int +to + ScheduleDate date + + + + + +srr. 11.12.18 +####################################################################### */ + + + +ALTER TABLE [snprc_scheduler].[TimelineItem] DROP CONSTRAINT [FK_TimelineItem_Timeline] +GO + +ALTER TABLE [snprc_scheduler].[TimelineItem] DROP CONSTRAINT [FK_TimelineItem_SNDProjectItem] +GO + +ALTER TABLE [snprc_scheduler].[Schedule] DROP CONSTRAINT [FK_ScheduleFK1] +GO + + +/****** Object: Table [snprc_scheduler].[TimelineItem] Script Date: 11/12/2018 11:42:24 AM ******/ +DROP TABLE [snprc_scheduler].[TimelineItem] +GO + +/****** Object: Table [snprc_scheduler].[TimelineItem] Script Date: 11/12/2018 11:42:24 AM ******/ +SET ANSI_NULLS ON +GO + +SET QUOTED_IDENTIFIER ON +GO + + + +/**/ +/*==============================================================*/ +/* Table: TimelineItem 2 */ +/*==============================================================*/ +create table SNPRC_Scheduler.TimelineItem ( + TimelineItemId int identity, + ProjectitemId int not null, + TimelineObjectId entityId not null, + StudyDay int null, + ScheduleDate datetime null, + Created datetime null, + CreatedBy int null, + Modified datetime null, + ModifiedBy int null, + ObjectId entityId not null +) + + go +alter table SNPRC_Scheduler.TimelineItem + add constraint PK_TimelineItem primary key nonclustered (TimelineItemId) + +go + +alter table SNPRC_Scheduler.TimelineItem + add constraint AK_TimelineItemObjectId unique (ObjectId) +go + + +/*==============================================================*/ +/* Index: IDXC_TimelineItemFK2 */ +/*==============================================================*/ +create clustered index IDXC_TimelineItemFK2 on SNPRC_Scheduler.TimelineItem ( +ProjectItemId ASC +) + +/*==============================================================*/ +/* Index: IDX_TimelineItemFK1 */ +/*==============================================================*/ +create index IDX_TimelineItemFK1 on SNPRC_Scheduler.TimelineItem ( + TimelineObjectId ASC +) + go + +alter table SNPRC_Scheduler.Timelineitem + add constraint FK_TimelineItem_Timeline foreign key (TimelineObjectId) +references SNPRC_Scheduler.Timeline (ObjectId) +go +alter table SNPRC_Scheduler.Timelineitem + add constraint FK_TimelineItem_SNDProjectItem foreign key (ProjectItemId) +references snd.ProjectItems (ProjectItemId) +go + +alter table SNPRC_Scheduler.Schedule + add constraint FK_ScheduleFK1 foreign key (TimelineItemId) +references SNPRC_Scheduler.TimelineItem (TimelineItemId) +go + +/*********************************************** +Adding Created,Modified and ObjectId + +Populate ObjectId for current data, +set to NOT NULL + + +srr 11.28.2018 +srr 12.05.2018 Removed SETs +***********************************************/ +--BEGIN TRANSACTION +/* +SET QUOTED_IDENTIFIER ON +SET ARITHABORT ON +SET NUMERIC_ROUNDABORT OFF +SET CONCAT_NULL_YIELDS_NULL ON +SET ANSI_NULLS ON +SET ANSI_PADDING ON +SET ANSI_WARNINGS ON +*/ +--COMMIT +BEGIN TRANSACTION +GO +ALTER TABLE snprc_scheduler.TimelineProjectItem ADD + Created datetime NULL, +CreatedBy int NULL, +Modified datetime NULL, +ModifiedBy int NULL, +ObjectId dbo.ENTITYID NULL +GO +UPDATE snprc_scheduler.TimelineProjectItem +SET ObjectID = NEWID() +WHERE ObjectID IS null + GO +ALTER TABLE snprc_scheduler.TimelineProjectItem + DROP COLUMN TimelineId, TimelineRevisionNum +GO +ALTER TABLE snprc_scheduler.TimelineProjectItem + ALTER COLUMN ObjectId dbo.ENTITYID NOT NULL + + +ALTER TABLE snprc_scheduler.TimelineProjectItem SET (LOCK_ESCALATION = TABLE) +GO +if @@trancount = 1 +COMMIT +--select 'All is well' + +/*********************************************** +Table snprc_Scheduler.Schedule was refactored out +Dropping the table and FK + + +srr 11.30.2018 +***********************************************/ +ALTER TABLE [snprc_scheduler].[Schedule] DROP CONSTRAINT [FK_ScheduleFK1] +GO + +/****** Object: Table [snprc_scheduler].[Schedule] Script Date: 11/29/2018 11:16:22 AM ******/ +DROP TABLE [snprc_scheduler].[Schedule] +GO + +/******************************************* +TimelineObjectId should be NOT NULL +srr 12.04.2018 +*******************************************/ +DROP INDEX [IDXC_TimelineAnimalJunctionFK1] ON [snprc_scheduler].[TimelineAnimalJunction] WITH ( ONLINE = OFF ) +GO +-- set TimelineObjectId to NOT NULL +ALTER TABLE snprc_scheduler.TimelineAnimalJunction + ALTER COLUMN TimelineObjectId ENTITYID NOT NULL + + +CREATE CLUSTERED INDEX [IDXC_TimelineAnimalJunctionFK1] ON [snprc_scheduler].[TimelineAnimalJunction] +( +[TimelineObjectId] ASC +) +GO + +/******************************************* +Add RC to Timeline table +srr 01.08.2019 +*******************************************/ +ALTER TABLE snprc_scheduler.Timeline ADD + RC nvarchar(50) NULL +GO + +/******************************************* +Add AnimalAccount to Timeline table +Will hold a concated list of animal acccount +for this project. +NOTE: Reference only, not validated +srr 05.22.2019 +*******************************************/ +ALTER TABLE snprc_scheduler.Timeline + ADD AnimalAccount NVARCHAR(255) NULL; +GO +/****************************************** +Add alternate key to TimlelineItem +for reference to the new StudyDayNotes table +srr 05.22.2019 +*******************************************/ + +-- Reference is not to unique row +-- will handle integrity via a different mechanism +-- srr 05.28.2019 + +/* +ALTER TABLE snprc_scheduler.TimelineItem + ADD CONSTRAINT AK_AK_STUDYDAYNOTES_TIMELINE + UNIQUE ( + TimelineObjectId, + StudyDay + ); +GO +*/ +-- drop table if it exists +EXEC core.fn_dropifexists 'StudyDayNotes','snprc_scheduler', 'TABLE'; + + +/******************************************** +Create table to hold day specific study notes. + +C:\MySoftware\labkey\trunk\externalModules\snprcEHRModules +snprcEHRModules +srr 05.22.2019 +*********************************************/ +/*==============================================================*/ +/* Table: StudyDayNotes */ +/*==============================================================*/ +CREATE TABLE snprc_scheduler.StudyDayNotes +( + ObjectId ENTITYID NOT NULL, + TimelineObjectId UNIQUEIDENTIFIER NULL, + StudyDay INT NULL, + StudyDayNote NVARCHAR(MAX) NULL, + QcState INT NULL, + Created DATETIME NULL, + CreatedBy INT NULL, + Modified DATETIME NULL, + ModifiedBy INT NULL +); +GO + +ALTER TABLE snprc_scheduler.StudyDayNotes + ADD CONSTRAINT PK_STUDYDAYNOTES + PRIMARY KEY (ObjectId); +GO + +-- Reference is not to unique row +-- will handle integrity via a different mechanism +-- srr 05.24.2019 +/* +ALTER TABLE snprc_scheduler.StudyDayNotes + ADD CONSTRAINT FK_STUDYDAY_FK_TIMELI_TIMELINE + FOREIGN KEY ( + TimelineObjectId, + StudyDay + ) + REFERENCES snprc_scheduler.TimelineItem ( + TimelineObjectId, + StudyDay + ); +*/ + +/*************************************** +Integrity triggers + + + srr 05.28.2019 + **************************************/ + +/***************************** snprc_scheduler.tia_StudyDayNotes *****************************************/ + +IF EXISTS +( + SELECT 1 + FROM sysobjects + WHERE id = OBJECT_ID('snprc_scheduler.tiu_StudyDayNotes') + AND type = 'TR' +) +DROP TRIGGER snprc_scheduler.tiu_StudyDayNotes; +GO + + +/****** Object: Trigger [snprc_scheduler].[tia_StudyDayNotes] Script Date: 5/28/2019 10:50:47 AM ******/ +/* +SET ANSI_NULLS ON +GO + +SET QUOTED_IDENTIFIER ON +GO +*/ + + +/* For referential integrity only. + Sole purpose is to prevent a note entry for (timeline-study day) that does not exists. + For INSERT and UPDATE + */ +CREATE TRIGGER snprc_scheduler.tiu_StudyDayNotes + ON snprc_scheduler.StudyDayNotes + FOR INSERT, UPDATE + AS +BEGIN +DECLARE @numrows INT, + @numnull INT, + @errno INT, + @errmsg VARCHAR(255); + +SELECT @numrows = @@rowcount; +IF @numrows = 0 + RETURN; +IF UPDATE(TimelineObjectId) + OR UPDATE(StudyDay) +BEGIN +IF +( + SELECT COUNT(*) + FROM snprc_scheduler.TimelineItem t + INNER JOIN INSERTED i + ON i.StudyDay = t.StudyDay + AND i.TimelineObjectId = t.TimelineObjectId +) < 1 +BEGIN +SELECT @errno = 50001, + @errmsg + = ' StudyDayNotes Trigger - TimelineObjectId - StudyDay pair do not exist SNPRC_Scheduler.Timelineitem. Cannot insert to SNPRC_Scheduler.StudyDayNotes'; +GOTO ERROR; +END; +END; + +-- Return if all is well +RETURN; + + /* Error handling */ +error: + RAISERROR('%d: %s', 16, 0, @errno, @errmsg); +-- LK will handle ROLLBACK TRANSACTION +END; +GO + +ALTER TABLE snprc_scheduler.StudyDayNotes ENABLE TRIGGER tiu_StudyDayNotes; + + + +/***************************** snprc_scheduler.td_TimelineItem *****************************************/ + + +IF EXISTS +( + SELECT 1 + FROM sysobjects + WHERE id = OBJECT_ID('snprc_scheduler.td_TimelineItem') + AND type = 'TR' +) +DROP TRIGGER snprc_scheduler.td_TimelineItem; +GO + + +CREATE TRIGGER [SNPRC_Scheduler].[td_TimelineItem] + ON [SNPRC_Scheduler].[TimelineItem] + FOR DELETE + AS +BEGIN +/***************************************************** + StudyDayNotes was an late addition to model. +This trigger’s sole purpose is to enforce referential integrity + between TimelineItem and StudyDayNotes tables +Each study day note requires one or more rows in the TimelineItem table. +This trigger used two table variables: +@Table_Notes holds rows that re required to have a matching row in the parent table. +@resultant uses EXCEPT operator to generate what the result of the delete will look like. +srr 06.07.19 + ****************************************************/ +DECLARE @numrows INT, + @numnull INT, + @errno INT, + @errmsg VARCHAR(255); + +-- No rows, nothing to do +SELECT @numrows = @@rowcount; +IF @numrows = 0 +RETURN; +-- No note rows for deleted items therefore nothing to do, exit +IF (SELECT COUNT(*) + FROM Deleted d + INNER JOIN SNPRC_Scheduler.StudyDayNotes n + ON n.StudyDay = d.StudyDay AND n.TimelineObjectId = d.TimelineObjectId) = 0 +RETURN; + + + +DECLARE @beforeCnt INT, + @afterCnt INT +-- table var to hold join columns +DECLARE @tbl_Notes TABLE( + StudyDay INT, + TimelineObjecId UNIQUEIDENTIFIER + ) +-- table var to hold resultant table +DECLARE @resultant TABLE( + StudyDay INT, + TimelineObjecId UNIQUEIDENTIFIER, + ProjectitemId int + ) + + -- these must exist in the post delete TimelineItem table +INSERT INTO @tbl_Notes(StudyDay, TimelineObjecId) +SELECT DISTINCT StudyDay, TimelineObjectId +FROM Deleted + -- EXCEPT used to generate the resultant table + INSERT INTO @resultant + ( + StudyDay, + TimelineObjecId, + ProjectitemId + ) +SELECT r.StudyDay, r.TimelineObjectId, r.ProjectitemId +FROM (-- new resultant table + SELECT b.TimelineItemId, b.ProjectitemId, b.TimelineObjectId, b.StudyDay + FROM SNPRC_Scheduler.TimelineItem b + INNER JOIN @tbl_Notes n + ON n.StudyDay = b.StudyDay AND n.TimelineObjecId = b.TimelineObjectId + EXCEPT + SELECT d.TimelineItemId, d.ProjectitemId, d.TimelineObjectId, d.StudyDay + FROM Deleted d + INNER JOIN @tbl_Notes n + ON n.StudyDay = d.StudyDay AND n.TimelineObjecId = d.TimelineObjectId + ) r + + + IF( SELECT COUNT(*) + --SELECT n.StudyDay, n.TimelineObjecId, r.StudyDay, r.TimelineObjecId + FROM @tbl_Notes n + LEFT OUTER JOIN @resultant r + ON r.StudyDay = n.StudyDay AND r.TimelineObjecId = n.TimelineObjecId + WHERE r.StudyDay IS NULL OR r.TimelineObjecId IS null) > 0 +BEGIN +SELECT @errno = 50001, + @errmsg + = ' Timeline Item Trigger - TimelineItem has a StudyDayNote and only item on Study day. Note must be deleted first. Data not modified.'; +GOTO ERROR; + +END; + + +-- Return if all is well +RETURN; + + /* Error handling */ +error: + RAISERROR('%d: %s', 16, 0, @errno, @errmsg); +-- LK will handle ROLLBACK TRANSACTION +END; +GO +ALTER TABLE snprc_scheduler.TimelineItem ENABLE TRIGGER td_TimelineItem; + +/******************************************/ +-- setting ProjectItemID to NOT NULL + + +ALTER TABLE snprc_scheduler.TimelineItem + ALTER COLUMN ProjectitemId INT NULL; + +--srr 05.29.19 +/*****************************************/ + +/****************************** +Added unique index to FK columns + to ensure no duplicate rows. + +srr 06.10.2019 +******************************/ + +-- delete index if it exists +if exists (select 1 + from sysindexes + where id = object_id('snprc_scheduler.StudyDayNotes') + and name = 'idx_u_FK_to_TimelineItem' + + and indid > 0 + and indid < 255) +drop index idx_u_FK_to_TimelineItem ON snprc_scheduler.StudyDayNotes + +go + + + +/****** Object: Index [idx_u_FK_TimelineItem] Script Date: 6/10/2019 4:00:27 PM ******/ +CREATE UNIQUE NONCLUSTERED INDEX idx_u_FK_to_TimelineItem ON snprc_scheduler.StudyDayNotes +( + TimelineObjectId ASC, + StudyDay ASC +) +GO + +ALTER TABLE snprc_scheduler.StudyDayNotes + DROP COLUMN QcState +GO \ No newline at end of file diff --git a/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-18.21-18.22.sql b/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-18.21-18.22.sql deleted file mode 100644 index f011ece22..000000000 --- a/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-18.21-18.22.sql +++ /dev/null @@ -1,276 +0,0 @@ -/************************************************************************ -Create schema -Timeline, -TimelineItem, -Schedule, -TimelineAnimalJunction, -TimelinePojectItem - - -Do not have date bound reference from Timeline to TimelineAnimalJunction -Do not have date bound reference TimelineAnimalJunction to studyDataset_..._assignments - - -srr 10.30.18 - -Added qc and revisionNum columns -srr 11.01.2018 - -refactor keys, qcstate only in timeline. -pulled container from all tables - -Changed FK to snd.Projects back to composite. -Dropped ProjectObjectId from Timeline table. -srr 11.07.2018 - -************************************************************************/ - - - -/*==============================================================*/ -/* Table: Timeline */ -/*==============================================================*/ -create table SNPRC_Scheduler.Timeline ( - TimelineId int not null, - RevisionNum int not null, - ProjectObjectId entityId not null, - StartDate date null, - EndDate date null, - Description nvarchar(255) null, - LeadTech nvarchar(50) null, - Notes nvarchar(Max) null, - SchedulerNotes nvarchar(500) null, - QcState int null, - Created datetime null, - CreatedBy int null, - Modified datetime null, - ModifiedBy int null, - ObjectId entityId not null -) -go -execute sp_addextendedproperty 'MS_Description', - 'Data Dict', - 'Schema', 'SNPRC_Scheduler', 'table', 'timeline' -go - -execute sp_addextendedproperty 'MS_Description', - 'Added during 9/17/18 meeting. Size TBD', - 'Schema', 'SNPRC_Scheduler', 'table', 'timeline', 'column', 'Notes' -go - -execute sp_addextendedproperty 'MS_Description', - 'Will handle isActive function', - 'Schema', 'SNPRC_Scheduler', 'table', 'timeline', 'column', 'qcstate' -go - - -alter table SNPRC_Scheduler.Timeline - add constraint PK_Timeline primary key nonclustered (TimelineId, RevisionNum) -go - -alter table SNPRC_Scheduler.Timeline - add constraint AK_TimelineObjectId unique (ObjectId) -go - - - -/*==============================================================*/ -/* Index: IDX_TimelineFK1 */ -/*==============================================================*/ -create clustered index IDX_TimelineFK1 on SNPRC_Scheduler.Timeline ( - ProjectObjectId ASC -) -go - -alter table SNPRC_Scheduler.Timeline - add constraint FK_Timeline_SNDProject foreign key (ProjectObjectId) -references snd.projects (Objectid) -go - - - - - --- %% 111111111111 %%%%% end timeline - - -/*==============================================================*/ -/* Table: TimelineItem 2 */ -/*==============================================================*/ -create table SNPRC_Scheduler.TimelineItem ( - TimelineItemId int identity, - ProjectitemId int not null, - TimelineObjectId entityId not null, - StudyDay int null, - ScheduleDay int null, - Created datetime null, - CreatedBy int null, - Modified datetime null, - ModifiedBy int null, - ObjectId entityId not null -) - -go -alter table SNPRC_Scheduler.TimelineItem - add constraint PK_TimelineItem primary key nonclustered (TimelineItemId) - -go - -alter table SNPRC_Scheduler.TimelineItem - add constraint AK_TimelineItemObjectId unique (ObjectId) -go - - -/*==============================================================*/ -/* Index: IDXC_TimelineItemFK2 */ -/*==============================================================*/ -create clustered index IDXC_TimelineItemFK2 on SNPRC_Scheduler.TimelineItem ( - ProjectItemId ASC -) - -/*==============================================================*/ -/* Index: IDX_TimelineItemFK1 */ -/*==============================================================*/ -create index IDX_TimelineItemFK1 on SNPRC_Scheduler.TimelineItem ( - TimelineObjectId ASC -) -go - -alter table SNPRC_Scheduler.Timelineitem - add constraint FK_TimelineItem_Timeline foreign key (TimelineObjectId) -references SNPRC_Scheduler.Timeline (ObjectId) -go -alter table SNPRC_Scheduler.Timelineitem - add constraint FK_TimelineItem_SNDProjectItem foreign key (ProjectItemId) -references snd.ProjectItems (ProjectItemId) -go --- %% 22222222222 %% end timelineItem - -/*==============================================================*/ -/* Table: Schedule 3 */ -/*==============================================================*/ -create table SNPRC_Scheduler.Schedule ( - ScheduleId int identity, - TimelineItemId int not null, - TargetDate datetime not null, - Created datetime null, - CreatedBy int null, - Modified datetime null, - ModifiedBy int null, - ObjectId EntityId not null -) - -go - -alter table SNPRC_Scheduler.Schedule - add constraint PK_Schedule primary key (ScheduleId) -go - - -/*==============================================================*/ -/* Index: IDX_ScheduleFK1 */ -/*==============================================================*/ -create index IDX_ScheduleFK1 on SNPRC_Scheduler.Schedule ( - TimelineItemId ASC -) - - -alter table SNPRC_Scheduler.Schedule - add constraint FK_ScheduleFK1 foreign key (TimelineItemId) -references SNPRC_Scheduler.TimelineItem (TimelineItemId) -go - - --- %%% 333333 %%% end Schedule - -/*==============================================================*/ -/* Table: TimelineProjectItem 4 */ -/*==============================================================*/ -create table SNPRC_Scheduler.TimelineProjectItem ( - ProjectItemId int not null, - TimelineObjectId entityId not null, - TimelineId int not null, - TimelineRevisionNum int not null, - TimelineFootNotes nvarchar(100) null, - SortOrder int not null -) -go - -alter table SNPRC_Scheduler.TimelineProjectItem - add constraint PK_TimelineProjectItem primary key (TimelineObjectId, ProjectItemId) -go - -/*==============================================================*/ -/* Index: IDX_TimelineProjectItemFK1 */ -/*==============================================================*/ -create index IDX_TimelineProjectItemFK1 on SNPRC_Scheduler.TimelineProjectItem ( - TimelineObjectid ASC -) -go - -/*==============================================================*/ -/* Index: IDX_TimelineItemFK2 */ -/*==============================================================*/ -create index IDX_TimelineProjectItemFK2 on SNPRC_Scheduler.TimelineProjectItem ( - ProjectItemId ASC -) -go - -alter table SNPRC_Scheduler.TimelineProjectItem - add constraint FK_TimelineProjectItem_SndProject foreign key (ProjectItemId) -references snd.ProjectItems (ProjectitemId) -go - -alter table SNPRC_Scheduler.TimelineProjectItem - add constraint FK_TimelineProjectItem foreign key (TimelineObjectId) -references SNPRC_Scheduler.Timeline (ObjectId) -go - - - --- %%%%%%% 4444444 %%%%%%%%%%%% end TimelineProjectItem - -/*==============================================================*/ -/* Table: timelineanimaljunction 5 */ -/*==============================================================*/ -create table SNPRC_Scheduler.TimelineAnimalJunction ( - RowId int identity, - AnimalId nvarchar(50) not null, - TimelineObjectId entityId null, - StartDate date null, - StopDate date null, - Created datetime null, - CreatedBy int null, - Modified datetime null, - ModifiedBy int null, - ObjectId entityId not null -) - -go - -alter table SNPRC_Scheduler.TimelineAnimalJunction - add constraint PK_TimelineAnimalJunction primary key nonclustered (RowId) -go - - -/*==============================================================*/ -/* Index: IDXC_TimelineAnimalJunctionFK1 */ -/*==============================================================*/ -create clustered index IDXC_TimelineAnimalJunctionFK1 on SNPRC_Scheduler.TimelineAnimalJunction ( - TimelineObjectId ASC -) -go - -/*==============================================================*/ -/* Index: IDX_TimelineAnimalJunctionFK2 */ -/*==============================================================*/ -create index IDX_TimelineAnimalJunctionFK2 on SNPRC_Scheduler.TimelineAnimalJunction ( - AnimalID ASC -) -go - - -alter table SNPRC_Scheduler.TimelineAnimalJunction - add constraint FK_TimelineJunction_Timeline foreign key (TimelineObjectId) -references SNPRC_Scheduler.Timeline (ObjectId) -go \ No newline at end of file diff --git a/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-18.22-18.23.sql b/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-18.22-18.23.sql deleted file mode 100644 index 0717d3745..000000000 --- a/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-18.22-18.23.sql +++ /dev/null @@ -1,120 +0,0 @@ -/****************************************************** - - rename snprc_scheduler.TimelineAnimalJunction.StopDate to EndDate - -srr 11.12.2018 -******************************************************/ -SET QUOTED_IDENTIFIER ON -SET ARITHABORT ON -SET NUMERIC_ROUNDABORT OFF -SET CONCAT_NULL_YIELDS_NULL ON -SET ANSI_NULLS ON -SET ANSI_PADDING ON -SET ANSI_WARNINGS ON - -GO -EXECUTE sp_rename N'snprc_scheduler.TimelineAnimalJunction.StopDate', N'Tmp_EndDate', 'COLUMN' -GO -EXECUTE sp_rename N'snprc_scheduler.TimelineAnimalJunction.Tmp_EndDate', N'EndDate', 'COLUMN' -GO -ALTER TABLE snprc_scheduler.TimelineAnimalJunction - DROP COLUMN StartDate -GO --- END rename StopDate to EndDate - - - -/*####################################################################### -Change column: - ScheduleDay int -to - ScheduleDate date - - - - - -srr. 11.12.18 -####################################################################### */ - - - -ALTER TABLE [snprc_scheduler].[TimelineItem] DROP CONSTRAINT [FK_TimelineItem_Timeline] -GO - -ALTER TABLE [snprc_scheduler].[TimelineItem] DROP CONSTRAINT [FK_TimelineItem_SNDProjectItem] -GO - -ALTER TABLE [snprc_scheduler].[Schedule] DROP CONSTRAINT [FK_ScheduleFK1] -GO - - -/****** Object: Table [snprc_scheduler].[TimelineItem] Script Date: 11/12/2018 11:42:24 AM ******/ -DROP TABLE [snprc_scheduler].[TimelineItem] -GO - -/****** Object: Table [snprc_scheduler].[TimelineItem] Script Date: 11/12/2018 11:42:24 AM ******/ -SET ANSI_NULLS ON -GO - -SET QUOTED_IDENTIFIER ON -GO - - - -/**/ -/*==============================================================*/ -/* Table: TimelineItem 2 */ -/*==============================================================*/ -create table SNPRC_Scheduler.TimelineItem ( - TimelineItemId int identity, - ProjectitemId int not null, - TimelineObjectId entityId not null, - StudyDay int null, - ScheduleDate datetime null, - Created datetime null, - CreatedBy int null, - Modified datetime null, - ModifiedBy int null, - ObjectId entityId not null -) - - go -alter table SNPRC_Scheduler.TimelineItem - add constraint PK_TimelineItem primary key nonclustered (TimelineItemId) - -go - -alter table SNPRC_Scheduler.TimelineItem - add constraint AK_TimelineItemObjectId unique (ObjectId) -go - - -/*==============================================================*/ -/* Index: IDXC_TimelineItemFK2 */ -/*==============================================================*/ -create clustered index IDXC_TimelineItemFK2 on SNPRC_Scheduler.TimelineItem ( -ProjectItemId ASC -) - -/*==============================================================*/ -/* Index: IDX_TimelineItemFK1 */ -/*==============================================================*/ -create index IDX_TimelineItemFK1 on SNPRC_Scheduler.TimelineItem ( - TimelineObjectId ASC -) - go - -alter table SNPRC_Scheduler.Timelineitem - add constraint FK_TimelineItem_Timeline foreign key (TimelineObjectId) -references SNPRC_Scheduler.Timeline (ObjectId) -go -alter table SNPRC_Scheduler.Timelineitem - add constraint FK_TimelineItem_SNDProjectItem foreign key (ProjectItemId) -references snd.ProjectItems (ProjectItemId) -go - -alter table SNPRC_Scheduler.Schedule - add constraint FK_ScheduleFK1 foreign key (TimelineItemId) -references SNPRC_Scheduler.TimelineItem (TimelineItemId) -go diff --git a/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-18.23-18.24.sql b/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-18.23-18.24.sql deleted file mode 100644 index a127ce96c..000000000 --- a/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-18.23-18.24.sql +++ /dev/null @@ -1,46 +0,0 @@ -/*********************************************** -Adding Created,Modified and ObjectId - -Populate ObjectId for current data, -set to NOT NULL - - -srr 11.28.2018 -srr 12.05.2018 Removed SETs -***********************************************/ ---BEGIN TRANSACTION -/* -SET QUOTED_IDENTIFIER ON -SET ARITHABORT ON -SET NUMERIC_ROUNDABORT OFF -SET CONCAT_NULL_YIELDS_NULL ON -SET ANSI_NULLS ON -SET ANSI_PADDING ON -SET ANSI_WARNINGS ON -*/ ---COMMIT -BEGIN TRANSACTION -GO -ALTER TABLE snprc_scheduler.TimelineProjectItem ADD - Created datetime NULL, -CreatedBy int NULL, -Modified datetime NULL, -ModifiedBy int NULL, -ObjectId dbo.ENTITYID NULL -GO -UPDATE snprc_scheduler.TimelineProjectItem -SET ObjectID = NEWID() -WHERE ObjectID IS null - GO -ALTER TABLE snprc_scheduler.TimelineProjectItem - DROP COLUMN TimelineId, TimelineRevisionNum -GO -ALTER TABLE snprc_scheduler.TimelineProjectItem - ALTER COLUMN ObjectId dbo.ENTITYID NOT NULL - - -ALTER TABLE snprc_scheduler.TimelineProjectItem SET (LOCK_ESCALATION = TABLE) -GO -if @@trancount = 1 -COMMIT ---select 'All is well' \ No newline at end of file diff --git a/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-18.24-18.25.sql b/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-18.24-18.25.sql deleted file mode 100644 index c573855be..000000000 --- a/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-18.24-18.25.sql +++ /dev/null @@ -1,13 +0,0 @@ -/*********************************************** -Table snprc_Scheduler.Schedule was refactored out -Dropping the table and FK - - -srr 11.30.2018 -***********************************************/ -ALTER TABLE [snprc_scheduler].[Schedule] DROP CONSTRAINT [FK_ScheduleFK1] -GO - -/****** Object: Table [snprc_scheduler].[Schedule] Script Date: 11/29/2018 11:16:22 AM ******/ -DROP TABLE [snprc_scheduler].[Schedule] -GO \ No newline at end of file diff --git a/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-18.25-18.26.sql b/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-18.25-18.26.sql deleted file mode 100644 index d280d5e38..000000000 --- a/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-18.25-18.26.sql +++ /dev/null @@ -1,16 +0,0 @@ -/******************************************* -TimelineObjectId should be NOT NULL -srr 12.04.2018 -*******************************************/ -DROP INDEX [IDXC_TimelineAnimalJunctionFK1] ON [snprc_scheduler].[TimelineAnimalJunction] WITH ( ONLINE = OFF ) -GO --- set TimelineObjectId to NOT NULL -ALTER TABLE snprc_scheduler.TimelineAnimalJunction - ALTER COLUMN TimelineObjectId ENTITYID NOT NULL - - -CREATE CLUSTERED INDEX [IDXC_TimelineAnimalJunctionFK1] ON [snprc_scheduler].[TimelineAnimalJunction] -( -[TimelineObjectId] ASC -) -GO \ No newline at end of file diff --git a/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-18.26-18.27.sql b/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-18.26-18.27.sql deleted file mode 100644 index 5f04f281c..000000000 --- a/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-18.26-18.27.sql +++ /dev/null @@ -1,7 +0,0 @@ -/******************************************* -Add RC to Timeline table -srr 01.08.2019 -*******************************************/ -ALTER TABLE snprc_scheduler.Timeline ADD - RC nvarchar(50) NULL -GO \ No newline at end of file diff --git a/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-18.27-18.28.sql b/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-18.27-18.28.sql deleted file mode 100644 index 042a66d69..000000000 --- a/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-18.27-18.28.sql +++ /dev/null @@ -1,77 +0,0 @@ -/******************************************* -Add AnimalAccount to Timeline table -Will hold a concated list of animal acccount -for this project. -NOTE: Reference only, not validated -srr 05.22.2019 -*******************************************/ -ALTER TABLE snprc_scheduler.Timeline - ADD AnimalAccount NVARCHAR(255) NULL; -GO -/****************************************** -Add alternate key to TimlelineItem -for reference to the new StudyDayNotes table -srr 05.22.2019 -*******************************************/ - --- Reference is not to unique row --- will handle integrity via a different mechanism --- srr 05.28.2019 - -/* -ALTER TABLE snprc_scheduler.TimelineItem - ADD CONSTRAINT AK_AK_STUDYDAYNOTES_TIMELINE - UNIQUE ( - TimelineObjectId, - StudyDay - ); -GO -*/ --- drop table if it exists -EXEC core.fn_dropifexists 'StudyDayNotes','snprc_scheduler', 'TABLE'; - - -/******************************************** -Create table to hold day specific study notes. - -C:\MySoftware\labkey\trunk\externalModules\snprcEHRModules -snprcEHRModules -srr 05.22.2019 -*********************************************/ -/*==============================================================*/ -/* Table: StudyDayNotes */ -/*==============================================================*/ -CREATE TABLE snprc_scheduler.StudyDayNotes -( - ObjectId ENTITYID NOT NULL, - TimelineObjectId UNIQUEIDENTIFIER NULL, - StudyDay INT NULL, - StudyDayNote NVARCHAR(MAX) NULL, - QcState INT NULL, - Created DATETIME NULL, - CreatedBy INT NULL, - Modified DATETIME NULL, - ModifiedBy INT NULL -); -GO - -ALTER TABLE snprc_scheduler.StudyDayNotes - ADD CONSTRAINT PK_STUDYDAYNOTES - PRIMARY KEY (ObjectId); -GO - --- Reference is not to unique row --- will handle integrity via a different mechanism --- srr 05.24.2019 -/* -ALTER TABLE snprc_scheduler.StudyDayNotes - ADD CONSTRAINT FK_STUDYDAY_FK_TIMELI_TIMELINE - FOREIGN KEY ( - TimelineObjectId, - StudyDay - ) - REFERENCES snprc_scheduler.TimelineItem ( - TimelineObjectId, - StudyDay - ); -*/ \ No newline at end of file diff --git a/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-18.28-18.29.sql b/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-18.28-18.29.sql deleted file mode 100644 index 2d58c2ad6..000000000 --- a/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-18.28-18.29.sql +++ /dev/null @@ -1,201 +0,0 @@ -/*************************************** -Integrity triggers - - - srr 05.28.2019 - **************************************/ - -/***************************** snprc_scheduler.tia_StudyDayNotes *****************************************/ - -IF EXISTS -( - SELECT 1 - FROM sysobjects - WHERE id = OBJECT_ID('snprc_scheduler.tiu_StudyDayNotes') - AND type = 'TR' -) -DROP TRIGGER snprc_scheduler.tiu_StudyDayNotes; -GO - - -/****** Object: Trigger [snprc_scheduler].[tia_StudyDayNotes] Script Date: 5/28/2019 10:50:47 AM ******/ -/* -SET ANSI_NULLS ON -GO - -SET QUOTED_IDENTIFIER ON -GO -*/ - - -/* For referential integrity only. - Sole purpose is to prevent a note entry for (timeline-study day) that does not exists. - For INSERT and UPDATE - */ -CREATE TRIGGER snprc_scheduler.tiu_StudyDayNotes - ON snprc_scheduler.StudyDayNotes - FOR INSERT, UPDATE - AS -BEGIN -DECLARE @numrows INT, - @numnull INT, - @errno INT, - @errmsg VARCHAR(255); - -SELECT @numrows = @@rowcount; -IF @numrows = 0 - RETURN; -IF UPDATE(TimelineObjectId) - OR UPDATE(StudyDay) -BEGIN -IF -( - SELECT COUNT(*) - FROM snprc_scheduler.TimelineItem t - INNER JOIN INSERTED i - ON i.StudyDay = t.StudyDay - AND i.TimelineObjectId = t.TimelineObjectId -) < 1 -BEGIN -SELECT @errno = 50001, - @errmsg - = ' StudyDayNotes Trigger - TimelineObjectId - StudyDay pair do not exist SNPRC_Scheduler.Timelineitem. Cannot insert to SNPRC_Scheduler.StudyDayNotes'; -GOTO ERROR; -END; -END; - --- Return if all is well -RETURN; - - /* Error handling */ -error: - RAISERROR('%d: %s', 16, 0, @errno, @errmsg); --- LK will handle ROLLBACK TRANSACTION -END; -GO - -ALTER TABLE snprc_scheduler.StudyDayNotes ENABLE TRIGGER tiu_StudyDayNotes; - - - -/***************************** snprc_scheduler.td_TimelineItem *****************************************/ - - -IF EXISTS -( - SELECT 1 - FROM sysobjects - WHERE id = OBJECT_ID('snprc_scheduler.td_TimelineItem') - AND type = 'TR' -) -DROP TRIGGER snprc_scheduler.td_TimelineItem; -GO - - -CREATE TRIGGER [SNPRC_Scheduler].[td_TimelineItem] - ON [SNPRC_Scheduler].[TimelineItem] - FOR DELETE - AS -BEGIN -/***************************************************** - StudyDayNotes was an late addition to model. -This trigger’s sole purpose is to enforce referential integrity - between TimelineItem and StudyDayNotes tables -Each study day note requires one or more rows in the TimelineItem table. -This trigger used two table variables: -@Table_Notes holds rows that re required to have a matching row in the parent table. -@resultant uses EXCEPT operator to generate what the result of the delete will look like. -srr 06.07.19 - ****************************************************/ -DECLARE @numrows INT, - @numnull INT, - @errno INT, - @errmsg VARCHAR(255); - --- No rows, nothing to do -SELECT @numrows = @@rowcount; -IF @numrows = 0 -RETURN; --- No note rows for deleted items therefore nothing to do, exit -IF (SELECT COUNT(*) - FROM Deleted d - INNER JOIN SNPRC_Scheduler.StudyDayNotes n - ON n.StudyDay = d.StudyDay AND n.TimelineObjectId = d.TimelineObjectId) = 0 -RETURN; - - - -DECLARE @beforeCnt INT, - @afterCnt INT --- table var to hold join columns -DECLARE @tbl_Notes TABLE( - StudyDay INT, - TimelineObjecId UNIQUEIDENTIFIER - ) --- table var to hold resultant table -DECLARE @resultant TABLE( - StudyDay INT, - TimelineObjecId UNIQUEIDENTIFIER, - ProjectitemId int - ) - - -- these must exist in the post delete TimelineItem table -INSERT INTO @tbl_Notes(StudyDay, TimelineObjecId) -SELECT DISTINCT StudyDay, TimelineObjectId -FROM Deleted - -- EXCEPT used to generate the resultant table - INSERT INTO @resultant - ( - StudyDay, - TimelineObjecId, - ProjectitemId - ) -SELECT r.StudyDay, r.TimelineObjectId, r.ProjectitemId -FROM (-- new resultant table - SELECT b.TimelineItemId, b.ProjectitemId, b.TimelineObjectId, b.StudyDay - FROM SNPRC_Scheduler.TimelineItem b - INNER JOIN @tbl_Notes n - ON n.StudyDay = b.StudyDay AND n.TimelineObjecId = b.TimelineObjectId - EXCEPT - SELECT d.TimelineItemId, d.ProjectitemId, d.TimelineObjectId, d.StudyDay - FROM Deleted d - INNER JOIN @tbl_Notes n - ON n.StudyDay = d.StudyDay AND n.TimelineObjecId = d.TimelineObjectId - ) r - - - IF( SELECT COUNT(*) - --SELECT n.StudyDay, n.TimelineObjecId, r.StudyDay, r.TimelineObjecId - FROM @tbl_Notes n - LEFT OUTER JOIN @resultant r - ON r.StudyDay = n.StudyDay AND r.TimelineObjecId = n.TimelineObjecId - WHERE r.StudyDay IS NULL OR r.TimelineObjecId IS null) > 0 -BEGIN -SELECT @errno = 50001, - @errmsg - = ' Timeline Item Trigger - TimelineItem has a StudyDayNote and only item on Study day. Note must be deleted first. Data not modified.'; -GOTO ERROR; - -END; - - --- Return if all is well -RETURN; - - /* Error handling */ -error: - RAISERROR('%d: %s', 16, 0, @errno, @errmsg); --- LK will handle ROLLBACK TRANSACTION -END; -GO -ALTER TABLE snprc_scheduler.TimelineItem ENABLE TRIGGER td_TimelineItem; - -/******************************************/ --- setting ProjectItemID to NOT NULL - - -ALTER TABLE snprc_scheduler.TimelineItem - ALTER COLUMN ProjectitemId INT NULL; - ---srr 05.29.19 -/*****************************************/ \ No newline at end of file diff --git a/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-18.29-18.30.sql b/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-18.29-18.30.sql deleted file mode 100644 index 02a0c4a2a..000000000 --- a/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-18.29-18.30.sql +++ /dev/null @@ -1,28 +0,0 @@ -/****************************** -Added unique index to FK columns - to ensure no duplicate rows. - -srr 06.10.2019 -******************************/ - --- delete index if it exists -if exists (select 1 - from sysindexes - where id = object_id('snprc_scheduler.StudyDayNotes') - and name = 'idx_u_FK_to_TimelineItem' - - and indid > 0 - and indid < 255) -drop index idx_u_FK_to_TimelineItem ON snprc_scheduler.StudyDayNotes - -go - - - -/****** Object: Index [idx_u_FK_TimelineItem] Script Date: 6/10/2019 4:00:27 PM ******/ -CREATE UNIQUE NONCLUSTERED INDEX idx_u_FK_to_TimelineItem ON snprc_scheduler.StudyDayNotes -( - TimelineObjectId ASC, - StudyDay ASC -) -GO diff --git a/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-18.30-23.00.sql b/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-18.30-23.00.sql deleted file mode 100644 index 46f6655c8..000000000 --- a/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-18.30-23.00.sql +++ /dev/null @@ -1,3 +0,0 @@ -ALTER TABLE snprc_scheduler.StudyDayNotes - DROP COLUMN QcState -GO \ No newline at end of file diff --git a/snprc_scheduler/src/org/labkey/snprc_scheduler/SNPRC_schedulerModule.java b/snprc_scheduler/src/org/labkey/snprc_scheduler/SNPRC_schedulerModule.java index 5f904d793..d84f890db 100644 --- a/snprc_scheduler/src/org/labkey/snprc_scheduler/SNPRC_schedulerModule.java +++ b/snprc_scheduler/src/org/labkey/snprc_scheduler/SNPRC_schedulerModule.java @@ -44,7 +44,7 @@ public String getName() @Override public @Nullable Double getSchemaVersion() { - return 23.00; + return 25.000; } @Override From 831e61e879781469d95eeb74201084976dcd3594 Mon Sep 17 00:00:00 2001 From: Adam Rauch Date: Wed, 10 Jun 2026 16:30:38 -0700 Subject: [PATCH 2/8] Bit o' cleanup --- .../snprc_scheduler-0.000-25.000.sql | 76 ++++--------------- 1 file changed, 14 insertions(+), 62 deletions(-) diff --git a/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-0.000-25.000.sql b/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-0.000-25.000.sql index d86da32bd..7a6b249df 100644 --- a/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-0.000-25.000.sql +++ b/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-0.000-25.000.sql @@ -3,8 +3,6 @@ CREATE SCHEMA snprc_scheduler; GO -/* 18.xxx SQL scripts */ - /************************************************************************ Create schema Timeline, @@ -86,12 +84,12 @@ go /*==============================================================*/ create clustered index IDX_TimelineFK1 on SNPRC_Scheduler.Timeline ( ProjectObjectId ASC -) +); go alter table SNPRC_Scheduler.Timeline add constraint FK_Timeline_SNDProject foreign key (ProjectObjectId) -references snd.projects (Objectid) +references snd.projects (Objectid); go @@ -119,12 +117,12 @@ create table SNPRC_Scheduler.TimelineItem ( go alter table SNPRC_Scheduler.TimelineItem - add constraint PK_TimelineItem primary key nonclustered (TimelineItemId) + add constraint PK_TimelineItem primary key nonclustered (TimelineItemId); go alter table SNPRC_Scheduler.TimelineItem - add constraint AK_TimelineItemObjectId unique (ObjectId) + add constraint AK_TimelineItemObjectId unique (ObjectId); go @@ -133,23 +131,23 @@ go /*==============================================================*/ create clustered index IDXC_TimelineItemFK2 on SNPRC_Scheduler.TimelineItem ( ProjectItemId ASC -) +); /*==============================================================*/ /* Index: IDX_TimelineItemFK1 */ /*==============================================================*/ create index IDX_TimelineItemFK1 on SNPRC_Scheduler.TimelineItem ( TimelineObjectId ASC -) +); go alter table SNPRC_Scheduler.Timelineitem add constraint FK_TimelineItem_Timeline foreign key (TimelineObjectId) -references SNPRC_Scheduler.Timeline (ObjectId) +references SNPRC_Scheduler.Timeline (ObjectId); go alter table SNPRC_Scheduler.Timelineitem add constraint FK_TimelineItem_SNDProjectItem foreign key (ProjectItemId) -references snd.ProjectItems (ProjectItemId) +references snd.ProjectItems (ProjectItemId); go -- %% 22222222222 %% end timelineItem @@ -165,12 +163,12 @@ create table SNPRC_Scheduler.Schedule ( Modified datetime null, ModifiedBy int null, ObjectId EntityId not null -) +); go alter table SNPRC_Scheduler.Schedule - add constraint PK_Schedule primary key (ScheduleId) + add constraint PK_Schedule primary key (ScheduleId); go @@ -323,18 +321,18 @@ srr. 11.12.18 -ALTER TABLE [snprc_scheduler].[TimelineItem] DROP CONSTRAINT [FK_TimelineItem_Timeline] +ALTER TABLE [snprc_scheduler].[TimelineItem] DROP CONSTRAINT [FK_TimelineItem_Timeline]; GO -ALTER TABLE [snprc_scheduler].[TimelineItem] DROP CONSTRAINT [FK_TimelineItem_SNDProjectItem] +ALTER TABLE [snprc_scheduler].[TimelineItem] DROP CONSTRAINT [FK_TimelineItem_SNDProjectItem]; GO -ALTER TABLE [snprc_scheduler].[Schedule] DROP CONSTRAINT [FK_ScheduleFK1] +ALTER TABLE [snprc_scheduler].[Schedule] DROP CONSTRAINT [FK_ScheduleFK1]; GO /****** Object: Table [snprc_scheduler].[TimelineItem] Script Date: 11/12/2018 11:42:24 AM ******/ -DROP TABLE [snprc_scheduler].[TimelineItem] +DROP TABLE [snprc_scheduler].[TimelineItem]; GO /****** Object: Table [snprc_scheduler].[TimelineItem] Script Date: 11/12/2018 11:42:24 AM ******/ @@ -576,25 +574,7 @@ Integrity triggers /***************************** snprc_scheduler.tia_StudyDayNotes *****************************************/ -IF EXISTS -( - SELECT 1 - FROM sysobjects - WHERE id = OBJECT_ID('snprc_scheduler.tiu_StudyDayNotes') - AND type = 'TR' -) -DROP TRIGGER snprc_scheduler.tiu_StudyDayNotes; -GO - - /****** Object: Trigger [snprc_scheduler].[tia_StudyDayNotes] Script Date: 5/28/2019 10:50:47 AM ******/ -/* -SET ANSI_NULLS ON -GO - -SET QUOTED_IDENTIFIER ON -GO -*/ /* For referential integrity only. @@ -645,22 +625,8 @@ GO ALTER TABLE snprc_scheduler.StudyDayNotes ENABLE TRIGGER tiu_StudyDayNotes; - - /***************************** snprc_scheduler.td_TimelineItem *****************************************/ - -IF EXISTS -( - SELECT 1 - FROM sysobjects - WHERE id = OBJECT_ID('snprc_scheduler.td_TimelineItem') - AND type = 'TR' -) -DROP TRIGGER snprc_scheduler.td_TimelineItem; -GO - - CREATE TRIGGER [SNPRC_Scheduler].[td_TimelineItem] ON [SNPRC_Scheduler].[TimelineItem] FOR DELETE @@ -776,20 +742,6 @@ Added unique index to FK columns srr 06.10.2019 ******************************/ --- delete index if it exists -if exists (select 1 - from sysindexes - where id = object_id('snprc_scheduler.StudyDayNotes') - and name = 'idx_u_FK_to_TimelineItem' - - and indid > 0 - and indid < 255) -drop index idx_u_FK_to_TimelineItem ON snprc_scheduler.StudyDayNotes - -go - - - /****** Object: Index [idx_u_FK_TimelineItem] Script Date: 6/10/2019 4:00:27 PM ******/ CREATE UNIQUE NONCLUSTERED INDEX idx_u_FK_to_TimelineItem ON snprc_scheduler.StudyDayNotes ( From f8415a952ba946c9b6fc2b34d2c1ec22a6fd87f0 Mon Sep 17 00:00:00 2001 From: Adam Rauch Date: Wed, 10 Jun 2026 18:23:29 -0700 Subject: [PATCH 3/8] Load-bearing GO --- .../schemas/dbscripts/sqlserver/snprc_scheduler-0.000-25.000.sql | 1 + 1 file changed, 1 insertion(+) diff --git a/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-0.000-25.000.sql b/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-0.000-25.000.sql index 7a6b249df..b5ab3c5e0 100644 --- a/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-0.000-25.000.sql +++ b/snprc_scheduler/resources/schemas/dbscripts/sqlserver/snprc_scheduler-0.000-25.000.sql @@ -624,6 +624,7 @@ END; GO ALTER TABLE snprc_scheduler.StudyDayNotes ENABLE TRIGGER tiu_StudyDayNotes; +GO /***************************** snprc_scheduler.td_TimelineItem *****************************************/ From 005a62d9a240552e2d16828fc8dfa136c2f5eb06 Mon Sep 17 00:00:00 2001 From: Adam Rauch Date: Wed, 10 Jun 2026 19:54:07 -0700 Subject: [PATCH 4/8] Override getEarliestUpgradeVersion() --- .../org/labkey/snprc_scheduler/SNPRC_schedulerModule.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/snprc_scheduler/src/org/labkey/snprc_scheduler/SNPRC_schedulerModule.java b/snprc_scheduler/src/org/labkey/snprc_scheduler/SNPRC_schedulerModule.java index d84f890db..46e5e78d6 100644 --- a/snprc_scheduler/src/org/labkey/snprc_scheduler/SNPRC_schedulerModule.java +++ b/snprc_scheduler/src/org/labkey/snprc_scheduler/SNPRC_schedulerModule.java @@ -47,6 +47,13 @@ public String getName() return 25.000; } + @Override + public double getEarliestUpgradeVersion() + { + // Allow upgrades from 23.000+ + return 23.000; + } + @Override public boolean hasScripts() { @@ -74,7 +81,6 @@ public SchedulerWebPart getWebPartView(@NotNull ViewContext portalCtx, @NotNull @Override protected void init() { - addController(SNPRC_schedulerController.NAME, SNPRC_schedulerController.class); ServiceRegistry.get().registerService(SNPRC_schedulerService.class, SNPRC_schedulerServiceImpl.INSTANCE); From 519b8eef4a087753a507870188a8a917c2207dbb Mon Sep 17 00:00:00 2001 From: Adam Rauch Date: Wed, 10 Jun 2026 20:53:32 -0700 Subject: [PATCH 5/8] PostgreSQL script --- .../snprc_scheduler-0.000-25.000.sql | 550 ++++++++++++++++++ 1 file changed, 550 insertions(+) create mode 100644 snprc_scheduler/resources/schemas/dbscripts/postgresql/snprc_scheduler-0.000-25.000.sql diff --git a/snprc_scheduler/resources/schemas/dbscripts/postgresql/snprc_scheduler-0.000-25.000.sql b/snprc_scheduler/resources/schemas/dbscripts/postgresql/snprc_scheduler-0.000-25.000.sql new file mode 100644 index 000000000..e3cfe2947 --- /dev/null +++ b/snprc_scheduler/resources/schemas/dbscripts/postgresql/snprc_scheduler-0.000-25.000.sql @@ -0,0 +1,550 @@ +/* + +TODO: Review the UNIQUE INDEX on StudyDayNotes (idx_u_FK_to_TimelineItem). It has different NULL behavior vs. the +SQL Server version. SQL Server treats NULLs as equal in unique indexes (only one NULL combo allowed); PostgreSQL +treats NULLs as always distinct (multiple identical NULL combos allowed). You can match SQL Server semantics with +NULLS NOT DISTINCT. + + */ + +-- Create schema, tables, indexes, and constraints used for Snprc_scheduler module here +-- All SQL VIEW definitions should be created in snprc_scheduler-create.sql and dropped in snprc_scheduler-drop.sql +CREATE SCHEMA snprc_scheduler; + +/************************************************************************ +Create schema +Timeline, +TimelineItem, +Schedule, +TimelineAnimalJunction, +TimelinePojectItem + + +Do not have date bound reference from Timeline to TimelineAnimalJunction +Do not have date bound reference TimelineAnimalJunction to studyDataset_..._assignments + + +srr 10.30.18 + +Added qc and revisionNum columns +srr 11.01.2018 + +refactor keys, qcstate only in timeline. +pulled container from all tables + +Changed FK to snd.Projects back to composite. +Dropped ProjectObjectId from Timeline table. +srr 11.07.2018 + +************************************************************************/ + + + +/*==============================================================*/ +/* Table: Timeline */ +/*==============================================================*/ +CREATE TABLE snprc_scheduler.Timeline ( + TimelineId int not null, + RevisionNum int not null, + ProjectObjectId ENTITYID not null, + StartDate date null, + EndDate date null, + Description varchar(255) null, + LeadTech varchar(50) null, + Notes text null, + SchedulerNotes varchar(500) null, + QcState int null, + Created timestamp null, + CreatedBy int null, + Modified timestamp null, + ModifiedBy int null, + ObjectId ENTITYID not null +); + +COMMENT ON TABLE snprc_scheduler.Timeline IS 'Data Dict'; + +COMMENT ON COLUMN snprc_scheduler.Timeline.Notes IS 'Added during 9/17/18 meeting. Size TBD'; + +COMMENT ON COLUMN snprc_scheduler.Timeline.QcState IS 'Will handle isActive function'; + + +ALTER TABLE snprc_scheduler.Timeline + ADD CONSTRAINT PK_Timeline PRIMARY KEY (TimelineId, RevisionNum); + +ALTER TABLE snprc_scheduler.Timeline + ADD CONSTRAINT AK_TimelineObjectId UNIQUE (ObjectId); + + + +/*==============================================================*/ +/* Index: IDX_TimelineFK1 */ +/*==============================================================*/ +CREATE INDEX IDX_TimelineFK1 ON snprc_scheduler.Timeline ( + ProjectObjectId ASC +); + +ALTER TABLE snprc_scheduler.Timeline + ADD CONSTRAINT FK_Timeline_SNDProject FOREIGN KEY (ProjectObjectId) +REFERENCES snd.projects (Objectid); + + + + + +-- %% 111111111111 %%%%% end timeline + + +/*==============================================================*/ +/* Table: TimelineItem 2 */ +/*==============================================================*/ +CREATE TABLE snprc_scheduler.TimelineItem ( + TimelineItemId integer GENERATED BY DEFAULT AS IDENTITY, + ProjectitemId int not null, + TimelineObjectId ENTITYID not null, + StudyDay int null, + ScheduleDay int null, + Created timestamp null, + CreatedBy int null, + Modified timestamp null, + ModifiedBy int null, + ObjectId ENTITYID not null +); + +ALTER TABLE snprc_scheduler.TimelineItem + ADD CONSTRAINT PK_TimelineItem PRIMARY KEY (TimelineItemId); + +ALTER TABLE snprc_scheduler.TimelineItem + ADD CONSTRAINT AK_TimelineItemObjectId UNIQUE (ObjectId); + + +/*==============================================================*/ +/* Index: IDXC_TimelineItemFK2 */ +/*==============================================================*/ +CREATE INDEX IDXC_TimelineItemFK2 ON snprc_scheduler.TimelineItem ( + ProjectItemId ASC +); + +/*==============================================================*/ +/* Index: IDX_TimelineItemFK1 */ +/*==============================================================*/ +CREATE INDEX IDX_TimelineItemFK1 ON snprc_scheduler.TimelineItem ( + TimelineObjectId ASC +); + +ALTER TABLE snprc_scheduler.Timelineitem + ADD CONSTRAINT FK_TimelineItem_Timeline FOREIGN KEY (TimelineObjectId) +REFERENCES snprc_scheduler.Timeline (ObjectId); + +ALTER TABLE snprc_scheduler.Timelineitem + ADD CONSTRAINT FK_TimelineItem_SNDProjectItem FOREIGN KEY (ProjectItemId) +REFERENCES snd.ProjectItems (ProjectItemId); + +-- %% 2222222222 %% end timelineItem + +/*==============================================================*/ +/* Table: Schedule 3 */ +/*==============================================================*/ +CREATE TABLE snprc_scheduler.Schedule ( + ScheduleId integer GENERATED BY DEFAULT AS IDENTITY, + TimelineItemId int not null, + TargetDate timestamp not null, + Created timestamp null, + CreatedBy int null, + Modified timestamp null, + ModifiedBy int null, + ObjectId ENTITYID not null +); + +ALTER TABLE snprc_scheduler.Schedule + ADD CONSTRAINT PK_Schedule PRIMARY KEY (ScheduleId); + + +/*==============================================================*/ +/* Index: IDX_ScheduleFK1 */ +/*==============================================================*/ +CREATE INDEX IDX_ScheduleFK1 ON snprc_scheduler.Schedule ( + TimelineItemId ASC +); + + +ALTER TABLE snprc_scheduler.Schedule + ADD CONSTRAINT FK_ScheduleFK1 FOREIGN KEY (TimelineItemId) +REFERENCES snprc_scheduler.TimelineItem (TimelineItemId); + + +-- %%% 333333 %%% end Schedule + +/*==============================================================*/ +/* Table: TimelineProjectItem 4 */ +/*==============================================================*/ +CREATE TABLE snprc_scheduler.TimelineProjectItem ( + ProjectItemId int not null, + TimelineObjectId ENTITYID not null, + TimelineId int not null, + TimelineRevisionNum int not null, + TimelineFootNotes varchar(100) null, + SortOrder int not null +); + +ALTER TABLE snprc_scheduler.TimelineProjectItem + ADD CONSTRAINT PK_TimelineProjectItem PRIMARY KEY (TimelineObjectId, ProjectItemId); + +/*==============================================================*/ +/* Index: IDX_TimelineProjectItemFK1 */ +/*==============================================================*/ +CREATE INDEX IDX_TimelineProjectItemFK1 ON snprc_scheduler.TimelineProjectItem ( + TimelineObjectid ASC +); + +/*==============================================================*/ +/* Index: IDX_TimelineItemFK2 */ +/*==============================================================*/ +CREATE INDEX IDX_TimelineProjectItemFK2 ON snprc_scheduler.TimelineProjectItem ( + ProjectItemId ASC +); + +ALTER TABLE snprc_scheduler.TimelineProjectItem + ADD CONSTRAINT FK_TimelineProjectItem_SndProject FOREIGN KEY (ProjectItemId) +REFERENCES snd.ProjectItems (ProjectitemId); + +ALTER TABLE snprc_scheduler.TimelineProjectItem + ADD CONSTRAINT FK_TimelineProjectItem FOREIGN KEY (TimelineObjectId) +REFERENCES snprc_scheduler.Timeline (ObjectId); + + + +-- %%%%%%% 4444444 %%%%%%%%%%%% end TimelineProjectItem + +/*==============================================================*/ +/* Table: timelineanimaljunction 5 */ +/*==============================================================*/ +CREATE TABLE snprc_scheduler.TimelineAnimalJunction ( + RowId integer GENERATED BY DEFAULT AS IDENTITY, + AnimalId varchar(50) not null, + TimelineObjectId ENTITYID null, + StartDate date null, + StopDate date null, + Created timestamp null, + CreatedBy int null, + Modified timestamp null, + ModifiedBy int null, + ObjectId ENTITYID not null +); + +ALTER TABLE snprc_scheduler.TimelineAnimalJunction + ADD CONSTRAINT PK_TimelineAnimalJunction PRIMARY KEY (RowId); + + +/*==============================================================*/ +/* Index: IDXC_TimelineAnimalJunctionFK1 */ +/*==============================================================*/ +CREATE INDEX IDXC_TimelineAnimalJunctionFK1 ON snprc_scheduler.TimelineAnimalJunction ( + TimelineObjectId ASC +); + +/*==============================================================*/ +/* Index: IDX_TimelineAnimalJunctionFK2 */ +/*==============================================================*/ +CREATE INDEX IDX_TimelineAnimalJunctionFK2 ON snprc_scheduler.TimelineAnimalJunction ( + AnimalID ASC +); + + +ALTER TABLE snprc_scheduler.TimelineAnimalJunction + ADD CONSTRAINT FK_TimelineJunction_Timeline FOREIGN KEY (TimelineObjectId) +REFERENCES snprc_scheduler.Timeline (ObjectId); + +/****************************************************** + + rename snprc_scheduler.TimelineAnimalJunction.StopDate to EndDate + +srr 11.12.2018 +******************************************************/ + +ALTER TABLE snprc_scheduler.TimelineAnimalJunction RENAME COLUMN StopDate TO Tmp_EndDate; +ALTER TABLE snprc_scheduler.TimelineAnimalJunction RENAME COLUMN Tmp_EndDate TO EndDate; + +ALTER TABLE snprc_scheduler.TimelineAnimalJunction + DROP COLUMN StartDate; +-- END rename StopDate to EndDate + + + +/*####################################################################### +Change column: + ScheduleDay int +to + ScheduleDate date + + + + + +srr. 11.12.18 +####################################################################### */ + + + +ALTER TABLE snprc_scheduler.TimelineItem DROP CONSTRAINT FK_TimelineItem_Timeline; + +ALTER TABLE snprc_scheduler.TimelineItem DROP CONSTRAINT FK_TimelineItem_SNDProjectItem; + +ALTER TABLE snprc_scheduler.Schedule DROP CONSTRAINT FK_ScheduleFK1; + + +/****** Object: Table [snprc_scheduler].[TimelineItem] Script Date: 11/12/2018 11:42:24 AM ******/ +DROP TABLE snprc_scheduler.TimelineItem; + + + + +/**/ +/*==============================================================*/ +/* Table: TimelineItem 2 */ +/*==============================================================*/ +CREATE TABLE snprc_scheduler.TimelineItem ( + TimelineItemId integer GENERATED BY DEFAULT AS IDENTITY, + ProjectitemId int not null, + TimelineObjectId ENTITYID not null, + StudyDay int null, + ScheduleDate timestamp null, + Created timestamp null, + CreatedBy int null, + Modified timestamp null, + ModifiedBy int null, + ObjectId ENTITYID not null +); + +ALTER TABLE snprc_scheduler.TimelineItem + ADD CONSTRAINT PK_TimelineItem PRIMARY KEY (TimelineItemId); + +ALTER TABLE snprc_scheduler.TimelineItem + ADD CONSTRAINT AK_TimelineItemObjectId UNIQUE (ObjectId); + + +/*==============================================================*/ +/* Index: IDXC_TimelineItemFK2 */ +/*==============================================================*/ +CREATE INDEX IDXC_TimelineItemFK2 ON snprc_scheduler.TimelineItem ( +ProjectItemId ASC +); + +/*==============================================================*/ +/* Index: IDX_TimelineItemFK1 */ +/*==============================================================*/ +CREATE INDEX IDX_TimelineItemFK1 ON snprc_scheduler.TimelineItem ( + TimelineObjectId ASC +); + +ALTER TABLE snprc_scheduler.Timelineitem + ADD CONSTRAINT FK_TimelineItem_Timeline FOREIGN KEY (TimelineObjectId) +REFERENCES snprc_scheduler.Timeline (ObjectId); + +ALTER TABLE snprc_scheduler.Timelineitem + ADD CONSTRAINT FK_TimelineItem_SNDProjectItem FOREIGN KEY (ProjectItemId) +REFERENCES snd.ProjectItems (ProjectItemId); + +ALTER TABLE snprc_scheduler.Schedule + ADD CONSTRAINT FK_ScheduleFK1 FOREIGN KEY (TimelineItemId) +REFERENCES snprc_scheduler.TimelineItem (TimelineItemId); + +/*********************************************** +Adding Created,Modified and ObjectId + +Populate ObjectId for current data, +set to NOT NULL + + +srr 11.28.2018 +srr 12.05.2018 Removed SETs +***********************************************/ + +BEGIN; + +ALTER TABLE snprc_scheduler.TimelineProjectItem + ADD COLUMN Created timestamp NULL, + ADD COLUMN CreatedBy int NULL, + ADD COLUMN Modified timestamp NULL, + ADD COLUMN ModifiedBy int NULL, + ADD COLUMN ObjectId ENTITYID NULL; + +ALTER TABLE snprc_scheduler.TimelineProjectItem + DROP COLUMN TimelineId, + DROP COLUMN TimelineRevisionNum; + +ALTER TABLE snprc_scheduler.TimelineProjectItem + ALTER COLUMN ObjectId SET NOT NULL; + +COMMIT; + +/*********************************************** +Table snprc_Scheduler.Schedule was refactored out +Dropping the table and FK + + +srr 11.30.2018 +***********************************************/ +ALTER TABLE snprc_scheduler.Schedule DROP CONSTRAINT IF EXISTS FK_ScheduleFK1; + +DROP TABLE IF EXISTS snprc_scheduler.Schedule; + +/******************************************* +TimelineObjectId should be NOT NULL +srr 12.04.2018 +*******************************************/ +DROP INDEX IF EXISTS snprc_scheduler.IDXC_TimelineAnimalJunctionFK1; + +-- set TimelineObjectId to NOT NULL +ALTER TABLE snprc_scheduler.TimelineAnimalJunction + ALTER COLUMN TimelineObjectId SET NOT NULL; + + +CREATE INDEX IDXC_TimelineAnimalJunctionFK1 ON snprc_scheduler.TimelineAnimalJunction +( +TimelineObjectId ASC +); + +/******************************************* +Add RC to Timeline table +srr 01.08.2019 +*******************************************/ +ALTER TABLE snprc_scheduler.Timeline ADD COLUMN RC varchar(50) NULL; + +/******************************************* +Add AnimalAccount to Timeline table +Will hold a concated list of animal acccount +for this project. +NOTE: Reference only, not validated +srr 05.22.2019 +*******************************************/ +ALTER TABLE snprc_scheduler.Timeline + ADD COLUMN AnimalAccount varchar(255) NULL; + +-- drop table if it exists +SELECT core.fn_dropifexists('StudyDayNotes','snprc_scheduler', 'TABLE'); + + +/******************************************** +Create table to hold day specific study notes. + +C:\MySoftware\labkey\trunk\externalModules\snprcEHRModules +snprcEHRModules +srr 05.22.2019 +*********************************************/ +/*==============================================================*/ +/* Table: StudyDayNotes */ +/*==============================================================*/ +CREATE TABLE snprc_scheduler.StudyDayNotes +( + ObjectId ENTITYID NOT NULL, + TimelineObjectId UNIQUEIDENTIFIER NULL, + StudyDay INT NULL, + StudyDayNote TEXT NULL, + QcState INT NULL, + Created TIMESTAMP NULL, + CreatedBy INT NULL, + Modified TIMESTAMP NULL, + ModifiedBy INT NULL +); + +ALTER TABLE snprc_scheduler.StudyDayNotes + ADD CONSTRAINT PK_STUDYDAYNOTES + PRIMARY KEY (ObjectId); + + +/*************************************** +Integrity triggers + + + srr 05.28.2019 + **************************************/ + +/***************************** snprc_scheduler.tia_StudyDayNotes *****************************************/ + +/* For referential integrity only. + Sole purpose is to prevent a note entry for (timeline-study day) that does not exists. + For INSERT and UPDATE + */ +CREATE OR REPLACE FUNCTION snprc_scheduler.tiu_StudyDayNotes_fn() +RETURNS TRIGGER AS $$ +BEGIN + IF NOT EXISTS ( + SELECT 1 + FROM snprc_scheduler.TimelineItem t + WHERE t.StudyDay = NEW.StudyDay + AND t.TimelineObjectId = NEW.TimelineObjectId + ) THEN + RAISE EXCEPTION 'StudyDayNotes Trigger - TimelineObjectId - StudyDay pair do not exist SNPRC_Scheduler.Timelineitem. Cannot insert to SNPRC_Scheduler.StudyDayNotes'; + END IF; + RETURN NEW; +END; +$$ LANGUAGE plpgsql; + +CREATE TRIGGER tiu_StudyDayNotes + BEFORE INSERT OR UPDATE + ON snprc_scheduler.StudyDayNotes + FOR EACH ROW + EXECUTE FUNCTION snprc_scheduler.tiu_StudyDayNotes_fn(); + +ALTER TABLE snprc_scheduler.StudyDayNotes ENABLE TRIGGER tiu_StudyDayNotes; + +/***************************** snprc_scheduler.td_TimelineItem *****************************************/ + +CREATE OR REPLACE FUNCTION snprc_scheduler.td_TimelineItem_fn() +RETURNS TRIGGER AS $$ +BEGIN + /***************************************************** + StudyDayNotes was an late addition to model. + This trigger’s sole purpose is to enforce referential integrity + between TimelineItem and StudyDayNotes tables + Each study day note requires one or more rows in the TimelineItem table. + srr 06.07.19 + ****************************************************/ + IF EXISTS ( + SELECT 1 + FROM snprc_scheduler.StudyDayNotes n + WHERE n.StudyDay = OLD.StudyDay AND n.TimelineObjectId = OLD.TimelineObjectId + ) AND NOT EXISTS ( + SELECT 1 + FROM snprc_scheduler.TimelineItem t + WHERE t.StudyDay = OLD.StudyDay AND t.TimelineObjectId = OLD.TimelineObjectId + ) THEN + RAISE EXCEPTION 'Timeline Item Trigger - TimelineItem has a StudyDayNote and only item on Study day. Note must be deleted first. Data not modified.'; + END IF; + RETURN OLD; +END; +$$ LANGUAGE plpgsql; + +CREATE TRIGGER td_TimelineItem + AFTER DELETE + ON snprc_scheduler.TimelineItem + FOR EACH ROW + EXECUTE FUNCTION snprc_scheduler.td_TimelineItem_fn(); + +ALTER TABLE snprc_scheduler.TimelineItem ENABLE TRIGGER td_TimelineItem; + +/******************************************/ +-- setting ProjectItemID to NOT NULL + + +ALTER TABLE snprc_scheduler.TimelineItem + ALTER COLUMN ProjectitemId DROP NOT NULL; + +--srr 05.29.19 +/*****************************************/ + +/****************************** +Added unique index to FK columns + to ensure no duplicate rows. + +srr 06.10.2019 +******************************/ + +CREATE UNIQUE INDEX idx_u_FK_to_TimelineItem ON snprc_scheduler.StudyDayNotes +( + TimelineObjectId ASC, + StudyDay ASC +); + +ALTER TABLE snprc_scheduler.StudyDayNotes + DROP COLUMN QcState; From ef1e9832db8284ab0740547bab89aaffade61a80 Mon Sep 17 00:00:00 2001 From: Adam Rauch Date: Wed, 10 Jun 2026 21:08:39 -0700 Subject: [PATCH 6/8] Fix --- .../dbscripts/postgresql/snprc_scheduler-0.000-25.000.sql | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/snprc_scheduler/resources/schemas/dbscripts/postgresql/snprc_scheduler-0.000-25.000.sql b/snprc_scheduler/resources/schemas/dbscripts/postgresql/snprc_scheduler-0.000-25.000.sql index e3cfe2947..10e738d54 100644 --- a/snprc_scheduler/resources/schemas/dbscripts/postgresql/snprc_scheduler-0.000-25.000.sql +++ b/snprc_scheduler/resources/schemas/dbscripts/postgresql/snprc_scheduler-0.000-25.000.sql @@ -412,7 +412,7 @@ ALTER TABLE snprc_scheduler.Timeline ADD COLUMN RC varchar(50) NULL; /******************************************* Add AnimalAccount to Timeline table -Will hold a concated list of animal acccount +Will hold a concatenated list of animal accounts for this project. NOTE: Reference only, not validated srr 05.22.2019 @@ -420,10 +420,6 @@ srr 05.22.2019 ALTER TABLE snprc_scheduler.Timeline ADD COLUMN AnimalAccount varchar(255) NULL; --- drop table if it exists -SELECT core.fn_dropifexists('StudyDayNotes','snprc_scheduler', 'TABLE'); - - /******************************************** Create table to hold day specific study notes. From 7d0ca800a97bf4375a9b5f0ae4578fa763863d2a Mon Sep 17 00:00:00 2001 From: Adam Rauch Date: Wed, 10 Jun 2026 21:21:17 -0700 Subject: [PATCH 7/8] Clean up the script --- .../snprc_scheduler-0.000-25.000.sql | 444 ++---------------- 1 file changed, 48 insertions(+), 396 deletions(-) diff --git a/snprc_scheduler/resources/schemas/dbscripts/postgresql/snprc_scheduler-0.000-25.000.sql b/snprc_scheduler/resources/schemas/dbscripts/postgresql/snprc_scheduler-0.000-25.000.sql index 10e738d54..84a902d0d 100644 --- a/snprc_scheduler/resources/schemas/dbscripts/postgresql/snprc_scheduler-0.000-25.000.sql +++ b/snprc_scheduler/resources/schemas/dbscripts/postgresql/snprc_scheduler-0.000-25.000.sql @@ -1,45 +1,5 @@ -/* - -TODO: Review the UNIQUE INDEX on StudyDayNotes (idx_u_FK_to_TimelineItem). It has different NULL behavior vs. the -SQL Server version. SQL Server treats NULLs as equal in unique indexes (only one NULL combo allowed); PostgreSQL -treats NULLs as always distinct (multiple identical NULL combos allowed). You can match SQL Server semantics with -NULLS NOT DISTINCT. - - */ - --- Create schema, tables, indexes, and constraints used for Snprc_scheduler module here --- All SQL VIEW definitions should be created in snprc_scheduler-create.sql and dropped in snprc_scheduler-drop.sql CREATE SCHEMA snprc_scheduler; -/************************************************************************ -Create schema -Timeline, -TimelineItem, -Schedule, -TimelineAnimalJunction, -TimelinePojectItem - - -Do not have date bound reference from Timeline to TimelineAnimalJunction -Do not have date bound reference TimelineAnimalJunction to studyDataset_..._assignments - - -srr 10.30.18 - -Added qc and revisionNum columns -srr 11.01.2018 - -refactor keys, qcstate only in timeline. -pulled container from all tables - -Changed FK to snd.Projects back to composite. -Dropped ProjectObjectId from Timeline table. -srr 11.07.2018 - -************************************************************************/ - - - /*==============================================================*/ /* Table: Timeline */ /*==============================================================*/ @@ -58,409 +18,132 @@ CREATE TABLE snprc_scheduler.Timeline ( CreatedBy int null, Modified timestamp null, ModifiedBy int null, - ObjectId ENTITYID not null + ObjectId ENTITYID not null, + RC varchar(50) null, + AnimalAccount varchar(255) null, + CONSTRAINT PK_Timeline PRIMARY KEY (TimelineId, RevisionNum), + CONSTRAINT AK_TimelineObjectId UNIQUE (ObjectId), + CONSTRAINT FK_Timeline_SNDProject FOREIGN KEY (ProjectObjectId) REFERENCES snd.projects (Objectid) ); COMMENT ON TABLE snprc_scheduler.Timeline IS 'Data Dict'; - COMMENT ON COLUMN snprc_scheduler.Timeline.Notes IS 'Added during 9/17/18 meeting. Size TBD'; - COMMENT ON COLUMN snprc_scheduler.Timeline.QcState IS 'Will handle isActive function'; - -ALTER TABLE snprc_scheduler.Timeline - ADD CONSTRAINT PK_Timeline PRIMARY KEY (TimelineId, RevisionNum); - -ALTER TABLE snprc_scheduler.Timeline - ADD CONSTRAINT AK_TimelineObjectId UNIQUE (ObjectId); - - - -/*==============================================================*/ -/* Index: IDX_TimelineFK1 */ -/*==============================================================*/ CREATE INDEX IDX_TimelineFK1 ON snprc_scheduler.Timeline ( ProjectObjectId ASC ); -ALTER TABLE snprc_scheduler.Timeline - ADD CONSTRAINT FK_Timeline_SNDProject FOREIGN KEY (ProjectObjectId) -REFERENCES snd.projects (Objectid); - - - - - --- %% 111111111111 %%%%% end timeline - /*==============================================================*/ -/* Table: TimelineItem 2 */ +/* Table: TimelineItem */ /*==============================================================*/ CREATE TABLE snprc_scheduler.TimelineItem ( TimelineItemId integer GENERATED BY DEFAULT AS IDENTITY, - ProjectitemId int not null, + ProjectitemId int null, TimelineObjectId ENTITYID not null, StudyDay int null, - ScheduleDay int null, + ScheduleDate timestamp null, Created timestamp null, CreatedBy int null, Modified timestamp null, ModifiedBy int null, - ObjectId ENTITYID not null + ObjectId ENTITYID not null, + CONSTRAINT PK_TimelineItem PRIMARY KEY (TimelineItemId), + CONSTRAINT AK_TimelineItemObjectId UNIQUE (ObjectId), + CONSTRAINT FK_TimelineItem_Timeline FOREIGN KEY (TimelineObjectId) REFERENCES snprc_scheduler.Timeline (ObjectId), + CONSTRAINT FK_TimelineItem_SNDProjectItem FOREIGN KEY (ProjectItemId) REFERENCES snd.ProjectItems (ProjectItemId) ); -ALTER TABLE snprc_scheduler.TimelineItem - ADD CONSTRAINT PK_TimelineItem PRIMARY KEY (TimelineItemId); - -ALTER TABLE snprc_scheduler.TimelineItem - ADD CONSTRAINT AK_TimelineItemObjectId UNIQUE (ObjectId); - - -/*==============================================================*/ -/* Index: IDXC_TimelineItemFK2 */ -/*==============================================================*/ CREATE INDEX IDXC_TimelineItemFK2 ON snprc_scheduler.TimelineItem ( ProjectItemId ASC ); -/*==============================================================*/ -/* Index: IDX_TimelineItemFK1 */ -/*==============================================================*/ CREATE INDEX IDX_TimelineItemFK1 ON snprc_scheduler.TimelineItem ( TimelineObjectId ASC ); -ALTER TABLE snprc_scheduler.Timelineitem - ADD CONSTRAINT FK_TimelineItem_Timeline FOREIGN KEY (TimelineObjectId) -REFERENCES snprc_scheduler.Timeline (ObjectId); - -ALTER TABLE snprc_scheduler.Timelineitem - ADD CONSTRAINT FK_TimelineItem_SNDProjectItem FOREIGN KEY (ProjectItemId) -REFERENCES snd.ProjectItems (ProjectItemId); - --- %% 2222222222 %% end timelineItem - -/*==============================================================*/ -/* Table: Schedule 3 */ -/*==============================================================*/ -CREATE TABLE snprc_scheduler.Schedule ( - ScheduleId integer GENERATED BY DEFAULT AS IDENTITY, - TimelineItemId int not null, - TargetDate timestamp not null, - Created timestamp null, - CreatedBy int null, - Modified timestamp null, - ModifiedBy int null, - ObjectId ENTITYID not null -); - -ALTER TABLE snprc_scheduler.Schedule - ADD CONSTRAINT PK_Schedule PRIMARY KEY (ScheduleId); - /*==============================================================*/ -/* Index: IDX_ScheduleFK1 */ -/*==============================================================*/ -CREATE INDEX IDX_ScheduleFK1 ON snprc_scheduler.Schedule ( - TimelineItemId ASC -); - - -ALTER TABLE snprc_scheduler.Schedule - ADD CONSTRAINT FK_ScheduleFK1 FOREIGN KEY (TimelineItemId) -REFERENCES snprc_scheduler.TimelineItem (TimelineItemId); - - --- %%% 333333 %%% end Schedule - -/*==============================================================*/ -/* Table: TimelineProjectItem 4 */ +/* Table: TimelineProjectItem */ /*==============================================================*/ CREATE TABLE snprc_scheduler.TimelineProjectItem ( ProjectItemId int not null, TimelineObjectId ENTITYID not null, - TimelineId int not null, - TimelineRevisionNum int not null, TimelineFootNotes varchar(100) null, - SortOrder int not null + SortOrder int not null, + Created timestamp null, + CreatedBy int null, + Modified timestamp null, + ModifiedBy int null, + ObjectId ENTITYID not null, + CONSTRAINT PK_TimelineProjectItem PRIMARY KEY (TimelineObjectId, ProjectItemId), + CONSTRAINT FK_TimelineProjectItem_SndProject FOREIGN KEY (ProjectItemId) REFERENCES snd.ProjectItems (ProjectitemId), + CONSTRAINT FK_TimelineProjectItem FOREIGN KEY (TimelineObjectId) REFERENCES snprc_scheduler.Timeline (ObjectId) ); -ALTER TABLE snprc_scheduler.TimelineProjectItem - ADD CONSTRAINT PK_TimelineProjectItem PRIMARY KEY (TimelineObjectId, ProjectItemId); - -/*==============================================================*/ -/* Index: IDX_TimelineProjectItemFK1 */ -/*==============================================================*/ CREATE INDEX IDX_TimelineProjectItemFK1 ON snprc_scheduler.TimelineProjectItem ( - TimelineObjectid ASC + TimelineObjectId ASC ); -/*==============================================================*/ -/* Index: IDX_TimelineItemFK2 */ -/*==============================================================*/ CREATE INDEX IDX_TimelineProjectItemFK2 ON snprc_scheduler.TimelineProjectItem ( ProjectItemId ASC ); -ALTER TABLE snprc_scheduler.TimelineProjectItem - ADD CONSTRAINT FK_TimelineProjectItem_SndProject FOREIGN KEY (ProjectItemId) -REFERENCES snd.ProjectItems (ProjectitemId); - -ALTER TABLE snprc_scheduler.TimelineProjectItem - ADD CONSTRAINT FK_TimelineProjectItem FOREIGN KEY (TimelineObjectId) -REFERENCES snprc_scheduler.Timeline (ObjectId); - - - --- %%%%%%% 4444444 %%%%%%%%%%%% end TimelineProjectItem /*==============================================================*/ -/* Table: timelineanimaljunction 5 */ +/* Table: TimelineAnimalJunction */ /*==============================================================*/ CREATE TABLE snprc_scheduler.TimelineAnimalJunction ( RowId integer GENERATED BY DEFAULT AS IDENTITY, AnimalId varchar(50) not null, - TimelineObjectId ENTITYID null, - StartDate date null, - StopDate date null, + TimelineObjectId ENTITYID not null, + EndDate date null, Created timestamp null, CreatedBy int null, Modified timestamp null, ModifiedBy int null, - ObjectId ENTITYID not null + ObjectId ENTITYID not null, + CONSTRAINT PK_TimelineAnimalJunction PRIMARY KEY (RowId), + CONSTRAINT FK_TimelineJunction_Timeline FOREIGN KEY (TimelineObjectId) REFERENCES snprc_scheduler.Timeline (ObjectId) ); -ALTER TABLE snprc_scheduler.TimelineAnimalJunction - ADD CONSTRAINT PK_TimelineAnimalJunction PRIMARY KEY (RowId); - - -/*==============================================================*/ -/* Index: IDXC_TimelineAnimalJunctionFK1 */ -/*==============================================================*/ CREATE INDEX IDXC_TimelineAnimalJunctionFK1 ON snprc_scheduler.TimelineAnimalJunction ( TimelineObjectId ASC ); -/*==============================================================*/ -/* Index: IDX_TimelineAnimalJunctionFK2 */ -/*==============================================================*/ CREATE INDEX IDX_TimelineAnimalJunctionFK2 ON snprc_scheduler.TimelineAnimalJunction ( - AnimalID ASC + AnimalId ASC ); -ALTER TABLE snprc_scheduler.TimelineAnimalJunction - ADD CONSTRAINT FK_TimelineJunction_Timeline FOREIGN KEY (TimelineObjectId) -REFERENCES snprc_scheduler.Timeline (ObjectId); - -/****************************************************** - - rename snprc_scheduler.TimelineAnimalJunction.StopDate to EndDate - -srr 11.12.2018 -******************************************************/ - -ALTER TABLE snprc_scheduler.TimelineAnimalJunction RENAME COLUMN StopDate TO Tmp_EndDate; -ALTER TABLE snprc_scheduler.TimelineAnimalJunction RENAME COLUMN Tmp_EndDate TO EndDate; - -ALTER TABLE snprc_scheduler.TimelineAnimalJunction - DROP COLUMN StartDate; --- END rename StopDate to EndDate - - - -/*####################################################################### -Change column: - ScheduleDay int -to - ScheduleDate date - - - - - -srr. 11.12.18 -####################################################################### */ - - - -ALTER TABLE snprc_scheduler.TimelineItem DROP CONSTRAINT FK_TimelineItem_Timeline; - -ALTER TABLE snprc_scheduler.TimelineItem DROP CONSTRAINT FK_TimelineItem_SNDProjectItem; - -ALTER TABLE snprc_scheduler.Schedule DROP CONSTRAINT FK_ScheduleFK1; - - -/****** Object: Table [snprc_scheduler].[TimelineItem] Script Date: 11/12/2018 11:42:24 AM ******/ -DROP TABLE snprc_scheduler.TimelineItem; - - - - -/**/ /*==============================================================*/ -/* Table: TimelineItem 2 */ +/* Table: StudyDayNotes */ /*==============================================================*/ -CREATE TABLE snprc_scheduler.TimelineItem ( - TimelineItemId integer GENERATED BY DEFAULT AS IDENTITY, - ProjectitemId int not null, - TimelineObjectId ENTITYID not null, +CREATE TABLE snprc_scheduler.StudyDayNotes ( + ObjectId ENTITYID not null, + TimelineObjectId UNIQUEIDENTIFIER null, StudyDay int null, - ScheduleDate timestamp null, + StudyDayNote text null, Created timestamp null, CreatedBy int null, Modified timestamp null, ModifiedBy int null, - ObjectId ENTITYID not null + CONSTRAINT PK_StudyDayNotes PRIMARY KEY (ObjectId) ); -ALTER TABLE snprc_scheduler.TimelineItem - ADD CONSTRAINT PK_TimelineItem PRIMARY KEY (TimelineItemId); - -ALTER TABLE snprc_scheduler.TimelineItem - ADD CONSTRAINT AK_TimelineItemObjectId UNIQUE (ObjectId); +-- Using NULLS NOT DISTINCT (PostgreSQL 15+) to match SQL Server behavior for unique constraints with NULLs +CREATE UNIQUE INDEX idx_u_FK_to_TimelineItem ON snprc_scheduler.StudyDayNotes ( + TimelineObjectId ASC, + StudyDay ASC +) NULLS NOT DISTINCT; /*==============================================================*/ -/* Index: IDXC_TimelineItemFK2 */ -/*==============================================================*/ -CREATE INDEX IDXC_TimelineItemFK2 ON snprc_scheduler.TimelineItem ( -ProjectItemId ASC -); - -/*==============================================================*/ -/* Index: IDX_TimelineItemFK1 */ +/* Triggers & Integrity Functions */ /*==============================================================*/ -CREATE INDEX IDX_TimelineItemFK1 ON snprc_scheduler.TimelineItem ( - TimelineObjectId ASC -); - -ALTER TABLE snprc_scheduler.Timelineitem - ADD CONSTRAINT FK_TimelineItem_Timeline FOREIGN KEY (TimelineObjectId) -REFERENCES snprc_scheduler.Timeline (ObjectId); - -ALTER TABLE snprc_scheduler.Timelineitem - ADD CONSTRAINT FK_TimelineItem_SNDProjectItem FOREIGN KEY (ProjectItemId) -REFERENCES snd.ProjectItems (ProjectItemId); - -ALTER TABLE snprc_scheduler.Schedule - ADD CONSTRAINT FK_ScheduleFK1 FOREIGN KEY (TimelineItemId) -REFERENCES snprc_scheduler.TimelineItem (TimelineItemId); - -/*********************************************** -Adding Created,Modified and ObjectId -Populate ObjectId for current data, -set to NOT NULL - - -srr 11.28.2018 -srr 12.05.2018 Removed SETs -***********************************************/ - -BEGIN; - -ALTER TABLE snprc_scheduler.TimelineProjectItem - ADD COLUMN Created timestamp NULL, - ADD COLUMN CreatedBy int NULL, - ADD COLUMN Modified timestamp NULL, - ADD COLUMN ModifiedBy int NULL, - ADD COLUMN ObjectId ENTITYID NULL; - -ALTER TABLE snprc_scheduler.TimelineProjectItem - DROP COLUMN TimelineId, - DROP COLUMN TimelineRevisionNum; - -ALTER TABLE snprc_scheduler.TimelineProjectItem - ALTER COLUMN ObjectId SET NOT NULL; - -COMMIT; - -/*********************************************** -Table snprc_Scheduler.Schedule was refactored out -Dropping the table and FK - - -srr 11.30.2018 -***********************************************/ -ALTER TABLE snprc_scheduler.Schedule DROP CONSTRAINT IF EXISTS FK_ScheduleFK1; - -DROP TABLE IF EXISTS snprc_scheduler.Schedule; - -/******************************************* -TimelineObjectId should be NOT NULL -srr 12.04.2018 -*******************************************/ -DROP INDEX IF EXISTS snprc_scheduler.IDXC_TimelineAnimalJunctionFK1; - --- set TimelineObjectId to NOT NULL -ALTER TABLE snprc_scheduler.TimelineAnimalJunction - ALTER COLUMN TimelineObjectId SET NOT NULL; - - -CREATE INDEX IDXC_TimelineAnimalJunctionFK1 ON snprc_scheduler.TimelineAnimalJunction -( -TimelineObjectId ASC -); - -/******************************************* -Add RC to Timeline table -srr 01.08.2019 -*******************************************/ -ALTER TABLE snprc_scheduler.Timeline ADD COLUMN RC varchar(50) NULL; - -/******************************************* -Add AnimalAccount to Timeline table -Will hold a concatenated list of animal accounts -for this project. -NOTE: Reference only, not validated -srr 05.22.2019 -*******************************************/ -ALTER TABLE snprc_scheduler.Timeline - ADD COLUMN AnimalAccount varchar(255) NULL; - -/******************************************** -Create table to hold day specific study notes. - -C:\MySoftware\labkey\trunk\externalModules\snprcEHRModules -snprcEHRModules -srr 05.22.2019 -*********************************************/ -/*==============================================================*/ -/* Table: StudyDayNotes */ -/*==============================================================*/ -CREATE TABLE snprc_scheduler.StudyDayNotes -( - ObjectId ENTITYID NOT NULL, - TimelineObjectId UNIQUEIDENTIFIER NULL, - StudyDay INT NULL, - StudyDayNote TEXT NULL, - QcState INT NULL, - Created TIMESTAMP NULL, - CreatedBy INT NULL, - Modified TIMESTAMP NULL, - ModifiedBy INT NULL -); - -ALTER TABLE snprc_scheduler.StudyDayNotes - ADD CONSTRAINT PK_STUDYDAYNOTES - PRIMARY KEY (ObjectId); - - -/*************************************** -Integrity triggers - - - srr 05.28.2019 - **************************************/ - -/***************************** snprc_scheduler.tia_StudyDayNotes *****************************************/ - -/* For referential integrity only. - Sole purpose is to prevent a note entry for (timeline-study day) that does not exists. - For INSERT and UPDATE - */ +-- Trigger Function: tiu_StudyDayNotes_fn CREATE OR REPLACE FUNCTION snprc_scheduler.tiu_StudyDayNotes_fn() RETURNS TRIGGER AS $$ BEGIN @@ -476,6 +159,7 @@ BEGIN END; $$ LANGUAGE plpgsql; +-- Trigger: tiu_StudyDayNotes CREATE TRIGGER tiu_StudyDayNotes BEFORE INSERT OR UPDATE ON snprc_scheduler.StudyDayNotes @@ -484,18 +168,11 @@ CREATE TRIGGER tiu_StudyDayNotes ALTER TABLE snprc_scheduler.StudyDayNotes ENABLE TRIGGER tiu_StudyDayNotes; -/***************************** snprc_scheduler.td_TimelineItem *****************************************/ +-- Trigger Function: td_TimelineItem_fn CREATE OR REPLACE FUNCTION snprc_scheduler.td_TimelineItem_fn() RETURNS TRIGGER AS $$ BEGIN - /***************************************************** - StudyDayNotes was an late addition to model. - This trigger’s sole purpose is to enforce referential integrity - between TimelineItem and StudyDayNotes tables - Each study day note requires one or more rows in the TimelineItem table. - srr 06.07.19 - ****************************************************/ IF EXISTS ( SELECT 1 FROM snprc_scheduler.StudyDayNotes n @@ -511,6 +188,7 @@ BEGIN END; $$ LANGUAGE plpgsql; +-- Trigger: td_TimelineItem CREATE TRIGGER td_TimelineItem AFTER DELETE ON snprc_scheduler.TimelineItem @@ -518,29 +196,3 @@ CREATE TRIGGER td_TimelineItem EXECUTE FUNCTION snprc_scheduler.td_TimelineItem_fn(); ALTER TABLE snprc_scheduler.TimelineItem ENABLE TRIGGER td_TimelineItem; - -/******************************************/ --- setting ProjectItemID to NOT NULL - - -ALTER TABLE snprc_scheduler.TimelineItem - ALTER COLUMN ProjectitemId DROP NOT NULL; - ---srr 05.29.19 -/*****************************************/ - -/****************************** -Added unique index to FK columns - to ensure no duplicate rows. - -srr 06.10.2019 -******************************/ - -CREATE UNIQUE INDEX idx_u_FK_to_TimelineItem ON snprc_scheduler.StudyDayNotes -( - TimelineObjectId ASC, - StudyDay ASC -); - -ALTER TABLE snprc_scheduler.StudyDayNotes - DROP COLUMN QcState; From ef55405cb6b963fba44117ed2ca38bedf5157f3b Mon Sep 17 00:00:00 2001 From: Adam Rauch Date: Fri, 12 Jun 2026 14:39:39 -0700 Subject: [PATCH 8/8] Bump to 26.000 to appease test --- .../src/org/labkey/snprc_scheduler/SNPRC_schedulerModule.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snprc_scheduler/src/org/labkey/snprc_scheduler/SNPRC_schedulerModule.java b/snprc_scheduler/src/org/labkey/snprc_scheduler/SNPRC_schedulerModule.java index 46e5e78d6..f9b539cd2 100644 --- a/snprc_scheduler/src/org/labkey/snprc_scheduler/SNPRC_schedulerModule.java +++ b/snprc_scheduler/src/org/labkey/snprc_scheduler/SNPRC_schedulerModule.java @@ -44,7 +44,7 @@ public String getName() @Override public @Nullable Double getSchemaVersion() { - return 25.000; + return 26.000; } @Override