-
Notifications
You must be signed in to change notification settings - Fork 0
/
db-migration.sql
84 lines (74 loc) · 3.29 KB
/
db-migration.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
------------
-- MySQL DB Migration File
-- Creates the entire DB schema for the app
------------
-- Cache Workflow:
-- 1. When fetching a social media post, check the 'cache_flag' field:
-- 1.1. If 'cache_flag' is true -> Use cached data from DB
-- 1.2. If 'cache_flag' is false -> Data is stale, fetch again and compare new fetched data with existing data in DB
-- 1.2.1. If same -> Use cached data from DB (Twitter post and sentiment analysis from AI which is saved in DB)
-- 1.2.2. If different -> Save new twitter post fetched data to DB, re-run sentiment analysis AI, save AI data to tables ('cache_flag' should be set to true in the application logic)
CREATE DATABASE IF NOT EXISTS sentiment_media;
USE sentiment_media;
-- Disable foreign key checks (Permit creation of tables with foreign keys before the referenced table is created)
SET FOREIGN_KEY_CHECKS = 0;
-- Table for storing social media platforms
CREATE TABLE IF NOT EXISTS social_platforms (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL UNIQUE
);
INSERT INTO social_platforms (name) VALUES ('twitter');
-- Table for storing social media posts
CREATE TABLE IF NOT EXISTS social_posts (
id INT AUTO_INCREMENT PRIMARY KEY,
platform_id INT NOT NULL,
username VARCHAR(255) NOT NULL,
post_id VARCHAR(255) NOT NULL,
post_url VARCHAR(255) NOT NULL,
added_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
content TEXT,
check_count INT DEFAULT 1, -- number of times the post has been checked (1 by default because it's being checked now). Should be handled in the application logic
cache_flag BOOLEAN DEFAULT TRUE, -- True means the data is fresh. If false, the data needs to be verified again
last_checked_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, -- When the data was last verified (for cache)
FOREIGN KEY (platform_id) REFERENCES social_platforms(id) ON DELETE CASCADE,
UNIQUE (platform_id, username, post_id)
);
-- Enable events
SET GLOBAL event_scheduler = ON;
-- Cache System: Automatically update the cache_flag to false if the post hasn't been checked in the last 24 hours and the cache_flag was true (data is stale)
CREATE EVENT IF NOT EXISTS update_cache_flag
ON SCHEDULE EVERY 1 HOUR
DO
UPDATE social_posts
SET cache_flag = FALSE
WHERE last_checked_at < DATE_SUB(NOW(), INTERVAL 1 DAY)
AND cache_flag = TRUE;
-- Table for storing images associated with social media posts
CREATE TABLE IF NOT EXISTS social_posts_images (
id INT AUTO_INCREMENT PRIMARY KEY,
post_id INT NOT NULL,
image_url VARCHAR(255) NOT NULL,
FOREIGN KEY (post_id) REFERENCES social_posts(id) ON DELETE CASCADE
);
-- Table for storing AI sentiment analysis results
CREATE TABLE IF NOT EXISTS ai_sentiment_analysis (
id INT AUTO_INCREMENT PRIMARY KEY,
post_id INT NOT NULL,
general_summary TEXT NOT NULL,
general_emotion INT DEFAULT 0, -- 0 -> Neutral, 1 -> Positive, 2 -> Negative
joy FLOAT DEFAULT 0,
love FLOAT DEFAULT 0,
hope FLOAT DEFAULT 0,
pride FLOAT DEFAULT 0,
nostalgia FLOAT DEFAULT 0,
fear FLOAT DEFAULT 0,
sadness FLOAT DEFAULT 0,
disgust FLOAT DEFAULT 0,
anger FLOAT DEFAULT 0,
shame FLOAT DEFAULT 0,
guilt FLOAT DEFAULT 0,
surprise FLOAT DEFAULT 0,
FOREIGN KEY (post_id) REFERENCES social_posts(id) ON DELETE CASCADE
);
-- Enable foreign key checks
SET FOREIGN_KEY_CHECKS = 1;