JvArchive

Schéma JvArchive

Dec 14th, 2025 (edited)
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- =====================================================
  2. -- IMPORT JVA FINAL SAFE V2
  3. -- =====================================================
  4.  
  5. -- CONFIG IMPORT
  6. SET client_encoding = 'UTF8';
  7. SET synchronous_commit = OFF;
  8. SET maintenance_work_mem = '4GB';
  9. SET work_mem = '64MB';
  10.  
  11. -- =====================================================
  12. -- DROP TABLES EXISTANTES (optionnel si réimport)
  13. -- =====================================================
  14. DROP TABLE IF EXISTS messages CASCADE;
  15. DROP TABLE IF EXISTS topics CASCADE;
  16. DROP TABLE IF EXISTS auteurs CASCADE;
  17.  
  18. -- =====================================================
  19. -- TABLE AUTEURS
  20. -- =====================================================
  21. CREATE TABLE auteurs (
  22.   id BIGINT PRIMARY KEY,
  23.   pseudo VARCHAR(50),
  24.   avatar VARCHAR(255)
  25. );
  26.  
  27. -- =====================================================
  28. -- TABLE TOPICS (restaure / delete_by_modo = TEXT pour import)
  29. -- =====================================================
  30. CREATE TABLE topics (
  31.   id BIGINT PRIMARY KEY,
  32.   titre VARCHAR(255),
  33.  
  34.   nb_messages INTEGER,
  35.   date_creation TIMESTAMPTZ,
  36.   date_suppression TIMESTAMPTZ,
  37.  
  38.   restaure TEXT,
  39.   delete_by_modo TEXT,
  40.  
  41.   auteur_id BIGINT,
  42.  
  43.   date_dernier_message TIMESTAMPTZ,
  44.   date_dernier_message_enregistre TIMESTAMPTZ,
  45.   nb_messages_enregistre INTEGER
  46. );
  47.  
  48. -- =====================================================
  49. -- TABLE MESSAGES (partitionnée)
  50. -- =====================================================
  51. CREATE TABLE messages (
  52.   id BIGINT,
  53.   date_post TIMESTAMPTZ,
  54.   texte TEXT,
  55.   topic_id BIGINT,
  56.   auteur_id BIGINT
  57. ) PARTITION BY RANGE (date_post);
  58.  
  59. -- =====================================================
  60. -- CREATION DES PARTITIONS MESSAGES (2004 → 2026)
  61. -- =====================================================
  62. DO $$
  63. DECLARE
  64.   y INTEGER;
  65. BEGIN
  66.   FOR y IN 2004..2026 LOOP
  67.     EXECUTE format(
  68.       'CREATE TABLE messages_%s PARTITION OF messages
  69.       FOR VALUES FROM (%L) TO (%L);',
  70.       y,
  71.       y || '-01-01',
  72.       (y + 1) || '-01-01'
  73.     );
  74.   END LOOP;
  75. END$$;
  76.  
  77. -- Partition de secours (messages hors plage)
  78. CREATE TABLE messages_default PARTITION OF messages DEFAULT;
  79.  
  80. -- =====================================================
  81. -- IMPORT CSV
  82. -- =====================================================
  83.  
  84. -- AUTEURS
  85. COPY auteurs (id, pseudo, avatar)
  86. FROM 'auteurs.csv'
  87. WITH (FORMAT csv, HEADER true);
  88.  
  89. -- TOPICS
  90. COPY topics (
  91.   id,
  92.   titre,
  93.   nb_messages,
  94.   date_creation,
  95.   date_suppression,
  96.   restaure,
  97.   delete_by_modo,
  98.   auteur_id,
  99.   date_dernier_message,
  100.   date_dernier_message_enregistre,
  101.   nb_messages_enregistre
  102. )
  103. FROM 'topics.csv'
  104. WITH (FORMAT csv, HEADER true);
  105.  
  106. -- MESSAGES
  107. COPY messages (id, date_post, texte, topic_id, auteur_id)
  108. FROM 'messages.csv'
  109. WITH (FORMAT csv, HEADER true);
  110.  
  111. -- =====================================================
  112. -- POST-TRAITEMENT : conversion TEXT → BOOLEAN
  113. -- =====================================================
  114. UPDATE topics SET
  115.   restaure = CASE WHEN restaure IN ('1','t','true') THEN 'true'
  116.                   ELSE 'false' END,
  117.   delete_by_modo = CASE WHEN delete_by_modo IN ('1','t','true') THEN 'true'
  118.                         ELSE 'false' END;
  119.  
  120. ALTER TABLE topics
  121.   ALTER COLUMN restaure TYPE BOOLEAN USING restaure::BOOLEAN;
  122.  
  123. ALTER TABLE topics
  124.   ALTER COLUMN delete_by_modo TYPE BOOLEAN USING delete_by_modo::BOOLEAN;
  125.  
  126. -- =====================================================
  127. -- INDEX (APRÈS IMPORT)
  128. -- =====================================================
  129.  
  130. -- auteurs
  131. CREATE INDEX idx_auteurs_pseudo ON auteurs(pseudo);
  132.  
  133. -- topics
  134. CREATE INDEX idx_topics_last_msg
  135. ON topics (date_dernier_message DESC);
  136.  
  137. CREATE INDEX idx_topics_auteur
  138. ON topics (auteur_id);
  139.  
  140. -- messages
  141. CREATE INDEX idx_messages_topic_date
  142. ON messages (topic_id, date_post);
  143.  
  144. CREATE INDEX idx_messages_auteur_date
  145. ON messages (auteur_id, date_post);
  146.  
  147. -- =====================================================
  148. -- ANALYZE
  149. -- =====================================================
  150. ANALYZE auteurs;
  151. ANALYZE topics;
  152. ANALYZE messages;
  153.  
  154. -- Activation de pg_trgm (recherches par message)
  155. CREATE EXTENSION IF NOT EXISTS pg_trgm;
  156.  
  157. -- Créer un index GIN sur le texte
  158. CREATE INDEX idx_messages_texte_trgm
  159. ON messages USING GIN (texte gin_trgm_ops);
  160.  
  161.  
  162. -- =====================================================
  163. -- FIN
  164. -- =====================================================
  165.  
Advertisement
Add Comment
Please, Sign In to add comment