IF (NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'RevitServerModelPublic'))
BEGIN

	CREATE TABLE [dbo].[RevitServerModelPublished](
		[Id] [int] IDENTITY(1,1) NOT NULL,
		[ExternalId] [uniqueidentifier] NOT NULL,
		[RevitServerModelId] [int] NOT NULL,
		[RevitServerModelExternalId] [nvarchar](50) NULL,
		[ClarityModelId] [uniqueidentifier] NOT NULL,
		[ProjectId] [int] NOT NULL,
		[CreatedAtUtc] [datetime] NOT NULL
	) ON [PRIMARY]

	ALTER TABLE [dbo].[RevitServerModelPublished] ADD  CONSTRAINT [PK_RevitServerModelPublished] PRIMARY KEY CLUSTERED 
	(
		[Id] ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

	CREATE NONCLUSTERED INDEX [Index_RevitServerModelPublished_ProjectId] ON [dbo].[RevitServerModelPublished]
	(
		[ProjectId] ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

	ALTER TABLE [dbo].[RevitServerModelPublished] ADD  CONSTRAINT [DEFAULT_RevitServerModelPublished_ExteranlId]  DEFAULT (newsequentialid()) FOR [ExternalId]

	ALTER TABLE [dbo].[RevitServerModelPublished] ADD  CONSTRAINT [DEFAULT_RevitServerModelPublished_CreatedAtUtc]  DEFAULT (getutcdate()) FOR [CreatedAtUtc]

	ALTER TABLE [dbo].[RevitServerModelPublished]  WITH CHECK ADD  CONSTRAINT [FK_RevitServerModelPublished_RevitServerModels] FOREIGN KEY([RevitServerModelId])
	REFERENCES [dbo].[RevitServerModels] ([Id])
	ON DELETE CASCADE

	ALTER TABLE [dbo].[RevitServerModelPublished] CHECK CONSTRAINT [FK_RevitServerModelPublished_RevitServerModels]

END
