Add created and update column
This commit is contained in:
parent
dc873a12a5
commit
b7c7e32d1d
28
users.go
28
users.go
@ -2,6 +2,7 @@ package users
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"gitlab.quimbo.fr/odwrtw/canape-sql/random"
|
||||
@ -10,19 +11,30 @@ import (
|
||||
const usersCreate = `
|
||||
CREATE TABLE users (
|
||||
id SERIAL PRIMARY KEY,
|
||||
name text NOT NULL UNIQUE
|
||||
name text NOT NULL UNIQUE,
|
||||
updated timestamp DEFAULT current_timestamp,
|
||||
created timestamp DEFAULT current_timestamp
|
||||
);
|
||||
|
||||
CREATE TABLE tokens (
|
||||
id SERIAL,
|
||||
value text NOT NULL UNIQUE,
|
||||
users_id integer REFERENCES users (id) ON DELETE CASCADE
|
||||
users_id integer REFERENCES users (id) ON DELETE CASCADE,
|
||||
updated timestamp DEFAULT current_timestamp,
|
||||
created timestamp DEFAULT current_timestamp
|
||||
);
|
||||
|
||||
CREATE OR REPLACE FUNCTION update_modified_column()
|
||||
RETURNS TRIGGER AS $$
|
||||
BEGIN NEW.updated = now(); RETURN NEW; END; $$ language 'plpgsql';
|
||||
|
||||
CREATE TRIGGER update_users BEFORE UPDATE ON users FOR EACH ROW EXECUTE PROCEDURE update_modified_column();
|
||||
CREATE TRIGGER update_tokens BEFORE UPDATE ON tokens FOR EACH ROW EXECUTE PROCEDURE update_modified_column();
|
||||
`
|
||||
const (
|
||||
addUserQuery = `INSERT INTO users (name) VALUES ($1) RETURNING id;`
|
||||
getUserQuery = `SELECT * FROM users WHERE name=$1;`
|
||||
updateUserQuery = `UPDATE users SET (name)=(:name);`
|
||||
updateUserQuery = `UPDATE users SET name=$1 RETURNING *;`
|
||||
deleteUseQuery = `DELETE FROM users WHERE id=:id;`
|
||||
|
||||
addTokenQuery = `INSERT INTO tokens (value, users_id) VALUES ($1, $2) RETURNING id;`
|
||||
@ -31,14 +43,22 @@ const (
|
||||
deleteTokenQuery = `DELETE FROM tokens WHERE users_id=$1 AND value=$2;`
|
||||
)
|
||||
|
||||
// BaseTable have to be embeded in all your struct which reflect a table
|
||||
type BaseTable struct {
|
||||
Updated time.Time
|
||||
Created time.Time
|
||||
}
|
||||
|
||||
// User represents an user
|
||||
type User struct {
|
||||
BaseTable
|
||||
ID int
|
||||
Name string
|
||||
}
|
||||
|
||||
// Token represents a token
|
||||
type Token struct {
|
||||
BaseTable
|
||||
ID int
|
||||
Value string
|
||||
}
|
||||
@ -66,7 +86,7 @@ func (u *User) Add(q sqlx.Queryer) error {
|
||||
|
||||
// Update user on database or raise an error
|
||||
func (u *User) Update(ex *sqlx.DB) error {
|
||||
_, err := ex.NamedExec(updateUserQuery, u)
|
||||
err := ex.Get(u, updateUserQuery, u.Name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -13,7 +13,10 @@ import (
|
||||
|
||||
const drop = `
|
||||
DROP TABLE tokens;
|
||||
DROP TABLE users;`
|
||||
DROP TABLE users;
|
||||
|
||||
DROP FUNCTION update_modified_column();
|
||||
`
|
||||
|
||||
var db *sqlx.DB
|
||||
|
||||
@ -182,3 +185,16 @@ func TestTokenCheck(t *testing.T) {
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestAutoUpdateCols(t *testing.T) {
|
||||
sqltest.RunWithSchema(db, usersCreate, drop, t, func(db *sqlx.DB, t *testing.T) {
|
||||
u := &User{Name: "plop"}
|
||||
u.Add(db)
|
||||
u.Name = "toto"
|
||||
u.Update(db)
|
||||
|
||||
if !u.Created.Before(u.Updated) {
|
||||
t.Fatalf("colum updated not auto updated on table users")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user