From b8c09865b7b7f5db3e1b76c2271cfb6fff965998 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Sat, 13 May 2017 16:38:26 +0200 Subject: [PATCH] migration improvements --- store/datastore/ddl/migrate.go | 76 ++++++++++++++ store/datastore/ddl/mysql/ddl_gen.go | 126 +++++++++++------------ store/datastore/ddl/postgres/ddl_gen.go | 126 +++++++++++------------ store/datastore/ddl/sqlite/ddl_gen.go | 131 ++++++++++++------------ 4 files changed, 269 insertions(+), 190 deletions(-) diff --git a/store/datastore/ddl/migrate.go b/store/datastore/ddl/migrate.go index 1faa3034b5..2af366d089 100644 --- a/store/datastore/ddl/migrate.go +++ b/store/datastore/ddl/migrate.go @@ -2,6 +2,7 @@ package ddl import ( "database/sql" + "errors" "github.com/drone/drone/store/datastore/ddl/mysql" "github.com/drone/drone/store/datastore/ddl/postgres" @@ -20,6 +21,9 @@ const ( 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) @@ -27,3 +31,75 @@ func Migrate(driver string, db *sql.DB) error { 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 OR 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) +) +` diff --git a/store/datastore/ddl/mysql/ddl_gen.go b/store/datastore/ddl/mysql/ddl_gen.go index 7b574b551c..75800edc81 100644 --- a/store/datastore/ddl/mysql/ddl_gen.go +++ b/store/datastore/ddl/mysql/ddl_gen.go @@ -6,87 +6,87 @@ import ( var migrations = []struct { name string - stmt []string + stmt string }{ { - name: "001_create_table_users.sql", - stmt: []string{ - createTableUsers, - }, + name: "create-table-users", + stmt: createTableUsers, }, { - name: "002_create_table_repos.sql", - stmt: []string{ - createTableRepos, - }, + name: "create-table-repos", + stmt: createTableRepos, }, { - name: "003_create_table_builds.sql", - stmt: []string{ - createTableBuilds, - createIndexBuildsRepo, - createIndexBuildsAuthor, - }, + name: "create-table-builds", + stmt: createTableBuilds, }, { - name: "004_create_table_procs.sql", - stmt: []string{ - createTableProcs, - createIndexProcsBuild, - }, + name: "create-index-builds-repo", + stmt: createIndexBuildsRepo, }, { - name: "005_create_table_logs.sql", - stmt: []string{ - createTableLogs, - }, + name: "create-index-builds-author", + stmt: createIndexBuildsAuthor, }, { - name: "006_create_table_files.sql", - stmt: []string{ - createTableFiles, - createIndexFilesBuilds, - createIndexFilesProcs, - }, + name: "create-table-procs", + stmt: createTableProcs, }, { - name: "007_create_table_secets.sql", - stmt: []string{ - createTableSecrets, - createIndexSecretsRepo, - }, + name: "create-index-procs-build", + stmt: createIndexProcsBuild, }, { - name: "008_create_table_registry.sql", - stmt: []string{ - createTableRegistry, - createIndexRegistryRepo, - }, + name: "create-table-logs", + stmt: createTableLogs, }, { - name: "009_create_table_config.sql", - stmt: []string{ - createTableConfig, - }, + name: "create-table-files", + stmt: createTableFiles, }, { - name: "010_create_table_tasks.sql", - stmt: []string{ - createTableTasks, - }, + name: "create-index-files-builds", + stmt: createIndexFilesBuilds, }, { - name: "011_create_table_agents.sql", - stmt: []string{ - createTableAgents, - }, + name: "create-index-files-procs", + stmt: createIndexFilesProcs, }, { - name: "012_create_table_senders.sql", - stmt: []string{ - createTableSenders, - createIndexSenderRepos, - }, + name: "create-table-secrets", + stmt: createTableSecrets, + }, + { + name: "create-index-secrets-repo", + stmt: createIndexSecretsRepo, + }, + { + name: "create-table-registry", + stmt: createTableRegistry, + }, + { + name: "create-index-registry-repo", + stmt: createIndexRegistryRepo, + }, + { + name: "create-table-config", + stmt: createTableConfig, + }, + { + name: "create-table-tasks", + stmt: createTableTasks, + }, + { + name: "create-table-agents", + stmt: createTableAgents, + }, + { + name: "create-table-senders", + stmt: createTableSenders, + }, + { + name: "create-index-sender-repos", + stmt: createIndexSenderRepos, }, } @@ -101,18 +101,18 @@ func Migrate(db *sql.DB) error { return err } for _, migration := range migrations { - _, ok := completed[migration.name] - if ok { + if _, ok := completed[migration.name]; ok { + continue } - for _, stmt := range migration.stmt { - if _, err := db.Exec(stmt); err != nil { - return err - } + + if _, err := db.Exec(migration.stmt); err != nil { + return err } if err := insertMigration(db, migration.name); err != nil { return err } + } return nil } diff --git a/store/datastore/ddl/postgres/ddl_gen.go b/store/datastore/ddl/postgres/ddl_gen.go index c587d6396c..81053a1edc 100644 --- a/store/datastore/ddl/postgres/ddl_gen.go +++ b/store/datastore/ddl/postgres/ddl_gen.go @@ -6,87 +6,87 @@ import ( var migrations = []struct { name string - stmt []string + stmt string }{ { - name: "001_create_table_users.sql", - stmt: []string{ - createTableUsers, - }, + name: "create-table-users", + stmt: createTableUsers, }, { - name: "002_create_table_repos.sql", - stmt: []string{ - createTableRepos, - }, + name: "create-table-repos", + stmt: createTableRepos, }, { - name: "003_create_table_builds.sql", - stmt: []string{ - createTableBuilds, - createIndexBuildsRepo, - createIndexBuildsAuthor, - }, + name: "create-table-builds", + stmt: createTableBuilds, }, { - name: "004_create_table_procs.sql", - stmt: []string{ - createTableProcs, - createIndexProcsBuild, - }, + name: "create-index-builds-repo", + stmt: createIndexBuildsRepo, }, { - name: "005_create_table_logs.sql", - stmt: []string{ - createTableLogs, - }, + name: "create-index-builds-author", + stmt: createIndexBuildsAuthor, }, { - name: "006_create_table_files.sql", - stmt: []string{ - createTableFiles, - createIndexFilesBuilds, - createIndexFilesProcs, - }, + name: "create-table-procs", + stmt: createTableProcs, }, { - name: "007_create_table_secets.sql", - stmt: []string{ - createTableSecrets, - createIndexSecretsRepo, - }, + name: "create-index-procs-build", + stmt: createIndexProcsBuild, }, { - name: "008_create_table_registry.sql", - stmt: []string{ - createTableRegistry, - createIndexRegistryRepo, - }, + name: "create-table-logs", + stmt: createTableLogs, }, { - name: "009_create_table_config.sql", - stmt: []string{ - createTableConfig, - }, + name: "create-table-files", + stmt: createTableFiles, }, { - name: "010_create_table_tasks.sql", - stmt: []string{ - createTableTasks, - }, + name: "create-index-files-builds", + stmt: createIndexFilesBuilds, }, { - name: "011_create_table_agents.sql", - stmt: []string{ - createTableAgents, - }, + name: "create-index-files-procs", + stmt: createIndexFilesProcs, }, { - name: "012_create_table_senders.sql", - stmt: []string{ - createTableSenders, - createIndexSenderRepos, - }, + name: "create-table-secrets", + stmt: createTableSecrets, + }, + { + name: "create-index-secrets-repo", + stmt: createIndexSecretsRepo, + }, + { + name: "create-table-registry", + stmt: createTableRegistry, + }, + { + name: "create-index-registry-repo", + stmt: createIndexRegistryRepo, + }, + { + name: "create-table-config", + stmt: createTableConfig, + }, + { + name: "create-table-tasks", + stmt: createTableTasks, + }, + { + name: "create-table-agents", + stmt: createTableAgents, + }, + { + name: "create-table-senders", + stmt: createTableSenders, + }, + { + name: "create-index-sender-repos", + stmt: createIndexSenderRepos, }, } @@ -101,18 +101,18 @@ func Migrate(db *sql.DB) error { return err } for _, migration := range migrations { - _, ok := completed[migration.name] - if ok { + if _, ok := completed[migration.name]; ok { + continue } - for _, stmt := range migration.stmt { - if _, err := db.Exec(stmt); err != nil { - return err - } + + if _, err := db.Exec(migration.stmt); err != nil { + return err } if err := insertMigration(db, migration.name); err != nil { return err } + } return nil } diff --git a/store/datastore/ddl/sqlite/ddl_gen.go b/store/datastore/ddl/sqlite/ddl_gen.go index 1a4b623807..d00be51403 100644 --- a/store/datastore/ddl/sqlite/ddl_gen.go +++ b/store/datastore/ddl/sqlite/ddl_gen.go @@ -6,88 +6,91 @@ import ( var migrations = []struct { name string - stmt []string + stmt string }{ { - name: "001_create_table_users.sql", - stmt: []string{ - createTableUsers, - }, + name: "create-table-users", + stmt: createTableUsers, }, { - name: "002_create_table_repos.sql", - stmt: []string{ - createTableRepos, - }, + name: "create-table-repos", + stmt: createTableRepos, }, { - name: "003_create_table_builds.sql", - stmt: []string{ - createTableBuilds, - createIndexBuildsRepo, - createIndexBuildsAuthor, - createIndexBuildsStatus, - }, + name: "create-table-builds", + stmt: createTableBuilds, }, { - name: "004_create_table_procs.sql", - stmt: []string{ - createTableProcs, - createIndexProcsBuild, - }, + name: "create-index-builds-repo", + stmt: createIndexBuildsRepo, }, { - name: "005_create_table_logs.sql", - stmt: []string{ - createTableLogs, - }, + name: "create-index-builds-author", + stmt: createIndexBuildsAuthor, }, { - name: "006_create_table_files.sql", - stmt: []string{ - createTableFiles, - createIndexFilesBuilds, - createIndexFilesProcs, - }, + name: "create-index-builds-status", + stmt: createIndexBuildsStatus, }, { - name: "007_create_table_secets.sql", - stmt: []string{ - createTableSecrets, - createIndexSecretsRepo, - }, + name: "create-table-procs", + stmt: createTableProcs, }, { - name: "008_create_table_registry.sql", - stmt: []string{ - createTableRegistry, - createIndexRegistryRepo, - }, + name: "create-index-procs-build", + stmt: createIndexProcsBuild, }, { - name: "009_create_table_config.sql", - stmt: []string{ - createTableConfig, - }, + name: "create-table-logs", + stmt: createTableLogs, }, { - name: "010_create_table_tasks.sql", - stmt: []string{ - createTableTasks, - }, + name: "create-table-files", + stmt: createTableFiles, }, { - name: "011_create_table_agents.sql", - stmt: []string{ - createTableAgents, - }, + name: "create-index-files-builds", + stmt: createIndexFilesBuilds, }, { - name: "012_create_table_senders.sql", - stmt: []string{ - createTableSenders, - createIndexSenderRepos, - }, + name: "create-index-files-procs", + stmt: createIndexFilesProcs, + }, + { + name: "create-table-secrets", + stmt: createTableSecrets, + }, + { + name: "create-index-secrets-repo", + stmt: createIndexSecretsRepo, + }, + { + name: "create-table-registry", + stmt: createTableRegistry, + }, + { + name: "create-index-registry-repo", + stmt: createIndexRegistryRepo, + }, + { + name: "create-table-config", + stmt: createTableConfig, + }, + { + name: "create-table-tasks", + stmt: createTableTasks, + }, + { + name: "create-table-agents", + stmt: createTableAgents, + }, + { + name: "create-table-senders", + stmt: createTableSenders, + }, + { + name: "create-index-sender-repos", + stmt: createIndexSenderRepos, }, } @@ -102,18 +105,18 @@ func Migrate(db *sql.DB) error { return err } for _, migration := range migrations { - _, ok := completed[migration.name] - if ok { + if _, ok := completed[migration.name]; ok { + continue } - for _, stmt := range migration.stmt { - if _, err := db.Exec(stmt); err != nil { - return err - } + + if _, err := db.Exec(migration.stmt); err != nil { + return err } if err := insertMigration(db, migration.name); err != nil { return err } + } return nil }