2017-05-13 07:56:23 +00:00
|
|
|
package ddl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2017-05-13 14:38:26 +00:00
|
|
|
"errors"
|
2017-05-13 07:56:23 +00:00
|
|
|
|
|
|
|
"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 {
|
2017-06-01 12:30:49 +00:00
|
|
|
if err := checkPriorMigration(db); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-13 07:56:23 +00:00
|
|
|
switch driver {
|
|
|
|
case DriverMysql:
|
|
|
|
return mysql.Migrate(db)
|
|
|
|
case DriverPostgres:
|
|
|
|
return postgres.Migrate(db)
|
|
|
|
default:
|
|
|
|
return sqlite.Migrate(db)
|
|
|
|
}
|
|
|
|
}
|
2017-05-13 14:38:26 +00:00
|
|
|
|
|
|
|
// 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+")
|
|
|
|
}
|
2017-06-01 12:47:06 +00:00
|
|
|
db.Exec(createMigrationsTable)
|
|
|
|
db.Exec(legacyMigrationsImport)
|
2017-05-13 14:38:26 +00:00
|
|
|
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 = `
|
2017-06-01 12:47:06 +00:00
|
|
|
INSERT INTO migrations (name) VALUES
|
2017-05-13 14:38:26 +00:00
|
|
|
('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 = `
|
2017-06-01 12:47:06 +00:00
|
|
|
CREATE TABLE migrations (
|
2017-05-20 19:46:49 +00:00
|
|
|
name VARCHAR(255)
|
2017-05-13 14:38:26 +00:00
|
|
|
,UNIQUE(name)
|
|
|
|
)
|
|
|
|
`
|