Skip to content

Commit

Permalink
fix(be): migration 2.8.26 for postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
fiftin committed Jan 31, 2022
1 parent cb5ec3e commit 221454a
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 59 deletions.
1 change: 1 addition & 0 deletions db/Migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func GetMigrations() []Migration {
{Version: "2.8.25"},
{Version: "2.8.26"},
{Version: "2.8.36"},
{Version: "2.8.38"},
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package migrations
package bolt

import (
"encoding/json"
Expand Down
5 changes: 2 additions & 3 deletions db/bolt/Migration_2_8_28_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package bolt

import (
"encoding/json"
"github.com/ansible-semaphore/semaphore/db/bolt/migrations"
"go.etcd.io/bbolt"
"testing"
)
Expand Down Expand Up @@ -36,7 +35,7 @@ func TestMigration_2_8_28_Apply(t *testing.T) {
t.Fatal(err)
}

err = migrations.Migration_2_8_28{DB: store.db}.Apply()
err = Migration_2_8_28{DB: store.db}.Apply()
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -78,7 +77,7 @@ func TestMigration_2_8_28_Apply2(t *testing.T) {
t.Fatal(err)
}

err = migrations.Migration_2_8_28{DB: store.db}.Apply()
err = Migration_2_8_28{DB: store.db}.Apply()
if err != nil {
t.Fatal(err)
}
Expand Down
3 changes: 1 addition & 2 deletions db/bolt/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package bolt
import (
"encoding/json"
"github.com/ansible-semaphore/semaphore/db"
"github.com/ansible-semaphore/semaphore/db/bolt/migrations"
"go.etcd.io/bbolt"
)

Expand Down Expand Up @@ -37,7 +36,7 @@ func (d *BoltDb) IsMigrationApplied(migration db.Migration) (bool, error) {
func (d *BoltDb) ApplyMigration(migration db.Migration) (err error) {
switch migration.Version {
case "2.8.26":
err = migrations.Migration_2_8_28{DB: d.db}.Apply()
err = Migration_2_8_28{DB: d.db}.Apply()
}

if err != nil {
Expand Down
53 changes: 53 additions & 0 deletions db/sql/Migration_2_8_28.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package sql

import (
"github.com/go-gorp/gorp/v3"
"strings"
)

type Migration_2_8_26 struct {
DB *SqlDb
}

func (m Migration_2_8_26) Apply(tx *gorp.Transaction) error {
rows, err := tx.Query(m.DB.PrepareQuery("SELECT id, git_url FROM project__repository"))
if err != nil {
return err
}

repoUrls := make(map[string]string)

for rows.Next() {
var id, url string

err3 := rows.Scan(&id, &url)
if err3 != nil {
continue
}

repoUrls[id] = url
}

err = rows.Close()
if err != nil {
return err
}

for id, url := range repoUrls {
branch := "master"
parts := strings.Split(url, "#")
if len(parts) > 1 {
url, branch = parts[0], parts[1]
}
q := m.DB.PrepareQuery("UPDATE project__repository " +
"SET git_url = ?, git_branch = ? " +
"WHERE id = ?")
_, err = tx.Exec(q, url, branch, id)

if err != nil {
return err
}
}

return nil
}
12 changes: 6 additions & 6 deletions db/sql/SqlDb.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (d *SqlDb) prepareQueryWithDialect(query string, dialect gorp.Dialect) stri
return query
}

func (d *SqlDb) prepareQuery(query string) string {
func (d *SqlDb) PrepareQuery(query string) string {
return d.prepareQueryWithDialect(query, d.sql.Dialect)
}

Expand All @@ -102,7 +102,7 @@ func (d *SqlDb) insert(primaryKeyColumnName string, query string, args ...interf
case gorp.PostgresDialect:
query += " returning " + primaryKeyColumnName

err := d.sql.QueryRow(d.prepareQuery(query), args...).Scan(&insertId)
err := d.sql.QueryRow(d.PrepareQuery(query), args...).Scan(&insertId)

if err != nil {
return 0, err
Expand All @@ -125,16 +125,16 @@ func (d *SqlDb) insert(primaryKeyColumnName string, query string, args ...interf
}

func (d *SqlDb) exec(query string, args ...interface{}) (sql.Result, error) {
q := d.prepareQuery(query)
q := d.PrepareQuery(query)
return d.sql.Exec(q, args...)
}

func (d *SqlDb) selectOne(holder interface{}, query string, args ...interface{}) error {
return d.sql.SelectOne(holder, d.prepareQuery(query), args...)
return d.sql.SelectOne(holder, d.PrepareQuery(query), args...)
}

func (d *SqlDb) selectAll(i interface{}, query string, args ...interface{}) ([]interface{}, error) {
q := d.prepareQuery(query)
q := d.PrepareQuery(query)
return d.sql.Select(i, q, args...)
}

Expand Down Expand Up @@ -343,6 +343,6 @@ func (d *SqlDb) Sql() *gorp.DbMap {
}

func (d *SqlDb) IsInitialized() (bool, error) {
_, err := d.sql.SelectInt(d.prepareQuery("select count(1) from migrations"))
_, err := d.sql.SelectInt(d.PrepareQuery("select count(1) from migrations"))
return err == nil, nil
}
7 changes: 3 additions & 4 deletions db/sql/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/ansible-semaphore/semaphore/db"
"github.com/ansible-semaphore/semaphore/db/sql/migrations"
"github.com/go-gorp/gorp/v3"
"regexp"
"strings"
Expand Down Expand Up @@ -73,7 +72,7 @@ func (d *SqlDb) IsMigrationApplied(migration db.Migration) (bool, error) {
}

exists, err := d.sql.SelectInt(
d.prepareQuery("select count(1) as ex from migrations where version = ?"),
d.PrepareQuery("select count(1) as ex from migrations where version = ?"),
migration.Version)

if err != nil {
Expand Down Expand Up @@ -123,15 +122,15 @@ func (d *SqlDb) ApplyMigration(migration db.Migration) error {
}
}

_, err = tx.Exec(d.prepareQuery("insert into migrations(version, upgraded_date) values (?, ?)"), migration.Version, time.Now())
_, err = tx.Exec(d.PrepareQuery("insert into migrations(version, upgraded_date) values (?, ?)"), migration.Version, time.Now())
if err != nil {
handleRollbackError(tx.Rollback())
return err
}

switch migration.Version {
case "2.8.26":
err = migrations.Migration_2_8_26{Sql: d.sql}.Apply()
err = Migration_2_8_26{DB: d}.Apply(tx)
}

if err != nil {
Expand Down
38 changes: 0 additions & 38 deletions db/sql/migrations/v2.8.26.go

This file was deleted.

39 changes: 39 additions & 0 deletions db/sql/migrations/v2.8.38.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
delete
from project__schedule
where template_id is null;

delete
from project__schedule
where (select count(*) from project__template where project__template.id = project__schedule.template_id) = 0;

delete
from project__schedule
where (select count(*) from project where project.id = project__schedule.project_id) = 0;

update project__schedule
set repository_id = null
where repository_id is not null
and (select count(*) from project__repository where project__repository.id = project__schedule.repository_id) = 0;

alter table `project__schedule`
rename to `project__schedule_backup_8436583`;

create table project__schedule
(
id integer primary key autoincrement,
template_id int not null,
project_id int not null,
cron_format varchar(255) not null,
repository_id int null,
last_commit_hash varchar(40) null,

foreign key (`template_id`) references project__template(`id`) on delete cascade,
foreign key (`project_id`) references project(`id`) on delete cascade,
foreign key (`repository_id`) references project__repository(`id`)
);

insert into project__schedule
select *
from project__schedule_backup_8436583;

drop table project__schedule_backup_8436583;
2 changes: 1 addition & 1 deletion db/sql/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (d *SqlDb) DeleteProject(projectID int) error {
}

for _, statement := range statements {
_, err = tx.Exec(d.prepareQuery(statement), projectID)
_, err = tx.Exec(d.PrepareQuery(statement), projectID)

if err != nil {
err = tx.Rollback()
Expand Down
5 changes: 2 additions & 3 deletions db/sql/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (d *SqlDb) CreateAPIToken(token db.APIToken) (db.APIToken, error) {
}

func (d *SqlDb) GetAPIToken(tokenID string) (token db.APIToken, err error) {
err = d.selectOne(&token, d.prepareQuery("select * from user__token where id=? and expired=false"), tokenID)
err = d.selectOne(&token, d.PrepareQuery("select * from user__token where id=? and expired=false"), tokenID)

if err == sql.ErrNoRows {
err = db.ErrNotFound
Expand Down Expand Up @@ -56,12 +56,11 @@ func (d *SqlDb) TouchSession(userID int, sessionID int) error {
}

func (d *SqlDb) GetAPITokens(userID int) (tokens []db.APIToken, err error) {
_, err = d.selectAll(&tokens, d.prepareQuery("select * from user__token where user_id=?"), userID)
_, err = d.selectAll(&tokens, d.PrepareQuery("select * from user__token where user_id=?"), userID)

if err == sql.ErrNoRows {
err = db.ErrNotFound
}

return
}

2 changes: 1 addition & 1 deletion db/sql/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func (d *SqlDb) GetUsers(params db.RetrieveQueryParams) (users []db.User, err er
func (d *SqlDb) GetUserByLoginOrEmail(login string, email string) (existingUser db.User, err error) {
err = d.selectOne(
&existingUser,
d.prepareQuery("select * from `user` where email=? or username=?"),
d.PrepareQuery("select * from `user` where email=? or username=?"),
email, login)

if err == sql.ErrNoRows {
Expand Down

0 comments on commit 221454a

Please sign in to comment.