From 4c706f5876a456f23e2d7251854cb8c93b32ec26 Mon Sep 17 00:00:00 2001 From: Denis Gukov Date: Wed, 19 Jan 2022 02:09:42 +0500 Subject: [PATCH] feat(be): add migration for new template and schedule fields --- db/Schedule.go | 2 +- db/Store.go | 2 ++ db/bolt/user.go | 28 ----------------- db/sql/Version.go | 1 + db/sql/migrations/v2.8.25.sql | 4 +++ db/sql/schedule.go | 24 ++++++++++++--- db/sql/template.go | 46 ++++++++++++++++------------ db/sql/user.go | 31 ------------------- util/config.go | 3 +- web2/src/components/SurveyVars.vue | 4 ++- web2/src/components/TemplateForm.vue | 8 ++--- 11 files changed, 62 insertions(+), 91 deletions(-) create mode 100644 db/sql/migrations/v2.8.25.sql diff --git a/db/Schedule.go b/db/Schedule.go index c030730c..9f53e34a 100644 --- a/db/Schedule.go +++ b/db/Schedule.go @@ -6,5 +6,5 @@ type Schedule struct { TemplateID int `db:"template_id" json:"template_id"` CronFormat string `db:"cron_format" json:"cron_format"` RepositoryID *int `db:"repository_id" json:"repository_id"` - LastCommitHash *string `db:"last_commit_hash" json:"last_commit_hash"` + LastCommitHash *string `db:"last_commit_hash" json:"-"` } diff --git a/db/Store.go b/db/Store.go index e325d828..4cbf3646 100644 --- a/db/Store.go +++ b/db/Store.go @@ -100,6 +100,8 @@ type Store interface { GetUser(userID int) (User, error) GetUserByLoginOrEmail(login string, email string) (User, error) + // CreatePlaceholderUser creates placeholder user which can be used + // for register first admin user. CreatePlaceholderUser() error GetPlaceholderUser() (User, error) diff --git a/db/bolt/user.go b/db/bolt/user.go index b066437a..3154da10 100644 --- a/db/bolt/user.go +++ b/db/bolt/user.go @@ -223,31 +223,3 @@ func (d *BoltDb) GetPlaceholderUser() (placeholderUser db.User, err error) { err = db.ErrNotFound return } - -//func (d *BoltDb) HasPlaceholderUser() (bool, error) { -// _, err := d.getPlaceholderUser() -// -// if err == nil { -// return true, nil -// } -// -// if err == db.ErrNotFound { -// return false, nil -// } -// -// return false, err -//} -// -//func (d *BoltDb) ReplacePlaceholderUser(user db.UserWithPwd) (newUser db.User, err error) { -// placeholder, err := d.getPlaceholderUser() -// if err != nil { -// return -// } -// user.ID = placeholder.ID -// err = d.UpdateUser(user) -// if err != nil { -// return -// } -// newUser = user.User -// return -//} diff --git a/db/sql/Version.go b/db/sql/Version.go index 3c5b7db6..a02b0d5d 100644 --- a/db/sql/Version.go +++ b/db/sql/Version.go @@ -88,5 +88,6 @@ func init() { {Major: 2, Minor: 8, Patch: 7}, {Major: 2, Minor: 8, Patch: 8}, {Major: 2, Minor: 8, Patch: 20}, + {Major: 2, Minor: 8, Patch: 25}, } } diff --git a/db/sql/migrations/v2.8.25.sql b/db/sql/migrations/v2.8.25.sql new file mode 100644 index 00000000..11a9af86 --- /dev/null +++ b/db/sql/migrations/v2.8.25.sql @@ -0,0 +1,4 @@ +alter table `project__template` add survey_vars longtext; +alter table `project__template` add autorun boolean default false; +alter table `project__schedule` add repository_id int null references project__repository(`id`) on delete set null; +alter table `project__schedule` add last_commit_hash varchar(40); diff --git a/db/sql/schedule.go b/db/sql/schedule.go index ce64a4d8..22b27a53 100644 --- a/db/sql/schedule.go +++ b/db/sql/schedule.go @@ -8,11 +8,12 @@ import ( func (d *SqlDb) CreateSchedule(schedule db.Schedule) (newSchedule db.Schedule, err error) { insertID, err := d.insert( "id", - "insert into project__schedule (project_id, template_id, cron_format)" + - "values (?, ?, ?)", + "insert into project__schedule (project_id, template_id, cron_format, repository_id)"+ + "values (?, ?, ?, ?)", schedule.ProjectID, schedule.TemplateID, - schedule.CronFormat) + schedule.CronFormat, + schedule.RepositoryID) if err != nil { return @@ -24,12 +25,25 @@ func (d *SqlDb) CreateSchedule(schedule db.Schedule) (newSchedule db.Schedule, e return } +func (d *SqlDb) SetScheduleLastCommitHash(projectID int, scheduleID int, lastCommentHash string) error { + _, err := d.exec("update project__schedule set "+ + "last_commit_hash=? "+ + "where project_id=? and id=?", + lastCommentHash, + projectID, + scheduleID) + return err +} + func (d *SqlDb) UpdateSchedule(schedule db.Schedule) error { - _, err := d.exec("update project__schedule set cron_format=? where project_id=? and id=?", + _, err := d.exec("update project__schedule set "+ + "cron_format=? "+ + "repository_id=? "+ + "where project_id=? and id=?", schedule.CronFormat, + schedule.RepositoryID, schedule.ProjectID, schedule.ID) - return err } diff --git a/db/sql/template.go b/db/sql/template.go index 769fc697..f3bbc633 100644 --- a/db/sql/template.go +++ b/db/sql/template.go @@ -15,10 +15,10 @@ func (d *SqlDb) CreateTemplate(template db.Template) (newTemplate db.Template, e insertID, err := d.insert( "id", - "insert into project__template (project_id, inventory_id, repository_id, environment_id, " + - "alias, playbook, arguments, override_args, description, vault_key_id, `type`, start_version," + - "build_template_id, view_id)" + - "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", + "insert into project__template (project_id, inventory_id, repository_id, environment_id, "+ + "alias, playbook, arguments, override_args, description, vault_key_id, `type`, start_version,"+ + "build_template_id, view_id, autorun, survey_vars)"+ + "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", template.ProjectID, template.InventoryID, template.RepositoryID, @@ -32,7 +32,9 @@ func (d *SqlDb) CreateTemplate(template db.Template) (newTemplate db.Template, e template.Type, template.StartVersion, template.BuildTemplateID, - template.ViewID) + template.ViewID, + template.Autorun, + template.SurveyVariables) if err != nil { return @@ -57,20 +59,22 @@ func (d *SqlDb) UpdateTemplate(template db.Template) error { return err } - _, err = d.exec("update project__template set " + - "inventory_id=?, " + - "repository_id=?, " + - "environment_id=?, " + - "alias=?, " + - "playbook=?, " + - "arguments=?, " + - "override_args=?, " + - "description=?, " + - "vault_key_id=?, " + - "`type`=?, " + - "start_version=?," + - "build_template_id=?, " + - "view_id=? " + + _, err = d.exec("update project__template set "+ + "inventory_id=?, "+ + "repository_id=?, "+ + "environment_id=?, "+ + "alias=?, "+ + "playbook=?, "+ + "arguments=?, "+ + "override_args=?, "+ + "description=?, "+ + "vault_key_id=?, "+ + "`type`=?, "+ + "start_version=?,"+ + "build_template_id=?, "+ + "view_id=? "+ + "autorun=? "+ + "survey_vars=? "+ "where removed = false and id=? and project_id=?", template.InventoryID, template.RepositoryID, @@ -85,6 +89,8 @@ func (d *SqlDb) UpdateTemplate(template db.Template) error { template.StartVersion, template.BuildTemplateID, template.ViewID, + template.Autorun, + template.SurveyVariables, template.ID, template.ProjectID, ) @@ -153,7 +159,7 @@ func (d *SqlDb) getTemplates(projectID int, viewID *int, params db.RetrieveQuery return } -func (d *SqlDb) GetTemplates(projectID int, params db.RetrieveQueryParams) ( []db.Template, error) { +func (d *SqlDb) GetTemplates(projectID int, params db.RetrieveQueryParams) ([]db.Template, error) { return d.getTemplates(projectID, nil, params) } diff --git a/db/sql/user.go b/db/sql/user.go index 2da64163..d4cde161 100644 --- a/db/sql/user.go +++ b/db/sql/user.go @@ -225,39 +225,8 @@ func (d *SqlDb) CreatePlaceholderUser() error { func (d *SqlDb) GetPlaceholderUser() (user db.User, err error) { err = d.selectOne(&user, d.prepareQuery("select id from `user` where username=''")) - if err == sql.ErrNoRows { err = db.ErrNotFound } - return } - -// -//func (d *SqlDb) HasPlaceholderUser() (bool, error) { -// _, err := d.getPlaceholderUser() -// -// if err == nil { -// return true, nil -// } -// -// if err == db.ErrNotFound { -// return false, nil -// } -// -// return false, err -//} -// -//func (d *SqlDb) ReplacePlaceholderUser(user db.UserWithPwd) (newUser db.User, err error) { -// placeholder, err := d.getPlaceholderUser() -// if err != nil { -// return -// } -// user.ID = placeholder.ID -// err = d.UpdateUser(user) -// if err != nil { -// return -// } -// newUser = user.User -// return -//} diff --git a/util/config.go b/util/config.go index c7ac8f67..4eeecbc8 100644 --- a/util/config.go +++ b/util/config.go @@ -124,7 +124,8 @@ type ConfigType struct { // Default both via environment variables and via extra vars. VariablesPassingMethod VariablesPassingMethod `json:"variables_passing_method"` - // RegisterFirstUser allows register new user from web interface if no user exists in database. + // RegisterFirstUser allows register new user from web interface + // in 10 minutes after installation. RegisterFirstUser bool `json:"register_first_user"` } diff --git a/web2/src/components/SurveyVars.vue b/web2/src/components/SurveyVars.vue index 83a23776..d3d4ba19 100644 --- a/web2/src/components/SurveyVars.vue +++ b/web2/src/components/SurveyVars.vue @@ -1,5 +1,5 @@