feat(be): add migration for new template and schedule fields

This commit is contained in:
Denis Gukov 2022-01-19 02:09:42 +05:00
parent ba8ea4c650
commit 4c706f5876
11 changed files with 62 additions and 91 deletions

View File

@ -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:"-"`
}

View File

@ -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)

View File

@ -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
//}

View File

@ -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},
}
}

View File

@ -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);

View File

@ -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
}

View File

@ -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)
}

View File

@ -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
//}

View File

@ -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"`
}

View File

@ -1,5 +1,5 @@
<template>
<div>
<div class="pb-6">
<v-dialog
v-model="editDialog"
hide-overlay
@ -12,10 +12,12 @@
<v-text-field
label="Name"
v-model="editedVar.name"
required
/>
<v-text-field
label="Title"
v-model="editedVar.title"
required
/>
</v-form>
</v-card-text>

View File

@ -180,7 +180,7 @@
<v-textarea
outlined
v-model="item.description"
label="Description"
label="Description (Optional)"
:disabled="formSaving"
rows="5"
></v-textarea>
@ -205,7 +205,7 @@
-->
<v-select
v-model="item.view_id"
label="View"
label="View (Optional)"
clearable
:items="views"
item-value="id"
@ -216,7 +216,7 @@
<v-text-field
class="mt-6"
v-model="cronFormat"
label="Cron"
label="Cron (Optional)"
:disabled="formSaving"
placeholder="Example: * 1 * * * *"
v-if="schedules == null || schedules.length <= 1"
@ -226,7 +226,7 @@
<v-select
v-model="cronRepositoryId"
label="Cron Condition Repository"
label="Cron Condition Repository (Optional)"
placeholder="Cron checks new commit before run"
:items="repositories"
item-value="id"