From ebbea98248b72b4fa31e0d13ee0a55cdfc7b2fd8 Mon Sep 17 00:00:00 2001 From: Denis Gukov Date: Mon, 6 Sep 2021 17:55:26 +0500 Subject: [PATCH] fix(be): schedule migration --- .dredd/hooks/helpers.go | 2 +- db/sql/migrations/v2.7.13.sql | 12 +++++++++--- db/sql/schedule.go | 4 ++-- db/sql/template.go | 6 +++--- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/.dredd/hooks/helpers.go b/.dredd/hooks/helpers.go index de28a0c5..a94f17a0 100644 --- a/.dredd/hooks/helpers.go +++ b/.dredd/hooks/helpers.go @@ -25,7 +25,7 @@ var tablesShouldBeTruncated = [...]string { "project__inventory", "project__repository", "project__template", - "project__template_schedule", + "project__schedule", "project__user", "user", } diff --git a/db/sql/migrations/v2.7.13.sql b/db/sql/migrations/v2.7.13.sql index f6aa247f..7c509479 100644 --- a/db/sql/migrations/v2.7.13.sql +++ b/db/sql/migrations/v2.7.13.sql @@ -1,3 +1,9 @@ -alter table project__template_schedule rename to project__schedule; -alter table `project__schedule` add `id` integer primary key autoincrement; -alter table `project__schedule` add `project_id` int not null references project(`id`); +drop table project__template_schedule; + +create table `project__schedule` +( + `id` integer primary key autoincrement, + `template_id` int references project__template (`id`) on delete cascade, + `project_id` int not null references project (`id`) on delete cascade, + `cron_format` varchar(255) not null +); diff --git a/db/sql/schedule.go b/db/sql/schedule.go index da0822ac..93bed620 100644 --- a/db/sql/schedule.go +++ b/db/sql/schedule.go @@ -53,13 +53,13 @@ func (d *SqlDb) DeleteSchedule(projectID int, scheduleID int) error { } func (d *SqlDb) GetSchedules() (schedules []db.Schedule, err error) { - _, err = d.selectAll(&schedules, "select * from project__template_schedule where cron_format != ''") + _, err = d.selectAll(&schedules, "select * from project__schedule where cron_format != ''") return } func (d *SqlDb) GetTemplateSchedules(projectID int, templateID int) (schedules []db.Schedule, err error) { _, err = d.selectAll(&schedules, - "select * from project__template_schedule where project_id=? and template_id=?", + "select * from project__schedule where project_id=? and template_id=?", projectID, templateID) return diff --git a/db/sql/template.go b/db/sql/template.go index bbacf78a..42a1d592 100644 --- a/db/sql/template.go +++ b/db/sql/template.go @@ -55,20 +55,20 @@ func (d *SqlDb) UpdateTemplate(template db.Template) error { // //if template.CronFormat == "" { // _, err = d.exec( - // "delete from project__template_schedule where project_id =? and template_id=?", + // "delete from project__schedule where project_id =? and template_id=?", // template.ProjectID, // template.ID) //} else { // _, err = d.GetTemplateSchedules(template.ProjectID, template.ID) // if err == nil { // _, err = d.exec( - // "update project__template_schedule set cron_format=? where project_id =? and template_id=?", + // "update project__schedule set cron_format=? where project_id =? and template_id=?", // template.CronFormat, // template.ProjectID, // template.ID) // } else if err == db.ErrNotFound { // _, err = d.exec( - // "insert into project__template_schedule (template_id, cron_format) values (?, ?)", + // "insert into project__schedule (template_id, cron_format) values (?, ?)", // template.ID, // template.CronFormat) // }