From faff0ba943199e1c333950bcbcbb2bf8530b2f9d Mon Sep 17 00:00:00 2001
From: Dariusz Kedzierski <dkedzierski@syncad.com>
Date: Mon, 15 Jun 2020 00:00:20 +0200
Subject: [PATCH] [WIP] Native votes support: removed votes columns from
 migration script and schema, added conversion routine to convert old votes
 format (as text blob) to hive_votest table

---
 hive/db/schema.py              |  1 -
 hive/indexer/votes.py          |  1 -
 scripts/update_hivemind_db.sql | 22 ++++++++++++++++++++--
 3 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/hive/db/schema.py b/hive/db/schema.py
index 605e8f4d2..ad6dec1cc 100644
--- a/hive/db/schema.py
+++ b/hive/db/schema.py
@@ -165,7 +165,6 @@ def build_metadata():
         sa.Column('preview', VARCHAR(1024), nullable=False),
         sa.Column('img_url', VARCHAR(1024), nullable=False),
         sa.Column('body', TEXT),
-        sa.Column('votes', TEXT),
         sa.Column('json', sa.JSON)
     )
 
diff --git a/hive/indexer/votes.py b/hive/indexer/votes.py
index 9ff633d8e..0bd2dc8bf 100644
--- a/hive/indexer/votes.py
+++ b/hive/indexer/votes.py
@@ -84,4 +84,3 @@ class Votes:
         rshares = 0
         DB.query(sql, weight=weight, rshares=rshares, vote_percent=vote_percent, last_update=date,
                  id=vote_id)
-
diff --git a/scripts/update_hivemind_db.sql b/scripts/update_hivemind_db.sql
index c99b5b1ea..10b2c484f 100644
--- a/scripts/update_hivemind_db.sql
+++ b/scripts/update_hivemind_db.sql
@@ -146,7 +146,6 @@ CREATE TABLE IF NOT EXISTS hive_post_data (
   preview VARCHAR(1024) NOT NULL,
   img_url VARCHAR(1024) NOT NULL,
   body TEXT,
-  votes TEXT,
   json JSON
 );
 CREATE INDEX IF NOT EXISTS hive_post_data_id_idx ON hive_post_data (id);
@@ -215,7 +214,26 @@ UPDATE hive_posts_new hpn SET (
 
 -- Populate table hive_post_data with bulk data from hive_posts_cache
 -- RAISE NOTICE 'Populate table hive_post_data with bulk data from hive_posts_cache';
-INSERT INTO hive_post_data (id, title, preview, img_url, body, votes, json) SELECT post_id, title, preview, img_url, body, votes, json::json FROM hive_posts_cache;
+INSERT INTO hive_post_data (id, title, preview, img_url, body, votes, json) SELECT post_id, title, preview, img_url, body, json::json FROM hive_posts_cache;
+
+-- Populate hive_votes table
+-- RAISE NOTICE 'Populate table hive_votes with bulk data from hive_posts_cache';
+INSERT INTO 
+    hive_votes (voter_id, author_id, permlink_id, rshares, vote_percent)
+SELECT 
+    (SELECT id from hive_accounts WHERE name = vote_data.regexp_split_to_array[1]) AS voter_id,
+    (SELECT author_id FROM hive_posts WHERE id = vote_data.id) AS author_id,
+    (SELECT permlink_id FROM hive_posts WHERE id = vote_data.id) AS permlink_id,  
+    (vote_data.regexp_split_to_array[2])::bigint AS rshares,
+    (vote_data.regexp_split_to_array[3])::int AS vote_percent
+FROM 
+    (SELECT 
+        votes.id, regexp_split_to_array(votes.regexp_split_to_table::text, E',') 
+     FROM 
+        (SELECT id, regexp_split_to_table(votes::text, E'\n') 
+         FROM hive_posts_cache) 
+    AS votes) 
+AS vote_data;
 
 -- Helper type for use with json_populate_record
 -- RAISE NOTICE 'Creating legacy_comment_data table';
-- 
GitLab