From bf6745af21f82562af0b85de566f4e7b7ef5df8c Mon Sep 17 00:00:00 2001
From: Nathan Perry <avaglir@gmail.com>
Date: Wed, 4 Apr 2018 22:09:13 -0400
Subject: revamp database structure

---
 migrations/2018-02-15-031841_create_memes/up.sql | 71 ++++++++++++++++++++++--
 1 file changed, 65 insertions(+), 6 deletions(-)

(limited to 'migrations/2018-02-15-031841_create_memes/up.sql')

diff --git a/migrations/2018-02-15-031841_create_memes/up.sql b/migrations/2018-02-15-031841_create_memes/up.sql
index 38097e7..dc24710 100644
--- a/migrations/2018-02-15-031841_create_memes/up.sql
+++ b/migrations/2018-02-15-031841_create_memes/up.sql
@@ -1,14 +1,73 @@
+CREATE TABLE metadata (
+    id          SERIAL PRIMARY KEY,
+
+    created     TIMESTAMP NOT NULL DEFAULT current_timestamp,
+    created_by  BIGINT NOT NULL
+);
+
+CREATE INDEX metadata_created on metadata (created);
+CREATE INDEX metadata_created_by on metadata (created_by);
+
+
+CREATE TABLE audit_records (
+    id          SERIAL PRIMARY KEY,
+
+    updated     TIMESTAMP NOT NULL DEFAULT current_timestamp,
+    updated_by  BIGINT NOT NULL,
+
+    metadata_id INTEGER REFERENCES metadata NOT NULL
+);
+
+CREATE INDEX audit_updated on audit_records (updated);
+CREATE INDEX audit_updated_by on audit_records (updated_by);
+CREATE INDEX audit_metadata on audit_records (metadata_id);
+
+CREATE INDEX audit_metadata_updated_by on audit_records (metadata_id, updated_by);
+
+
+CREATE TABLE images (
+    id    SERIAL PRIMARY KEY,
+    data  bytea NOT NULL,
+
+    metadata_id INTEGER REFERENCES metadata UNIQUE NOT NULL
+);
+
+
+CREATE TABLE audio (
+    id    SERIAL PRIMARY KEY,
+    data  bytea NOT NULL,
+
+    metadata_id INTEGER REFERENCES metadata UNIQUE NOT NULL
+);
+
+
 CREATE TABLE text_memes (
     id          SERIAL PRIMARY KEY,
     title       varchar UNIQUE NOT NULL,
     content     TEXT NOT NULL,
-    pic_related varchar NULL,
-    UNIQUE(content, pic_related)
+    image_id    INTEGER REFERENCES images NULL,
+    audio_id    INTEGER REFERENCES audio NULL,
+
+    metadata_id INTEGER REFERENCES metadata UNIQUE NOT NULL,
+    UNIQUE(content, image_id, audio_id)
 );
 
-CREATE TABLE audio_memes (
-    id      SERIAL PRIMARY KEY,
-    title   varchar UNIQUE NOT NULL,
-    link    varchar UNIQUE NOT NULL
+
+CREATE TABLE image_memes (
+    id          SERIAL PRIMARY KEY,
+    title       varchar UNIQUE NOT NULL,
+    image_id    INTEGER REFERENCES images NOT NULL,
+
+    metadata_id INTEGER REFERENCES metadata UNIQUE NOT NULL,
+    UNIQUE(title, image_id)
 );
 
+
+CREATE TABLE audio_memes (
+    id          SERIAL PRIMARY KEY,
+    title       varchar UNIQUE NOT NULL,
+    audio_id    INTEGER REFERENCES audio NOT NULL,
+
+    metadata_id INTEGER REFERENCES metadata UNIQUE NOT NULL,
+    UNIQUE(title, audio_id)
+);
-- 
cgit v1.3.1

