use AnP

if object_id(N'dbo.permissions_tables_remove', N'P') is not null drop procedure dbo.permissions_tables_remove
go
create procedure dbo.permissions_tables_remove as begin set nocount on 

    if object_id(N'dbo.PermissionsGroups', N'U') is not null execute sp_executesql N'alter table dbo.PermissionsGroups set (system_versioning = off)'
    if object_id(N'dbo.Permissions', N'U') is not null execute sp_executesql N'alter table dbo.Permissions set (system_versioning = off)'

    if object_id(N'dbo._PermissionsGroups', N'U') is not null drop table dbo._PermissionsGroups
    if object_id(N'dbo._Permissions', N'U') is not null drop table dbo._Permissions

    if object_id(N'dbo.PermissionsGroups', N'U') is not null drop table dbo.PermissionsGroups
    if object_id(N'dbo.Permissions', N'U') is not null drop table dbo.Permissions

end
go

if object_id(N'dbo.permissions_tables_create', N'P') is not null drop procedure dbo.permissions_tables_create
go
create procedure dbo.permissions_tables_create as begin set nocount on 

    if object_id(N'dbo._Permissions', N'U') is null create table dbo._Permissions(
        id integer not null identity(1, 1), 
        entity integer not null, 
        [procedure] integer not null, 
        [session] integer not null, 
        [name] varchar(64) not null, 
        [description] varchar(512) not null, 
        date_in datetime not null, 
        date_out datetime, 
        history_in datetime2 not null, 
        history_out datetime2 not null, 
        constraint pk__permissions primary key clustered(id), 
        index ix__permissions_history nonclustered(entity, history_out, history_in)
    )

    if object_id(N'dbo._PermissionsGroups', N'U') is null create table dbo._PermissionsGroups(
        id integer not null identity(1, 1), 
        entity integer not null, 
        [procedure] integer not null, 
        [session] integer not null, 
        permission integer not null, 
        [group] integer not null,
        belongs bit not null, 
        date_in datetime not null, 
        date_out datetime, 
        history_in datetime2 not null, 
        history_out datetime2 not null,
        constraint pk_permissions_groups primary key clustered(id), 
        index ix__permissions_groups_history nonclustered(entity, history_out, history_in)
    )

    if object_id(N'dbo.Permissions', N'U') is null begin 
        create table dbo.Permissions(
            id integer not null identity(1, 1), 
            [procedure] integer not null, 
            [session] integer not null, 
            [name] varchar(64) not null, 
            [description] varchar(512) not null, 
            date_in datetime not null constraint df_permissions_date_in default getdate(), 
            date_out datetime, 
            history_in datetime2 generated always as row start hidden not null constraint df_permissions_history_in default sysutcdatetime(), 
            history_out datetime2 generated always as row end hidden not null constraint df_permissions_history_out default convert(datetime2, '9999-12-31 23:59:59.9999999'),
            constraint pk_permissions primary key clustered(id), 
            constraint fk_permissions_procedure foreign key([procedure]) references dbo.Procedures(id) 
                on update no action 
                on delete no action,
            constraint fk_permissions_session foreign key([session]) references dbo.Sessions(id) 
                on update no action 
                on delete no action,
            constraint uq_permissions_name unique([name]) with(fillfactor = 90), 
            constraint ck_permissions_name check([name] != '' and [name] not like '%[^a-zA-Z0-9_]%' and [name] not like '[^a-zA-Z_]%'), 
            period for system_time(history_in, history_out)
        )
        execute sp_executesql N'alter table dbo.Permissions set (system_versioning = on (history_table = dbo._Permissions, data_consistency_check = on))'
    end

    if object_id(N'dbo.PermissionsGroups', N'U') is null begin 
        create table dbo._PermissionsGroups(
            id integer not null identity(1, 1), 
            [procedure] integer not null, 
            [session] integer not null, 
            permission integer not null, 
            [group] integer not null,
            belongs bit not null constraint df_permissions_groups_belongs default 1,
            date_in datetime not null constraint df_permissions_groups_date_in default getdate(), 
            date_out datetime, 
            history_in datetime2 generated always as row start hidden not null constraint df_permissions_groups_history_in default sysutcdatetime(), 
            history_out datetime2 generated always as row end hidden not null constraint df_permissions_groups_history_out default convert(datetime2, '9999-12-31 23:59:59.9999999'),
            constraint pk_permissions_groups primary key clustered(id), 
            constraint fk_permissions_groups_procedure foreign key([procedure]) references dbo.Procedures(id) 
                on update no action 
                on delete no action,
            constraint fk_permissions_groups_session foreign key([session]) references dbo.Sessions(id) 
                on update no action 
                on delete no action, 
            constraint fk_permissions_groups_permission foreign key(permission) references dbo.Permissions(id) 
                on update no action 
                on delete no action,
            constraint fk_permissions_groups_group foreign key([group]) references dbo.Users(id) 
                on update no action
                on delete no action, 
            constraint uq_permissions_groups unique(permission, [group]) with(fillfactor = 90), 
            period for system_time(history_in, history_out)
        )
        execute sp_executesql N'alter table dbo.PermissionsGroups set (system_versioning = on (history_table = dbo._PermissionsGroups, data_consistency_check = on))'
    end

end
go

execute dbo.permissions_tables_create