init_prod.sql 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. -- ARP Initial Data Bootstrap Script
  2. -- Run this script to set up initial permissions, roles, and an admin user
  3. --
  4. -- Note: The password hash below is for "secret123" using bcrypt.
  5. -- Enable foreign keys
  6. PRAGMA foreign_keys = ON;
  7. -- Permissions table (no created_at/updated_at in model)
  8. CREATE TABLE IF NOT EXISTS permissions (
  9. id INTEGER PRIMARY KEY AUTOINCREMENT,
  10. code TEXT NOT NULL UNIQUE,
  11. description TEXT
  12. );
  13. -- Roles table (no created_at/updated_at in model)
  14. CREATE TABLE IF NOT EXISTS roles (
  15. id INTEGER PRIMARY KEY AUTOINCREMENT,
  16. name TEXT NOT NULL UNIQUE,
  17. description TEXT
  18. );
  19. -- Role-Permission join table
  20. CREATE TABLE IF NOT EXISTS role_permissions (
  21. role_id INTEGER NOT NULL,
  22. permission_id INTEGER NOT NULL,
  23. PRIMARY KEY (role_id, permission_id)
  24. );
  25. -- Users table
  26. CREATE TABLE IF NOT EXISTS users (
  27. id INTEGER PRIMARY KEY AUTOINCREMENT,
  28. email TEXT NOT NULL UNIQUE,
  29. password TEXT NOT NULL,
  30. created_at DATETIME,
  31. updated_at DATETIME
  32. );
  33. -- User-Role join table
  34. CREATE TABLE IF NOT EXISTS user_roles (
  35. user_id INTEGER NOT NULL,
  36. role_id INTEGER NOT NULL,
  37. PRIMARY KEY (user_id, role_id)
  38. );
  39. -- Task Statuses table
  40. CREATE TABLE IF NOT EXISTS task_statuses (
  41. id INTEGER PRIMARY KEY AUTOINCREMENT,
  42. code TEXT NOT NULL UNIQUE,
  43. label TEXT,
  44. created_at DATETIME,
  45. updated_at DATETIME
  46. );
  47. -- Services table
  48. CREATE TABLE IF NOT EXISTS services (
  49. id INTEGER PRIMARY KEY AUTOINCREMENT,
  50. name TEXT NOT NULL,
  51. description TEXT,
  52. created_by_id INTEGER,
  53. created_at DATETIME,
  54. updated_at DATETIME
  55. );
  56. -- Service Participants join table
  57. CREATE TABLE IF NOT EXISTS service_participants (
  58. service_id INTEGER NOT NULL,
  59. user_id INTEGER NOT NULL,
  60. PRIMARY KEY (service_id, user_id)
  61. );
  62. -- Tasks table
  63. CREATE TABLE IF NOT EXISTS tasks (
  64. id INTEGER PRIMARY KEY AUTOINCREMENT,
  65. title TEXT NOT NULL,
  66. content TEXT NOT NULL,
  67. service_id INTEGER,
  68. created_by_id INTEGER NOT NULL,
  69. updated_by_id INTEGER NOT NULL,
  70. assignee_id INTEGER,
  71. status_id INTEGER,
  72. due_date DATETIME,
  73. priority TEXT,
  74. created_at DATETIME,
  75. updated_at DATETIME
  76. );
  77. -- Notes table
  78. CREATE TABLE IF NOT EXISTS notes (
  79. id INTEGER PRIMARY KEY AUTOINCREMENT,
  80. title TEXT NOT NULL,
  81. content TEXT NOT NULL,
  82. user_id INTEGER,
  83. service_id INTEGER,
  84. created_at DATETIME,
  85. updated_at DATETIME
  86. );
  87. -- Channels table
  88. CREATE TABLE IF NOT EXISTS channels (
  89. id INTEGER PRIMARY KEY AUTOINCREMENT,
  90. title TEXT NOT NULL,
  91. created_at DATETIME,
  92. updated_at DATETIME
  93. );
  94. -- Conversation Participants join table
  95. CREATE TABLE IF NOT EXISTS conversation_participants (
  96. channel_id INTEGER NOT NULL,
  97. user_id INTEGER NOT NULL,
  98. PRIMARY KEY (channel_id, user_id)
  99. );
  100. -- Messages table
  101. CREATE TABLE IF NOT EXISTS messages (
  102. id INTEGER PRIMARY KEY AUTOINCREMENT,
  103. sender_id INTEGER NOT NULL,
  104. content TEXT NOT NULL,
  105. sent_at DATETIME,
  106. created_at DATETIME,
  107. updated_at DATETIME
  108. );
  109. -- ============================================
  110. -- INSERT STATEMENTS
  111. -- ============================================
  112. -- Permissions
  113. INSERT INTO permissions (id, code, description) VALUES
  114. (1, 'user:create', 'Create users'),
  115. (2, 'user:read', 'Read users'),
  116. (3, 'user:update', 'Update users'),
  117. (4, 'user:delete', 'Delete users'),
  118. (5, 'role:create', 'Create roles'),
  119. (6, 'role:read', 'Read roles'),
  120. (7, 'role:update', 'Update roles'),
  121. (8, 'role:delete', 'Delete roles'),
  122. (9, 'permission:create', 'Create permissions'),
  123. (10, 'permission:read', 'Read permissions'),
  124. (11, 'permission:update', 'Update permissions'),
  125. (12, 'permission:delete', 'Delete permissions'),
  126. (13, 'service:create', 'Create services'),
  127. (14, 'service:read', 'Read services'),
  128. (15, 'service:update', 'Update services'),
  129. (16, 'service:delete', 'Delete services'),
  130. (17, 'task:create', 'Create tasks'),
  131. (18, 'task:read', 'Read tasks'),
  132. (19, 'task:update', 'Update tasks'),
  133. (20, 'task:delete', 'Delete tasks'),
  134. (21, 'note:create', 'Create notes'),
  135. (22, 'note:read', 'Read notes'),
  136. (23, 'note:update', 'Update notes'),
  137. (24, 'note:delete', 'Delete notes'),
  138. (25, 'channel:create', 'Create channels'),
  139. (26, 'channel:read', 'Read channels'),
  140. (27, 'channel:update', 'Update channels'),
  141. (28, 'channel:delete', 'Delete channels'),
  142. (29, 'message:create', 'Create messages'),
  143. (30, 'message:read', 'Read messages'),
  144. (31, 'message:update', 'Update messages'),
  145. (32, 'message:delete', 'Delete messages'),
  146. (33, 'taskstatus:create', 'Create task statuses'),
  147. (34, 'taskstatus:read', 'Read task statuses'),
  148. (35, 'taskstatus:update', 'Update task statuses'),
  149. (36, 'taskstatus:delete', 'Delete task statuses');
  150. -- Roles
  151. INSERT INTO roles (id, name, description) VALUES
  152. (1, 'admin', 'Administrator with full access'),
  153. (2, 'manager', 'Service manager with task management'),
  154. (3, 'user', 'Regular user with limited access');
  155. -- Role-Permission associations (admin gets all permissions)
  156. INSERT INTO role_permissions (role_id, permission_id)
  157. SELECT 1, id FROM permissions;
  158. -- Manager role permissions (service, task, note operations)
  159. INSERT INTO role_permissions (role_id, permission_id) VALUES
  160. (2, 13), (2, 14), (2, 15), (2, 16), -- service:*
  161. (2, 17), (2, 18), (2, 19), (2, 20), -- task:*
  162. (2, 21), (2, 22), (2, 23), (2, 24), -- note:*
  163. (2, 25), (2, 26), (2, 27), (2, 28), -- channel:*
  164. (2, 29), (2, 30), (2, 31), (2, 32), -- message:*
  165. (2, 33), (2, 34), (2, 35), (2, 36); -- taskstatus:*
  166. -- User role permissions (read-only + create notes/messages)
  167. INSERT INTO role_permissions (role_id, permission_id) VALUES
  168. (3, 2), (3, 6), (3, 10), (3, 14), (3, 18), (3, 22), (3, 26), (3, 30), (3, 34), -- read permissions
  169. (3, 21), (3, 29); -- create notes and messages
  170. -- Admin user (password: secret123)
  171. -- bcrypt hash generated with cost 10
  172. INSERT INTO users (id, email, password, created_at, updated_at) VALUES
  173. (1, 'admin@example.com', '$2a$10$9CNePaChncemsl8ZgMFDfeFm.Rl1K1l8rurgZxVx7C6sbv5tojUDC', datetime('now'), datetime('now'));
  174. -- Associate admin user with admin role
  175. INSERT INTO user_roles (user_id, role_id) VALUES (1, 1);
  176. -- Task Statuses (common workflow states)
  177. INSERT INTO task_statuses (id, code, label, created_at, updated_at) VALUES
  178. (1, 'open', 'Open', datetime('now'), datetime('now')),
  179. (2, 'in_progress', 'In Progress', datetime('now'), datetime('now')),
  180. (3, 'blocked', 'Blocked', datetime('now'), datetime('now')),
  181. (4, 'review', 'In Review', datetime('now'), datetime('now')),
  182. (5, 'done', 'Done', datetime('now'), datetime('now')),
  183. (6, 'cancelled', 'Cancelled', datetime('now'), datetime('now'));