use Envisage
GO

set xact_abort on;
go

begin transaction;
go
if not exists (select 1 from sys.all_columns where name like 'ParentProjectId' and object_id = OBJECT_ID('Project'))
begin
	ALTER TABLE [Project]
		ADD ParentProjectId [uniqueidentifier] NULL

	ALTER TABLE [dbo].[Project]  WITH CHECK ADD  CONSTRAINT [FK_Project_Project] FOREIGN KEY([ParentProjectId])
	REFERENCES [dbo].[Project] ([Id])

	ALTER TABLE [dbo].[Project] CHECK CONSTRAINT [FK_Project_Project]

end
go

if not exists (select 1 from sys.all_columns where name like 'HasChildren' and object_id = OBJECT_ID('Project'))
begin
	ALTER TABLE [Project] ADD [HasChildren] [bit] NULL CONSTRAINT [DF_Project_HasChildren]  DEFAULT (0)
end
go
	
UPDATE [Project] SET [HasChildren] = 0 WHERE [HasChildren] IS NULL
go

ALTER TABLE [Project] ALTER COLUMN [HasChildren] [bit] NOT NULL

commit
go


