adds create and update time to actions
All checks were successful
gitea-deepak/gogmagog/pipeline/head This commit looks good

This commit is contained in:
2020-12-26 15:04:51 -06:00
parent 3a5961f003
commit 2c5e84d836
4 changed files with 35 additions and 11 deletions

View File

@@ -1 +1,3 @@
DROP TABLE IF EXISTS actions; DROP TABLE IF EXISTS actions;
DROP FUNCTION IF EXISTS trigger_set_timestamp;

View File

@@ -1,4 +1,20 @@
CREATE TABLE IF NOT EXISTS actions( CREATE TABLE IF NOT EXISTS actions(
id serial PRIMARY KEY, id serial PRIMARY KEY,
description VARCHAR (500) description VARCHAR (500),
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
); );
CREATE OR REPLACE FUNCTION trigger_set_timestamp()
RETURNS TRIGGER AS $set_updated$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$set_updated$ LANGUAGE plpgsql;
CREATE TRIGGER set_updated
BEFORE UPDATE ON actions
FOR EACH ROW
EXECUTE PROCEDURE trigger_set_timestamp();

View File

@@ -61,7 +61,7 @@ func GetStore() *postgresStore {
func (store *postgresStore) SelectActions() ([]*models.Action, error) { func (store *postgresStore) SelectActions() ([]*models.Action, error) {
actions := make([]*models.Action, 0) actions := make([]*models.Action, 0)
err := store.db.Select(&actions, "SELECT id, description FROM actions") err := store.db.Select(&actions, "SELECT id, description, created_at AS createdat, updated_at AS updatedat FROM actions")
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@@ -1,10 +1,16 @@
package models package models
type Action struct { import (
ID int64 "time"
Description string )
}
type Action struct {
func (m *Model) Actions() ([]*Action, error) { ID int64
return m.SelectActions() Description string
} CreatedAt time.Time
UpdatedAt time.Time
}
func (m *Model) Actions() ([]*Action, error) {
return m.SelectActions()
}