-- SETUP/RESTORE USERS IF OTHER DATABASES ARE PRESENT

USE [VIGIL-Audit]
GO

IF EXISTS (SELECT * FROM sys.databases WHERE name=N'VIGIL-DVR')
BEGIN
	CREATE USER [vigil] FOR LOGIN [vigil]
	ALTER ROLE [db_owner] ADD MEMBER [vigil]

	EXEC [VIGIL-DVR]..sp_executesql N'IF EXISTS (SELECT * FROM sys.database_principals WHERE name = N''audit'') DROP USER [audit]'
	EXEC [VIGIL-DVR]..sp_executesql N'CREATE USER [audit] FOR LOGIN [audit]'
	EXEC [VIGIL-DVR]..sp_executesql N'ALTER ROLE [db_datareader] ADD MEMBER [audit]'
END
GO

IF EXISTS (SELECT * FROM sys.databases WHERE name=N'VIGIL-POS')
BEGIN
	EXEC [VIGIL-POS]..sp_executesql N'IF EXISTS (SELECT * FROM sys.database_principals WHERE name = N''audit'') DROP USER [audit]'
	EXEC [VIGIL-POS]..sp_executesql N'CREATE USER [audit] FOR LOGIN [audit]'
	EXEC [VIGIL-POS]..sp_executesql N'ALTER ROLE [db_datareader] ADD MEMBER [audit]'
END
GO


-- MISC. FINAL ACTIONS

USE [master]
GO

ALTER DATABASE [VIGIL-Audit] SET  READ_WRITE 
GO
