mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-12-30 10:11:23 +02:00
Integrate migrations with drone.
Also add migration to tests.
This commit is contained in:
parent
da8d4346ee
commit
4ef0f1b437
1
Makefile
1
Makefile
@ -41,6 +41,7 @@ test:
|
|||||||
go test -v github.com/drone/drone/pkg/channel
|
go test -v github.com/drone/drone/pkg/channel
|
||||||
go test -v github.com/drone/drone/pkg/database
|
go test -v github.com/drone/drone/pkg/database
|
||||||
go test -v github.com/drone/drone/pkg/database/encrypt
|
go test -v github.com/drone/drone/pkg/database/encrypt
|
||||||
|
go test -v github.com/drone/drone/pkg/database/migrate
|
||||||
go test -v github.com/drone/drone/pkg/database/testing
|
go test -v github.com/drone/drone/pkg/database/testing
|
||||||
go test -v github.com/drone/drone/pkg/mail
|
go test -v github.com/drone/drone/pkg/mail
|
||||||
go test -v github.com/drone/drone/pkg/model
|
go test -v github.com/drone/drone/pkg/model
|
||||||
|
@ -15,6 +15,7 @@ import (
|
|||||||
|
|
||||||
"github.com/drone/drone/pkg/channel"
|
"github.com/drone/drone/pkg/channel"
|
||||||
"github.com/drone/drone/pkg/database"
|
"github.com/drone/drone/pkg/database"
|
||||||
|
"github.com/drone/drone/pkg/database/migrate"
|
||||||
"github.com/drone/drone/pkg/handler"
|
"github.com/drone/drone/pkg/handler"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -55,8 +56,9 @@ func main() {
|
|||||||
// setup the database connection and register with the
|
// setup the database connection and register with the
|
||||||
// global database package.
|
// global database package.
|
||||||
func setupDatabase() {
|
func setupDatabase() {
|
||||||
// inform meddler we're using sqlite
|
// inform meddler and migration we're using sqlite
|
||||||
meddler.Default = meddler.SQLite
|
meddler.Default = meddler.SQLite
|
||||||
|
migrate.Driver = migrate.SQLite
|
||||||
|
|
||||||
// connect to the SQLite database
|
// connect to the SQLite database
|
||||||
db, err := sql.Open(driver, datasource)
|
db, err := sql.Open(driver, datasource)
|
||||||
@ -65,6 +67,9 @@ func setupDatabase() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
database.Set(db)
|
database.Set(db)
|
||||||
|
|
||||||
|
migration := migrate.New(db)
|
||||||
|
migration.All().Migrate()
|
||||||
}
|
}
|
||||||
|
|
||||||
// setup routes for static assets. These assets may
|
// setup routes for static assets. These assets may
|
||||||
|
@ -0,0 +1,23 @@
|
|||||||
|
package migrate
|
||||||
|
|
||||||
|
type Rev1 struct{}
|
||||||
|
|
||||||
|
var RenamePrivelegedToPrivileged = &Rev1{}
|
||||||
|
|
||||||
|
func (r *Rev1) Revision() int64 {
|
||||||
|
return 201402200603
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Rev1) Up(op Operation) error {
|
||||||
|
_, err := op.RenameColumns("repos", map[string]string{
|
||||||
|
"priveleged": "privileged",
|
||||||
|
})
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Rev1) Down(op Operation) error {
|
||||||
|
_, err := op.RenameColumns("repos", map[string]string{
|
||||||
|
"privileged": "priveleged",
|
||||||
|
})
|
||||||
|
return err
|
||||||
|
}
|
11
pkg/database/migrate/all.go
Normal file
11
pkg/database/migrate/all.go
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package migrate
|
||||||
|
|
||||||
|
func (m *Migration) All() *Migration {
|
||||||
|
|
||||||
|
// List all migrations here
|
||||||
|
m.Add(RenamePrivelegedToPrivileged)
|
||||||
|
|
||||||
|
// m.Add(...)
|
||||||
|
// ...
|
||||||
|
return m
|
||||||
|
}
|
@ -13,7 +13,7 @@ const repoTable = "repos"
|
|||||||
// SQL Queries to retrieve a list of all repos belonging to a User.
|
// SQL Queries to retrieve a list of all repos belonging to a User.
|
||||||
const repoStmt = `
|
const repoStmt = `
|
||||||
SELECT id, slug, host, owner, name, private, disabled, disabled_pr, scm, url, username, password,
|
SELECT id, slug, host, owner, name, private, disabled, disabled_pr, scm, url, username, password,
|
||||||
public_key, private_key, params, timeout, priveleged, created, updated, user_id, team_id
|
public_key, private_key, params, timeout, privileged, created, updated, user_id, team_id
|
||||||
FROM repos
|
FROM repos
|
||||||
WHERE user_id = ? AND team_id = 0
|
WHERE user_id = ? AND team_id = 0
|
||||||
ORDER BY slug ASC
|
ORDER BY slug ASC
|
||||||
@ -22,7 +22,7 @@ ORDER BY slug ASC
|
|||||||
// SQL Queries to retrieve a list of all repos belonging to a Team.
|
// SQL Queries to retrieve a list of all repos belonging to a Team.
|
||||||
const repoTeamStmt = `
|
const repoTeamStmt = `
|
||||||
SELECT id, slug, host, owner, name, private, disabled, disabled_pr, scm, url, username, password,
|
SELECT id, slug, host, owner, name, private, disabled, disabled_pr, scm, url, username, password,
|
||||||
public_key, private_key, params, timeout, priveleged, created, updated, user_id, team_id
|
public_key, private_key, params, timeout, privileged, created, updated, user_id, team_id
|
||||||
FROM repos
|
FROM repos
|
||||||
WHERE team_id = ?
|
WHERE team_id = ?
|
||||||
ORDER BY slug ASC
|
ORDER BY slug ASC
|
||||||
@ -31,7 +31,7 @@ ORDER BY slug ASC
|
|||||||
// SQL Queries to retrieve a repo by id.
|
// SQL Queries to retrieve a repo by id.
|
||||||
const repoFindStmt = `
|
const repoFindStmt = `
|
||||||
SELECT id, slug, host, owner, name, private, disabled, disabled_pr, scm, url, username, password,
|
SELECT id, slug, host, owner, name, private, disabled, disabled_pr, scm, url, username, password,
|
||||||
public_key, private_key, params, timeout, priveleged, created, updated, user_id, team_id
|
public_key, private_key, params, timeout, privileged, created, updated, user_id, team_id
|
||||||
FROM repos
|
FROM repos
|
||||||
WHERE id = ?
|
WHERE id = ?
|
||||||
`
|
`
|
||||||
@ -39,7 +39,7 @@ WHERE id = ?
|
|||||||
// SQL Queries to retrieve a repo by name.
|
// SQL Queries to retrieve a repo by name.
|
||||||
const repoFindSlugStmt = `
|
const repoFindSlugStmt = `
|
||||||
SELECT id, slug, host, owner, name, private, disabled, disabled_pr, scm, url, username, password,
|
SELECT id, slug, host, owner, name, private, disabled, disabled_pr, scm, url, username, password,
|
||||||
public_key, private_key, params, timeout, priveleged, created, updated, user_id, team_id
|
public_key, private_key, params, timeout, privileged, created, updated, user_id, team_id
|
||||||
FROM repos
|
FROM repos
|
||||||
WHERE slug = ?
|
WHERE slug = ?
|
||||||
`
|
`
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
|
|
||||||
"github.com/drone/drone/pkg/database"
|
"github.com/drone/drone/pkg/database"
|
||||||
"github.com/drone/drone/pkg/database/encrypt"
|
"github.com/drone/drone/pkg/database/encrypt"
|
||||||
|
"github.com/drone/drone/pkg/database/migrate"
|
||||||
. "github.com/drone/drone/pkg/model"
|
. "github.com/drone/drone/pkg/model"
|
||||||
|
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
@ -31,6 +32,7 @@ func init() {
|
|||||||
|
|
||||||
// notify meddler that we are working with sqlite
|
// notify meddler that we are working with sqlite
|
||||||
meddler.Default = meddler.SQLite
|
meddler.Default = meddler.SQLite
|
||||||
|
migrate.Driver = migrate.SQLite
|
||||||
}
|
}
|
||||||
|
|
||||||
func Setup() {
|
func Setup() {
|
||||||
@ -40,6 +42,9 @@ func Setup() {
|
|||||||
// make sure all the tables and indexes are created
|
// make sure all the tables and indexes are created
|
||||||
database.Set(db)
|
database.Set(db)
|
||||||
|
|
||||||
|
migration := migrate.New(db)
|
||||||
|
migration.All().Migrate()
|
||||||
|
|
||||||
// create dummy user data
|
// create dummy user data
|
||||||
user1 := User{
|
user1 := User{
|
||||||
Password: "$2a$10$b8d63QsTL38vx7lj0HEHfOdbu1PCAg6Gfca74UavkXooIBx9YxopS",
|
Password: "$2a$10$b8d63QsTL38vx7lj0HEHfOdbu1PCAg6Gfca74UavkXooIBx9YxopS",
|
||||||
|
@ -41,7 +41,7 @@ func (h UserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
// AdminHandler wraps the default http.HandlerFunc to include
|
// AdminHandler wraps the default http.HandlerFunc to include
|
||||||
// the currently authenticated User in the method signature,
|
// the currently authenticated User in the method signature,
|
||||||
// in addition to handling an error as the return value. It also
|
// in addition to handling an error as the return value. It also
|
||||||
// verifies the user has Administrative priveleges.
|
// verifies the user has Administrative privileges.
|
||||||
type AdminHandler func(w http.ResponseWriter, r *http.Request, user *User) error
|
type AdminHandler func(w http.ResponseWriter, r *http.Request, user *User) error
|
||||||
|
|
||||||
func (h AdminHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (h AdminHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
@ -51,7 +51,7 @@ func (h AdminHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// User MUST have administrative priveleges in order
|
// User MUST have administrative privileges in order
|
||||||
// to execute the handler.
|
// to execute the handler.
|
||||||
if user.Admin == false {
|
if user.Admin == false {
|
||||||
RenderNotFound(w)
|
RenderNotFound(w)
|
||||||
|
@ -88,9 +88,9 @@ type Repo struct {
|
|||||||
// before exceeding its timelimit and being killed.
|
// before exceeding its timelimit and being killed.
|
||||||
Timeout int64 `meddler:"timeout" json:"timeout"`
|
Timeout int64 `meddler:"timeout" json:"timeout"`
|
||||||
|
|
||||||
// Indicates the build should be executed in priveleged
|
// Indicates the build should be executed in privileged
|
||||||
// mode. This could, for example, be used to run Docker in Docker.
|
// mode. This could, for example, be used to run Docker in Docker.
|
||||||
Priveleged bool `meddler:"priveleged" json:"priveleged"`
|
Privileged bool `meddler:"privileged" json:"privileged"`
|
||||||
|
|
||||||
// Foreign keys signify the User that created
|
// Foreign keys signify the User that created
|
||||||
// the repository and team account linked to
|
// the repository and team account linked to
|
||||||
|
Loading…
Reference in New Issue
Block a user