schema_pg.sql 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. -- Users table
  2. CREATE TABLE IF NOT EXISTS users (
  3. id SERIAL PRIMARY KEY,
  4. username VARCHAR(255) NOT NULL UNIQUE,
  5. password_hash TEXT NOT NULL,
  6. role VARCHAR(50) NOT NULL DEFAULT 'user',
  7. created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
  8. );
  9. -- God Logs table
  10. CREATE TABLE IF NOT EXISTS god_logs (
  11. id SERIAL PRIMARY KEY,
  12. god_user_id INTEGER NOT NULL,
  13. action TEXT NOT NULL,
  14. details TEXT,
  15. timestamp TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
  16. FOREIGN KEY (god_user_id) REFERENCES users(id) ON DELETE CASCADE
  17. );
  18. -- Personas table
  19. CREATE TABLE IF NOT EXISTS personas (
  20. id SERIAL PRIMARY KEY,
  21. owner_id INTEGER NOT NULL,
  22. name VARCHAR(255) NOT NULL,
  23. title VARCHAR(255),
  24. bio TEXT,
  25. theories JSONB, -- JSON string in SQLite, JSONB in PG
  26. stance TEXT,
  27. system_prompt TEXT,
  28. is_public BOOLEAN DEFAULT FALSE,
  29. created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
  30. FOREIGN KEY (owner_id) REFERENCES users(id) ON DELETE CASCADE
  31. );
  32. -- Moderators table
  33. CREATE TABLE IF NOT EXISTS moderators (
  34. id SERIAL PRIMARY KEY,
  35. name VARCHAR(255) NOT NULL,
  36. title VARCHAR(255) DEFAULT '主持人',
  37. bio TEXT,
  38. system_prompt TEXT,
  39. greeting_template TEXT,
  40. closing_template TEXT,
  41. summary_template TEXT,
  42. creator_id INTEGER NOT NULL,
  43. created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
  44. FOREIGN KEY (creator_id) REFERENCES users(id) ON DELETE CASCADE
  45. );
  46. -- Forums table
  47. CREATE TABLE IF NOT EXISTS forums (
  48. id SERIAL PRIMARY KEY,
  49. topic TEXT NOT NULL,
  50. creator_id INTEGER NOT NULL,
  51. moderator_id INTEGER,
  52. status VARCHAR(50) DEFAULT 'active',
  53. summary_history JSONB DEFAULT '[]',
  54. start_time TIMESTAMP WITH TIME ZONE,
  55. end_time TIMESTAMP WITH TIME ZONE,
  56. duration_minutes INTEGER DEFAULT 30,
  57. FOREIGN KEY (creator_id) REFERENCES users(id) ON DELETE CASCADE,
  58. FOREIGN KEY (moderator_id) REFERENCES moderators(id)
  59. );
  60. -- Forum Participants table
  61. CREATE TABLE IF NOT EXISTS forum_participants (
  62. forum_id INTEGER NOT NULL,
  63. persona_id INTEGER NOT NULL,
  64. thoughts_history JSONB DEFAULT '[]',
  65. PRIMARY KEY (forum_id, persona_id),
  66. FOREIGN KEY (forum_id) REFERENCES forums(id) ON DELETE CASCADE,
  67. FOREIGN KEY (persona_id) REFERENCES personas(id) ON DELETE CASCADE
  68. );
  69. -- Messages table
  70. CREATE TABLE IF NOT EXISTS messages (
  71. id SERIAL PRIMARY KEY,
  72. forum_id INTEGER NOT NULL,
  73. persona_id INTEGER,
  74. moderator_id INTEGER,
  75. speaker_name VARCHAR(255) NOT NULL,
  76. content TEXT NOT NULL,
  77. turn_count INTEGER DEFAULT 0,
  78. thought TEXT,
  79. timestamp TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
  80. FOREIGN KEY (forum_id) REFERENCES forums(id) ON DELETE CASCADE,
  81. FOREIGN KEY (persona_id) REFERENCES personas(id),
  82. FOREIGN KEY (moderator_id) REFERENCES moderators(id)
  83. );
  84. -- Observations table
  85. CREATE TABLE IF NOT EXISTS observations (
  86. id SERIAL PRIMARY KEY,
  87. user_id INTEGER NOT NULL,
  88. forum_id INTEGER NOT NULL,
  89. joined_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
  90. left_at TIMESTAMP WITH TIME ZONE,
  91. FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
  92. FOREIGN KEY (forum_id) REFERENCES forums(id) ON DELETE CASCADE
  93. );
  94. -- System Logs table
  95. CREATE TABLE IF NOT EXISTS system_logs (
  96. id SERIAL PRIMARY KEY,
  97. forum_id INTEGER NOT NULL,
  98. level VARCHAR(50) DEFAULT 'info',
  99. source VARCHAR(255),
  100. content TEXT NOT NULL,
  101. timestamp TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
  102. FOREIGN KEY (forum_id) REFERENCES forums(id) ON DELETE CASCADE
  103. );