Skip to content

Commit

Permalink
Merge pull request harness#2040 from bradrydzewski/database-migration
Browse files Browse the repository at this point in the history
Generated migration, remove Makefile
  • Loading branch information
bradrydzewski authored May 14, 2017
2 parents 02f9cf4 + 07a8a54 commit bf04c79
Show file tree
Hide file tree
Showing 106 changed files with 2,321 additions and 5,899 deletions.
25 changes: 21 additions & 4 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,27 @@ pipeline:
test:
image: golang:1.8
commands:
- make deps gen
- make test test_postgres test_mysql
- go get -u github.com/drone/drone-ui/dist
- go get -u golang.org/x/tools/cmd/cover
- go test -cover $(go list ./... | grep -v /vendor/)

dist:
test_postgres:
image: golang:1.8
environment:
- DATABASE_DRIVER=postgres
- DATABASE_CONFIG=host=postgres user=postgres dbname=postgres sslmode=disable
commands:
- go test github.com/drone/drone/store/datastore

test_mysql:
image: golang:1.8
environment:
- DATABASE_DRIVER=mysql
- DATABASE_CONFIG=root@tcp(mysql:3306)/test?parseTime=true
commands:
- go test github.com/drone/drone/store/datastore

build:
image: golang:1.8
commands:
- ./ci.sh
Expand Down Expand Up @@ -54,7 +71,7 @@ pipeline:

services:
postgres:
image: postgres:9.4.5
image: postgres:9.6
environment:
- POSTGRES_USER=postgres
mysql:
Expand Down
22 changes: 0 additions & 22 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,31 +1,9 @@
drone/drone
*.sqlite
*_gen.go
!store/datastore/sql/sqlite/sql_gen.go
!store/datastore/sql/mysql/sql_gen.go
!store/datastore/sql/postgres/sql_gen.go
!server/template/template_gen.go
#*.css
*.txt
*.zip
*.gz
*.out
*.min.js
*.deb
.env
temp/
extras/
release/

server/frontend/bower_components
server/frontend/build
server/swagger/files/*.json

# vendored repositories that we don't actually need
# to vendor. so exclude them

vendor/google.golang.org/cloud
vendor/github.com/bugagazavr

# IDE/Editor stuff
.idea
67 changes: 0 additions & 67 deletions MAINTAINERS

This file was deleted.

70 changes: 0 additions & 70 deletions Makefile

This file was deleted.

3 changes: 0 additions & 3 deletions store/datastore/ddl/ddl.go

This file was deleted.

105 changes: 105 additions & 0 deletions store/datastore/ddl/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package ddl

import (
"database/sql"
"errors"

"github.com/drone/drone/store/datastore/ddl/mysql"
"github.com/drone/drone/store/datastore/ddl/postgres"
"github.com/drone/drone/store/datastore/ddl/sqlite"
)

// Supported database drivers
const (
DriverSqlite = "sqlite3"
DriverMysql = "mysql"
DriverPostgres = "postgres"
)

// Migrate performs the database migration. If the migration fails
// and error is returned.
func Migrate(driver string, db *sql.DB) error {
switch driver {
case DriverMysql:
if err := checkPriorMigration(db); err != nil {
return err
}
return mysql.Migrate(db)
case DriverPostgres:
return postgres.Migrate(db)
default:
return sqlite.Migrate(db)
}
}

// we need to check and see if there was a previous migration
// for drone 0.6 or prior and migrate to the new migration
// system. Attempting to migrate from 0.5 or below to 0.7 or
// above will result in an error.
//
// this can be removed once we get to 1.0 with the reasonable
// expectation that people are no longer using 0.5.
func checkPriorMigration(db *sql.DB) error {
var none int
if err := db.QueryRow(legacyMigrationsExist).Scan(&none); err != nil {
// if no legacy migrations exist, this is a fresh install
// and we can proceed as normal.
return nil
}
if err := db.QueryRow(legacyMigrationsCurrent).Scan(&none); err != nil {
// this indicates an attempted upgrade from 0.5 or lower to
// version 0.7 or higher and will fail.
return errors.New("Please upgrade to 0.6 before upgrading to 0.7+")
}
if _, err := db.Exec(createMigrationsTable); err != nil {
return err
}
if _, err := db.Exec(legacyMigrationsImport); err != nil {
return err
}
return nil
}

var legacyMigrationsExist = `
SELECT 1
FROM gorp_migrations
LIMIT 1
`

var legacyMigrationsCurrent = `
SELECT 1
FROM gorp_migrations
WHERE id = '16.sql'
LIMIT 1
`

var legacyMigrationsImport = `
INSERT IGNORE INTO migrations (name) VALUES
('create-table-users')
,('create-table-repos')
,('create-table-builds')
,('create-index-builds-repo')
,('create-index-builds-author')
,('create-table-procs')
,('create-index-procs-build')
,('create-table-logs')
,('create-table-files')
,('create-index-files-builds')
,('create-index-files-procs')
,('create-table-secrets')
,('create-index-secrets-repo')
,('create-table-registry')
,('create-index-registry-repo')
,('create-table-config')
,('create-table-tasks')
,('create-table-agents')
,('create-table-senders')
,('create-index-sender-repos')
`

var createMigrationsTable = `
CREATE TABLE IF NOT EXISTS migrations (
name VARCHAR(512)
,UNIQUE(name)
)
`
12 changes: 0 additions & 12 deletions store/datastore/ddl/mysql/10.sql

This file was deleted.

8 changes: 0 additions & 8 deletions store/datastore/ddl/mysql/11.sql

This file was deleted.

18 changes: 0 additions & 18 deletions store/datastore/ddl/mysql/12.sql

This file was deleted.

Loading

0 comments on commit bf04c79

Please sign in to comment.