-- ARP Initial Data Bootstrap Script -- Run this script to set up initial permissions, roles, and an admin user -- -- Note: The password hash below is for "secret123" using bcrypt. -- Enable foreign keys PRAGMA foreign_keys = ON; -- Permissions table (no created_at/updated_at in model) CREATE TABLE IF NOT EXISTS permissions ( id INTEGER PRIMARY KEY AUTOINCREMENT, code TEXT NOT NULL UNIQUE, description TEXT ); -- Roles table (no created_at/updated_at in model) CREATE TABLE IF NOT EXISTS roles ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL UNIQUE, description TEXT ); -- Role-Permission join table CREATE TABLE IF NOT EXISTS role_permissions ( role_id INTEGER NOT NULL, permission_id INTEGER NOT NULL, PRIMARY KEY (role_id, permission_id), FOREIGN KEY (role_id) REFERENCES roles(id) ON DELETE CASCADE, FOREIGN KEY (permission_id) REFERENCES permissions(id) ON DELETE CASCADE ); -- Users table CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY AUTOINCREMENT, email TEXT NOT NULL UNIQUE, password TEXT NOT NULL, created_at DATETIME, updated_at DATETIME ); -- User-Role join table CREATE TABLE IF NOT EXISTS user_roles ( user_id INTEGER NOT NULL, role_id INTEGER NOT NULL, PRIMARY KEY (user_id, role_id), FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE, FOREIGN KEY (role_id) REFERENCES roles(id) ON DELETE CASCADE ); -- Task Statuses table CREATE TABLE IF NOT EXISTS task_statuses ( id INTEGER PRIMARY KEY AUTOINCREMENT, code TEXT NOT NULL UNIQUE, label TEXT, created_at DATETIME, updated_at DATETIME ); -- Services table CREATE TABLE IF NOT EXISTS services ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, description TEXT, created_by_id INTEGER, created_at DATETIME, updated_at DATETIME, FOREIGN KEY (created_by_id) REFERENCES users(id) ); -- Service Participants join table CREATE TABLE IF NOT EXISTS service_participants ( service_id INTEGER NOT NULL, user_id INTEGER NOT NULL, PRIMARY KEY (service_id, user_id), FOREIGN KEY (service_id) REFERENCES services(id) ON DELETE CASCADE, FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE ); -- Tasks table CREATE TABLE IF NOT EXISTS tasks ( id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, content TEXT NOT NULL, service_id INTEGER, created_by_id INTEGER NOT NULL, updated_by_id INTEGER NOT NULL, assignee_id INTEGER, status_id INTEGER, due_date DATETIME, priority TEXT, created_at DATETIME, updated_at DATETIME, FOREIGN KEY (service_id) REFERENCES services(id), FOREIGN KEY (created_by_id) REFERENCES users(id), FOREIGN KEY (updated_by_id) REFERENCES users(id), FOREIGN KEY (assignee_id) REFERENCES users(id), FOREIGN KEY (status_id) REFERENCES task_statuses(id) ON UPDATE CASCADE ON DELETE SET NULL ); -- Notes table CREATE TABLE IF NOT EXISTS notes ( id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, content TEXT NOT NULL, user_id INTEGER, service_id INTEGER, created_at DATETIME, updated_at DATETIME, FOREIGN KEY (user_id) REFERENCES users(id), FOREIGN KEY (service_id) REFERENCES services(id) ); -- Channels table CREATE TABLE IF NOT EXISTS channels ( id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, created_at DATETIME, updated_at DATETIME ); -- Conversation Participants join table CREATE TABLE IF NOT EXISTS conversation_participants ( channel_id INTEGER NOT NULL, user_id INTEGER NOT NULL, PRIMARY KEY (channel_id, user_id), FOREIGN KEY (channel_id) REFERENCES channels(id) ON DELETE CASCADE, FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE ); -- Messages table CREATE TABLE IF NOT EXISTS messages ( id INTEGER PRIMARY KEY AUTOINCREMENT, conversation_id INTEGER NOT NULL, sender_id INTEGER NOT NULL, content TEXT NOT NULL, sent_at DATETIME, created_at DATETIME, updated_at DATETIME, FOREIGN KEY (conversation_id) REFERENCES channels(id), FOREIGN KEY (sender_id) REFERENCES users(id) ); -- ============================================ -- INSERT STATEMENTS -- ============================================ -- Permissions INSERT INTO permissions (id, code, description) VALUES (1, 'user:create', 'Create users'), (2, 'user:read', 'Read users'), (3, 'user:update', 'Update users'), (4, 'user:delete', 'Delete users'), (5, 'role:create', 'Create roles'), (6, 'role:read', 'Read roles'), (7, 'role:update', 'Update roles'), (8, 'role:delete', 'Delete roles'), (9, 'permission:create', 'Create permissions'), (10, 'permission:read', 'Read permissions'), (11, 'permission:update', 'Update permissions'), (12, 'permission:delete', 'Delete permissions'), (13, 'service:create', 'Create services'), (14, 'service:read', 'Read services'), (15, 'service:update', 'Update services'), (16, 'service:delete', 'Delete services'), (17, 'task:create', 'Create tasks'), (18, 'task:read', 'Read tasks'), (19, 'task:update', 'Update tasks'), (20, 'task:delete', 'Delete tasks'), (21, 'note:create', 'Create notes'), (22, 'note:read', 'Read notes'), (23, 'note:update', 'Update notes'), (24, 'note:delete', 'Delete notes'), (25, 'channel:create', 'Create channels'), (26, 'channel:read', 'Read channels'), (27, 'channel:update', 'Update channels'), (28, 'channel:delete', 'Delete channels'), (29, 'message:create', 'Create messages'), (30, 'message:read', 'Read messages'), (31, 'message:update', 'Update messages'), (32, 'message:delete', 'Delete messages'), (33, 'taskstatus:create', 'Create task statuses'), (34, 'taskstatus:read', 'Read task statuses'), (35, 'taskstatus:update', 'Update task statuses'), (36, 'taskstatus:delete', 'Delete task statuses'); -- Roles INSERT INTO roles (id, name, description) VALUES (1, 'admin', 'Administrator with full access'), (2, 'manager', 'Service manager with task management'), (3, 'user', 'Regular user with limited access'); -- Role-Permission associations (admin gets all permissions) INSERT INTO role_permissions (role_id, permission_id) SELECT 1, id FROM permissions; -- Manager role permissions (service, task, note operations) INSERT INTO role_permissions (role_id, permission_id) VALUES (2, 13), (2, 14), (2, 15), (2, 16), -- service:* (2, 17), (2, 18), (2, 19), (2, 20), -- task:* (2, 21), (2, 22), (2, 23), (2, 24), -- note:* (2, 25), (2, 26), (2, 27), (2, 28), -- channel:* (2, 29), (2, 30), (2, 31), (2, 32), -- message:* (2, 33), (2, 34), (2, 35), (2, 36); -- taskstatus:* -- User role permissions (read-only + create notes/messages) INSERT INTO role_permissions (role_id, permission_id) VALUES (3, 2), (3, 6), (3, 10), (3, 14), (3, 18), (3, 22), (3, 26), (3, 30), (3, 34), -- read permissions (3, 21), (3, 29); -- create notes and messages -- Admin user (password: secret123) -- bcrypt hash generated with cost 10 INSERT INTO users (id, email, password, created_at, updated_at) VALUES (1, 'admin@example.com', '$2a$10$9CNePaChncemsl8ZgMFDfeFm.Rl1K1l8rurgZxVx7C6sbv5tojUDC', datetime('now'), datetime('now')); -- Associate admin user with admin role INSERT INTO user_roles (user_id, role_id) VALUES (1, 1); -- Task Statuses (common workflow states) INSERT INTO task_statuses (id, code, label, created_at, updated_at) VALUES (1, 'open', 'Open', datetime('now'), datetime('now')), (2, 'in_progress', 'In Progress', datetime('now'), datetime('now')), (3, 'blocked', 'Blocked', datetime('now'), datetime('now')), (4, 'review', 'In Review', datetime('now'), datetime('now')), (5, 'done', 'Done', datetime('now'), datetime('now')), (6, 'cancelled', 'Cancelled', datetime('now'), datetime('now'));