Last active
September 22, 2018 05:41
-
-
Save marufmax/53e133c1ae694a8b9cdd0eb213a1c761 to your computer and use it in GitHub Desktop.
Laravel package Entrust Migration in SQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Creating Roles Table | |
-- Create table for storing roles | |
create table roles ( id number(10,0) not null, name varchar2(255) | |
not null, display_name varchar2(255) null, description varchar2(255) null, created_at timestamp null, updated_at timestamp null, | |
constraint roles_id_pk primary key ( id ) ) | |
alter table roles add constraint roles_name_uk unique ( name ) | |
create sequence roles_id_seq start with 1 | |
create trigger roles_id_trg | |
before insert on roles | |
for each row | |
begin | |
if :new.id is null then | |
select roles_id_seq.nextval into :new.id from dual; | |
end if; | |
end; | |
-- Roles and User Relation Table | |
-- Create table for associating roles to users (Many-to-Many) | |
create table role_user ( user_id number(10,0) | |
not null, role_id number(10,0) | |
not null, constraint role_user_user_id_fk foreign key ( user_id ) | |
references SCR_APP_USER_INFO ( user_id ) | |
on delete cascade, constraint role_user_role_id_fk foreign key ( role_id ) references roles ( id ) | |
on delete cascade, constraint role_user_user_id_role_id_pk primary key ( user_id, role_id ) ) | |
-- Creating Permissions Table | |
-- Create table for storing permissions | |
create table permissions ( id number(10,0) not null, | |
name varchar2(255) not null, | |
display_name varchar2(255) null, | |
description varchar2(255) null, | |
created_at timestamp null, | |
updated_at timestamp null, | |
constraint permissions_id_pk primary key ( id ) ) | |
alter table permissions add constraint permissions_name_uk unique ( name ) | |
create sequence permissions_id_seq start with 1 | |
create trigger permissions_id_trg | |
before insert on permissions | |
for each row | |
begin | |
if :new.id is null then | |
select permissions_id_seq.nextval into :new.id from dual; | |
end if; | |
end; | |
-- Persmission and Role Relation Table | |
-- Create table for associating permissions to roles (Many-to-Many) | |
create table permission_role ( permission_id number(10,0) | |
not null, role_id number(10,0) | |
not null, constraint permission_role_permission_id_ foreign key ( permission_id ) | |
references permissions ( id ) | |
on delete cascade, constraint permission_role_role_id_fk foreign key ( role_id ) | |
references roles ( id ) | |
on delete cascade, | |
constraint permission_role_permission_id_ primary key ( permission_id, role_id ) ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment