refactor: renamed codebase to "targets"
This commit is contained in:
parent
faec08f846
commit
102a7f8899
19 changed files with 205 additions and 191 deletions
|
|
@ -13,8 +13,12 @@ struct MigrationVersion {
|
|||
}
|
||||
|
||||
const (
|
||||
migrations_up = [$embed_file('migrations/001-initial/up.sql')]
|
||||
migrations_down = [$embed_file('migrations/001-initial/down.sql')]
|
||||
migrations_up = [
|
||||
$embed_file('migrations/001-initial/up.sql'),
|
||||
$embed_file('migrations/002-rename-to-targets/up.sql'),
|
||||
]
|
||||
migrations_down = [$embed_file('migrations/001-initial/down.sql'),
|
||||
$embed_file('migrations/002-rename-to-targets/down.sql')]
|
||||
)
|
||||
|
||||
// init initializes a database & adds the correct tables.
|
||||
|
|
|
|||
99
src/db/git.v
99
src/db/git.v
|
|
@ -1,99 +0,0 @@
|
|||
module db
|
||||
|
||||
import models { GitRepo, GitRepoArch, GitRepoFilter }
|
||||
|
||||
// get_git_repos returns all GitRepo's in the database.
|
||||
pub fn (db &VieterDb) get_git_repos(filter GitRepoFilter) []GitRepo {
|
||||
// This seems to currently be blocked by a bug in the ORM, I'll have to ask
|
||||
// around.
|
||||
if filter.repo != '' {
|
||||
res := sql db.conn {
|
||||
select from GitRepo where repo == filter.repo order by id limit filter.limit offset filter.offset
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
res := sql db.conn {
|
||||
select from GitRepo order by id limit filter.limit offset filter.offset
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
// get_git_repo tries to return a specific GitRepo.
|
||||
pub fn (db &VieterDb) get_git_repo(repo_id int) ?GitRepo {
|
||||
res := sql db.conn {
|
||||
select from GitRepo where id == repo_id
|
||||
}
|
||||
|
||||
// If a select statement fails, it returns a zeroed object. By
|
||||
// checking one of the required fields, we can see whether the query
|
||||
// returned a result or not.
|
||||
if res.id == 0 {
|
||||
return none
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
// add_git_repo inserts the given GitRepo into the database.
|
||||
pub fn (db &VieterDb) add_git_repo(repo GitRepo) {
|
||||
sql db.conn {
|
||||
insert repo into GitRepo
|
||||
}
|
||||
}
|
||||
|
||||
// delete_git_repo deletes the repo with the given ID from the database.
|
||||
pub fn (db &VieterDb) delete_git_repo(repo_id int) {
|
||||
sql db.conn {
|
||||
delete from GitRepo where id == repo_id
|
||||
delete from GitRepoArch where repo_id == repo_id
|
||||
}
|
||||
}
|
||||
|
||||
// update_git_repo updates any non-array values for a given GitRepo.
|
||||
pub fn (db &VieterDb) update_git_repo(repo_id int, params map[string]string) {
|
||||
mut values := []string{}
|
||||
|
||||
// TODO does this allow for SQL injection?
|
||||
$for field in GitRepo.fields {
|
||||
if field.name in params {
|
||||
// Any fields that are array types require their own update method
|
||||
$if field.typ is string {
|
||||
values << "$field.name = '${params[field.name]}'"
|
||||
}
|
||||
}
|
||||
}
|
||||
values_str := values.join(', ')
|
||||
// I think this is actual SQL & not the ORM language
|
||||
query := 'update GitRepo set $values_str where id == $repo_id'
|
||||
|
||||
db.conn.exec_none(query)
|
||||
}
|
||||
|
||||
// update_git_repo_archs updates a given GitRepo's arch value.
|
||||
pub fn (db &VieterDb) update_git_repo_archs(repo_id int, archs []GitRepoArch) {
|
||||
archs_with_id := archs.map(GitRepoArch{
|
||||
...it
|
||||
repo_id: repo_id
|
||||
})
|
||||
|
||||
sql db.conn {
|
||||
delete from GitRepoArch where repo_id == repo_id
|
||||
}
|
||||
|
||||
for arch in archs_with_id {
|
||||
sql db.conn {
|
||||
insert arch into GitRepoArch
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// git_repo_exists is a utility function that checks whether a repo with the
|
||||
// given id exists.
|
||||
pub fn (db &VieterDb) git_repo_exists(repo_id int) bool {
|
||||
db.get_git_repo(repo_id) or { return false }
|
||||
|
||||
return true
|
||||
}
|
||||
|
|
@ -7,8 +7,8 @@ import time
|
|||
pub fn (db &VieterDb) get_build_logs(filter BuildLogFilter) []BuildLog {
|
||||
mut where_parts := []string{}
|
||||
|
||||
if filter.repo != 0 {
|
||||
where_parts << 'repo_id == $filter.repo'
|
||||
if filter.target != 0 {
|
||||
where_parts << 'target_id == $filter.target'
|
||||
}
|
||||
|
||||
if filter.before != time.Time{} {
|
||||
|
|
@ -55,11 +55,11 @@ pub fn (db &VieterDb) get_build_logs(filter BuildLogFilter) []BuildLog {
|
|||
return res
|
||||
}
|
||||
|
||||
// get_build_logs_for_repo returns all BuildLog's in the database for a given
|
||||
// repo.
|
||||
pub fn (db &VieterDb) get_build_logs_for_repo(repo_id int) []BuildLog {
|
||||
// get_build_logs_for_target returns all BuildLog's in the database for a given
|
||||
// target.
|
||||
pub fn (db &VieterDb) get_build_logs_for_target(target_id int) []BuildLog {
|
||||
res := sql db.conn {
|
||||
select from BuildLog where repo_id == repo_id order by id
|
||||
select from BuildLog where target_id == target_id order by id
|
||||
}
|
||||
|
||||
return res
|
||||
|
|
|
|||
5
src/db/migrations/002-rename-to-targets/down.sql
Normal file
5
src/db/migrations/002-rename-to-targets/down.sql
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
ALTER TABLE Target RENAME TO GitRepo;
|
||||
ALTER TABLE TargetArch RENAME TO GitRepoArch;
|
||||
|
||||
ALTER TABLE GitRepoArch RENAME COLUMN target_id TO repo_id;
|
||||
ALTER TABLE BuildLog RENAME COLUMN target_id TO repo_id;
|
||||
5
src/db/migrations/002-rename-to-targets/up.sql
Normal file
5
src/db/migrations/002-rename-to-targets/up.sql
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
ALTER TABLE GitRepo RENAME TO Target;
|
||||
ALTER TABLE GitRepoArch RENAME TO TargetArch;
|
||||
|
||||
ALTER TABLE TargetArch RENAME COLUMN repo_id TO target_id;
|
||||
ALTER TABLE BuildLog RENAME COLUMN repo_id TO target_id;
|
||||
99
src/db/targets.v
Normal file
99
src/db/targets.v
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
module db
|
||||
|
||||
import models { Target, TargetArch, TargetFilter }
|
||||
|
||||
// get_targets returns all targets in the database.
|
||||
pub fn (db &VieterDb) get_targets(filter TargetFilter) []Target {
|
||||
// This seems to currently be blocked by a bug in the ORM, I'll have to ask
|
||||
// around.
|
||||
if filter.repo != '' {
|
||||
res := sql db.conn {
|
||||
select from Target where repo == filter.repo order by id limit filter.limit offset filter.offset
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
res := sql db.conn {
|
||||
select from Target order by id limit filter.limit offset filter.offset
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
// get_target tries to return a specific target.
|
||||
pub fn (db &VieterDb) get_target(target_id int) ?Target {
|
||||
res := sql db.conn {
|
||||
select from Target where id == target_id
|
||||
}
|
||||
|
||||
// If a select statement fails, it returns a zeroed object. By
|
||||
// checking one of the required fields, we can see whether the query
|
||||
// returned a result or not.
|
||||
if res.id == 0 {
|
||||
return none
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
// add_target inserts the given target into the database.
|
||||
pub fn (db &VieterDb) add_target(repo Target) {
|
||||
sql db.conn {
|
||||
insert repo into Target
|
||||
}
|
||||
}
|
||||
|
||||
// delete_target deletes the target with the given id from the database.
|
||||
pub fn (db &VieterDb) delete_target(target_id int) {
|
||||
sql db.conn {
|
||||
delete from Target where id == target_id
|
||||
delete from TargetArch where target_id == target_id
|
||||
}
|
||||
}
|
||||
|
||||
// update_target updates any non-array values for a given target.
|
||||
pub fn (db &VieterDb) update_target(target_id int, params map[string]string) {
|
||||
mut values := []string{}
|
||||
|
||||
// TODO does this allow for SQL injection?
|
||||
$for field in Target.fields {
|
||||
if field.name in params {
|
||||
// Any fields that are array types require their own update method
|
||||
$if field.typ is string {
|
||||
values << "$field.name = '${params[field.name]}'"
|
||||
}
|
||||
}
|
||||
}
|
||||
values_str := values.join(', ')
|
||||
// I think this is actual SQL & not the ORM language
|
||||
query := 'update Target set $values_str where id == $target_id'
|
||||
|
||||
db.conn.exec_none(query)
|
||||
}
|
||||
|
||||
// update_target_archs updates a given target's arch value.
|
||||
pub fn (db &VieterDb) update_target_archs(target_id int, archs []TargetArch) {
|
||||
archs_with_id := archs.map(TargetArch{
|
||||
...it
|
||||
target_id: target_id
|
||||
})
|
||||
|
||||
sql db.conn {
|
||||
delete from TargetArch where target_id == target_id
|
||||
}
|
||||
|
||||
for arch in archs_with_id {
|
||||
sql db.conn {
|
||||
insert arch into TargetArch
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// target_exists is a utility function that checks whether a target with the
|
||||
// given id exists.
|
||||
pub fn (db &VieterDb) target_exists(target_id int) bool {
|
||||
db.get_target(target_id) or { return false }
|
||||
|
||||
return true
|
||||
}
|
||||
Reference in a new issue