From d2f982c5d192d93cfe4efc51a38df35aa989ab34 Mon Sep 17 00:00:00 2001 From: Farhan Khan Date: Wed, 28 Oct 2020 20:49:11 +0000 Subject: [PATCH] Modified insert query for accounts Accounts/Posts are now relational --- poll/engine.go | 14 +++++++++++--- poll/tables.sql | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 poll/tables.sql diff --git a/poll/engine.go b/poll/engine.go index 2458f9c..6dd3699 100644 --- a/poll/engine.go +++ b/poll/engine.go @@ -269,17 +269,25 @@ func writePost(pool *pgxpool.Pool, reportpost ReportPost) { defer conn.Release() // Insert new account if new - _, err = conn.Exec(context.Background(), "INSERT INTO accounts (acct, avatar, bot, created_at, display_name, url) VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT (acct) DO NOTHING", reportpost.Account.Acct, reportpost.Account.Avatar, reportpost.Account.Bot, reportpost.Account.Created_at, reportpost.Account.Display_name, reportpost.Account.Url) + var acctid int + err = conn.QueryRow(context.Background(), "INSERT INTO accounts (acct, avatar, bot, created_at, display_name, url) VALUES($1, $2, $3, $4, $5, $6) ON CONFLICT(acct) DO UPDATE SET acct=EXCLUDED.acct RETURNING id", reportpost.Account.Acct, reportpost.Account.Avatar, reportpost.Account.Bot, reportpost.Account.Created_at, reportpost.Account.Display_name, reportpost.Account.Url).Scan(&acctid) if err != nil { + fmt.Println("First ", err) + fmt.Println(reportpost.Account.Acct, reportpost.Account.Avatar, reportpost.Account.Bot, reportpost.Account.Created_at, reportpost.Account.Display_name, reportpost.Account.Url) + os.Exit(1) // For now I want this to die and learn why it failed return } // Insert new post if new - _, err = conn.Exec(context.Background(), "INSERT INTO posts (url, content, created_at, strippedcontent, posthash) VALUES ($1, $2, $3, $4, $5) ON CONFLICT (posthash) DO NOTHING", reportpost.Url, reportpost.Content, reportpost.Created_at, reportpost.StrippedContent, reportpost.Posthash) - if err != nil { + _, err = conn.Exec(context.Background(), "INSERT INTO posts (url, content, created_at, strippedcontent, acct_id, posthash) VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT (posthash) DO NOTHING", reportpost.Url, reportpost.Content, reportpost.Created_at, reportpost.StrippedContent, acctid, reportpost.Posthash) + if err != nil { // For now I want to know why this failed. + fmt.Println("Second ", err) + os.Exit(1) // For now I want this to die and learn why it failed return } + fmt.Println("Properly executed") + } func SuspendInstance(suspendinstance ReportInstance, runninginstances *[]RunningInstance) { diff --git a/poll/tables.sql b/poll/tables.sql new file mode 100644 index 0000000..b88a3e1 --- /dev/null +++ b/poll/tables.sql @@ -0,0 +1,32 @@ +CREATE TABLE accounts ( + id serial NOT NULL PRIMARY KEY, + acct VARCHAR(100) NOT NULL UNIQUE, + avatar VARCHAR(2083) NOT NULL, + bot boolean, + created_at VARCHAR(100) NOT NULL, + display_name VARCHAR(100) NOT NULL, + url VARCHAR(2083) NOT NULL +); + +CREATE TABLE posts ( + id serial NOT NULL PRIMARY KEY, + url VARCHAR(2083) NOT NULL, + content text, + strippedcontent text, + created_at timestamptz DEFAULT NOW(), + + acct_id int NOT NULL REFERENCES accounts (id), + + posthash bytea UNIQUE +); + +CREATE TABLE instances ( + id serial NOT NULL PRIMARY KEY, + endpoint VARCHAR(2083) NOT NULL, + autostart boolean, + state varchar(16), + username varchar(32), + password varchar(32), + + software varchar(50) +);