This commit is contained in:
Nicolas Duhamel 2016-02-16 22:23:19 +01:00
parent ba833aa770
commit 369e438bf2
5 changed files with 36 additions and 30 deletions

View File

@ -78,7 +78,7 @@ func (s *Show) IsTracked() bool {
} }
func (s *Show) Add(db *sqlx.DB) error { func (s *Show) Add(db *sqlx.DB) error {
var id int var id string
r, err := db.NamedQuery(addShowQuery, s) r, err := db.NamedQuery(addShowQuery, s)
if err != nil { if err != nil {
return err return err

View File

@ -5,5 +5,7 @@ DROP TABLE shows;
DROP TABLE tokens; DROP TABLE tokens;
DROP TABLE users; DROP TABLE users;
DROP TABLE base;
DROP FUNCTION update_modified_column(); DROP FUNCTION update_modified_column();

View File

@ -1,3 +1,6 @@
-- Enable UUID generation
CREATE EXTENSION IF NOT EXISTS pgcrypto;
CREATE OR REPLACE FUNCTION update_modified_column() CREATE OR REPLACE FUNCTION update_modified_column()
RETURNS TRIGGER AS $$ RETURNS TRIGGER AS $$
BEGIN BEGIN
@ -5,50 +8,51 @@ BEGIN
RETURN NEW; RETURN NEW;
END; $$ language 'plpgsql'; END; $$ language 'plpgsql';
CREATE TABLE base (
updated timestamp DEFAULT current_timestamp,
created timestamp DEFAULT current_timestamp
);
CREATE TABLE users ( CREATE TABLE users (
id SERIAL PRIMARY KEY, id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
name text NOT NULL UNIQUE, name text NOT NULL UNIQUE,
updated timestamp DEFAULT current_timestamp, LIKE base INCLUDING DEFAULTS
created timestamp DEFAULT current_timestamp
); );
CREATE TRIGGER update_users BEFORE UPDATE ON users FOR EACH ROW EXECUTE PROCEDURE update_modified_column(); CREATE TRIGGER update_users BEFORE UPDATE ON users FOR EACH ROW EXECUTE PROCEDURE update_modified_column();
CREATE TABLE tokens ( CREATE TABLE tokens (
id SERIAL, id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
value text NOT NULL UNIQUE, value text NOT NULL UNIQUE,
users_id integer REFERENCES users (id) ON DELETE CASCADE, users_id uuid REFERENCES users (id) ON DELETE CASCADE,
updated timestamp DEFAULT current_timestamp, LIKE base INCLUDING DEFAULTS
created timestamp DEFAULT current_timestamp
); );
CREATE TRIGGER update_tokens BEFORE UPDATE ON tokens FOR EACH ROW EXECUTE PROCEDURE update_modified_column(); CREATE TRIGGER update_tokens BEFORE UPDATE ON tokens FOR EACH ROW EXECUTE PROCEDURE update_modified_column();
CREATE TABLE shows ( CREATE TABLE shows (
id SERIAL PRIMARY KEY, id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
imdbid text NOT NULL UNIQUE, imdbid text NOT NULL UNIQUE,
title text NOT NULL, title text NOT NULL,
updated timestamp DEFAULT current_timestamp, LIKE base INCLUDING DEFAULTS
created timestamp DEFAULT current_timestamp
); );
CREATE TRIGGER update_shows BEFORE UPDATE ON shows FOR EACH ROW EXECUTE PROCEDURE update_modified_column(); CREATE TRIGGER update_shows BEFORE UPDATE ON shows FOR EACH ROW EXECUTE PROCEDURE update_modified_column();
CREATE TABLE episodes ( CREATE TABLE episodes (
id SERIAL PRIMARY KEY, id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
shows_id integer REFERENCES shows (id) ON DELETE CASCADE, shows_id uuid REFERENCES shows (id) ON DELETE CASCADE,
title text NOT NULL, title text NOT NULL,
season integer NOT NULL, season integer NOT NULL,
episode integer NOT NULL, episode integer NOT NULL,
updated timestamp DEFAULT current_timestamp, LIKE base INCLUDING DEFAULTS
created timestamp DEFAULT current_timestamp
); );
CREATE TRIGGER update_episodes BEFORE UPDATE ON episodes FOR EACH ROW EXECUTE PROCEDURE update_modified_column(); CREATE TRIGGER update_episodes BEFORE UPDATE ON episodes FOR EACH ROW EXECUTE PROCEDURE update_modified_column();
CREATE TABLE shows_tracked ( CREATE TABLE shows_tracked (
shows_id integer NOT NULL REFERENCES shows (id) ON DELETE CASCADE, shows_id uuid NOT NULL REFERENCES shows (id) ON DELETE CASCADE,
users_id integer NOT NULL REFERENCES users (id) ON DELETE CASCADE, users_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE,
season integer NOT NULL, season integer NOT NULL,
episode integer NOT NULL episode integer NOT NULL
); );

View File

@ -11,7 +11,7 @@ import (
// BaseTable have to be embeded in all your struct which reflect a table // BaseTable have to be embeded in all your struct which reflect a table
type BaseTable struct { type BaseTable struct {
ID int ID string
Updated time.Time Updated time.Time
Created time.Time Created time.Time
} }

View File

@ -44,7 +44,7 @@ func Get(q sqlx.Queryer, name string) (*User, error) {
// Add user to database or raises an error // Add user to database or raises an error
func (u *User) Add(q sqlx.Queryer) error { func (u *User) Add(q sqlx.Queryer) error {
var id int var id string
err := q.QueryRowx(addUserQuery, u.Name).Scan(&id) err := q.QueryRowx(addUserQuery, u.Name).Scan(&id)
if err != nil { if err != nil {
return err return err
@ -87,7 +87,7 @@ func (u *User) NewToken(ex *sqlx.DB) (*Token, error) {
Value: random.String(50), Value: random.String(50),
} }
var id int var id string
err := ex.QueryRowx(addTokenQuery, t.Value, u.ID).Scan(&id) err := ex.QueryRowx(addTokenQuery, t.Value, u.ID).Scan(&id)
if err != nil { if err != nil {
return nil, err return nil, err