init_prod.sql 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. -- Workflow Templates table
  110. CREATE TABLE IF NOT EXISTS workflow_templates (
  111. id INTEGER PRIMARY KEY AUTOINCREMENT,
  112. name TEXT NOT NULL UNIQUE,
  113. description TEXT,
  114. definition TEXT NOT NULL,
  115. is_active INTEGER DEFAULT 1,
  116. created_by_id INTEGER,
  117. created_at DATETIME,
  118. updated_at DATETIME
  119. );
  120. -- Workflow Instances table
  121. CREATE TABLE IF NOT EXISTS workflow_instances (
  122. id INTEGER PRIMARY KEY AUTOINCREMENT,
  123. workflow_template_id INTEGER NOT NULL,
  124. status TEXT NOT NULL,
  125. context TEXT,
  126. service_id INTEGER,
  127. created_at DATETIME,
  128. updated_at DATETIME,
  129. completed_at DATETIME
  130. );
  131. -- Workflow Nodes table
  132. CREATE TABLE IF NOT EXISTS workflow_nodes (
  133. id INTEGER PRIMARY KEY AUTOINCREMENT,
  134. workflow_instance_id INTEGER NOT NULL,
  135. node_key TEXT NOT NULL,
  136. node_type TEXT NOT NULL,
  137. status TEXT NOT NULL,
  138. task_id INTEGER,
  139. retry_count INTEGER DEFAULT 0,
  140. input_data TEXT,
  141. output_data TEXT,
  142. created_at DATETIME,
  143. updated_at DATETIME,
  144. started_at DATETIME,
  145. completed_at DATETIME
  146. );
  147. -- Workflow Edges table
  148. CREATE TABLE IF NOT EXISTS workflow_edges (
  149. id INTEGER PRIMARY KEY AUTOINCREMENT,
  150. workflow_instance_id INTEGER NOT NULL,
  151. from_node_id INTEGER NOT NULL,
  152. to_node_id INTEGER NOT NULL,
  153. condition TEXT
  154. );
  155. -- ============================================
  156. -- INSERT STATEMENTS
  157. -- ============================================
  158. -- Permissions
  159. INSERT INTO permissions (id, code, description) VALUES
  160. (1, 'user:create', 'Create users'),
  161. (2, 'user:read', 'Read users'),
  162. (3, 'user:update', 'Update users'),
  163. (4, 'user:delete', 'Delete users'),
  164. (5, 'role:create', 'Create roles'),
  165. (6, 'role:read', 'Read roles'),
  166. (7, 'role:update', 'Update roles'),
  167. (8, 'role:delete', 'Delete roles'),
  168. (9, 'permission:create', 'Create permissions'),
  169. (10, 'permission:read', 'Read permissions'),
  170. (11, 'permission:update', 'Update permissions'),
  171. (12, 'permission:delete', 'Delete permissions'),
  172. (13, 'service:create', 'Create services'),
  173. (14, 'service:read', 'Read services'),
  174. (15, 'service:update', 'Update services'),
  175. (16, 'service:delete', 'Delete services'),
  176. (17, 'task:create', 'Create tasks'),
  177. (18, 'task:read', 'Read tasks'),
  178. (19, 'task:update', 'Update tasks'),
  179. (20, 'task:delete', 'Delete tasks'),
  180. (21, 'note:create', 'Create notes'),
  181. (22, 'note:read', 'Read notes'),
  182. (23, 'note:update', 'Update notes'),
  183. (24, 'note:delete', 'Delete notes'),
  184. (25, 'channel:create', 'Create channels'),
  185. (26, 'channel:read', 'Read channels'),
  186. (27, 'channel:update', 'Update channels'),
  187. (28, 'channel:delete', 'Delete channels'),
  188. (29, 'message:create', 'Create messages'),
  189. (30, 'message:read', 'Read messages'),
  190. (31, 'message:update', 'Update messages'),
  191. (32, 'message:delete', 'Delete messages'),
  192. (33, 'taskstatus:create', 'Create task statuses'),
  193. (34, 'taskstatus:read', 'Read task statuses'),
  194. (35, 'taskstatus:update', 'Update task statuses'),
  195. (36, 'taskstatus:delete', 'Delete task statuses'),
  196. (37, 'workflow:create', 'Create workflow templates'),
  197. (38, 'workflow:start', 'Start workflow instances'),
  198. (39, 'workflow:manage', 'Manage workflow templates'),
  199. (40, 'workflow:intervene', 'Manual intervention for failed workflow nodes'),
  200. (41, 'workflow:view', 'View workflows and instances');
  201. -- Roles
  202. INSERT INTO roles (id, name, description) VALUES
  203. (1, 'admin', 'Administrator with full access'),
  204. (2, 'manager', 'Service manager with task management'),
  205. (3, 'user', 'Regular user with limited access');
  206. -- Role-Permission associations (admin gets all permissions)
  207. INSERT INTO role_permissions (role_id, permission_id)
  208. SELECT 1, id FROM permissions;
  209. -- Manager role permissions (service, task, note operations)
  210. INSERT INTO role_permissions (role_id, permission_id) VALUES
  211. (2, 13), (2, 14), (2, 15), (2, 16), -- service:*
  212. (2, 17), (2, 18), (2, 19), (2, 20), -- task:*
  213. (2, 21), (2, 22), (2, 23), (2, 24), -- note:*
  214. (2, 25), (2, 26), (2, 27), (2, 28), -- channel:*
  215. (2, 29), (2, 30), (2, 31), (2, 32), -- message:*
  216. (2, 33), (2, 34), (2, 35), (2, 36); -- taskstatus:*
  217. -- Manager role workflow permissions (idempotent for existing databases)
  218. INSERT OR IGNORE INTO role_permissions (role_id, permission_id) VALUES
  219. (2, 37), (2, 38), (2, 39), (2, 40), (2, 41); -- workflow:*
  220. -- User role permissions (read-only + create notes/messages)
  221. INSERT INTO role_permissions (role_id, permission_id) VALUES
  222. (3, 2), (3, 6), (3, 10), (3, 14), (3, 18), (3, 22), (3, 26), (3, 30), (3, 34), -- read permissions
  223. (3, 21), (3, 29); -- create notes and messages
  224. -- User role workflow permissions (idempotent for existing databases)
  225. INSERT OR IGNORE INTO role_permissions (role_id, permission_id) VALUES
  226. (3, 38), (3, 41); -- workflow:start, workflow:view
  227. -- Admin user (password: secret123)
  228. -- bcrypt hash generated with cost 10
  229. INSERT INTO users (id, email, password, created_at, updated_at) VALUES
  230. (1, 'admin@example.com', '$2a$10$9CNePaChncemsl8ZgMFDfeFm.Rl1K1l8rurgZxVx7C6sbv5tojUDC', datetime('now'), datetime('now'));
  231. -- Associate admin user with admin role
  232. INSERT INTO user_roles (user_id, role_id) VALUES (1, 1);
  233. -- Task Statuses (common workflow states)
  234. INSERT INTO task_statuses (id, code, label, created_at, updated_at) VALUES
  235. (1, 'open', 'Open', datetime('now'), datetime('now')),
  236. (2, 'in_progress', 'In Progress', datetime('now'), datetime('now')),
  237. (3, 'blocked', 'Blocked', datetime('now'), datetime('now')),
  238. (4, 'review', 'In Review', datetime('now'), datetime('now')),
  239. (5, 'done', 'Done', datetime('now'), datetime('now')),
  240. (6, 'cancelled', 'Cancelled', datetime('now'), datetime('now'));