setup.sql 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. -- Web Publication System Complete Database Schema
  2. -- MariaDB/MySQL compatible
  3. -- Combined schema with all migrations for setup.php
  4. -- Note: Database creation and selection is handled by setup script
  5. -- Publications table
  6. CREATE TABLE publications (
  7. id INT AUTO_INCREMENT PRIMARY KEY,
  8. title VARCHAR(255) NOT NULL,
  9. slug VARCHAR(255),
  10. content TEXT NOT NULL,
  11. summary VARCHAR(500),
  12. author VARCHAR(100) NOT NULL,
  13. status ENUM('draft', 'published', 'archived') DEFAULT 'draft',
  14. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  15. updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  16. published_at TIMESTAMP NULL DEFAULT NULL,
  17. INDEX idx_publications_slug (slug)
  18. );
  19. -- Users table for admin authentication with LDAP support
  20. CREATE TABLE users (
  21. id INT AUTO_INCREMENT PRIMARY KEY,
  22. username VARCHAR(50) UNIQUE NOT NULL,
  23. password VARCHAR(255),
  24. email VARCHAR(255),
  25. role ENUM('admin', 'editor', 'user') DEFAULT 'editor',
  26. auth_type ENUM('local', 'ldap') DEFAULT 'local',
  27. ldap_dn VARCHAR(255),
  28. status ENUM('active', 'inactive') NOT NULL DEFAULT 'active',
  29. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  30. last_login TIMESTAMP NULL,
  31. updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  32. INDEX idx_users_role (role),
  33. INDEX idx_users_status (status)
  34. );
  35. -- Categories table
  36. CREATE TABLE categories (
  37. id INT AUTO_INCREMENT PRIMARY KEY,
  38. name VARCHAR(100) UNIQUE NOT NULL,
  39. description TEXT,
  40. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
  41. );
  42. -- Publication categories junction table
  43. CREATE TABLE publication_categories (
  44. publication_id INT,
  45. category_id INT,
  46. PRIMARY KEY (publication_id, category_id),
  47. FOREIGN KEY (publication_id) REFERENCES publications(id) ON DELETE CASCADE,
  48. FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE CASCADE
  49. );
  50. -- Images table for WYSIWYG editor
  51. CREATE TABLE IF NOT EXISTS images (
  52. id INT AUTO_INCREMENT PRIMARY KEY,
  53. filename VARCHAR(255) NOT NULL,
  54. original_name VARCHAR(255) NOT NULL,
  55. file_size INT NOT NULL,
  56. mime_type VARCHAR(100),
  57. uploaded_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  58. uploaded_by INT,
  59. INDEX idx_uploaded_at (uploaded_at),
  60. INDEX idx_uploaded_by (uploaded_by)
  61. );
  62. -- Add foreign key constraint for images table
  63. ALTER TABLE images ADD CONSTRAINT fk_images_user FOREIGN KEY (uploaded_by) REFERENCES users(id) ON DELETE SET NULL;
  64. -- Insert default admin user (password: admin123)
  65. INSERT INTO users (username, password, email, role, auth_type, status) VALUES
  66. ('admin', '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', 'admin@example.com', 'admin', 'local', 'active');
  67. -- Insert sample categories
  68. INSERT INTO categories (name, description) VALUES
  69. ('Technology', 'Articles about technology and programming'),
  70. ('News', 'Current events and news articles'),
  71. ('Tutorial', 'How-to guides and tutorials'),
  72. ('Opinion', 'Editorials and opinion pieces');
  73. -- Insert sample publications
  74. INSERT INTO publications (title, slug, content, summary, author, status, published_at) VALUES
  75. ('Welcome to Web Publication System', 'welcome-to-web-publication-system', 'This is a sample publication to demonstrate the system capabilities. You can edit or delete this publication from the admin panel.', 'A welcome message for new users', 'admin', 'published', NOW()),
  76. ('Getting Started with PHP', 'getting-started-with-php', 'PHP is a popular server-side scripting language designed for web development. This article covers the basics of getting started with PHP development.', 'Introduction to PHP programming', 'admin', 'published', NOW()),
  77. ('Database Design Best Practices', 'database-design-best-practices', 'Good database design is crucial for application performance and maintainability. This article covers essential principles and practices.', 'Tips for designing efficient databases', 'admin', 'draft', NULL);
  78. -- Update existing publications to generate slugs from titles (for any existing data)
  79. UPDATE publications
  80. SET slug = LOWER(REPLACE(REPLACE(REPLACE(title, '[^a-z0-9 ]', ''), ' ', '-'), '-', ''))
  81. WHERE slug IS NULL OR slug = '';
  82. -- Update published_at for existing published publications (for any existing data)
  83. UPDATE publications
  84. SET published_at = created_at
  85. WHERE status = 'published' AND published_at IS NULL;
  86. -- Update existing users to have default role and status (for any existing data)
  87. UPDATE users SET role = 'admin' WHERE id = 1; -- Make first user admin
  88. UPDATE users SET status = 'active' WHERE status IS NULL;