From 7351d347bc929a15fb9b0bad793e41a1de41dbf0 Mon Sep 17 00:00:00 2001
From: Guillaume Heurtier <guillaume.heurtier@maarch.org>
Date: Mon, 10 Feb 2020 17:56:27 +0100
Subject: [PATCH] FEAT #12471 TIME 0:10 refactor tags sql

---
 migration/20.03/2003.sql | 17 ++++-------------
 sql/structure.sql        | 14 ++------------
 2 files changed, 6 insertions(+), 25 deletions(-)

diff --git a/migration/20.03/2003.sql b/migration/20.03/2003.sql
index d10ecc031ff..e39a11862bb 100644
--- a/migration/20.03/2003.sql
+++ b/migration/20.03/2003.sql
@@ -242,9 +242,9 @@ DO $$ BEGIN
       ALTER TABLE tags DROP COLUMN IF EXISTS parent_id;
       ALTER TABLE tags ADD COLUMN parent_id INT;
       ALTER TABLE tags DROP COLUMN IF EXISTS creation_date;
-      ALTER TABLE tags ADD COLUMN creation_date TIMESTAMP;
+      ALTER TABLE tags ADD COLUMN creation_date TIMESTAMP DEFAULT NOW();
       ALTER TABLE tags DROP COLUMN IF EXISTS links;
-      ALTER TABLE tags ADD COLUMN links jsonb DEFAULT cast('[]' as jsonb);
+      ALTER TABLE tags ADD COLUMN links jsonb DEFAULT '[]';
       ALTER TABLE tags DROP COLUMN IF EXISTS usage;
       ALTER TABLE tags ADD COLUMN usage TEXT;
   END IF;
@@ -254,17 +254,8 @@ SELECT setval('tags_id_seq', (SELECT MAX(id) from tags));
 DROP TABLE IF EXISTS tags_entities;
 
 DROP TABLE IF EXISTS resources_tags;
-CREATE TABLE resources_tags
-(
-    id SERIAL NOT NULL,
-    res_id INT,
-    tag_id INT,
-    CONSTRAINT resources_tags_id_pkey PRIMARY KEY (id)
-)
-WITH (OIDS=FALSE);
-INSERT INTO resources_tags (res_id, tag_id) SELECT res_id, tag_id FROM tag_res;
-
-DROP TABLE IF EXISTS tag_res;
+ALTER TABLE tag_res RENAME TO resources_tags;
+ALTER TABLE tag_res ADD COLUMN id serial NOT NULL;
 
 /* DOCTYPES */
 DO $$ BEGIN
diff --git a/sql/structure.sql b/sql/structure.sql
index 5c69b53b1a5..f0f620987e5 100755
--- a/sql/structure.sql
+++ b/sql/structure.sql
@@ -903,8 +903,8 @@ CREATE TABLE tags
   entity_id_owner character varying(32),
   description text,
   parent_id INT,
-  creation_date timestamp,
-  links jsonb,
+  creation_date timestamp DEFAULT NOW(),
+  links jsonb  DEFAULT '[]',
   usage text,
   CONSTRAINT tags_id_pkey PRIMARY KEY (id)
 )
@@ -919,16 +919,6 @@ CREATE TABLE resources_tags
 )
 WITH (OIDS=FALSE);
 
-CREATE TABLE tag_res
-(
-  res_id bigint NOT NULL,
-  tag_id bigint NOT NULL,
-  CONSTRAINT tag_res_pkey PRIMARY KEY (res_id,tag_id)
-)
-WITH (
-  OIDS=FALSE
-);
-
 CREATE SEQUENCE res_id_mlb_seq
   INCREMENT 1
   MINVALUE 1
-- 
GitLab