1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2025-01-17 17:45:03 +02:00

SQL changes for Postgres and MySQL

This commit is contained in:
Laszlo Fogas 2019-06-14 09:20:46 +02:00
parent 3f50cafe94
commit a18b7bb46e
14 changed files with 229 additions and 77 deletions

View File

@ -18,8 +18,8 @@ import (
"fmt" "fmt"
"testing" "testing"
"github.com/laszlocph/drone-oss-08/model"
"github.com/franela/goblin" "github.com/franela/goblin"
"github.com/laszlocph/drone-oss-08/model"
) )
func TestBuilds(t *testing.T) { func TestBuilds(t *testing.T) {

View File

@ -23,6 +23,9 @@ import (
func TestConfig(t *testing.T) { func TestConfig(t *testing.T) {
s := newTest() s := newTest()
defer func() { defer func() {
s.Exec("delete from repos")
s.Exec("delete from builds")
s.Exec("delete from procs")
s.Exec("delete from config") s.Exec("delete from config")
s.Close() s.Close()
}() }()
@ -30,11 +33,21 @@ func TestConfig(t *testing.T) {
var ( var (
data = "pipeline: [ { image: golang, commands: [ go build, go test ] } ]" data = "pipeline: [ { image: golang, commands: [ go build, go test ] } ]"
hash = "8d8647c9aa90d893bfb79dddbe901f03e258588121e5202632f8ae5738590b26" hash = "8d8647c9aa90d893bfb79dddbe901f03e258588121e5202632f8ae5738590b26"
buildID = int64(1)
) )
repo := &model.Repo{
UserID: 1,
FullName: "bradrydzewski/drone",
Owner: "bradrydzewski",
Name: "drone",
}
if err := s.CreateRepo(repo); err != nil {
t.Errorf("Unexpected error: insert repo: %s", err)
return
}
config := &model.Config{ config := &model.Config{
RepoID: 2, RepoID: repo.ID,
Data: data, Data: data,
Hash: hash, Hash: hash,
Name: "default", Name: "default",
@ -44,17 +57,27 @@ func TestConfig(t *testing.T) {
return return
} }
if err := s.BuildConfigCreate( build := &model.Build{
&model.BuildConfig{ RepoID: repo.ID,
ConfigID: config.ID, Status: model.StatusRunning,
BuildID: buildID, Commit: "85f8c029b902ed9400bc600bac301a0aadb144ac",
}, }
); err != nil { if err := s.CreateBuild(build); err != nil {
t.Errorf("Unexpected error: insert config: %s", err) t.Errorf("Unexpected error: insert build: %s", err)
return return
} }
config, err := s.ConfigFindIdentical(int64(2), hash) if err := s.BuildConfigCreate(
&model.BuildConfig{
ConfigID: config.ID,
BuildID: build.ID,
},
); err != nil {
t.Errorf("Unexpected error: insert build config: %s", err)
return
}
config, err := s.ConfigFindIdentical(repo.ID, hash)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
return return
@ -62,7 +85,7 @@ func TestConfig(t *testing.T) {
if got, want := config.ID, int64(1); got != want { if got, want := config.ID, int64(1); got != want {
t.Errorf("Want config id %d, got %d", want, got) t.Errorf("Want config id %d, got %d", want, got)
} }
if got, want := config.RepoID, int64(2); got != want { if got, want := config.RepoID, repo.ID; got != want {
t.Errorf("Want config repo id %d, got %d", want, got) t.Errorf("Want config repo id %d, got %d", want, got)
} }
if got, want := config.Data, data; got != want { if got, want := config.Data, data; got != want {
@ -75,7 +98,7 @@ func TestConfig(t *testing.T) {
t.Errorf("Want config name %s, got %s", want, got) t.Errorf("Want config name %s, got %s", want, got)
} }
loaded, err := s.ConfigsForBuild(buildID) loaded, err := s.ConfigsForBuild(build.ID)
if err != nil { if err != nil {
t.Errorf("Want config by id, got error %q", err) t.Errorf("Want config by id, got error %q", err)
return return
@ -88,9 +111,10 @@ func TestConfig(t *testing.T) {
func TestConfigApproved(t *testing.T) { func TestConfigApproved(t *testing.T) {
s := newTest() s := newTest()
defer func() { defer func() {
s.Exec("delete from config")
s.Exec("delete from builds")
s.Exec("delete from repos") s.Exec("delete from repos")
s.Exec("delete from builds")
s.Exec("delete from procs")
s.Exec("delete from config")
s.Close() s.Close()
}() }()
@ -100,7 +124,10 @@ func TestConfigApproved(t *testing.T) {
Owner: "bradrydzewski", Owner: "bradrydzewski",
Name: "drone", Name: "drone",
} }
s.CreateRepo(repo) if err := s.CreateRepo(repo); err != nil {
t.Errorf("Unexpected error: insert repo: %s", err)
return
}
var ( var (
data = "pipeline: [ { image: golang, commands: [ go build, go test ] } ]" data = "pipeline: [ { image: golang, commands: [ go build, go test ] } ]"
@ -122,16 +149,25 @@ func TestConfigApproved(t *testing.T) {
} }
) )
s.CreateBuild(buildBlocked) if err := s.CreateBuild(buildBlocked); err != nil {
s.CreateBuild(buildPending) t.Errorf("Unexpected error: insert build: %s", err)
return
}
if err := s.CreateBuild(buildPending); err != nil {
t.Errorf("Unexpected error: insert build: %s", err)
return
}
conf := &model.Config{ conf := &model.Config{
ID: int64(8),
RepoID: repo.ID, RepoID: repo.ID,
Data: data, Data: data,
Hash: hash, Hash: hash,
} }
if err := s.ConfigCreate(conf); err != nil {
t.Errorf("Unexpected error: insert config: %s", err)
return
}
buildConfig := &model.BuildConfig{ buildConfig := &model.BuildConfig{
ConfigID: int64(8), ConfigID: conf.ID,
BuildID: buildBlocked.ID, BuildID: buildBlocked.ID,
} }
if err := s.BuildConfigCreate(buildConfig); err != nil { if err := s.BuildConfigCreate(buildConfig); err != nil {
@ -146,13 +182,16 @@ func TestConfigApproved(t *testing.T) {
s.CreateBuild(buildRunning) s.CreateBuild(buildRunning)
conf2 := &model.Config{ conf2 := &model.Config{
ID: int64(9),
RepoID: repo.ID, RepoID: repo.ID,
Data: data, Data: data,
Hash: "xxx", Hash: "xxx",
} }
if err := s.ConfigCreate(conf2); err != nil {
t.Errorf("Unexpected error: insert config: %s", err)
return
}
buildConfig2 := &model.BuildConfig{ buildConfig2 := &model.BuildConfig{
ConfigID: int64(9), ConfigID: conf2.ID,
BuildID: buildRunning.ID, BuildID: buildRunning.ID,
} }
if err := s.BuildConfigCreate(buildConfig2); err != nil { if err := s.BuildConfigCreate(buildConfig2); err != nil {
@ -169,6 +208,9 @@ func TestConfigApproved(t *testing.T) {
func TestConfigIndexes(t *testing.T) { func TestConfigIndexes(t *testing.T) {
s := newTest() s := newTest()
defer func() { defer func() {
s.Exec("delete from repos")
s.Exec("delete from builds")
s.Exec("delete from procs")
s.Exec("delete from config") s.Exec("delete from config")
s.Close() s.Close()
}() }()

View File

@ -1,17 +1,3 @@
// Copyright 2018 Drone.IO Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mysql package mysql
import ( import (
@ -170,6 +156,18 @@ var migrations = []struct {
name: "alter-table-update-file-meta", name: "alter-table-update-file-meta",
stmt: alterTableUpdateFileMeta, stmt: alterTableUpdateFileMeta,
}, },
{
name: "create-table-build-config",
stmt: createTableBuildConfig,
},
{
name: "alter-table-add-config-name",
stmt: alterTableAddConfigName,
},
{
name: "update-table-set-config-name",
stmt: updateTableSetConfigName,
},
} }
// Migrate performs the database migration. If the migration fails // Migrate performs the database migration. If the migration fails
@ -636,3 +634,29 @@ UPDATE files SET
,file_meta_failed=0 ,file_meta_failed=0
,file_meta_skipped=0 ,file_meta_skipped=0
` `
//
// 019_create_table_build_config.sql
//
var createTableBuildConfig = `
CREATE TABLE IF NOT EXISTS build_config (
config_id INTEGER NOT NULL
,build_id INTEGER NOT NULL
,PRIMARY KEY (config_id, build_id)
,FOREIGN KEY (config_id) REFERENCES config (config_id)
,FOREIGN KEY (build_id) REFERENCES builds (build_id)
);
`
//
// 020_add_column_config_name.sql
//
var alterTableAddConfigName = `
ALTER TABLE config ADD COLUMN config_name TEXT
`
var updateTableSetConfigName = `
UPDATE config SET config_name = "drone"
`

View File

@ -0,0 +1,9 @@
-- name: create-table-build-config
CREATE TABLE IF NOT EXISTS build_config (
config_id INTEGER NOT NULL
,build_id INTEGER NOT NULL
,PRIMARY KEY (config_id, build_id)
,FOREIGN KEY (config_id) REFERENCES config (config_id)
,FOREIGN KEY (build_id) REFERENCES builds (build_id)
);

View File

@ -0,0 +1,7 @@
-- name: alter-table-add-config-name
ALTER TABLE config ADD COLUMN config_name TEXT
-- name: update-table-set-config-name
UPDATE config SET config_name = "drone"

View File

@ -1,17 +1,3 @@
// Copyright 2018 Drone.IO Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package postgres package postgres
import ( import (
@ -170,6 +156,18 @@ var migrations = []struct {
name: "alter-table-update-file-meta", name: "alter-table-update-file-meta",
stmt: alterTableUpdateFileMeta, stmt: alterTableUpdateFileMeta,
}, },
{
name: "create-table-build-config",
stmt: createTableBuildConfig,
},
{
name: "alter-table-add-config-name",
stmt: alterTableAddConfigName,
},
{
name: "update-table-set-config-name",
stmt: updateTableSetConfigName,
},
} }
// Migrate performs the database migration. If the migration fails // Migrate performs the database migration. If the migration fails
@ -530,7 +528,7 @@ CREATE INDEX IF NOT EXISTS sender_repo_ix ON senders (sender_repo_id);
// //
var alterTableAddRepoVisibility = ` var alterTableAddRepoVisibility = `
ALTER TABLE repos ADD COLUMN repo_visibility VARCHAR(50) ALTER TABLE repos ADD COLUMN repo_visibility VARCHAR(50);
` `
var updateTableSetRepoVisibility = ` var updateTableSetRepoVisibility = `
@ -538,7 +536,7 @@ UPDATE repos
SET repo_visibility = (CASE SET repo_visibility = (CASE
WHEN repo_private = false THEN 'public' WHEN repo_private = false THEN 'public'
ELSE 'private' ELSE 'private'
END) END);
` `
// //
@ -554,12 +552,13 @@ UPDATE repos SET repo_counter = (
SELECT max(build_number) SELECT max(build_number)
FROM builds FROM builds
WHERE builds.build_repo_id = repos.repo_id WHERE builds.build_repo_id = repos.repo_id
) );
` `
var updateTableSetRepoSeqDefault = ` var updateTableSetRepoSeqDefault = `
UPDATE repos SET repo_counter = 0 UPDATE repos SET repo_counter = 0
WHERE repo_counter IS NULL WHERE repo_counter IS NULL
;
` `
// //
@ -567,11 +566,11 @@ WHERE repo_counter IS NULL
// //
var alterTableAddRepoActive = ` var alterTableAddRepoActive = `
ALTER TABLE repos ADD COLUMN repo_active BOOLEAN ALTER TABLE repos ADD COLUMN repo_active BOOLEAN;
` `
var updateTableSetRepoActive = ` var updateTableSetRepoActive = `
UPDATE repos SET repo_active = true UPDATE repos SET repo_active = true;
` `
// //
@ -583,7 +582,7 @@ ALTER TABLE users ADD COLUMN user_synced INTEGER;
` `
var updateTableSetUserSynced = ` var updateTableSetUserSynced = `
UPDATE users SET user_synced = 0 UPDATE users SET user_synced = 0;
` `
// //
@ -615,19 +614,19 @@ CREATE INDEX IF NOT EXISTS ix_perms_user ON perms (perm_user_id);
// //
var alterTableAddFilePid = ` var alterTableAddFilePid = `
ALTER TABLE files ADD COLUMN file_pid INTEGER ALTER TABLE files ADD COLUMN file_pid INTEGER;
` `
var alterTableAddFileMetaPassed = ` var alterTableAddFileMetaPassed = `
ALTER TABLE files ADD COLUMN file_meta_passed INTEGER ALTER TABLE files ADD COLUMN file_meta_passed INTEGER;
` `
var alterTableAddFileMetaFailed = ` var alterTableAddFileMetaFailed = `
ALTER TABLE files ADD COLUMN file_meta_failed INTEGER ALTER TABLE files ADD COLUMN file_meta_failed INTEGER;
` `
var alterTableAddFileMetaSkipped = ` var alterTableAddFileMetaSkipped = `
ALTER TABLE files ADD COLUMN file_meta_skipped INTEGER ALTER TABLE files ADD COLUMN file_meta_skipped INTEGER;
` `
var alterTableUpdateFileMeta = ` var alterTableUpdateFileMeta = `
@ -635,4 +634,31 @@ UPDATE files SET
file_meta_passed=0 file_meta_passed=0
,file_meta_failed=0 ,file_meta_failed=0
,file_meta_skipped=0 ,file_meta_skipped=0
;
`
//
// 019_create_table_build_config.sql
//
var createTableBuildConfig = `
CREATE TABLE IF NOT EXISTS build_config (
config_id INTEGER NOT NULL
,build_id INTEGER NOT NULL
,PRIMARY KEY (config_id, build_id)
,FOREIGN KEY (config_id) REFERENCES config (config_id)
,FOREIGN KEY (build_id) REFERENCES builds (build_id)
);
`
//
// 020_add_column_config_name.sql
//
var alterTableAddConfigName = `
ALTER TABLE config ADD COLUMN config_name TEXT
`
var updateTableSetConfigName = `
UPDATE config SET config_name = 'drone'
` `

View File

@ -0,0 +1,9 @@
-- name: create-table-build-config
CREATE TABLE IF NOT EXISTS build_config (
config_id INTEGER NOT NULL
,build_id INTEGER NOT NULL
,PRIMARY KEY (config_id, build_id)
,FOREIGN KEY (config_id) REFERENCES config (config_id)
,FOREIGN KEY (build_id) REFERENCES builds (build_id)
);

View File

@ -0,0 +1,7 @@
-- name: alter-table-add-config-name
ALTER TABLE config ADD COLUMN config_name TEXT
-- name: update-table-set-config-name
UPDATE config SET config_name = 'drone'

View File

@ -659,5 +659,5 @@ ALTER TABLE config ADD COLUMN config_name TEXT
` `
var updateTableSetConfigName = ` var updateTableSetConfigName = `
UPDATE config SET config_name = "default" UPDATE config SET config_name = "drone"
` `

View File

@ -4,4 +4,4 @@ ALTER TABLE config ADD COLUMN config_name TEXT
-- name: update-table-set-config-name -- name: update-table-set-config-name
UPDATE config SET config_name = "default" UPDATE config SET config_name = "drone"

View File

@ -1,12 +1,14 @@
-- name: config-find-id -- name: config-find-id
SELECT SELECT
config_id config.config_id
,config_repo_id ,config_repo_id
,config_hash ,config_hash
,config_data ,config_data
,config_name
FROM config FROM config
WHERE config_id = ? LEFT JOIN build_config ON config.config_id = build_config.config_id
WHERE build_config.build_id = ?
-- name: config-find-repo-hash -- name: config-find-repo-hash
@ -15,6 +17,7 @@ SELECT
,config_repo_id ,config_repo_id
,config_hash ,config_hash
,config_data ,config_data
,config_name
FROM config FROM config
WHERE config_repo_id = ? WHERE config_repo_id = ?
AND config_hash = ? AND config_hash = ?
@ -23,6 +26,10 @@ WHERE config_repo_id = ?
SELECT build_id FROM builds SELECT build_id FROM builds
WHERE build_repo_id = ? WHERE build_repo_id = ?
AND build_config_id = ? AND build_id in (
SELECT build_id
FROM build_config
WHERE build_config.config_id = ?
)
AND build_status NOT IN ('blocked', 'pending') AND build_status NOT IN ('blocked', 'pending')
LIMIT 1 LIMIT 1

View File

@ -55,12 +55,14 @@ var index = map[string]string{
var configFindId = ` var configFindId = `
SELECT SELECT
config_id config.config_id
,config_repo_id ,config_repo_id
,config_hash ,config_hash
,config_data ,config_data
,config_name
FROM config FROM config
WHERE config_id = ? LEFT JOIN build_config ON config.config_id = build_config.config_id
WHERE build_config.build_id = ?
` `
var configFindRepoHash = ` var configFindRepoHash = `
@ -69,6 +71,7 @@ SELECT
,config_repo_id ,config_repo_id
,config_hash ,config_hash
,config_data ,config_data
,config_name
FROM config FROM config
WHERE config_repo_id = ? WHERE config_repo_id = ?
AND config_hash = ? AND config_hash = ?
@ -77,7 +80,11 @@ WHERE config_repo_id = ?
var configFindApproved = ` var configFindApproved = `
SELECT build_id FROM builds SELECT build_id FROM builds
WHERE build_repo_id = ? WHERE build_repo_id = ?
AND build_config_id = ? AND build_id in (
SELECT build_id
FROM build_config
WHERE build_config.config_id = ?
)
AND build_status NOT IN ('blocked', 'pending') AND build_status NOT IN ('blocked', 'pending')
LIMIT 1 LIMIT 1
` `

View File

@ -1,12 +1,14 @@
-- name: config-find-id -- name: config-find-id
SELECT SELECT
config_id config.config_id
,config_repo_id ,config_repo_id
,config_hash ,config_hash
,config_data ,config_data
,config_name
FROM config FROM config
WHERE config_id = $1 LEFT JOIN build_config ON config.config_id = build_config.config_id
WHERE build_config.build_id = $1
-- name: config-find-repo-hash -- name: config-find-repo-hash
@ -15,6 +17,7 @@ SELECT
,config_repo_id ,config_repo_id
,config_hash ,config_hash
,config_data ,config_data
,config_name
FROM config FROM config
WHERE config_repo_id = $1 WHERE config_repo_id = $1
AND config_hash = $2 AND config_hash = $2
@ -23,6 +26,10 @@ WHERE config_repo_id = $1
SELECT build_id FROM builds SELECT build_id FROM builds
WHERE build_repo_id = $1 WHERE build_repo_id = $1
AND build_config_id = $2 AND build_id in (
SELECT build_id
FROM build_config
WHERE build_config.config_id = $2
)
AND build_status NOT IN ('blocked', 'pending') AND build_status NOT IN ('blocked', 'pending')
LIMIT 1 LIMIT 1

View File

@ -55,12 +55,14 @@ var index = map[string]string{
var configFindId = ` var configFindId = `
SELECT SELECT
config_id config.config_id
,config_repo_id ,config_repo_id
,config_hash ,config_hash
,config_data ,config_data
,config_name
FROM config FROM config
WHERE config_id = $1 LEFT JOIN build_config ON config.config_id = build_config.config_id
WHERE build_config.build_id = $1
` `
var configFindRepoHash = ` var configFindRepoHash = `
@ -69,6 +71,7 @@ SELECT
,config_repo_id ,config_repo_id
,config_hash ,config_hash
,config_data ,config_data
,config_name
FROM config FROM config
WHERE config_repo_id = $1 WHERE config_repo_id = $1
AND config_hash = $2 AND config_hash = $2
@ -77,7 +80,11 @@ WHERE config_repo_id = $1
var configFindApproved = ` var configFindApproved = `
SELECT build_id FROM builds SELECT build_id FROM builds
WHERE build_repo_id = $1 WHERE build_repo_id = $1
AND build_config_id = $2 AND build_id in (
SELECT build_id
FROM build_config
WHERE build_config.config_id = $2
)
AND build_status NOT IN ('blocked', 'pending') AND build_status NOT IN ('blocked', 'pending')
LIMIT 1 LIMIT 1
` `
@ -95,7 +102,7 @@ WHERE repo_active = true
var countBuilds = ` var countBuilds = `
SELECT count(1) SELECT count(1)
FROM builds; FROM builds
` `
var feedLatestBuild = ` var feedLatestBuild = `