From e79ece15aa3d30e3997391791d4012973911695b Mon Sep 17 00:00:00 2001 From: Lukas Bachschwell Date: Fri, 16 Nov 2018 13:37:18 +0100 Subject: [PATCH] Adding Modified By relation --- Readme.md | 2 +- controllers/companyData.go | 1 + controllers/companyUser.go | 1 + controllers/contact.go | 1 + controllers/post.go | 1 + database/migrations/20181114_181043_company_user.go | 2 +- database/migrations/20181114_181913_company_data.go | 2 +- database/migrations/20181114_181919_post.go | 2 +- database/migrations/20181114_181923_contact.go | 2 +- models/companyData.go | 13 +++++++------ models/companyUser.go | 13 +++++++------ models/contact.go | 1 + models/post.go | 13 +++++++------ scripts/migrateTenants.go | 8 +++++++- 14 files changed, 38 insertions(+), 24 deletions(-) diff --git a/Readme.md b/Readme.md index 0ac32e1..bd4b3fb 100644 --- a/Readme.md +++ b/Readme.md @@ -24,7 +24,7 @@ Todo till we can fork this repo * ~~company delete needs to exist and update usercompanymap~~ * ~~last modified in update for all tables~~ * checking some roles in all endpoints -* modified by relation (companyuserid) +* modifiedby (companyuserid) relation * return error in case of not found ## Notes: diff --git a/controllers/companyData.go b/controllers/companyData.go index 14c0559..b146cda 100644 --- a/controllers/companyData.go +++ b/controllers/companyData.go @@ -139,6 +139,7 @@ func (c *CompanyDataController) Put() { id, _ := strconv.Atoi(idStr) v := models.CompanyData{Id: id} if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil { + v.ModifiedBy = int64(currentUser.Id) if err := models.UpdateCompanyDataById(o, &v); err == nil { c.ServeJSONSuccess("Ok") } else { diff --git a/controllers/companyUser.go b/controllers/companyUser.go index 8d1d3d6..65e6a4a 100644 --- a/controllers/companyUser.go +++ b/controllers/companyUser.go @@ -189,6 +189,7 @@ func (c *CompanyUserController) Put() { id, _ := strconv.Atoi(idStr) v := models.CompanyUser{Id: id} if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil { + v.ModifiedBy = int64(currentUser.Id) if err := models.UpdateCompanyUserById(o, &v); err == nil { c.ServeJSONSuccess("Ok") } else { diff --git a/controllers/contact.go b/controllers/contact.go index bd3246c..209c784 100644 --- a/controllers/contact.go +++ b/controllers/contact.go @@ -140,6 +140,7 @@ func (c *ContactController) Put() { id, _ := strconv.Atoi(idStr) v := models.Contact{Id: id} if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil { + v.ModifiedBy = int64(currentUser.Id) if err := models.UpdateContactById(o, &v); err == nil { c.ServeJSONSuccess("Ok") } else { diff --git a/controllers/post.go b/controllers/post.go index bfb9b16..7086d7e 100644 --- a/controllers/post.go +++ b/controllers/post.go @@ -149,6 +149,7 @@ func (c *PostController) Put() { id, _ := strconv.Atoi(idStr) v := models.Post{Id: id} if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil { + v.ModifiedBy = int64(currentUser.Id) if err := models.UpdatePostById(o, &v); err == nil { c.ServeJSONSuccess("Updated Post") } else { diff --git a/database/migrations/20181114_181043_company_user.go b/database/migrations/20181114_181043_company_user.go index 7a0b639..760baef 100644 --- a/database/migrations/20181114_181043_company_user.go +++ b/database/migrations/20181114_181043_company_user.go @@ -20,7 +20,7 @@ func init() { // Run the migrations func (m *CompanyUser_20181114_181043) Up() { // use m.SQL("CREATE TABLE ...") to make schema update - m.SQL("CREATE TABLE public.company_user ( id integer NOT NULL, name text NOT NULL, role smallint NOT NULL, profile jsonb NOT NULL, created timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL, modified timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL);") + m.SQL("CREATE TABLE public.company_user ( id integer NOT NULL, name text NOT NULL, role smallint NOT NULL, profile jsonb NOT NULL, \"modifiedBy\" bigint NOT NULL, created timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL, modified timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL);") m.SQL("CREATE SEQUENCE public.\"companyUser_id_seq\" AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1;") m.SQL("ALTER SEQUENCE public.\"companyUser_id_seq\" OWNED BY public.company_user.id;") m.SQL("ALTER TABLE ONLY public.company_user ALTER COLUMN id SET DEFAULT nextval('public.\"companyUser_id_seq\"'::regclass);") diff --git a/database/migrations/20181114_181913_company_data.go b/database/migrations/20181114_181913_company_data.go index 06548c6..12b9890 100644 --- a/database/migrations/20181114_181913_company_data.go +++ b/database/migrations/20181114_181913_company_data.go @@ -20,7 +20,7 @@ func init() { // Run the migrations func (m *CompanyData_20181114_181913) Up() { // use m.SQL("CREATE TABLE ...") to make schema update - m.SQL("CREATE TABLE public.company_data ( id integer NOT NULL, key text NOT NULL, value text, data jsonb, created timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL, modified timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL);") + m.SQL("CREATE TABLE public.company_data ( id integer NOT NULL, key text NOT NULL, value text, data jsonb, \"modifiedBy\" bigint NOT NULL, created timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL, modified timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL);") m.SQL("CREATE SEQUENCE public.\"companyData_id_seq\" AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1;") m.SQL("ALTER SEQUENCE public.\"companyData_id_seq\" OWNED BY public.company_data.id;") m.SQL("ALTER TABLE ONLY public.company_data ALTER COLUMN id SET DEFAULT nextval('public.\"companyData_id_seq\"'::regclass);") diff --git a/database/migrations/20181114_181919_post.go b/database/migrations/20181114_181919_post.go index e30f507..46d3453 100644 --- a/database/migrations/20181114_181919_post.go +++ b/database/migrations/20181114_181919_post.go @@ -20,7 +20,7 @@ func init() { // Run the migrations func (m *Post_20181114_181919) Up() { // use m.SQL("CREATE TABLE ...") to make schema update - m.SQL("CREATE TABLE public.post ( id integer NOT NULL, name text, data text NOT NULL, meta jsonb NOT NULL, created timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL, modified timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL);") + m.SQL("CREATE TABLE public.post ( id integer NOT NULL, name text, data text NOT NULL, meta jsonb NOT NULL, \"modifiedBy\" bigint NOT NULL, created timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL, modified timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL);") m.SQL("CREATE SEQUENCE public.post_id_seq AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1;") m.SQL("ALTER SEQUENCE public.post_id_seq OWNED BY public.post.id;") m.SQL("ALTER TABLE ONLY public.post ALTER COLUMN id SET DEFAULT nextval('public.post_id_seq'::regclass);") diff --git a/database/migrations/20181114_181923_contact.go b/database/migrations/20181114_181923_contact.go index b48dfa4..aafbe2d 100644 --- a/database/migrations/20181114_181923_contact.go +++ b/database/migrations/20181114_181923_contact.go @@ -20,7 +20,7 @@ func init() { // Run the migrations func (m *Contact_20181114_181923) Up() { // use m.SQL("CREATE TABLE ...") to make schema update - m.SQL("CREATE TABLE public.contact ( id integer NOT NULL, \"firstName\" text, \"lastName\" text NOT NULL, \"phoneNumber\" text, email text, \"lastContact\" date, meta jsonb NOT NULL, created timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL, modified timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL);") + m.SQL("CREATE TABLE public.contact ( id integer NOT NULL, \"firstName\" text, \"lastName\" text NOT NULL, \"phoneNumber\" text, email text, \"lastContact\" date, meta jsonb NOT NULL, \"modifiedBy\" bigint NOT NULL, created timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL, modified timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL);") m.SQL("CREATE SEQUENCE public.contact_id_seq AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1;") m.SQL("ALTER SEQUENCE public.contact_id_seq OWNED BY public.contact.id;") m.SQL("ALTER TABLE ONLY public.contact ALTER COLUMN id SET DEFAULT nextval('public.contact_id_seq'::regclass);") diff --git a/models/companyData.go b/models/companyData.go index 601b697..9adc307 100644 --- a/models/companyData.go +++ b/models/companyData.go @@ -11,12 +11,13 @@ import ( ) type CompanyData struct { - Id int `orm:"column(id);"` - Key string `orm:"column(key)"` - Value string `orm:"column(value);null"` - Data string `orm:"column(data);null"` - Created time.Time `orm:"column(created);type(timestamp with time zone);auto_now_add"` - Modified time.Time `orm:"column(modified);type(timestamp with time zone);auto_now_add"` + Id int `orm:"column(id);"` + Key string `orm:"column(key)"` + Value string `orm:"column(value);null"` + Data string `orm:"column(data);null"` + ModifiedBy int64 `orm:"column(modifiedBy);"` + Created time.Time `orm:"column(created);type(timestamp with time zone);auto_now_add"` + Modified time.Time `orm:"column(modified);type(timestamp with time zone);auto_now_add"` } func (t *CompanyData) TableName() string { diff --git a/models/companyUser.go b/models/companyUser.go index 13e4be2..5c58029 100644 --- a/models/companyUser.go +++ b/models/companyUser.go @@ -11,12 +11,13 @@ import ( ) type CompanyUser struct { - Id int `orm:"column(id);"` - Name string `orm:"column(name)"` - Role int16 `orm:"column(role)"` - Profile string `orm:"column(profile)"` - Created time.Time `orm:"column(created);type(timestamp with time zone);auto_now_add"` - Modified time.Time `orm:"column(modified);type(timestamp with time zone);auto_now_add"` + Id int `orm:"column(id);"` + Name string `orm:"column(name)"` + Role int16 `orm:"column(role)"` + Profile string `orm:"column(profile)"` + ModifiedBy int64 `orm:"column(modifiedBy);"` + Created time.Time `orm:"column(created);type(timestamp with time zone);auto_now_add"` + Modified time.Time `orm:"column(modified);type(timestamp with time zone);auto_now_add"` } func (t *CompanyUser) TableName() string { diff --git a/models/contact.go b/models/contact.go index 5f60314..babdbd0 100644 --- a/models/contact.go +++ b/models/contact.go @@ -18,6 +18,7 @@ type Contact struct { Email string `orm:"column(email);null"` LastContact time.Time `orm:"column(lastContact);type(date);null"` Meta string `orm:"column(meta)"` + ModifiedBy int64 `orm:"column(modifiedBy);"` Created time.Time `orm:"column(created);type(timestamp with time zone);auto_now_add"` Modified time.Time `orm:"column(modified);type(timestamp with time zone);auto_now_add"` } diff --git a/models/post.go b/models/post.go index 14b9f66..d33e26c 100644 --- a/models/post.go +++ b/models/post.go @@ -11,12 +11,13 @@ import ( ) type Post struct { - Id int `orm:"column(id);"` - Name string `orm:"column(name);null"` - Data string `orm:"column(data)"` - Meta string `orm:"column(meta)"` - Created time.Time `orm:"column(created);type(timestamp with time zone);auto_now_add"` - Modified time.Time `orm:"column(modified);type(timestamp with time zone);auto_now_add"` + Id int `orm:"column(id);"` + Name string `orm:"column(name);null"` + Data string `orm:"column(data)"` + Meta string `orm:"column(meta)"` + ModifiedBy int64 `orm:"column(modifiedBy);"` + Created time.Time `orm:"column(created);type(timestamp with time zone);auto_now_add"` + Modified time.Time `orm:"column(modified);type(timestamp with time zone);auto_now_add"` } func (t *Post) TableName() string { diff --git a/scripts/migrateTenants.go b/scripts/migrateTenants.go index 1f9eb78..882c3af 100644 --- a/scripts/migrateTenants.go +++ b/scripts/migrateTenants.go @@ -84,7 +84,13 @@ func migrateDbName(dbName, migType string) { if migType != "" { cmd = exec.Command(cmdString, "migrate", migType, connString, dir) } else { - cmd = exec.Command(cmdString, "migrate", connString, dir) + fmt.Println("no command") + if dir == "" { + cmd = exec.Command(cmdString, "migrate", connString) + } else { + cmd = exec.Command(cmdString, "migrate", connString, dir) + + } } // Combine stdout and stderr