Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion DeploymentComponents/azure_sql_artifacts/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
# Example Control Table Records
# Azure SQL Artifacts

## Deployment

All SQL table creation scripts in the `tables/` directory are idempotent and can be safely run multiple times. This allows for:
- Re-running deployments without errors
- Updating existing databases with new schema changes
- Supporting continuous deployment pipelines

The scripts use `IF NOT EXISTS` checks before creating tables and adding constraints to ensure they don't fail when objects already exist in the database.

**Note:** Stored procedures use `CREATE OR ALTER` syntax which is inherently idempotent.

## Example Control Table Records

## Important Notes

Expand Down
15 changes: 12 additions & 3 deletions DeploymentComponents/azure_sql_artifacts/tables/ControlTable.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ GO
SET QUOTED_IDENTIFIER ON
GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ControlTable]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[ControlTable](
[Id] [int] IDENTITY(1,1) NOT NULL,
[SourceObjectSettings] [nvarchar](max) NULL,
Expand Down Expand Up @@ -35,15 +37,22 @@ WITH
(
SYSTEM_VERSIONING = ON (HISTORY_TABLE = [dbo].[ControlTableHistory])
)
GO

IF NOT EXISTS (SELECT * FROM sys.default_constraints WHERE name = 'DF_ControlTable_PurviewScanEnabled' AND parent_object_id = OBJECT_ID('[dbo].[ControlTable]'))
BEGIN
ALTER TABLE [dbo].[ControlTable] ADD CONSTRAINT [DF_ControlTable_PurviewScanEnabled] DEFAULT ((1)) FOR [PurviewScanEnabled]
GO
END

IF NOT EXISTS (SELECT * FROM sys.default_constraints WHERE name = 'DF_ControlTable_ValidFrom' AND parent_object_id = OBJECT_ID('[dbo].[ControlTable]'))
BEGIN
ALTER TABLE [dbo].[ControlTable] ADD CONSTRAINT [DF_ControlTable_ValidFrom] DEFAULT (sysutcdatetime()) FOR [ValidFrom]
GO
END

IF NOT EXISTS (SELECT * FROM sys.default_constraints WHERE name = 'DF_ControlTable_ValidTo' AND parent_object_id = OBJECT_ID('[dbo].[ControlTable]'))
BEGIN
ALTER TABLE [dbo].[ControlTable] ADD CONSTRAINT [DF_ControlTable_ValidTo] DEFAULT (CONVERT([datetime2],'9999-12-31 23:59:59.9999999')) FOR [ValidTo]
END
END
GO

DECLARE @controlTableRecords INTEGER
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DataContract]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[DataContract](
[ID] [int] IDENTITY(1,1) NOT NULL,
[ContractID] [nvarchar](255) NOT NULL,
Expand Down Expand Up @@ -49,9 +52,13 @@ CREATE TABLE [dbo].[DataContract](
[DataAssetTechnicalInformation] [nvarchar](max) NULL,
[hsActive] [bit] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

IF NOT EXISTS (SELECT * FROM sys.key_constraints WHERE name = 'PK__DataCont__3214EC27' AND parent_object_id = OBJECT_ID('[dbo].[DataContract]'))
BEGIN
ALTER TABLE [dbo].[DataContract] ADD PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
END
END
GO
15 changes: 12 additions & 3 deletions DeploymentComponents/azure_sql_artifacts/tables/DataMapping.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ GO
SET QUOTED_IDENTIFIER ON
GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DataMapping]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[DataMapping](
[id] [int] IDENTITY(1,1) NOT NULL,
[TimeStamp] [datetime2](7) NOT NULL,
Expand Down Expand Up @@ -38,14 +40,21 @@ CREATE TABLE [dbo].[DataMapping](
[id] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

IF NOT EXISTS (SELECT * FROM sys.default_constraints WHERE name = 'DF__DataMappi__activ__default' AND parent_object_id = OBJECT_ID('[dbo].[DataMapping]'))
BEGIN
ALTER TABLE [dbo].[DataMapping] ADD DEFAULT (NULL) FOR [active]
GO
END

IF NOT EXISTS (SELECT * FROM sys.default_constraints WHERE name = 'DF__DataMappi__activ__date1' AND parent_object_id = OBJECT_ID('[dbo].[DataMapping]'))
BEGIN
ALTER TABLE [dbo].[DataMapping] ADD DEFAULT (getdate()) FOR [activeDate]
GO
END

IF NOT EXISTS (SELECT * FROM sys.default_constraints WHERE name = 'DF__DataMappi__inact__date' AND parent_object_id = OBJECT_ID('[dbo].[DataMapping]'))
BEGIN
ALTER TABLE [dbo].[DataMapping] ADD DEFAULT (getdate()) FOR [inactiveDate]
END
END
GO

3 changes: 3 additions & 0 deletions DeploymentComponents/azure_sql_artifacts/tables/Handshake.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ GO
SET QUOTED_IDENTIFIER ON
GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Handshake]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Handshake](
[Id] [int] IDENTITY(1,1) NOT NULL,
[DataContractID] [nvarchar](200) NOT NULL,
Expand All @@ -33,4 +35,5 @@ PRIMARY KEY CLUSTERED
[Id] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
END
GO
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[IgnoredLandedFiles]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[IgnoredLandedFiles](
[filename] [nvarchar](255) NULL,
[folderpath] [nvarchar](255) NULL,
[condition] [nvarchar](255) NULL,
[reason] [nvarchar](1000) NULL
) ON [PRIMARY]
END
GO
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[IngestedLandingDataAudit]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[IngestedLandingDataAudit](
[id] [int] IDENTITY(1,1) NOT NULL,
[source_filename] [nvarchar](255) NULL,
Expand Down Expand Up @@ -33,10 +36,15 @@ WITH
(
SYSTEM_VERSIONING = ON (HISTORY_TABLE = [dbo].[IngestedLandingDataAuditHistory])
)
GO

IF NOT EXISTS (SELECT * FROM sys.default_constraints WHERE name = 'DF_IngestedLandingDataAudit_ValidFrom' AND parent_object_id = OBJECT_ID('[dbo].[IngestedLandingDataAudit]'))
BEGIN
ALTER TABLE [dbo].[IngestedLandingDataAudit] ADD CONSTRAINT [DF_IngestedLandingDataAudit_ValidFrom] DEFAULT (sysutcdatetime()) FOR [ValidFrom]
GO
END

IF NOT EXISTS (SELECT * FROM sys.default_constraints WHERE name = 'DF_IngestedLandingDataAudit_ValidTo' AND parent_object_id = OBJECT_ID('[dbo].[IngestedLandingDataAudit]'))
BEGIN
ALTER TABLE [dbo].[IngestedLandingDataAudit] ADD CONSTRAINT [DF_IngestedLandingDataAudit_ValidTo] DEFAULT (CONVERT([datetime2],'9999-12-31 23:59:59.9999999')) FOR [ValidTo]
END
END
GO
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ GO
SET QUOTED_IDENTIFIER ON
GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[IngestedSourceDataAudit]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[IngestedSourceDataAudit](
[id] [int] IDENTITY(1,1) NOT NULL,
[source] [nvarchar](max) NULL,
Expand All @@ -33,10 +35,15 @@ WITH
(
SYSTEM_VERSIONING = ON (HISTORY_TABLE = [dbo].[IngestedSourceDataAuditHistory])
)
GO

IF NOT EXISTS (SELECT * FROM sys.default_constraints WHERE name = 'DF_IngestedSourceDataAudit_ValidFrom' AND parent_object_id = OBJECT_ID('[dbo].[IngestedSourceDataAudit]'))
BEGIN
ALTER TABLE [dbo].[IngestedSourceDataAudit] ADD CONSTRAINT [DF_IngestedSourceDataAudit_ValidFrom] DEFAULT (sysutcdatetime()) FOR [ValidFrom]
GO
END

IF NOT EXISTS (SELECT * FROM sys.default_constraints WHERE name = 'DF_IngestedSourceDataAudit_ValidTo' AND parent_object_id = OBJECT_ID('[dbo].[IngestedSourceDataAudit]'))
BEGIN
ALTER TABLE [dbo].[IngestedSourceDataAudit] ADD CONSTRAINT [DF_IngestedSourceDataAudit_ValidTo] DEFAULT (CONVERT([datetime2],'9999-12-31 23:59:59.9999999')) FOR [ValidTo]
END
END
GO
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
Licensed under the MIT license. */


IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[IngestionServiceAudit]') AND type in (N'U'))
BEGIN
CREATE TABLE dbo.IngestionServiceAudit (
Id INT IDENTITY(1,1)
,LogDateTime DATETIME DEFAULT GETDATE()
,HttpRequest NVARCHAR(MAX)
,AuditMessage NVARCHAR(255)
,MetaDataInserted BIT
);
END
GO

Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
CREATE TABLE [dbo].[ControlTable] (
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ControlTable]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[ControlTable] (
[id] [int] IDENTITY(1,1) NOT NULL,
[source_id] [int] NULL,
[raw_filename] [nvarchar](255) NULL,
Expand All @@ -8,4 +10,5 @@
[processed_datetime_utc] [datetime2](7) NULL
PRIMARY KEY CLUSTERED ([Id] ASC)
);
END
GO
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[IngestedLandingDataAudit]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[IngestedLandingDataAudit](
[id] [int] NULL,
[source_filename] [nvarchar](255) NULL,
Expand All @@ -16,4 +19,5 @@ CREATE TABLE [dbo].[IngestedLandingDataAudit](
[control_table_record_id] int,
[pipeline_id] [nvarchar](255) NULL
) ON [PRIMARY]
END
GO
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
GO
SET QUOTED_IDENTIFIER ON
GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ControlTable]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[ControlTable] (
[id] [int] IDENTITY(1,1) NOT NULL,
[source_id] [int] NULL,
Expand All @@ -12,4 +15,5 @@ CREATE TABLE [dbo].[ControlTable] (
[processed_datetime_utc] [datetime2](7) NULL
PRIMARY KEY CLUSTERED ([Id] ASC)
);
END
GO
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ GO
SET QUOTED_IDENTIFIER ON
GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DataMapping]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[DataMapping](
[id] [int] IDENTITY(1,1) NOT NULL,
[TimeStamp] [datetime2](7) NOT NULL,
Expand Down Expand Up @@ -33,13 +35,20 @@ CREATE TABLE [dbo].[DataMapping](
[id] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

IF NOT EXISTS (SELECT * FROM sys.default_constraints WHERE name = 'DF__DataMappi__activ__default' AND parent_object_id = OBJECT_ID('[dbo].[DataMapping]'))
BEGIN
ALTER TABLE [dbo].[DataMapping] ADD DEFAULT (NULL) FOR [active]
GO
END

IF NOT EXISTS (SELECT * FROM sys.default_constraints WHERE name = 'DF__DataMappi__activ__date1' AND parent_object_id = OBJECT_ID('[dbo].[DataMapping]'))
BEGIN
ALTER TABLE [dbo].[DataMapping] ADD DEFAULT (getdate()) FOR [activeDate]
GO
END

IF NOT EXISTS (SELECT * FROM sys.default_constraints WHERE name = 'DF__DataMappi__inact__date' AND parent_object_id = OBJECT_ID('[dbo].[DataMapping]'))
BEGIN
ALTER TABLE [dbo].[DataMapping] ADD DEFAULT (getdate()) FOR [inactiveDate]
END
END
GO
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[IngestedLandingDataAudit]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[IngestedLandingDataAudit](
[id] [int] NULL,
[source_filename] [nvarchar](255) NULL,
Expand All @@ -16,4 +19,5 @@ CREATE TABLE [dbo].[IngestedLandingDataAudit](
[control_table_record_id] int,
[pipeline_id] [nvarchar](255) NULL
) ON [PRIMARY]
END
GO
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
CREATE TABLE [dbo].[ControlTable] (
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ControlTable]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[ControlTable] (
[id] [int] IDENTITY(1,1) NOT NULL,
[source_id] [int] NULL,
[raw_filename] [nvarchar](255) NULL,
Expand All @@ -8,4 +10,5 @@
[processed_datetime_utc] [datetime2](7) NULL
PRIMARY KEY CLUSTERED ([Id] ASC)
);
END
GO
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ GO
SET QUOTED_IDENTIFIER ON
GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DataMapping]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[DataMapping](
[id] [int] IDENTITY(1,1) NOT NULL,
[TimeStamp] [datetime2](7) NOT NULL,
Expand All @@ -28,4 +30,5 @@ CREATE TABLE [dbo].[DataMapping](
[id] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
END
GO
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[IngestedLandingDataAudit]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[IngestedLandingDataAudit](
[id] [int] NULL,
[source_filename] [nvarchar](255) NULL,
Expand All @@ -16,4 +19,5 @@ CREATE TABLE [dbo].[IngestedLandingDataAudit](
[control_table_record_id] int,
[pipeline_id] [nvarchar](255) NULL
) ON [PRIMARY]
END
GO
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
CREATE TABLE [dbo].[ControlTable] (
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ControlTable]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[ControlTable] (
[id] [int] IDENTITY(1,1) NOT NULL,
[source_id] [int] NULL,
[raw_filename] [nvarchar](255) NULL,
Expand All @@ -8,4 +10,5 @@
[processed_datetime_utc] [datetime2](7) NULL
PRIMARY KEY CLUSTERED ([Id] ASC)
);
END
GO
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[IngestedLandingDataAudit]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[IngestedLandingDataAudit](
[id] [int] NULL,
[source_filename] [nvarchar](255) NULL,
Expand All @@ -16,4 +19,5 @@ CREATE TABLE [dbo].[IngestedLandingDataAudit](
[control_table_record_id] int,
[pipeline_id] [nvarchar](255) NULL
) ON [PRIMARY]
END
GO