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 {
var id int
var id string
r, err := db.NamedQuery(addShowQuery, s)
if err != nil {
return err

View File

@ -5,5 +5,7 @@ DROP TABLE shows;
DROP TABLE tokens;
DROP TABLE users;
DROP TABLE base;
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()
RETURNS TRIGGER AS $$
BEGIN
@ -5,50 +8,51 @@ BEGIN
RETURN NEW;
END; $$ language 'plpgsql';
CREATE TABLE base (
updated timestamp DEFAULT current_timestamp,
created timestamp DEFAULT current_timestamp
);
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name text NOT NULL UNIQUE,
updated timestamp DEFAULT current_timestamp,
created timestamp DEFAULT current_timestamp
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
name text NOT NULL UNIQUE,
LIKE base INCLUDING DEFAULTS
);
CREATE TRIGGER update_users BEFORE UPDATE ON users FOR EACH ROW EXECUTE PROCEDURE update_modified_column();
CREATE TABLE tokens (
id SERIAL,
value text NOT NULL UNIQUE,
users_id integer REFERENCES users (id) ON DELETE CASCADE,
updated timestamp DEFAULT current_timestamp,
created timestamp DEFAULT current_timestamp
CREATE TABLE tokens (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
value text NOT NULL UNIQUE,
users_id uuid REFERENCES users (id) ON DELETE CASCADE,
LIKE base INCLUDING DEFAULTS
);
CREATE TRIGGER update_tokens BEFORE UPDATE ON tokens FOR EACH ROW EXECUTE PROCEDURE update_modified_column();
CREATE TABLE shows (
id SERIAL PRIMARY KEY,
imdbid text NOT NULL UNIQUE,
title text NOT NULL,
updated timestamp DEFAULT current_timestamp,
created timestamp DEFAULT current_timestamp
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
imdbid text NOT NULL UNIQUE,
title text NOT NULL,
LIKE base INCLUDING DEFAULTS
);
CREATE TRIGGER update_shows BEFORE UPDATE ON shows FOR EACH ROW EXECUTE PROCEDURE update_modified_column();
CREATE TABLE episodes (
id SERIAL PRIMARY KEY,
shows_id integer REFERENCES shows (id) ON DELETE CASCADE,
title text NOT NULL,
season integer NOT NULL,
episode integer NOT NULL,
updated timestamp DEFAULT current_timestamp,
created timestamp DEFAULT current_timestamp
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
shows_id uuid REFERENCES shows (id) ON DELETE CASCADE,
title text NOT NULL,
season integer NOT NULL,
episode integer NOT NULL,
LIKE base INCLUDING DEFAULTS
);
CREATE TRIGGER update_episodes BEFORE UPDATE ON episodes FOR EACH ROW EXECUTE PROCEDURE update_modified_column();
CREATE TABLE shows_tracked (
shows_id integer NOT NULL REFERENCES shows (id) ON DELETE CASCADE,
users_id integer NOT NULL REFERENCES users (id) ON DELETE CASCADE,
season integer NOT NULL,
episode integer NOT NULL
shows_id uuid NOT NULL REFERENCES shows (id) ON DELETE CASCADE,
users_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE,
season 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
type BaseTable struct {
ID int
ID string
Updated 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
func (u *User) Add(q sqlx.Queryer) error {
var id int
var id string
err := q.QueryRowx(addUserQuery, u.Name).Scan(&id)
if err != nil {
return err
@ -87,7 +87,7 @@ func (u *User) NewToken(ex *sqlx.DB) (*Token, error) {
Value: random.String(50),
}
var id int
var id string
err := ex.QueryRowx(addTokenQuery, t.Value, u.ID).Scan(&id)
if err != nil {
return nil, err