You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

schema-mssql.sql 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * Database schema required by \yii\rbac\DbManager.
  3. *
  4. * @author Qiang Xue <qiang.xue@gmail.com>
  5. * @author Alexander Kochetov <creocoder@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. * @since 2.0
  10. */
  11. drop table [auth_assignment];
  12. drop table [auth_item_child];
  13. drop table [auth_item];
  14. drop table [auth_rule];
  15. create table [auth_rule]
  16. (
  17. [name] varchar(64) not null,
  18. [data] text,
  19. [created_at] integer,
  20. [updated_at] integer,
  21. primary key ([name])
  22. );
  23. create table [auth_item]
  24. (
  25. [name] varchar(64) not null,
  26. [type] integer not null,
  27. [description] text,
  28. [rule_name] varchar(64),
  29. [data] text,
  30. [created_at] integer,
  31. [updated_at] integer,
  32. primary key ([name]),
  33. foreign key ([rule_name]) references [auth_rule] ([name])
  34. );
  35. create index [idx-auth_item-type] on [auth_item] ([type]);
  36. create table [auth_item_child]
  37. (
  38. [parent] varchar(64) not null,
  39. [child] varchar(64) not null,
  40. primary key ([parent],[child]),
  41. foreign key ([parent]) references [auth_item] ([name]),
  42. foreign key ([child]) references [auth_item] ([name])
  43. );
  44. create table [auth_assignment]
  45. (
  46. [item_name] varchar(64) not null,
  47. [user_id] varchar(64) not null,
  48. [created_at] integer,
  49. primary key ([item_name], [user_id]),
  50. foreign key ([item_name]) references [auth_item] ([name]) on delete cascade on update cascade
  51. );
  52. CREATE TRIGGER dbo.trigger_auth_item_child
  53. ON dbo.[auth_item]
  54. INSTEAD OF DELETE, UPDATE
  55. AS
  56. DECLARE @old_name VARCHAR (64) = (SELECT name FROM deleted)
  57. DECLARE @new_name VARCHAR (64) = (SELECT name FROM inserted)
  58. BEGIN
  59. IF COLUMNS_UPDATED() > 0
  60. BEGIN
  61. IF @old_name <> @new_name
  62. BEGIN
  63. ALTER TABLE auth_item_child NOCHECK CONSTRAINT FK__auth_item__child;
  64. UPDATE auth_item_child SET child = @new_name WHERE child = @old_name;
  65. END
  66. UPDATE auth_item
  67. SET name = (SELECT name FROM inserted),
  68. type = (SELECT type FROM inserted),
  69. description = (SELECT description FROM inserted),
  70. rule_name = (SELECT rule_name FROM inserted),
  71. data = (SELECT data FROM inserted),
  72. created_at = (SELECT created_at FROM inserted),
  73. updated_at = (SELECT updated_at FROM inserted)
  74. WHERE name IN (SELECT name FROM deleted)
  75. IF @old_name <> @new_name
  76. BEGIN
  77. ALTER TABLE auth_item_child CHECK CONSTRAINT FK__auth_item__child;
  78. END
  79. END
  80. ELSE
  81. BEGIN
  82. DELETE FROM dbo.[auth_item_child] WHERE parent IN (SELECT name FROM deleted) OR child IN (SELECT name FROM deleted);
  83. DELETE FROM dbo.[auth_item] WHERE name IN (SELECT name FROM deleted);
  84. END
  85. END;