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"
"testing"
"github.com/laszlocph/drone-oss-08/model"
"github.com/franela/goblin"
"github.com/laszlocph/drone-oss-08/model"
)
func TestBuilds(t *testing.T) {

View file

@ -23,18 +23,31 @@ import (
func TestConfig(t *testing.T) {
s := newTest()
defer func() {
s.Exec("delete from repos")
s.Exec("delete from builds")
s.Exec("delete from procs")
s.Exec("delete from config")
s.Close()
}()
var (
data = "pipeline: [ { image: golang, commands: [ go build, go test ] } ]"
hash = "8d8647c9aa90d893bfb79dddbe901f03e258588121e5202632f8ae5738590b26"
buildID = int64(1)
data = "pipeline: [ { image: golang, commands: [ go build, go test ] } ]"
hash = "8d8647c9aa90d893bfb79dddbe901f03e258588121e5202632f8ae5738590b26"
)
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{
RepoID: 2,
RepoID: repo.ID,
Data: data,
Hash: hash,
Name: "default",
@ -44,17 +57,27 @@ func TestConfig(t *testing.T) {
return
}
if err := s.BuildConfigCreate(
&model.BuildConfig{
ConfigID: config.ID,
BuildID: buildID,
},
); err != nil {
t.Errorf("Unexpected error: insert config: %s", err)
build := &model.Build{
RepoID: repo.ID,
Status: model.StatusRunning,
Commit: "85f8c029b902ed9400bc600bac301a0aadb144ac",
}
if err := s.CreateBuild(build); err != nil {
t.Errorf("Unexpected error: insert build: %s", err)
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 {
t.Error(err)
return
@ -62,7 +85,7 @@ func TestConfig(t *testing.T) {
if got, want := config.ID, int64(1); got != want {
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)
}
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)
}
loaded, err := s.ConfigsForBuild(buildID)
loaded, err := s.ConfigsForBuild(build.ID)
if err != nil {
t.Errorf("Want config by id, got error %q", err)
return
@ -88,9 +111,10 @@ func TestConfig(t *testing.T) {
func TestConfigApproved(t *testing.T) {
s := newTest()
defer func() {
s.Exec("delete from config")
s.Exec("delete from builds")
s.Exec("delete from repos")
s.Exec("delete from builds")
s.Exec("delete from procs")
s.Exec("delete from config")
s.Close()
}()
@ -100,7 +124,10 @@ func TestConfigApproved(t *testing.T) {
Owner: "bradrydzewski",
Name: "drone",
}
s.CreateRepo(repo)
if err := s.CreateRepo(repo); err != nil {
t.Errorf("Unexpected error: insert repo: %s", err)
return
}
var (
data = "pipeline: [ { image: golang, commands: [ go build, go test ] } ]"
@ -122,16 +149,25 @@ func TestConfigApproved(t *testing.T) {
}
)
s.CreateBuild(buildBlocked)
s.CreateBuild(buildPending)
if err := s.CreateBuild(buildBlocked); err != nil {
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{
ID: int64(8),
RepoID: repo.ID,
Data: data,
Hash: hash,
}
if err := s.ConfigCreate(conf); err != nil {
t.Errorf("Unexpected error: insert config: %s", err)
return
}
buildConfig := &model.BuildConfig{
ConfigID: int64(8),
ConfigID: conf.ID,
BuildID: buildBlocked.ID,
}
if err := s.BuildConfigCreate(buildConfig); err != nil {
@ -146,13 +182,16 @@ func TestConfigApproved(t *testing.T) {
s.CreateBuild(buildRunning)
conf2 := &model.Config{
ID: int64(9),
RepoID: repo.ID,
Data: data,
Hash: "xxx",
}
if err := s.ConfigCreate(conf2); err != nil {
t.Errorf("Unexpected error: insert config: %s", err)
return
}
buildConfig2 := &model.BuildConfig{
ConfigID: int64(9),
ConfigID: conf2.ID,
BuildID: buildRunning.ID,
}
if err := s.BuildConfigCreate(buildConfig2); err != nil {
@ -169,6 +208,9 @@ func TestConfigApproved(t *testing.T) {
func TestConfigIndexes(t *testing.T) {
s := newTest()
defer func() {
s.Exec("delete from repos")
s.Exec("delete from builds")
s.Exec("delete from procs")
s.Exec("delete from config")
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
import (
@ -170,6 +156,18 @@ var migrations = []struct {
name: "alter-table-update-file-meta",
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
@ -636,3 +634,29 @@ UPDATE files SET
,file_meta_failed=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
import (
@ -170,6 +156,18 @@ var migrations = []struct {
name: "alter-table-update-file-meta",
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
@ -530,7 +528,7 @@ CREATE INDEX IF NOT EXISTS sender_repo_ix ON senders (sender_repo_id);
//
var alterTableAddRepoVisibility = `
ALTER TABLE repos ADD COLUMN repo_visibility VARCHAR(50)
ALTER TABLE repos ADD COLUMN repo_visibility VARCHAR(50);
`
var updateTableSetRepoVisibility = `
@ -538,7 +536,7 @@ UPDATE repos
SET repo_visibility = (CASE
WHEN repo_private = false THEN 'public'
ELSE 'private'
END)
END);
`
//
@ -554,12 +552,13 @@ UPDATE repos SET repo_counter = (
SELECT max(build_number)
FROM builds
WHERE builds.build_repo_id = repos.repo_id
)
);
`
var updateTableSetRepoSeqDefault = `
UPDATE repos SET repo_counter = 0
WHERE repo_counter IS NULL
;
`
//
@ -567,11 +566,11 @@ WHERE repo_counter IS NULL
//
var alterTableAddRepoActive = `
ALTER TABLE repos ADD COLUMN repo_active BOOLEAN
ALTER TABLE repos ADD COLUMN repo_active BOOLEAN;
`
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 = `
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 = `
ALTER TABLE files ADD COLUMN file_pid INTEGER
ALTER TABLE files ADD COLUMN file_pid INTEGER;
`
var alterTableAddFileMetaPassed = `
ALTER TABLE files ADD COLUMN file_meta_passed INTEGER
ALTER TABLE files ADD COLUMN file_meta_passed INTEGER;
`
var alterTableAddFileMetaFailed = `
ALTER TABLE files ADD COLUMN file_meta_failed INTEGER
ALTER TABLE files ADD COLUMN file_meta_failed INTEGER;
`
var alterTableAddFileMetaSkipped = `
ALTER TABLE files ADD COLUMN file_meta_skipped INTEGER
ALTER TABLE files ADD COLUMN file_meta_skipped INTEGER;
`
var alterTableUpdateFileMeta = `
@ -635,4 +634,31 @@ UPDATE files SET
file_meta_passed=0
,file_meta_failed=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 = `
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
UPDATE config SET config_name = "default"
UPDATE config SET config_name = "drone"

View file

@ -1,12 +1,14 @@
-- name: config-find-id
SELECT
config_id
config.config_id
,config_repo_id
,config_hash
,config_data
,config_name
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
@ -15,6 +17,7 @@ SELECT
,config_repo_id
,config_hash
,config_data
,config_name
FROM config
WHERE config_repo_id = ?
AND config_hash = ?
@ -23,6 +26,10 @@ WHERE config_repo_id = ?
SELECT build_id FROM builds
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')
LIMIT 1

View file

@ -55,12 +55,14 @@ var index = map[string]string{
var configFindId = `
SELECT
config_id
config.config_id
,config_repo_id
,config_hash
,config_data
,config_name
FROM config
WHERE config_id = ?
LEFT JOIN build_config ON config.config_id = build_config.config_id
WHERE build_config.build_id = ?
`
var configFindRepoHash = `
@ -69,6 +71,7 @@ SELECT
,config_repo_id
,config_hash
,config_data
,config_name
FROM config
WHERE config_repo_id = ?
AND config_hash = ?
@ -77,7 +80,11 @@ WHERE config_repo_id = ?
var configFindApproved = `
SELECT build_id FROM builds
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')
LIMIT 1
`

View file

@ -1,12 +1,14 @@
-- name: config-find-id
SELECT
config_id
config.config_id
,config_repo_id
,config_hash
,config_data
,config_name
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
@ -15,6 +17,7 @@ SELECT
,config_repo_id
,config_hash
,config_data
,config_name
FROM config
WHERE config_repo_id = $1
AND config_hash = $2
@ -23,6 +26,10 @@ WHERE config_repo_id = $1
SELECT build_id FROM builds
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')
LIMIT 1

View file

@ -55,12 +55,14 @@ var index = map[string]string{
var configFindId = `
SELECT
config_id
config.config_id
,config_repo_id
,config_hash
,config_data
,config_name
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 = `
@ -69,6 +71,7 @@ SELECT
,config_repo_id
,config_hash
,config_data
,config_name
FROM config
WHERE config_repo_id = $1
AND config_hash = $2
@ -77,7 +80,11 @@ WHERE config_repo_id = $1
var configFindApproved = `
SELECT build_id FROM builds
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')
LIMIT 1
`
@ -95,7 +102,7 @@ WHERE repo_active = true
var countBuilds = `
SELECT count(1)
FROM builds;
FROM builds
`
var feedLatestBuild = `