From cea842bed593f3576caa740b4bdab3a14b8d32c5 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Sat, 13 May 2017 09:56:23 +0200 Subject: [PATCH] use generated migration --- store/datastore/ddl/ddl.go | 3 - store/datastore/ddl/migrate.go | 29 + store/datastore/ddl/mysql/10.sql | 12 - store/datastore/ddl/mysql/11.sql | 8 - store/datastore/ddl/mysql/12.sql | 18 - store/datastore/ddl/mysql/13.sql | 45 - store/datastore/ddl/mysql/15.sql | 11 - store/datastore/ddl/mysql/1_init.sql | 124 - store/datastore/ddl/mysql/2.sql | 12 - store/datastore/ddl/mysql/3.sql | 32 - store/datastore/ddl/mysql/4.sql | 9 - store/datastore/ddl/mysql/5.sql | 12 - store/datastore/ddl/mysql/7.sql | 19 - store/datastore/ddl/mysql/8.sql | 7 - store/datastore/ddl/mysql/9.sql | 12 - store/datastore/ddl/mysql/ddl.go | 3 + .../mysql/files/001_create_table_users.sql | 16 + .../mysql/files/002_create_table_repos.sql | 26 + .../mysql/files/003_create_table_builds.sql | 44 + .../mysql/files/004_create_table_procs.sql | 24 + .../ddl/mysql/files/005_create_table_logs.sql | 9 + .../mysql/files/006_create_table_files.sql | 22 + .../mysql/files/007_create_table_secets.sql | 18 + .../mysql/files/008_create_table_registry.sql | 17 + .../009_create_table_config.sql} | 11 +- .../mysql/files/010_create_table_tasks.sql | 7 + .../011_create_table_agents.sql} | 11 +- .../012_create_table_senders.sql} | 15 +- store/datastore/ddl/postgres/10.sql | 12 - store/datastore/ddl/postgres/11.sql | 8 - store/datastore/ddl/postgres/12.sql | 18 - store/datastore/ddl/postgres/13.sql | 47 - store/datastore/ddl/postgres/14.sql | 22 - store/datastore/ddl/postgres/15.sql | 11 - store/datastore/ddl/postgres/16.sql | 17 - store/datastore/ddl/postgres/1_init.sql | 126 - store/datastore/ddl/postgres/2.sql | 12 - store/datastore/ddl/postgres/3.sql | 32 - store/datastore/ddl/postgres/4.sql | 9 - store/datastore/ddl/postgres/5.sql | 16 - store/datastore/ddl/postgres/7.sql | 19 - store/datastore/ddl/postgres/8.sql | 7 - store/datastore/ddl/postgres/9.sql | 12 - store/datastore/ddl/postgres/ddl.go | 3 + .../postgres/files/001_create_table_users.sql | 16 + .../postgres/files/002_create_table_repos.sql | 26 + .../files/003_create_table_builds.sql | 44 + .../postgres/files/004_create_table_procs.sql | 24 + .../postgres/files/005_create_table_logs.sql | 9 + .../postgres/files/006_create_table_files.sql | 22 + .../files/007_create_table_secets.sql | 18 + .../files/008_create_table_registry.sql | 17 + .../files/009_create_table_config.sql | 10 + .../postgres/files/010_create_table_tasks.sql | 7 + .../011_create_table_agents.sql} | 11 +- .../files/012_create_table_senders.sql | 15 + store/datastore/ddl/sqlite/ddl.go | 3 + .../sqlite/files/001_create_table_users.sql | 15 + .../sqlite/files/002_create_table_repos.sql | 25 + .../sqlite/files/003_create_table_builds.sql | 48 + .../sqlite/files/004_create_table_procs.sql | 23 + .../sqlite/files/005_create_table_logs.sql | 8 + .../sqlite/files/006_create_table_files.sql | 21 + .../sqlite/files/007_create_table_secets.sql | 17 + .../files/008_create_table_registry.sql | 17 + .../sqlite/files/009_create_table_config.sql | 9 + .../sqlite/files/010_create_table_tasks.sql | 7 + .../files/011_create_table_agents.sql} | 9 +- .../sqlite/files/012_create_table_senders.sql | 15 + store/datastore/ddl/sqlite/files/XXX_init.xxx | 242 ++ store/datastore/ddl/sqlite3/10.sql | 12 - store/datastore/ddl/sqlite3/11.sql | 8 - store/datastore/ddl/sqlite3/12.sql | 18 - store/datastore/ddl/sqlite3/13.sql | 46 - store/datastore/ddl/sqlite3/14.sql | 22 - store/datastore/ddl/sqlite3/15.sql | 11 - store/datastore/ddl/sqlite3/16.sql | 17 - store/datastore/ddl/sqlite3/1_init.sql | 135 -- store/datastore/ddl/sqlite3/2.sql | 12 - store/datastore/ddl/sqlite3/3.sql | 34 - store/datastore/ddl/sqlite3/4.sql | 9 - store/datastore/ddl/sqlite3/5.sql | 16 - store/datastore/ddl/sqlite3/7.sql | 19 - store/datastore/ddl/sqlite3/8.sql | 7 - store/datastore/ddl/sqlite3/9.sql | 12 - store/datastore/store.go | 9 +- .../github.com/rubenv/sql-migrate/README.md | 245 -- vendor/github.com/rubenv/sql-migrate/doc.go | 199 -- .../github.com/rubenv/sql-migrate/migrate.go | 475 ---- .../rubenv/sql-migrate/sqlparse/README.md | 28 - .../rubenv/sql-migrate/sqlparse/sqlparse.go | 128 - vendor/gopkg.in/gorp.v1/LICENSE | 22 - vendor/gopkg.in/gorp.v1/Makefile | 6 - vendor/gopkg.in/gorp.v1/README.md | 672 ------ vendor/gopkg.in/gorp.v1/dialect.go | 692 ------ vendor/gopkg.in/gorp.v1/errors.go | 26 - vendor/gopkg.in/gorp.v1/gorp.go | 2085 ----------------- vendor/gopkg.in/gorp.v1/test_all.sh | 22 - vendor/vendor.json | 15 - 99 files changed, 891 insertions(+), 5736 deletions(-) delete mode 100644 store/datastore/ddl/ddl.go create mode 100644 store/datastore/ddl/migrate.go delete mode 100644 store/datastore/ddl/mysql/10.sql delete mode 100644 store/datastore/ddl/mysql/11.sql delete mode 100644 store/datastore/ddl/mysql/12.sql delete mode 100644 store/datastore/ddl/mysql/13.sql delete mode 100644 store/datastore/ddl/mysql/15.sql delete mode 100644 store/datastore/ddl/mysql/1_init.sql delete mode 100644 store/datastore/ddl/mysql/2.sql delete mode 100644 store/datastore/ddl/mysql/3.sql delete mode 100644 store/datastore/ddl/mysql/4.sql delete mode 100644 store/datastore/ddl/mysql/5.sql delete mode 100644 store/datastore/ddl/mysql/7.sql delete mode 100644 store/datastore/ddl/mysql/8.sql delete mode 100644 store/datastore/ddl/mysql/9.sql create mode 100644 store/datastore/ddl/mysql/ddl.go create mode 100644 store/datastore/ddl/mysql/files/001_create_table_users.sql create mode 100644 store/datastore/ddl/mysql/files/002_create_table_repos.sql create mode 100644 store/datastore/ddl/mysql/files/003_create_table_builds.sql create mode 100644 store/datastore/ddl/mysql/files/004_create_table_procs.sql create mode 100644 store/datastore/ddl/mysql/files/005_create_table_logs.sql create mode 100644 store/datastore/ddl/mysql/files/006_create_table_files.sql create mode 100644 store/datastore/ddl/mysql/files/007_create_table_secets.sql create mode 100644 store/datastore/ddl/mysql/files/008_create_table_registry.sql rename store/datastore/ddl/mysql/{16.sql => files/009_create_table_config.sql} (51%) create mode 100644 store/datastore/ddl/mysql/files/010_create_table_tasks.sql rename store/datastore/ddl/mysql/{6.sql => files/011_create_table_agents.sql} (62%) rename store/datastore/ddl/mysql/{14.sql => files/012_create_table_senders.sql} (52%) delete mode 100644 store/datastore/ddl/postgres/10.sql delete mode 100644 store/datastore/ddl/postgres/11.sql delete mode 100644 store/datastore/ddl/postgres/12.sql delete mode 100644 store/datastore/ddl/postgres/13.sql delete mode 100644 store/datastore/ddl/postgres/14.sql delete mode 100644 store/datastore/ddl/postgres/15.sql delete mode 100644 store/datastore/ddl/postgres/16.sql delete mode 100644 store/datastore/ddl/postgres/1_init.sql delete mode 100644 store/datastore/ddl/postgres/2.sql delete mode 100644 store/datastore/ddl/postgres/3.sql delete mode 100644 store/datastore/ddl/postgres/4.sql delete mode 100644 store/datastore/ddl/postgres/5.sql delete mode 100644 store/datastore/ddl/postgres/7.sql delete mode 100644 store/datastore/ddl/postgres/8.sql delete mode 100644 store/datastore/ddl/postgres/9.sql create mode 100644 store/datastore/ddl/postgres/ddl.go create mode 100644 store/datastore/ddl/postgres/files/001_create_table_users.sql create mode 100644 store/datastore/ddl/postgres/files/002_create_table_repos.sql create mode 100644 store/datastore/ddl/postgres/files/003_create_table_builds.sql create mode 100644 store/datastore/ddl/postgres/files/004_create_table_procs.sql create mode 100644 store/datastore/ddl/postgres/files/005_create_table_logs.sql create mode 100644 store/datastore/ddl/postgres/files/006_create_table_files.sql create mode 100644 store/datastore/ddl/postgres/files/007_create_table_secets.sql create mode 100644 store/datastore/ddl/postgres/files/008_create_table_registry.sql create mode 100644 store/datastore/ddl/postgres/files/009_create_table_config.sql create mode 100644 store/datastore/ddl/postgres/files/010_create_table_tasks.sql rename store/datastore/ddl/postgres/{6.sql => files/011_create_table_agents.sql} (60%) create mode 100644 store/datastore/ddl/postgres/files/012_create_table_senders.sql create mode 100644 store/datastore/ddl/sqlite/ddl.go create mode 100644 store/datastore/ddl/sqlite/files/001_create_table_users.sql create mode 100644 store/datastore/ddl/sqlite/files/002_create_table_repos.sql create mode 100644 store/datastore/ddl/sqlite/files/003_create_table_builds.sql create mode 100644 store/datastore/ddl/sqlite/files/004_create_table_procs.sql create mode 100644 store/datastore/ddl/sqlite/files/005_create_table_logs.sql create mode 100644 store/datastore/ddl/sqlite/files/006_create_table_files.sql create mode 100644 store/datastore/ddl/sqlite/files/007_create_table_secets.sql create mode 100644 store/datastore/ddl/sqlite/files/008_create_table_registry.sql create mode 100644 store/datastore/ddl/sqlite/files/009_create_table_config.sql create mode 100644 store/datastore/ddl/sqlite/files/010_create_table_tasks.sql rename store/datastore/ddl/{sqlite3/6.sql => sqlite/files/011_create_table_agents.sql} (71%) create mode 100644 store/datastore/ddl/sqlite/files/012_create_table_senders.sql create mode 100644 store/datastore/ddl/sqlite/files/XXX_init.xxx delete mode 100644 store/datastore/ddl/sqlite3/10.sql delete mode 100644 store/datastore/ddl/sqlite3/11.sql delete mode 100644 store/datastore/ddl/sqlite3/12.sql delete mode 100644 store/datastore/ddl/sqlite3/13.sql delete mode 100644 store/datastore/ddl/sqlite3/14.sql delete mode 100644 store/datastore/ddl/sqlite3/15.sql delete mode 100644 store/datastore/ddl/sqlite3/16.sql delete mode 100644 store/datastore/ddl/sqlite3/1_init.sql delete mode 100644 store/datastore/ddl/sqlite3/2.sql delete mode 100644 store/datastore/ddl/sqlite3/3.sql delete mode 100644 store/datastore/ddl/sqlite3/4.sql delete mode 100644 store/datastore/ddl/sqlite3/5.sql delete mode 100644 store/datastore/ddl/sqlite3/7.sql delete mode 100644 store/datastore/ddl/sqlite3/8.sql delete mode 100644 store/datastore/ddl/sqlite3/9.sql delete mode 100644 vendor/github.com/rubenv/sql-migrate/README.md delete mode 100644 vendor/github.com/rubenv/sql-migrate/doc.go delete mode 100644 vendor/github.com/rubenv/sql-migrate/migrate.go delete mode 100644 vendor/github.com/rubenv/sql-migrate/sqlparse/README.md delete mode 100644 vendor/github.com/rubenv/sql-migrate/sqlparse/sqlparse.go delete mode 100644 vendor/gopkg.in/gorp.v1/LICENSE delete mode 100644 vendor/gopkg.in/gorp.v1/Makefile delete mode 100644 vendor/gopkg.in/gorp.v1/README.md delete mode 100644 vendor/gopkg.in/gorp.v1/dialect.go delete mode 100644 vendor/gopkg.in/gorp.v1/errors.go delete mode 100644 vendor/gopkg.in/gorp.v1/gorp.go delete mode 100755 vendor/gopkg.in/gorp.v1/test_all.sh diff --git a/store/datastore/ddl/ddl.go b/store/datastore/ddl/ddl.go deleted file mode 100644 index 89eefbf3a..000000000 --- a/store/datastore/ddl/ddl.go +++ /dev/null @@ -1,3 +0,0 @@ -package ddl - -//go:generate go-bindata -pkg ddl -o ddl_gen.go sqlite3/ mysql/ postgres/ diff --git a/store/datastore/ddl/migrate.go b/store/datastore/ddl/migrate.go new file mode 100644 index 000000000..1faa3034b --- /dev/null +++ b/store/datastore/ddl/migrate.go @@ -0,0 +1,29 @@ +package ddl + +import ( + "database/sql" + + "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: + return mysql.Migrate(db) + case DriverPostgres: + return postgres.Migrate(db) + default: + return sqlite.Migrate(db) + } +} diff --git a/store/datastore/ddl/mysql/10.sql b/store/datastore/ddl/mysql/10.sql deleted file mode 100644 index 8b1692dc4..000000000 --- a/store/datastore/ddl/mysql/10.sql +++ /dev/null @@ -1,12 +0,0 @@ --- +migrate Up - -ALTER TABLE secrets ADD COLUMN secret_conceal BOOLEAN; -ALTER TABLE team_secrets ADD COLUMN team_secret_conceal BOOLEAN; - -UPDATE secrets SET secret_conceal = false; -UPDATE team_secrets SET team_secret_conceal = false; - --- +migrate Down - -ALTER TABLE secrets DROP COLUMN secret_conceal; -ALTER TABLE team_secrets DROP COLUMN team_secret_conceal; diff --git a/store/datastore/ddl/mysql/11.sql b/store/datastore/ddl/mysql/11.sql deleted file mode 100644 index ca46278c5..000000000 --- a/store/datastore/ddl/mysql/11.sql +++ /dev/null @@ -1,8 +0,0 @@ --- +migrate Up - -ALTER TABLE builds ADD COLUMN build_error VARCHAR(500); -UPDATE builds SET build_error = ''; - --- +migrate Down - -ALTER TABLE builds DROP COLUMN build_error; diff --git a/store/datastore/ddl/mysql/12.sql b/store/datastore/ddl/mysql/12.sql deleted file mode 100644 index 06a00e332..000000000 --- a/store/datastore/ddl/mysql/12.sql +++ /dev/null @@ -1,18 +0,0 @@ --- +migrate Up - -ALTER TABLE repos ADD COLUMN repo_config_path VARCHAR(255); -ALTER TABLE builds ADD COLUMN build_sender VARCHAR(255); -ALTER TABLE builds ADD COLUMN build_reviewer VARCHAR(255); -ALTER TABLE builds ADD COLUMN build_reviewed INTEGER; - -UPDATE repos SET repo_config_path = '.drone.yml'; -UPDATE builds SET build_reviewer = ''; -UPDATE builds SET build_reviewed = 0; -UPDATE builds SET build_sender = ''; - --- +migrate Down - -ALTER TABLE repos DROP COLUMN repo_config_path; -ALTER TABLE builds DROP COLUMN build_sender; -ALTER TABLE builds DROP COLUMN build_reviewer; -ALTER TABLE builds DROP COLUMN build_reviewed; diff --git a/store/datastore/ddl/mysql/13.sql b/store/datastore/ddl/mysql/13.sql deleted file mode 100644 index d03759bdf..000000000 --- a/store/datastore/ddl/mysql/13.sql +++ /dev/null @@ -1,45 +0,0 @@ --- +migrate Up - -CREATE TABLE procs ( - proc_id INTEGER PRIMARY KEY AUTO_INCREMENT -,proc_build_id INTEGER -,proc_pid INTEGER -,proc_ppid INTEGER -,proc_pgid INTEGER -,proc_name VARCHAR(250) -,proc_state VARCHAR(250) -,proc_error VARCHAR(500) -,proc_exit_code INTEGER -,proc_started INTEGER -,proc_stopped INTEGER -,proc_machine VARCHAR(250) -,proc_platform VARCHAR(250) -,proc_environ VARCHAR(2000) -,UNIQUE(proc_build_id, proc_pid) -); - -CREATE INDEX proc_build_ix ON procs (proc_build_id); - -CREATE TABLE files ( - file_id INTEGER PRIMARY KEY AUTO_INCREMENT -,file_build_id INTEGER -,file_proc_id INTEGER -,file_name VARCHAR(250) -,file_mime VARCHAR(250) -,file_size INTEGER -,file_time INTEGER -,file_data MEDIUMBLOB -,UNIQUE(file_proc_id,file_name) -); - -CREATE INDEX file_build_ix ON files (file_build_id); -CREATE INDEX file_proc_ix ON files (file_proc_id); - --- +migrate Down - -DROP INDEX file_build_ix; -DROP INDEX file_proc_ix; -DROP TABLE files; - -DROP INDEX proc_build_ix; -DROP TABLE procs; diff --git a/store/datastore/ddl/mysql/15.sql b/store/datastore/ddl/mysql/15.sql deleted file mode 100644 index fe01a9dd9..000000000 --- a/store/datastore/ddl/mysql/15.sql +++ /dev/null @@ -1,11 +0,0 @@ --- +migrate Up - -CREATE TABLE tasks ( - task_id VARCHAR(255) PRIMARY KEY -,task_data MEDIUMBLOB -,task_labels MEDIUMBLOB -); - --- +migrate Down - -DROP TABLE tasks; diff --git a/store/datastore/ddl/mysql/1_init.sql b/store/datastore/ddl/mysql/1_init.sql deleted file mode 100644 index 736dfa9b0..000000000 --- a/store/datastore/ddl/mysql/1_init.sql +++ /dev/null @@ -1,124 +0,0 @@ --- +migrate Up - -CREATE TABLE IF NOT EXISTS users ( - user_id INTEGER PRIMARY KEY AUTO_INCREMENT -,user_login VARCHAR(255) -,user_token VARCHAR(500) -,user_secret VARCHAR(500) -,user_expiry INTEGER -,user_email VARCHAR(500) -,user_avatar VARCHAR(500) -,user_active BOOLEAN -,user_admin BOOLEAN -,user_hash VARCHAR(500) - -,UNIQUE(user_login) -); - -CREATE TABLE IF NOT EXISTS repos ( - repo_id INTEGER PRIMARY KEY AUTO_INCREMENT -,repo_user_id INTEGER -,repo_owner VARCHAR(255) -,repo_name VARCHAR(255) -,repo_full_name VARCHAR(255) -,repo_avatar VARCHAR(500) -,repo_link VARCHAR(1000) -,repo_clone VARCHAR(1000) -,repo_branch VARCHAR(500) -,repo_timeout INTEGER -,repo_private BOOLEAN -,repo_trusted BOOLEAN -,repo_allow_pr BOOLEAN -,repo_allow_push BOOLEAN -,repo_allow_deploys BOOLEAN -,repo_allow_tags BOOLEAN -,repo_hash VARCHAR(500) - -,UNIQUE(repo_full_name) -); - -CREATE TABLE IF NOT EXISTS `keys` ( - key_id INTEGER PRIMARY KEY AUTO_INCREMENT -,key_repo_id INTEGER -,key_public MEDIUMBLOB -,key_private MEDIUMBLOB - -,UNIQUE(key_repo_id) -); - -CREATE TABLE IF NOT EXISTS builds ( - build_id INTEGER PRIMARY KEY AUTO_INCREMENT -,build_repo_id INTEGER -,build_number INTEGER -,build_event VARCHAR(500) -,build_status VARCHAR(500) -,build_enqueued INTEGER -,build_created INTEGER -,build_started INTEGER -,build_finished INTEGER -,build_commit VARCHAR(500) -,build_branch VARCHAR(500) -,build_ref VARCHAR(500) -,build_refspec VARCHAR(1000) -,build_remote VARCHAR(500) -,build_title VARCHAR(1000) -,build_message VARCHAR(2000) -,build_timestamp INTEGER -,build_author VARCHAR(500) -,build_avatar VARCHAR(1000) -,build_email VARCHAR(500) -,build_link VARCHAR(1000) - -,UNIQUE(build_number, build_repo_id) -); - -CREATE INDEX ix_build_repo ON builds (build_repo_id); - -CREATE TABLE IF NOT EXISTS jobs ( - job_id INTEGER PRIMARY KEY AUTO_INCREMENT -,job_node_id INTEGER -,job_build_id INTEGER -,job_number INTEGER -,job_status VARCHAR(500) -,job_exit_code INTEGER -,job_started INTEGER -,job_enqueued INTEGER -,job_finished INTEGER -,job_environment VARCHAR(2000) - -,UNIQUE(job_build_id, job_number) -); - -CREATE INDEX ix_job_build ON jobs (job_build_id); -CREATE INDEX ix_job_node ON jobs (job_node_id); - -CREATE TABLE IF NOT EXISTS logs ( - log_id INTEGER PRIMARY KEY AUTO_INCREMENT -,log_job_id INTEGER -,log_data MEDIUMBLOB - -,UNIQUE(log_job_id) -); - -CREATE TABLE IF NOT EXISTS nodes ( - node_id INTEGER PRIMARY KEY AUTO_INCREMENT -,node_addr VARCHAR(1024) -,node_arch VARCHAR(50) -,node_cert MEDIUMBLOB -,node_key MEDIUMBLOB -,node_ca MEDIUMBLOB -); - - -INSERT INTO nodes VALUES(null, 'unix:///var/run/docker.sock', 'linux_amd64', '', '', ''); -INSERT INTO nodes VALUES(null, 'unix:///var/run/docker.sock', 'linux_amd64', '', '', ''); - --- +migrate Down - -DROP TABLE nodes; -DROP TABLE logs; -DROP TABLE jobs; -DROP TABLE builds; -DROP TABLE `keys`; -DROP TABLE repos; -DROP TABLE users; diff --git a/store/datastore/ddl/mysql/2.sql b/store/datastore/ddl/mysql/2.sql deleted file mode 100644 index 4f507f65b..000000000 --- a/store/datastore/ddl/mysql/2.sql +++ /dev/null @@ -1,12 +0,0 @@ --- +migrate Up - -ALTER TABLE repos ADD COLUMN repo_scm VARCHAR(25); -ALTER TABLE builds ADD COLUMN build_deploy VARCHAR(500); - -UPDATE repos SET repo_scm = 'git' WHERE repo_scm = null; -UPDATE builds SET build_deploy = '' WHERE build_deploy = null; - --- +migrate Down - -ALTER TABLE repos DROP COLUMN repo_scm; -ALTER TABLE builds DROP COLUMN build_deploy; diff --git a/store/datastore/ddl/mysql/3.sql b/store/datastore/ddl/mysql/3.sql deleted file mode 100644 index 5b659cd5d..000000000 --- a/store/datastore/ddl/mysql/3.sql +++ /dev/null @@ -1,32 +0,0 @@ --- +migrate Up - -CREATE TABLE secrets ( - secret_id INTEGER PRIMARY KEY AUTO_INCREMENT -,secret_repo_id INTEGER -,secret_name VARCHAR(255) -,secret_value MEDIUMBLOB -,secret_images VARCHAR(2000) -,secret_events VARCHAR(2000) - -,UNIQUE(secret_name, secret_repo_id) -); - -CREATE TABLE registry ( - registry_id INTEGER PRIMARY KEY AUTO_INCREMENT -,registry_repo_id INTEGER -,registry_addr VARCHAR(255) -,registry_email VARCHAR(500) -,registry_username VARCHAR(2000) -,registry_password VARCHAR(2000) -,registry_token VARCHAR(2000) - -,UNIQUE(registry_addr, registry_repo_id) -); - -CREATE INDEX ix_secrets_repo ON secrets (secret_repo_id); -CREATE INDEX ix_registry_repo ON registry (registry_repo_id); - --- +migrate Down - -DROP INDEX ix_secrets_repo; -DROP INDEX ix_registry_repo; diff --git a/store/datastore/ddl/mysql/4.sql b/store/datastore/ddl/mysql/4.sql deleted file mode 100644 index ea4fc4576..000000000 --- a/store/datastore/ddl/mysql/4.sql +++ /dev/null @@ -1,9 +0,0 @@ --- +migrate Up - -ALTER TABLE jobs ADD COLUMN job_error VARCHAR(500); - -UPDATE jobs SET job_error = '' WHERE job_error = null; - --- +migrate Down - -ALTER TABLE jobs DROP COLUMN job_error; diff --git a/store/datastore/ddl/mysql/5.sql b/store/datastore/ddl/mysql/5.sql deleted file mode 100644 index 2f4702379..000000000 --- a/store/datastore/ddl/mysql/5.sql +++ /dev/null @@ -1,12 +0,0 @@ --- +migrate Up - -ALTER TABLE builds ADD COLUMN build_signed BOOLEAN; -ALTER TABLE builds ADD COLUMN build_verified BOOLEAN; - -UPDATE builds SET build_signed = false; -UPDATE builds SET build_verified = false; - --- +migrate Down - -ALTER TABLE builds DROP COLUMN build_signed; -ALTER TABLE builds DROP COLUMN build_verified; diff --git a/store/datastore/ddl/mysql/7.sql b/store/datastore/ddl/mysql/7.sql deleted file mode 100644 index e009572b6..000000000 --- a/store/datastore/ddl/mysql/7.sql +++ /dev/null @@ -1,19 +0,0 @@ --- +migrate Up - -CREATE TABLE team_secrets ( - team_secret_id INTEGER PRIMARY KEY AUTO_INCREMENT -,team_secret_key VARCHAR(255) -,team_secret_name VARCHAR(255) -,team_secret_value MEDIUMBLOB -,team_secret_images VARCHAR(2000) -,team_secret_events VARCHAR(2000) - -,UNIQUE(team_secret_name, team_secret_key) -); - -CREATE INDEX ix_team_secrets_key ON team_secrets (team_secret_key); - --- +migrate Down - -DROP INDEX ix_team_secrets_key; -DROP TABLE team_secrets; diff --git a/store/datastore/ddl/mysql/8.sql b/store/datastore/ddl/mysql/8.sql deleted file mode 100644 index ad6759b26..000000000 --- a/store/datastore/ddl/mysql/8.sql +++ /dev/null @@ -1,7 +0,0 @@ --- +migrate Up - -ALTER TABLE builds ADD COLUMN build_parent INTEGER DEFAULT 0; - --- +migrate Down - -ALTER TABLE builds DROP COLUMN build_parent; diff --git a/store/datastore/ddl/mysql/9.sql b/store/datastore/ddl/mysql/9.sql deleted file mode 100644 index d91ccdcbc..000000000 --- a/store/datastore/ddl/mysql/9.sql +++ /dev/null @@ -1,12 +0,0 @@ --- +migrate Up - -ALTER TABLE secrets ADD COLUMN secret_skip_verify BOOLEAN; -ALTER TABLE team_secrets ADD COLUMN team_secret_skip_verify BOOLEAN; - -UPDATE secrets SET secret_skip_verify = false; -UPDATE team_secrets SET team_secret_skip_verify = false; - --- +migrate Down - -ALTER TABLE secrets DROP COLUMN secret_skip_verify; -ALTER TABLE team_secrets DROP COLUMN team_secret_skip_verify; diff --git a/store/datastore/ddl/mysql/ddl.go b/store/datastore/ddl/mysql/ddl.go new file mode 100644 index 000000000..42ab9ab9f --- /dev/null +++ b/store/datastore/ddl/mysql/ddl.go @@ -0,0 +1,3 @@ +package mysql + +//go:generate togo ddl -package mysql -dialect mysql diff --git a/store/datastore/ddl/mysql/files/001_create_table_users.sql b/store/datastore/ddl/mysql/files/001_create_table_users.sql new file mode 100644 index 000000000..d54a8c690 --- /dev/null +++ b/store/datastore/ddl/mysql/files/001_create_table_users.sql @@ -0,0 +1,16 @@ +-- name: create-table-users + +CREATE TABLE IF NOT EXISTS users ( + user_id INTEGER PRIMARY KEY AUTO_INCREMENT +,user_login VARCHAR(250) +,user_token VARCHAR(500) +,user_secret VARCHAR(500) +,user_expiry INTEGER +,user_email VARCHAR(500) +,user_avatar VARCHAR(500) +,user_active BOOLEAN +,user_admin BOOLEAN +,user_hash VARCHAR(500) + +,UNIQUE(user_login) +); diff --git a/store/datastore/ddl/mysql/files/002_create_table_repos.sql b/store/datastore/ddl/mysql/files/002_create_table_repos.sql new file mode 100644 index 000000000..52e8b8465 --- /dev/null +++ b/store/datastore/ddl/mysql/files/002_create_table_repos.sql @@ -0,0 +1,26 @@ +-- name: create-table-repos + +CREATE TABLE IF NOT EXISTS repos ( + repo_id INTEGER PRIMARY KEY AUTO_INCREMENT +,repo_user_id INTEGER +,repo_owner VARCHAR(250) +,repo_name VARCHAR(250) +,repo_full_name VARCHAR(250) +,repo_avatar VARCHAR(500) +,repo_link VARCHAR(1000) +,repo_clone VARCHAR(1000) +,repo_branch VARCHAR(500) +,repo_timeout INTEGER +,repo_private BOOLEAN +,repo_trusted BOOLEAN +,repo_allow_pr BOOLEAN +,repo_allow_push BOOLEAN +,repo_allow_deploys BOOLEAN +,repo_allow_tags BOOLEAN +,repo_hash VARCHAR(500) +,repo_scm VARCHAR(50) +,repo_config_path VARCHAR(500) +,repo_gated BOOLEAN + +,UNIQUE(repo_full_name) +); diff --git a/store/datastore/ddl/mysql/files/003_create_table_builds.sql b/store/datastore/ddl/mysql/files/003_create_table_builds.sql new file mode 100644 index 000000000..39959b4ab --- /dev/null +++ b/store/datastore/ddl/mysql/files/003_create_table_builds.sql @@ -0,0 +1,44 @@ +-- name: create-table-builds + +CREATE TABLE IF NOT EXISTS builds ( + build_id INTEGER PRIMARY KEY AUTO_INCREMENT +,build_repo_id INTEGER +,build_number INTEGER +,build_event VARCHAR(500) +,build_status VARCHAR(500) +,build_enqueued INTEGER +,build_created INTEGER +,build_started INTEGER +,build_finished INTEGER +,build_commit VARCHAR(500) +,build_branch VARCHAR(500) +,build_ref VARCHAR(500) +,build_refspec VARCHAR(1000) +,build_remote VARCHAR(500) +,build_title VARCHAR(1000) +,build_message VARCHAR(2000) +,build_timestamp INTEGER +,build_author VARCHAR(500) +,build_avatar VARCHAR(1000) +,build_email VARCHAR(500) +,build_link VARCHAR(1000) +,build_deploy VARCHAR(500) +,build_signed BOOLEAN +,build_verified BOOLEAN +,build_parent INTEGER +,build_error VARCHAR(500) +,build_reviewer VARCHAR(250) +,build_reviewed INTEGER +,build_sender VARCHAR(250) +,build_config_id INTEGER + +,UNIQUE(build_number, build_repo_id) +); + +-- name: create-index-builds-repo + +CREATE INDEX ix_build_repo ON builds (build_repo_id); + +-- name: create-index-builds-author + +CREATE INDEX ix_build_author ON builds (build_author); diff --git a/store/datastore/ddl/mysql/files/004_create_table_procs.sql b/store/datastore/ddl/mysql/files/004_create_table_procs.sql new file mode 100644 index 000000000..784ef18e4 --- /dev/null +++ b/store/datastore/ddl/mysql/files/004_create_table_procs.sql @@ -0,0 +1,24 @@ +-- name: create-table-procs + +CREATE TABLE IF NOT EXISTS procs ( + proc_id INTEGER PRIMARY KEY AUTO_INCREMENT +,proc_build_id INTEGER +,proc_pid INTEGER +,proc_ppid INTEGER +,proc_pgid INTEGER +,proc_name VARCHAR(250) +,proc_state VARCHAR(250) +,proc_error VARCHAR(500) +,proc_exit_code INTEGER +,proc_started INTEGER +,proc_stopped INTEGER +,proc_machine VARCHAR(250) +,proc_platform VARCHAR(250) +,proc_environ VARCHAR(2000) + +,UNIQUE(proc_build_id, proc_pid) +); + +-- name: create-index-procs-build + +CREATE INDEX proc_build_ix ON procs (proc_build_id); diff --git a/store/datastore/ddl/mysql/files/005_create_table_logs.sql b/store/datastore/ddl/mysql/files/005_create_table_logs.sql new file mode 100644 index 000000000..d1e898c49 --- /dev/null +++ b/store/datastore/ddl/mysql/files/005_create_table_logs.sql @@ -0,0 +1,9 @@ +-- name: create-table-logs + +CREATE TABLE IF NOT EXISTS logs ( + log_id INTEGER PRIMARY KEY AUTO_INCREMENT +,log_job_id INTEGER +,log_data MEDIUMBLOB + +,UNIQUE(log_job_id) +); diff --git a/store/datastore/ddl/mysql/files/006_create_table_files.sql b/store/datastore/ddl/mysql/files/006_create_table_files.sql new file mode 100644 index 000000000..f76f8c90e --- /dev/null +++ b/store/datastore/ddl/mysql/files/006_create_table_files.sql @@ -0,0 +1,22 @@ +-- name: create-table-files + +CREATE TABLE IF NOT EXISTS files ( + file_id INTEGER PRIMARY KEY AUTO_INCREMENT +,file_build_id INTEGER +,file_proc_id INTEGER +,file_name VARCHAR(250) +,file_mime VARCHAR(250) +,file_size INTEGER +,file_time INTEGER +,file_data MEDIUMBLOB + +,UNIQUE(file_proc_id,file_name) +); + +-- name: create-index-files-builds + +CREATE INDEX file_build_ix ON files (file_build_id); + +-- name: create-index-files-procs + +CREATE INDEX file_proc_ix ON files (file_proc_id); diff --git a/store/datastore/ddl/mysql/files/007_create_table_secets.sql b/store/datastore/ddl/mysql/files/007_create_table_secets.sql new file mode 100644 index 000000000..6193fa9e7 --- /dev/null +++ b/store/datastore/ddl/mysql/files/007_create_table_secets.sql @@ -0,0 +1,18 @@ +-- name: create-table-secrets + +CREATE TABLE IF NOT EXISTS secrets ( + secret_id INTEGER PRIMARY KEY AUTO_INCREMENT +,secret_repo_id INTEGER +,secret_name VARCHAR(250) +,secret_value MEDIUMBLOB +,secret_images VARCHAR(2000) +,secret_events VARCHAR(2000) +,secret_skip_verify BOOLEAN +,secret_conceal BOOLEAN + +,UNIQUE(secret_name, secret_repo_id) +); + +-- name: create-index-secrets-repo + +CREATE INDEX ix_secrets_repo ON secrets (secret_repo_id); diff --git a/store/datastore/ddl/mysql/files/008_create_table_registry.sql b/store/datastore/ddl/mysql/files/008_create_table_registry.sql new file mode 100644 index 000000000..4e031f3a2 --- /dev/null +++ b/store/datastore/ddl/mysql/files/008_create_table_registry.sql @@ -0,0 +1,17 @@ +-- name: create-table-registry + +CREATE TABLE IF NOT EXISTS registry ( + registry_id INTEGER PRIMARY KEY AUTO_INCREMENT +,registry_repo_id INTEGER +,registry_addr VARCHAR(250) +,registry_email VARCHAR(500) +,registry_username VARCHAR(2000) +,registry_password VARCHAR(2000) +,registry_token VARCHAR(2000) + +,UNIQUE(registry_addr, registry_repo_id) +); + +-- name: create-index-registry-repo + +CREATE INDEX ix_registry_repo ON registry (registry_repo_id); diff --git a/store/datastore/ddl/mysql/16.sql b/store/datastore/ddl/mysql/files/009_create_table_config.sql similarity index 51% rename from store/datastore/ddl/mysql/16.sql rename to store/datastore/ddl/mysql/files/009_create_table_config.sql index d1a660d80..f907a23b3 100644 --- a/store/datastore/ddl/mysql/16.sql +++ b/store/datastore/ddl/mysql/files/009_create_table_config.sql @@ -1,6 +1,6 @@ --- +migrate Up +-- name: create-table-config -CREATE TABLE config ( +CREATE TABLE IF NOT EXISTS config ( config_id INTEGER PRIMARY KEY AUTO_INCREMENT ,config_repo_id INTEGER ,config_hash VARCHAR(250) @@ -8,10 +8,3 @@ CREATE TABLE config ( ,UNIQUE(config_hash, config_repo_id) ); - -ALTER TABLE builds ADD COLUMN build_config_id INTEGER; -UPDATE builds set build_config_id = 0; - --- +migrate Down - -DROP TABLE config; diff --git a/store/datastore/ddl/mysql/files/010_create_table_tasks.sql b/store/datastore/ddl/mysql/files/010_create_table_tasks.sql new file mode 100644 index 000000000..40f908b79 --- /dev/null +++ b/store/datastore/ddl/mysql/files/010_create_table_tasks.sql @@ -0,0 +1,7 @@ +-- name: create-table-tasks + +CREATE TABLE IF NOT EXISTS tasks ( + task_id VARCHAR(250) PRIMARY KEY +,task_data MEDIUMBLOB +,task_labels MEDIUMBLOB +); diff --git a/store/datastore/ddl/mysql/6.sql b/store/datastore/ddl/mysql/files/011_create_table_agents.sql similarity index 62% rename from store/datastore/ddl/mysql/6.sql rename to store/datastore/ddl/mysql/files/011_create_table_agents.sql index 98b0b7157..5a91b2e10 100644 --- a/store/datastore/ddl/mysql/6.sql +++ b/store/datastore/ddl/mysql/files/011_create_table_agents.sql @@ -1,8 +1,8 @@ --- +migrate Up +-- name: create-table-agents -CREATE TABLE agents ( +CREATE TABLE IF NOT EXISTS agents ( agent_id INTEGER PRIMARY KEY AUTO_INCREMENT -,agent_addr VARCHAR(255) +,agent_addr VARCHAR(250) ,agent_platform VARCHAR(500) ,agent_capacity INTEGER ,agent_created INTEGER @@ -10,8 +10,3 @@ CREATE TABLE agents ( ,UNIQUE(agent_addr) ); - - --- +migrate Down - -DROP TABLE agents; diff --git a/store/datastore/ddl/mysql/14.sql b/store/datastore/ddl/mysql/files/012_create_table_senders.sql similarity index 52% rename from store/datastore/ddl/mysql/14.sql rename to store/datastore/ddl/mysql/files/012_create_table_senders.sql index 3524e5512..02e5bd8e2 100644 --- a/store/datastore/ddl/mysql/14.sql +++ b/store/datastore/ddl/mysql/files/012_create_table_senders.sql @@ -1,9 +1,6 @@ --- +migrate Up +-- name: create-table-senders -ALTER TABLE repos ADD COLUMN repo_gated BOOLEAN; -UPDATE repos SET repo_gated = false; - -CREATE TABLE senders ( +CREATE TABLE IF NOT EXISTS senders ( sender_id INTEGER PRIMARY KEY AUTO_INCREMENT ,sender_repo_id INTEGER ,sender_login VARCHAR(250) @@ -13,10 +10,6 @@ CREATE TABLE senders ( ,UNIQUE(sender_repo_id,sender_login) ); +-- name: create-index-sender-repos + CREATE INDEX sender_repo_ix ON senders (sender_repo_id); - --- +migrate Down - -ALTER TABLE repos DROP COLUMN repo_gated; -DROP INDEX sender_repo_ix; -DROP TABLE senders; diff --git a/store/datastore/ddl/postgres/10.sql b/store/datastore/ddl/postgres/10.sql deleted file mode 100644 index 8b1692dc4..000000000 --- a/store/datastore/ddl/postgres/10.sql +++ /dev/null @@ -1,12 +0,0 @@ --- +migrate Up - -ALTER TABLE secrets ADD COLUMN secret_conceal BOOLEAN; -ALTER TABLE team_secrets ADD COLUMN team_secret_conceal BOOLEAN; - -UPDATE secrets SET secret_conceal = false; -UPDATE team_secrets SET team_secret_conceal = false; - --- +migrate Down - -ALTER TABLE secrets DROP COLUMN secret_conceal; -ALTER TABLE team_secrets DROP COLUMN team_secret_conceal; diff --git a/store/datastore/ddl/postgres/11.sql b/store/datastore/ddl/postgres/11.sql deleted file mode 100644 index ca46278c5..000000000 --- a/store/datastore/ddl/postgres/11.sql +++ /dev/null @@ -1,8 +0,0 @@ --- +migrate Up - -ALTER TABLE builds ADD COLUMN build_error VARCHAR(500); -UPDATE builds SET build_error = ''; - --- +migrate Down - -ALTER TABLE builds DROP COLUMN build_error; diff --git a/store/datastore/ddl/postgres/12.sql b/store/datastore/ddl/postgres/12.sql deleted file mode 100644 index 04b82a6df..000000000 --- a/store/datastore/ddl/postgres/12.sql +++ /dev/null @@ -1,18 +0,0 @@ --- +migrate Up - -ALTER TABLE repos ADD COLUMN repo_config_path VARCHAR(255); -ALTER TABLE builds ADD COLUMN build_reviewer VARCHAR(255); -ALTER TABLE builds ADD COLUMN build_reviewed INTEGER; -ALTER TABLE builds ADD COLUMN build_sender VARCHAR(255); - -UPDATE repos SET repo_config_path = '.drone.yml'; -UPDATE builds SET build_reviewer = ''; -UPDATE builds SET build_reviewed = 0; -UPDATE builds SET build_sender = ''; - --- +migrate Down - -ALTER TABLE repos DROP COLUMN repo_config_path; -ALTER TABLE builds DROP COLUMN build_reviewer; -ALTER TABLE builds DROP COLUMN build_reviewed; -ALTER TABLE builds DROP COLUMN build_sender; diff --git a/store/datastore/ddl/postgres/13.sql b/store/datastore/ddl/postgres/13.sql deleted file mode 100644 index f01954205..000000000 --- a/store/datastore/ddl/postgres/13.sql +++ /dev/null @@ -1,47 +0,0 @@ --- +migrate Up - -CREATE TABLE procs ( - proc_id SERIAL PRIMARY KEY -,proc_build_id INTEGER -,proc_pid INTEGER -,proc_ppid INTEGER -,proc_pgid INTEGER -,proc_name VARCHAR(250) -,proc_state VARCHAR(250) -,proc_error VARCHAR(500) -,proc_exit_code INTEGER -,proc_started INTEGER -,proc_stopped INTEGER -,proc_machine VARCHAR(250) -,proc_platform VARCHAR(250) -,proc_environ VARCHAR(2000) - -,UNIQUE(proc_build_id, proc_pid) -); - -CREATE INDEX proc_build_ix ON procs (proc_build_id); - -CREATE TABLE files ( - file_id SERIAL PRIMARY KEY -,file_build_id INTEGER -,file_proc_id INTEGER -,file_name VARCHAR(250) -,file_mime VARCHAR(250) -,file_size INTEGER -,file_time INTEGER -,file_data BYTEA - -,UNIQUE(file_proc_id,file_name) -); - -CREATE INDEX file_build_ix ON files (file_build_id); -CREATE INDEX file_proc_ix ON files (file_proc_id); - --- +migrate Down - -DROP INDEX file_build_ix; -DROP INDEX file_proc_ix; -DROP TABLE files; - -DROP INDEX proc_build_ix; -DROP TABLE procs; diff --git a/store/datastore/ddl/postgres/14.sql b/store/datastore/ddl/postgres/14.sql deleted file mode 100644 index a80fed769..000000000 --- a/store/datastore/ddl/postgres/14.sql +++ /dev/null @@ -1,22 +0,0 @@ --- +migrate Up - -ALTER TABLE repos ADD COLUMN repo_gated BOOLEAN; -UPDATE repos SET repo_gated = false; - -CREATE TABLE senders ( - sender_id SERIAL PRIMARY KEY -,sender_repo_id INTEGER -,sender_login VARCHAR(250) -,sender_allow BOOLEAN -,sender_block BOOLEAN - -,UNIQUE(sender_repo_id,sender_login) -); - -CREATE INDEX sender_repo_ix ON senders (sender_repo_id); - --- +migrate Down - -ALTER TABLE repos DROP COLUMN repo_gated; -DROP INDEX sender_repo_ix; -DROP TABLE senders; diff --git a/store/datastore/ddl/postgres/15.sql b/store/datastore/ddl/postgres/15.sql deleted file mode 100644 index d450430e3..000000000 --- a/store/datastore/ddl/postgres/15.sql +++ /dev/null @@ -1,11 +0,0 @@ --- +migrate Up - -CREATE TABLE tasks ( - task_id VARCHAR(255) PRIMARY KEY -,task_data BYTEA -,task_labels BYTEA -); - --- +migrate Down - -DROP TABLE tasks; diff --git a/store/datastore/ddl/postgres/16.sql b/store/datastore/ddl/postgres/16.sql deleted file mode 100644 index 63dbf86a0..000000000 --- a/store/datastore/ddl/postgres/16.sql +++ /dev/null @@ -1,17 +0,0 @@ --- +migrate Up - -CREATE TABLE config ( - config_id SERIAL PRIMARY KEY -,config_repo_id INTEGER -,config_hash VARCHAR(250) -,config_data BYTEA - -,UNIQUE(config_hash, config_repo_id) -); - -ALTER TABLE builds ADD COLUMN build_config_id INTEGER; -UPDATE builds set build_config_id = 0; - --- +migrate Down - -DROP TABLE config; diff --git a/store/datastore/ddl/postgres/1_init.sql b/store/datastore/ddl/postgres/1_init.sql deleted file mode 100644 index 1d5d6c0a5..000000000 --- a/store/datastore/ddl/postgres/1_init.sql +++ /dev/null @@ -1,126 +0,0 @@ --- +migrate Up - -CREATE TABLE users ( - user_id SERIAL PRIMARY KEY -,user_login VARCHAR(40) -,user_token VARCHAR(128) -,user_secret VARCHAR(128) -,user_expiry INTEGER -,user_email VARCHAR(256) -,user_avatar VARCHAR(256) -,user_active BOOLEAN -,user_admin BOOLEAN -,user_hash VARCHAR(128) - -,UNIQUE(user_login) -); - -CREATE TABLE repos ( - repo_id SERIAL PRIMARY KEY -,repo_user_id INTEGER -,repo_owner VARCHAR(255) -,repo_name VARCHAR(255) -,repo_full_name VARCHAR(511) -,repo_avatar VARCHAR(500) -,repo_link VARCHAR(1000) -,repo_clone VARCHAR(1000) -,repo_branch VARCHAR(500) -,repo_timeout INTEGER -,repo_private BOOLEAN -,repo_trusted BOOLEAN -,repo_allow_pr BOOLEAN -,repo_allow_push BOOLEAN -,repo_allow_deploys BOOLEAN -,repo_allow_tags BOOLEAN -,repo_hash VARCHAR(500) - -,UNIQUE(repo_full_name) -); - -CREATE TABLE keys ( - key_id SERIAL PRIMARY KEY -,key_repo_id INTEGER -,key_public BYTEA -,key_private BYTEA - -,UNIQUE(key_repo_id) -); - -CREATE TABLE builds ( - build_id SERIAL PRIMARY KEY -,build_repo_id INTEGER -,build_number INTEGER -,build_event VARCHAR(25) -,build_status VARCHAR(25) -,build_enqueued INTEGER -,build_created INTEGER -,build_started INTEGER -,build_finished INTEGER -,build_commit VARCHAR(40) -,build_branch VARCHAR(256) -,build_ref VARCHAR(512) -,build_refspec VARCHAR(512) -,build_remote VARCHAR(512) -,build_title VARCHAR(1000) -,build_message VARCHAR(2000) -,build_timestamp INTEGER -,build_author VARCHAR(40) -,build_avatar VARCHAR(1000) -,build_email VARCHAR(500) -,build_link VARCHAR(1000) - -,UNIQUE(build_number, build_repo_id) -); - -CREATE INDEX ix_build_repo ON builds (build_repo_id); - -CREATE TABLE jobs ( - job_id SERIAL PRIMARY KEY -,job_node_id INTEGER -,job_build_id INTEGER -,job_number INTEGER -,job_status VARCHAR(25) -,job_exit_code INTEGER -,job_started INTEGER -,job_enqueued INTEGER -,job_finished INTEGER -,job_environment VARCHAR(2000) - -,UNIQUE(job_build_id, job_number) -); - -CREATE INDEX ix_job_build ON jobs (job_build_id); -CREATE INDEX ix_job_node ON jobs (job_node_id); - -CREATE TABLE IF NOT EXISTS logs ( - log_id SERIAL PRIMARY KEY -,log_job_id INTEGER -,log_data BYTEA - -,UNIQUE(log_job_id) -); - -CREATE TABLE IF NOT EXISTS nodes ( - node_id SERIAL PRIMARY KEY -,node_addr VARCHAR(1024) -,node_arch VARCHAR(50) -,node_cert BYTEA -,node_key BYTEA -,node_ca BYTEA -); - - -INSERT INTO nodes (node_addr, node_arch, node_cert, node_key, node_ca) VALUES - ('unix:///var/run/docker.sock', 'linux_amd64', '', '', ''), - ('unix:///var/run/docker.sock', 'linux_amd64', '', '', ''); - --- +migrate Down - -DROP TABLE nodes; -DROP TABLE logs; -DROP TABLE jobs; -DROP TABLE builds; -DROP TABLE keys; -DROP TABLE stars; -DROP TABLE repos; -DROP TABLE users; diff --git a/store/datastore/ddl/postgres/2.sql b/store/datastore/ddl/postgres/2.sql deleted file mode 100644 index 7f3fb8879..000000000 --- a/store/datastore/ddl/postgres/2.sql +++ /dev/null @@ -1,12 +0,0 @@ --- +migrate Up - -ALTER TABLE repos ADD COLUMN repo_scm VARCHAR(25); -ALTER TABLE builds ADD COLUMN build_deploy VARCHAR(500); - -UPDATE repos SET repo_scm = 'git'; -UPDATE builds SET build_deploy = ''; - --- +migrate Down - -ALTER TABLE repos DROP COLUMN repo_scm; -ALTER TABLE builds DROP COLUMN build_deploy; diff --git a/store/datastore/ddl/postgres/3.sql b/store/datastore/ddl/postgres/3.sql deleted file mode 100644 index ac847e4f9..000000000 --- a/store/datastore/ddl/postgres/3.sql +++ /dev/null @@ -1,32 +0,0 @@ --- +migrate Up - -CREATE TABLE secrets ( - secret_id SERIAL PRIMARY KEY -,secret_repo_id INTEGER -,secret_name VARCHAR(500) -,secret_value BYTEA -,secret_images VARCHAR(2000) -,secret_events VARCHAR(2000) - -,UNIQUE(secret_name, secret_repo_id) -); - -CREATE TABLE registry ( - registry_id SERIAL PRIMARY KEY -,registry_repo_id INTEGER -,registry_addr VARCHAR(500) -,registry_email VARCHAR(500) -,registry_username VARCHAR(2000) -,registry_password VARCHAR(2000) -,registry_token VARCHAR(2000) - -,UNIQUE(registry_addr, registry_repo_id) -); - -CREATE INDEX ix_secrets_repo ON secrets (secret_repo_id); -CREATE INDEX ix_registry_repo ON registry (registry_repo_id); - --- +migrate Down - -DROP INDEX ix_secrets_repo; -DROP INDEX ix_registry_repo; diff --git a/store/datastore/ddl/postgres/4.sql b/store/datastore/ddl/postgres/4.sql deleted file mode 100644 index b50f43cb1..000000000 --- a/store/datastore/ddl/postgres/4.sql +++ /dev/null @@ -1,9 +0,0 @@ --- +migrate Up - -ALTER TABLE jobs ADD COLUMN job_error VARCHAR(500); - -UPDATE jobs SET job_error = ''; - --- +migrate Down - -ALTER TABLE jobs DROP COLUMN job_error; diff --git a/store/datastore/ddl/postgres/5.sql b/store/datastore/ddl/postgres/5.sql deleted file mode 100644 index 8265a9857..000000000 --- a/store/datastore/ddl/postgres/5.sql +++ /dev/null @@ -1,16 +0,0 @@ --- +migrate Up - -ALTER TABLE builds ADD COLUMN build_signed BOOLEAN; -ALTER TABLE builds ADD COLUMN build_verified BOOLEAN; - -UPDATE builds SET build_signed = false; -UPDATE builds SET build_verified = false; - -CREATE INDEX ix_build_status_running ON builds (build_status) - WHERE build_status IN ('pending', 'running'); - --- +migrate Down - -ALTER TABLE builds DROP COLUMN build_signed; -ALTER TABLE builds DROP COLUMN build_verified; -DROP INDEX ix_build_status_running; diff --git a/store/datastore/ddl/postgres/7.sql b/store/datastore/ddl/postgres/7.sql deleted file mode 100644 index 225ccb8a6..000000000 --- a/store/datastore/ddl/postgres/7.sql +++ /dev/null @@ -1,19 +0,0 @@ --- +migrate Up - -CREATE TABLE team_secrets ( - team_secret_id SERIAL PRIMARY KEY -,team_secret_key VARCHAR(255) -,team_secret_name VARCHAR(255) -,team_secret_value BYTEA -,team_secret_images VARCHAR(2000) -,team_secret_events VARCHAR(2000) - -,UNIQUE(team_secret_name, team_secret_key) -); - -CREATE INDEX ix_team_secrets_key ON team_secrets (team_secret_key); - --- +migrate Down - -DROP INDEX ix_team_secrets_key; -DROP TABLE team_secrets; diff --git a/store/datastore/ddl/postgres/8.sql b/store/datastore/ddl/postgres/8.sql deleted file mode 100644 index ad6759b26..000000000 --- a/store/datastore/ddl/postgres/8.sql +++ /dev/null @@ -1,7 +0,0 @@ --- +migrate Up - -ALTER TABLE builds ADD COLUMN build_parent INTEGER DEFAULT 0; - --- +migrate Down - -ALTER TABLE builds DROP COLUMN build_parent; diff --git a/store/datastore/ddl/postgres/9.sql b/store/datastore/ddl/postgres/9.sql deleted file mode 100644 index d91ccdcbc..000000000 --- a/store/datastore/ddl/postgres/9.sql +++ /dev/null @@ -1,12 +0,0 @@ --- +migrate Up - -ALTER TABLE secrets ADD COLUMN secret_skip_verify BOOLEAN; -ALTER TABLE team_secrets ADD COLUMN team_secret_skip_verify BOOLEAN; - -UPDATE secrets SET secret_skip_verify = false; -UPDATE team_secrets SET team_secret_skip_verify = false; - --- +migrate Down - -ALTER TABLE secrets DROP COLUMN secret_skip_verify; -ALTER TABLE team_secrets DROP COLUMN team_secret_skip_verify; diff --git a/store/datastore/ddl/postgres/ddl.go b/store/datastore/ddl/postgres/ddl.go new file mode 100644 index 000000000..cb1877039 --- /dev/null +++ b/store/datastore/ddl/postgres/ddl.go @@ -0,0 +1,3 @@ +package postgres + +//go:generate togo ddl -package postgres -dialect postgres diff --git a/store/datastore/ddl/postgres/files/001_create_table_users.sql b/store/datastore/ddl/postgres/files/001_create_table_users.sql new file mode 100644 index 000000000..963cf6317 --- /dev/null +++ b/store/datastore/ddl/postgres/files/001_create_table_users.sql @@ -0,0 +1,16 @@ +-- name: create-table-users + +CREATE TABLE IF NOT EXISTS users ( + user_id SERIAL PRIMARY KEY +,user_login VARCHAR(250) +,user_token VARCHAR(500) +,user_secret VARCHAR(500) +,user_expiry INTEGER +,user_email VARCHAR(500) +,user_avatar VARCHAR(500) +,user_active BOOLEAN +,user_admin BOOLEAN +,user_hash VARCHAR(500) + +,UNIQUE(user_login) +); diff --git a/store/datastore/ddl/postgres/files/002_create_table_repos.sql b/store/datastore/ddl/postgres/files/002_create_table_repos.sql new file mode 100644 index 000000000..1578c2d05 --- /dev/null +++ b/store/datastore/ddl/postgres/files/002_create_table_repos.sql @@ -0,0 +1,26 @@ +-- name: create-table-repos + +CREATE TABLE IF NOT EXISTS repos ( + repo_id SERIAL PRIMARY KEY +,repo_user_id INTEGER +,repo_owner VARCHAR(250) +,repo_name VARCHAR(250) +,repo_full_name VARCHAR(250) +,repo_avatar VARCHAR(500) +,repo_link VARCHAR(1000) +,repo_clone VARCHAR(1000) +,repo_branch VARCHAR(500) +,repo_timeout INTEGER +,repo_private BOOLEAN +,repo_trusted BOOLEAN +,repo_allow_pr BOOLEAN +,repo_allow_push BOOLEAN +,repo_allow_deploys BOOLEAN +,repo_allow_tags BOOLEAN +,repo_hash VARCHAR(500) +,repo_scm VARCHAR(50) +,repo_config_path VARCHAR(500) +,repo_gated BOOLEAN + +,UNIQUE(repo_full_name) +); diff --git a/store/datastore/ddl/postgres/files/003_create_table_builds.sql b/store/datastore/ddl/postgres/files/003_create_table_builds.sql new file mode 100644 index 000000000..81ded7600 --- /dev/null +++ b/store/datastore/ddl/postgres/files/003_create_table_builds.sql @@ -0,0 +1,44 @@ +-- name: create-table-builds + +CREATE TABLE IF NOT EXISTS builds ( + build_id SERIAL PRIMARY KEY +,build_repo_id INTEGER +,build_number INTEGER +,build_event VARCHAR(500) +,build_status VARCHAR(500) +,build_enqueued INTEGER +,build_created INTEGER +,build_started INTEGER +,build_finished INTEGER +,build_commit VARCHAR(500) +,build_branch VARCHAR(500) +,build_ref VARCHAR(500) +,build_refspec VARCHAR(1000) +,build_remote VARCHAR(500) +,build_title VARCHAR(1000) +,build_message VARCHAR(2000) +,build_timestamp INTEGER +,build_author VARCHAR(500) +,build_avatar VARCHAR(1000) +,build_email VARCHAR(500) +,build_link VARCHAR(1000) +,build_deploy VARCHAR(500) +,build_signed BOOLEAN +,build_verified BOOLEAN +,build_parent INTEGER +,build_error VARCHAR(500) +,build_reviewer VARCHAR(250) +,build_reviewed INTEGER +,build_sender VARCHAR(250) +,build_config_id INTEGER + +,UNIQUE(build_number, build_repo_id) +); + +-- name: create-index-builds-repo + +CREATE INDEX IF NOT EXISTS ix_build_repo ON builds (build_repo_id); + +-- name: create-index-builds-author + +CREATE INDEX IF NOT EXISTS ix_build_author ON builds (build_author); diff --git a/store/datastore/ddl/postgres/files/004_create_table_procs.sql b/store/datastore/ddl/postgres/files/004_create_table_procs.sql new file mode 100644 index 000000000..ac6d0ac79 --- /dev/null +++ b/store/datastore/ddl/postgres/files/004_create_table_procs.sql @@ -0,0 +1,24 @@ +-- name: create-table-procs + +CREATE TABLE IF NOT EXISTS procs ( + proc_id SERIAL PRIMARY KEY +,proc_build_id INTEGER +,proc_pid INTEGER +,proc_ppid INTEGER +,proc_pgid INTEGER +,proc_name VARCHAR(250) +,proc_state VARCHAR(250) +,proc_error VARCHAR(500) +,proc_exit_code INTEGER +,proc_started INTEGER +,proc_stopped INTEGER +,proc_machine VARCHAR(250) +,proc_platform VARCHAR(250) +,proc_environ VARCHAR(2000) + +,UNIQUE(proc_build_id, proc_pid) +); + +-- name: create-index-procs-build + +CREATE INDEX IF NOT EXISTS proc_build_ix ON procs (proc_build_id); diff --git a/store/datastore/ddl/postgres/files/005_create_table_logs.sql b/store/datastore/ddl/postgres/files/005_create_table_logs.sql new file mode 100644 index 000000000..b0cb852f1 --- /dev/null +++ b/store/datastore/ddl/postgres/files/005_create_table_logs.sql @@ -0,0 +1,9 @@ +-- name: create-table-logs + +CREATE TABLE IF NOT EXISTS logs ( + log_id SERIAL PRIMARY KEY +,log_job_id INTEGER +,log_data BYTEA + +,UNIQUE(log_job_id) +); diff --git a/store/datastore/ddl/postgres/files/006_create_table_files.sql b/store/datastore/ddl/postgres/files/006_create_table_files.sql new file mode 100644 index 000000000..ba0da0624 --- /dev/null +++ b/store/datastore/ddl/postgres/files/006_create_table_files.sql @@ -0,0 +1,22 @@ +-- name: create-table-files + +CREATE TABLE IF NOT EXISTS files ( + file_id SERIAL PRIMARY KEY +,file_build_id INTEGER +,file_proc_id INTEGER +,file_name VARCHAR(250) +,file_mime VARCHAR(250) +,file_size INTEGER +,file_time INTEGER +,file_data BYTEA + +,UNIQUE(file_proc_id,file_name) +); + +-- name: create-index-files-builds + +CREATE INDEX IF NOT EXISTS file_build_ix ON files (file_build_id); + +-- name: create-index-files-procs + +CREATE INDEX IF NOT EXISTS file_proc_ix ON files (file_proc_id); diff --git a/store/datastore/ddl/postgres/files/007_create_table_secets.sql b/store/datastore/ddl/postgres/files/007_create_table_secets.sql new file mode 100644 index 000000000..233eb691b --- /dev/null +++ b/store/datastore/ddl/postgres/files/007_create_table_secets.sql @@ -0,0 +1,18 @@ +-- name: create-table-secrets + +CREATE TABLE IF NOT EXISTS secrets ( + secret_id SERIAL PRIMARY KEY +,secret_repo_id INTEGER +,secret_name VARCHAR(250) +,secret_value BYTEA +,secret_images VARCHAR(2000) +,secret_events VARCHAR(2000) +,secret_skip_verify BOOLEAN +,secret_conceal BOOLEAN + +,UNIQUE(secret_name, secret_repo_id) +); + +-- name: create-index-secrets-repo + +CREATE INDEX IF NOT EXISTS ix_secrets_repo ON secrets (secret_repo_id); diff --git a/store/datastore/ddl/postgres/files/008_create_table_registry.sql b/store/datastore/ddl/postgres/files/008_create_table_registry.sql new file mode 100644 index 000000000..67499d081 --- /dev/null +++ b/store/datastore/ddl/postgres/files/008_create_table_registry.sql @@ -0,0 +1,17 @@ +-- name: create-table-registry + +CREATE TABLE IF NOT EXISTS registry ( + registry_id SERIAL PRIMARY KEY +,registry_repo_id INTEGER +,registry_addr VARCHAR(250) +,registry_email VARCHAR(500) +,registry_username VARCHAR(2000) +,registry_password VARCHAR(2000) +,registry_token VARCHAR(2000) + +,UNIQUE(registry_addr, registry_repo_id) +); + +-- name: create-index-registry-repo + +CREATE INDEX IF NOT EXISTS ix_registry_repo ON registry (registry_repo_id); diff --git a/store/datastore/ddl/postgres/files/009_create_table_config.sql b/store/datastore/ddl/postgres/files/009_create_table_config.sql new file mode 100644 index 000000000..ec115f896 --- /dev/null +++ b/store/datastore/ddl/postgres/files/009_create_table_config.sql @@ -0,0 +1,10 @@ +-- name: create-table-config + +CREATE TABLE IF NOT EXISTS config ( + config_id SERIAL PRIMARY KEY +,config_repo_id INTEGER +,config_hash VARCHAR(250) +,config_data BYTEA + +,UNIQUE(config_hash, config_repo_id) +); diff --git a/store/datastore/ddl/postgres/files/010_create_table_tasks.sql b/store/datastore/ddl/postgres/files/010_create_table_tasks.sql new file mode 100644 index 000000000..9cc4c5f69 --- /dev/null +++ b/store/datastore/ddl/postgres/files/010_create_table_tasks.sql @@ -0,0 +1,7 @@ +-- name: create-table-tasks + +CREATE TABLE IF NOT EXISTS tasks ( + task_id VARCHAR(250) PRIMARY KEY +,task_data BYTEA +,task_labels BYTEA +); diff --git a/store/datastore/ddl/postgres/6.sql b/store/datastore/ddl/postgres/files/011_create_table_agents.sql similarity index 60% rename from store/datastore/ddl/postgres/6.sql rename to store/datastore/ddl/postgres/files/011_create_table_agents.sql index 0189cd3f7..575dc2996 100644 --- a/store/datastore/ddl/postgres/6.sql +++ b/store/datastore/ddl/postgres/files/011_create_table_agents.sql @@ -1,8 +1,8 @@ --- +migrate Up +-- name: create-table-agents -CREATE TABLE agents ( +CREATE TABLE IF NOT EXISTS agents ( agent_id SERIAL PRIMARY KEY -,agent_addr VARCHAR(500) +,agent_addr VARCHAR(250) ,agent_platform VARCHAR(500) ,agent_capacity INTEGER ,agent_created INTEGER @@ -10,8 +10,3 @@ CREATE TABLE agents ( ,UNIQUE(agent_addr) ); - - --- +migrate Down - -DROP TABLE agents; diff --git a/store/datastore/ddl/postgres/files/012_create_table_senders.sql b/store/datastore/ddl/postgres/files/012_create_table_senders.sql new file mode 100644 index 000000000..b15f3d6d2 --- /dev/null +++ b/store/datastore/ddl/postgres/files/012_create_table_senders.sql @@ -0,0 +1,15 @@ +-- name: create-table-senders + +CREATE TABLE IF NOT EXISTS senders ( + sender_id SERIAL PRIMARY KEY +,sender_repo_id INTEGER +,sender_login VARCHAR(250) +,sender_allow BOOLEAN +,sender_block BOOLEAN + +,UNIQUE(sender_repo_id,sender_login) +); + +-- name: create-index-sender-repos + +CREATE INDEX IF NOT EXISTS sender_repo_ix ON senders (sender_repo_id); diff --git a/store/datastore/ddl/sqlite/ddl.go b/store/datastore/ddl/sqlite/ddl.go new file mode 100644 index 000000000..5573bad29 --- /dev/null +++ b/store/datastore/ddl/sqlite/ddl.go @@ -0,0 +1,3 @@ +package sqlite + +//go:generate togo ddl -package sqlite -dialect sqlite3 diff --git a/store/datastore/ddl/sqlite/files/001_create_table_users.sql b/store/datastore/ddl/sqlite/files/001_create_table_users.sql new file mode 100644 index 000000000..7e0adffbc --- /dev/null +++ b/store/datastore/ddl/sqlite/files/001_create_table_users.sql @@ -0,0 +1,15 @@ +-- name: create-table-users + +CREATE TABLE IF NOT EXISTS users ( + user_id INTEGER PRIMARY KEY AUTOINCREMENT +,user_login TEXT +,user_token TEXT +,user_secret TEXT +,user_expiry INTEGER +,user_email TEXT +,user_avatar TEXT +,user_active BOOLEAN +,user_admin BOOLEAN +,user_hash TEXT +,UNIQUE(user_login) +); diff --git a/store/datastore/ddl/sqlite/files/002_create_table_repos.sql b/store/datastore/ddl/sqlite/files/002_create_table_repos.sql new file mode 100644 index 000000000..d8f3683ec --- /dev/null +++ b/store/datastore/ddl/sqlite/files/002_create_table_repos.sql @@ -0,0 +1,25 @@ +-- name: create-table-repos + +CREATE TABLE IF NOT EXISTS repos ( + repo_id INTEGER PRIMARY KEY AUTOINCREMENT +,repo_user_id INTEGER +,repo_owner TEXT +,repo_name TEXT +,repo_full_name TEXT +,repo_avatar TEXT +,repo_link TEXT +,repo_clone TEXT +,repo_branch TEXT +,repo_timeout INTEGER +,repo_private BOOLEAN +,repo_trusted BOOLEAN +,repo_allow_pr BOOLEAN +,repo_allow_push BOOLEAN +,repo_allow_deploys BOOLEAN +,repo_allow_tags BOOLEAN +,repo_hash TEXT +,repo_scm TEXT +,repo_config_path TEXT +,repo_gated BOOLEAN +,UNIQUE(repo_full_name) +); diff --git a/store/datastore/ddl/sqlite/files/003_create_table_builds.sql b/store/datastore/ddl/sqlite/files/003_create_table_builds.sql new file mode 100644 index 000000000..e68a7b5d6 --- /dev/null +++ b/store/datastore/ddl/sqlite/files/003_create_table_builds.sql @@ -0,0 +1,48 @@ +-- name: create-table-builds + +CREATE TABLE IF NOT EXISTS builds ( + build_id INTEGER PRIMARY KEY AUTOINCREMENT +,build_repo_id INTEGER +,build_number INTEGER +,build_event TEXT +,build_status TEXT +,build_enqueued INTEGER +,build_created INTEGER +,build_started INTEGER +,build_finished INTEGER +,build_commit TEXT +,build_branch TEXT +,build_ref TEXT +,build_refspec TEXT +,build_remote TEXT +,build_title TEXT +,build_message TEXT +,build_timestamp INTEGER +,build_author TEXT +,build_avatar TEXT +,build_email TEXT +,build_link TEXT +,build_deploy TEXT +,build_signed BOOLEAN +,build_verified BOOLEAN +,build_parent INTEGER +,build_error TEXT +,build_reviewer TEXT +,build_reviewed INTEGER +,build_sender TEXT +,build_config_id INTEGER +,UNIQUE(build_number, build_repo_id) +); + +-- name: create-index-builds-repo + +CREATE INDEX IF NOT EXISTS ix_build_repo ON builds (build_repo_id); + +-- name: create-index-builds-author + +CREATE INDEX IF NOT EXISTS ix_build_author ON builds (build_author); + +-- name: create-index-builds-status + +CREATE INDEX IF NOT EXISTS ix_build_status_running ON builds (build_status) +WHERE build_status IN ('pending', 'running'); diff --git a/store/datastore/ddl/sqlite/files/004_create_table_procs.sql b/store/datastore/ddl/sqlite/files/004_create_table_procs.sql new file mode 100644 index 000000000..08c7080c6 --- /dev/null +++ b/store/datastore/ddl/sqlite/files/004_create_table_procs.sql @@ -0,0 +1,23 @@ +-- name: create-table-procs + +CREATE TABLE IF NOT EXISTS procs ( + proc_id INTEGER PRIMARY KEY AUTOINCREMENT +,proc_build_id INTEGER +,proc_pid INTEGER +,proc_ppid INTEGER +,proc_pgid INTEGER +,proc_name TEXT +,proc_state TEXT +,proc_error TEXT +,proc_exit_code INTEGER +,proc_started INTEGER +,proc_stopped INTEGER +,proc_machine TEXT +,proc_platform TEXT +,proc_environ TEXT +,UNIQUE(proc_build_id, proc_pid) +); + +-- name: create-index-procs-build + +CREATE INDEX IF NOT EXISTS proc_build_ix ON procs (proc_build_id); diff --git a/store/datastore/ddl/sqlite/files/005_create_table_logs.sql b/store/datastore/ddl/sqlite/files/005_create_table_logs.sql new file mode 100644 index 000000000..e6bf78c92 --- /dev/null +++ b/store/datastore/ddl/sqlite/files/005_create_table_logs.sql @@ -0,0 +1,8 @@ +-- name: create-table-logs + +CREATE TABLE IF NOT EXISTS logs ( + log_id INTEGER PRIMARY KEY AUTOINCREMENT +,log_job_id INTEGER +,log_data BLOB +,UNIQUE(log_job_id) +); diff --git a/store/datastore/ddl/sqlite/files/006_create_table_files.sql b/store/datastore/ddl/sqlite/files/006_create_table_files.sql new file mode 100644 index 000000000..fd08f035d --- /dev/null +++ b/store/datastore/ddl/sqlite/files/006_create_table_files.sql @@ -0,0 +1,21 @@ +-- name: create-table-files + +CREATE TABLE IF NOT EXISTS files ( + file_id INTEGER PRIMARY KEY AUTOINCREMENT +,file_build_id INTEGER +,file_proc_id INTEGER +,file_name TEXT +,file_mime TEXT +,file_size INTEGER +,file_time INTEGER +,file_data BLOB +,UNIQUE(file_proc_id,file_name) +); + +-- name: create-index-files-builds + +CREATE INDEX IF NOT EXISTS file_build_ix ON files (file_build_id); + +-- name: create-index-files-procs + +CREATE INDEX IF NOT EXISTS file_proc_ix ON files (file_proc_id); diff --git a/store/datastore/ddl/sqlite/files/007_create_table_secets.sql b/store/datastore/ddl/sqlite/files/007_create_table_secets.sql new file mode 100644 index 000000000..9305f16ad --- /dev/null +++ b/store/datastore/ddl/sqlite/files/007_create_table_secets.sql @@ -0,0 +1,17 @@ +-- name: create-table-secrets + +CREATE TABLE IF NOT EXISTS secrets ( + secret_id INTEGER PRIMARY KEY AUTOINCREMENT +,secret_repo_id INTEGER +,secret_name TEXT +,secret_value TEXT +,secret_images TEXT +,secret_events TEXT +,secret_skip_verify BOOLEAN +,secret_conceal BOOLEAN +,UNIQUE(secret_name, secret_repo_id) +); + +-- name: create-index-secrets-repo + +CREATE INDEX IF NOT EXISTS ix_secrets_repo ON secrets (secret_repo_id); diff --git a/store/datastore/ddl/sqlite/files/008_create_table_registry.sql b/store/datastore/ddl/sqlite/files/008_create_table_registry.sql new file mode 100644 index 000000000..10bd39685 --- /dev/null +++ b/store/datastore/ddl/sqlite/files/008_create_table_registry.sql @@ -0,0 +1,17 @@ +-- name: create-table-registry + +CREATE TABLE IF NOT EXISTS registry ( + registry_id INTEGER PRIMARY KEY AUTOINCREMENT +,registry_repo_id INTEGER +,registry_addr TEXT +,registry_username TEXT +,registry_password TEXT +,registry_email TEXT +,registry_token TEXT + +,UNIQUE(registry_addr, registry_repo_id) +); + +-- name: create-index-registry-repo + +CREATE INDEX IF NOT EXISTS ix_registry_repo ON registry (registry_repo_id); diff --git a/store/datastore/ddl/sqlite/files/009_create_table_config.sql b/store/datastore/ddl/sqlite/files/009_create_table_config.sql new file mode 100644 index 000000000..cf81a9889 --- /dev/null +++ b/store/datastore/ddl/sqlite/files/009_create_table_config.sql @@ -0,0 +1,9 @@ +-- name: create-table-config + +CREATE TABLE IF NOT EXISTS config ( + config_id INTEGER PRIMARY KEY AUTOINCREMENT +,config_repo_id INTEGER +,config_hash TEXT +,config_data BLOB +,UNIQUE(config_hash, config_repo_id) +); diff --git a/store/datastore/ddl/sqlite/files/010_create_table_tasks.sql b/store/datastore/ddl/sqlite/files/010_create_table_tasks.sql new file mode 100644 index 000000000..ddc9e7471 --- /dev/null +++ b/store/datastore/ddl/sqlite/files/010_create_table_tasks.sql @@ -0,0 +1,7 @@ +-- name: create-table-tasks + +CREATE TABLE IF NOT EXISTS tasks ( + task_id TEXT PRIMARY KEY +,task_data BLOB +,task_labels BLOB +); diff --git a/store/datastore/ddl/sqlite3/6.sql b/store/datastore/ddl/sqlite/files/011_create_table_agents.sql similarity index 71% rename from store/datastore/ddl/sqlite3/6.sql rename to store/datastore/ddl/sqlite/files/011_create_table_agents.sql index 829c69132..273c42b79 100644 --- a/store/datastore/ddl/sqlite3/6.sql +++ b/store/datastore/ddl/sqlite/files/011_create_table_agents.sql @@ -1,6 +1,6 @@ --- +migrate Up +-- name: create-table-agents -CREATE TABLE agents ( +CREATE TABLE IF NOT EXISTS agents ( agent_id INTEGER PRIMARY KEY AUTOINCREMENT ,agent_addr TEXT ,agent_platform TEXT @@ -10,8 +10,3 @@ CREATE TABLE agents ( ,UNIQUE(agent_addr) ); - - --- +migrate Down - -DROP TABLE agents; diff --git a/store/datastore/ddl/sqlite/files/012_create_table_senders.sql b/store/datastore/ddl/sqlite/files/012_create_table_senders.sql new file mode 100644 index 000000000..e1b37772f --- /dev/null +++ b/store/datastore/ddl/sqlite/files/012_create_table_senders.sql @@ -0,0 +1,15 @@ +-- name: create-table-senders + +CREATE TABLE IF NOT EXISTS senders ( + sender_id INTEGER PRIMARY KEY AUTOINCREMENT +,sender_repo_id INTEGER +,sender_login TEXT +,sender_allow BOOLEAN +,sender_block BOOLEAN + +,UNIQUE(sender_repo_id,sender_login) +); + +-- name: create-index-sender-repos + +CREATE INDEX IF NOT EXISTS sender_repo_ix ON senders (sender_repo_id); diff --git a/store/datastore/ddl/sqlite/files/XXX_init.xxx b/store/datastore/ddl/sqlite/files/XXX_init.xxx new file mode 100644 index 000000000..b8c567c59 --- /dev/null +++ b/store/datastore/ddl/sqlite/files/XXX_init.xxx @@ -0,0 +1,242 @@ +-- name: create-table-users + +CREATE TABLE users ( + user_id INTEGER PRIMARY KEY AUTOINCREMENT +,user_login TEXT +,user_token TEXT +,user_secret TEXT +,user_expiry INTEGER +,user_email TEXT +,user_avatar TEXT +,user_active BOOLEAN +,user_admin BOOLEAN +,user_hash TEXT + +,UNIQUE(user_login) +); + +-------------------------------------------------------------------------------- +-- name: create-table-repos + +CREATE TABLE repos ( + repo_id INTEGER PRIMARY KEY AUTOINCREMENT +,repo_user_id INTEGER +,repo_owner TEXT +,repo_name TEXT +,repo_full_name TEXT +,repo_avatar TEXT +,repo_link TEXT +,repo_clone TEXT +,repo_branch TEXT +,repo_timeout INTEGER +,repo_private BOOLEAN +,repo_trusted BOOLEAN +,repo_allow_pr BOOLEAN +,repo_allow_push BOOLEAN +,repo_allow_deploys BOOLEAN +,repo_allow_tags BOOLEAN +,repo_hash TEXT +,repo_scm TEXT +,repo_config_path TEXT +,repo_gated BOOLEAN +,UNIQUE(repo_full_name) +); + +-------------------------------------------------------------------------------- +-- name: create-table-secrets + +CREATE TABLE secrets ( + secret_id INTEGER PRIMARY KEY AUTOINCREMENT +,secret_repo_id INTEGER +,secret_name TEXT +,secret_value TEXT +,secret_images TEXT +,secret_events TEXT +,secret_skip_verify BOOLEAN +,secret_conceal BOOLEAN +,UNIQUE(secret_name, secret_repo_id) +); + +-- name: create-index-secrets-repo + +CREATE INDEX ix_secrets_repo ON secrets (secret_repo_id); + +-------------------------------------------------------------------------------- +-- name: create-table-registry + +CREATE TABLE registry ( + registry_id INTEGER PRIMARY KEY AUTOINCREMENT +,registry_repo_id INTEGER +,registry_addr TEXT +,registry_username TEXT +,registry_password TEXT +,registry_email TEXT +,registry_token TEXT + +,UNIQUE(registry_addr, registry_repo_id) +); + +-- name: create-index-registry-repo + +CREATE INDEX ix_registry_repo ON registry (registry_repo_id); + +-------------------------------------------------------------------------------- +-- name: create-table-builds + +CREATE TABLE builds ( + build_id INTEGER PRIMARY KEY AUTOINCREMENT +,build_repo_id INTEGER +,build_number INTEGER +,build_event TEXT +,build_status TEXT +,build_enqueued INTEGER +,build_created INTEGER +,build_started INTEGER +,build_finished INTEGER +,build_commit TEXT +,build_branch TEXT +,build_ref TEXT +,build_refspec TEXT +,build_remote TEXT +,build_title TEXT +,build_message TEXT +,build_timestamp INTEGER +,build_author TEXT +,build_avatar TEXT +,build_email TEXT +,build_link TEXT +,build_deploy TEXT +,build_signed BOOLEAN +,build_verified BOOLEAN +,build_parent INTEGER +,build_error TEXT +,build_reviewer TEXT +,build_reviewed INTEGER +,build_sender TEXT +,build_config_id INTEGER +,UNIQUE(build_number, build_repo_id) +); + +-- name: create-index-builds-repo + +CREATE INDEX ix_build_repo ON builds (build_repo_id); + +-- name: create-index-builds-author + +CREATE INDEX ix_build_author ON builds (build_author); + +-- name: create-index-builds-status + +CREATE INDEX ix_build_status_running ON builds (build_status) +WHERE build_status IN ('pending', 'running'); + +-------------------------------------------------------------------------------- +-- name: create-table-procs + +CREATE TABLE procs ( + proc_id INTEGER PRIMARY KEY AUTOINCREMENT +,proc_build_id INTEGER +,proc_pid INTEGER +,proc_ppid INTEGER +,proc_pgid INTEGER +,proc_name TEXT +,proc_state TEXT +,proc_error TEXT +,proc_exit_code INTEGER +,proc_started INTEGER +,proc_stopped INTEGER +,proc_machine TEXT +,proc_platform TEXT +,proc_environ TEXT +,UNIQUE(proc_build_id, proc_pid) +); + +-- name: create-index-procs-build + +CREATE INDEX proc_build_ix ON procs (proc_build_id); + +-------------------------------------------------------------------------------- +-- name: create-table-logs + +CREATE TABLE IF NOT EXISTS logs ( + log_id INTEGER PRIMARY KEY AUTOINCREMENT +,log_job_id INTEGER +,log_data BLOB +,UNIQUE(log_job_id) +); + +-------------------------------------------------------------------------------- +-- name: create-table-files + +CREATE TABLE IF NOT EXISTS files ( + file_id INTEGER PRIMARY KEY AUTOINCREMENT +,file_build_id INTEGER +,file_proc_id INTEGER +,file_name TEXT +,file_mime TEXT +,file_size INTEGER +,file_time INTEGER +,file_data BLOB +,UNIQUE(file_proc_id,file_name) +,FOREIGN KEY(file_proc_id) REFERENCES procs (proc_id) ON DELETE CASCADE +); + +-- name: create-index-files-builds + +CREATE INDEX file_build_ix ON files (file_build_id); + +-- name: create-index-files-procs + +CREATE INDEX file_proc_ix ON files (file_proc_id); + +-------------------------------------------------------------------------------- +-- name: create-table-senders + +CREATE TABLE IF NOT EXISTS senders ( + sender_id INTEGER PRIMARY KEY AUTOINCREMENT +,sender_repo_id INTEGER +,sender_login BOOLEAN +,sender_allow BOOLEAN +,sender_block BOOLEAN + +,UNIQUE(sender_repo_id,sender_login) +); + +-- name: create-index-sender-repos + +CREATE INDEX sender_repo_ix ON senders (sender_repo_id); + +-------------------------------------------------------------------------------- +-- name: create-table-config + +CREATE TABLE IF NOT EXISTS config ( + config_id INTEGER PRIMARY KEY AUTOINCREMENT +,config_repo_id INTEGER +,config_hash TEXT +,config_data BLOB + +,UNIQUE(config_hash, config_repo_id) +); + +-------------------------------------------------------------------------------- +-- name: create-table-tasks + +CREATE TABLE IF NOT EXISTS tasks ( + task_id TEXT PRIMARY KEY +,task_data BLOB +,task_labels BLOB +); + +-------------------------------------------------------------------------------- +-- name: create-table-agents + +CREATE TABLE IF NOT EXISTS agents ( + agent_id INTEGER PRIMARY KEY AUTOINCREMENT +,agent_addr TEXT +,agent_platform TEXT +,agent_capacity INTEGER +,agent_created INTEGER +,agent_updated INTEGER + +,UNIQUE(agent_addr) +); diff --git a/store/datastore/ddl/sqlite3/10.sql b/store/datastore/ddl/sqlite3/10.sql deleted file mode 100644 index 91630acac..000000000 --- a/store/datastore/ddl/sqlite3/10.sql +++ /dev/null @@ -1,12 +0,0 @@ --- +migrate Up - -ALTER TABLE secrets ADD COLUMN secret_conceal BOOLEAN; -ALTER TABLE team_secrets ADD COLUMN team_secret_conceal BOOLEAN; - -UPDATE secrets SET secret_conceal = 0; -UPDATE team_secrets SET team_secret_conceal = 0; - --- +migrate Down - -ALTER TABLE secrets DROP COLUMN secret_conceal; -ALTER TABLE team_secrets DROP COLUMN team_secret_conceal; diff --git a/store/datastore/ddl/sqlite3/11.sql b/store/datastore/ddl/sqlite3/11.sql deleted file mode 100644 index d5e3c47d0..000000000 --- a/store/datastore/ddl/sqlite3/11.sql +++ /dev/null @@ -1,8 +0,0 @@ --- +migrate Up - -ALTER TABLE builds ADD COLUMN build_error TEXT; -UPDATE builds SET build_error = ''; - --- +migrate Down - -ALTER TABLE builds DROP COLUMN build_error; diff --git a/store/datastore/ddl/sqlite3/12.sql b/store/datastore/ddl/sqlite3/12.sql deleted file mode 100644 index c641eec9d..000000000 --- a/store/datastore/ddl/sqlite3/12.sql +++ /dev/null @@ -1,18 +0,0 @@ --- +migrate Up - -ALTER TABLE repos ADD COLUMN repo_config_path TEXT; -ALTER TABLE builds ADD COLUMN build_reviewer TEXT; -ALTER TABLE builds ADD COLUMN build_reviewed INTEGER; -ALTER TABLE builds ADD COLUMN build_sender TEXT; - -UPDATE repos SET repo_config_path = '.drone.yml'; -UPDATE builds SET build_reviewer = ''; -UPDATE builds SET build_reviewed = 0; -UPDATE builds SET build_sender = ''; - --- +migrate Down - -ALTER TABLE repos DROP COLUMN repo_config_path; -ALTER TABLE builds DROP COLUMN build_reviewer; -ALTER TABLE builds DROP COLUMN build_reviewed; -ALTER TABLE builds DROP COLUMN build_sender; diff --git a/store/datastore/ddl/sqlite3/13.sql b/store/datastore/ddl/sqlite3/13.sql deleted file mode 100644 index e57ca6f9b..000000000 --- a/store/datastore/ddl/sqlite3/13.sql +++ /dev/null @@ -1,46 +0,0 @@ --- +migrate Up - -CREATE TABLE procs ( - proc_id INTEGER PRIMARY KEY AUTOINCREMENT -,proc_build_id INTEGER -,proc_pid INTEGER -,proc_ppid INTEGER -,proc_pgid INTEGER -,proc_name TEXT -,proc_state TEXT -,proc_error TEXT -,proc_exit_code INTEGER -,proc_started INTEGER -,proc_stopped INTEGER -,proc_machine TEXT -,proc_platform TEXT -,proc_environ TEXT -,UNIQUE(proc_build_id, proc_pid) -); - -CREATE INDEX proc_build_ix ON procs (proc_build_id); - -CREATE TABLE files ( - file_id INTEGER PRIMARY KEY AUTOINCREMENT -,file_build_id INTEGER -,file_proc_id INTEGER -,file_name TEXT -,file_mime TEXT -,file_size INTEGER -,file_time INTEGER -,file_data BLOB -,UNIQUE(file_proc_id,file_name) -,FOREIGN KEY(file_proc_id) REFERENCES procs (proc_id) ON DELETE CASCADE -); - -CREATE INDEX file_build_ix ON files (file_build_id); -CREATE INDEX file_proc_ix ON files (file_proc_id); - --- +migrate Down - -DROP INDEX file_build_ix; -DROP INDEX file_proc_ix; -DROP TABLE files; - -DROP INDEX proc_build_ix; -DROP TABLE procs; diff --git a/store/datastore/ddl/sqlite3/14.sql b/store/datastore/ddl/sqlite3/14.sql deleted file mode 100644 index cc91f2024..000000000 --- a/store/datastore/ddl/sqlite3/14.sql +++ /dev/null @@ -1,22 +0,0 @@ --- +migrate Up - -ALTER TABLE repos ADD COLUMN repo_gated BOOLEAN; -UPDATE repos SET repo_gated = 0; - -CREATE TABLE senders ( - sender_id INTEGER PRIMARY KEY AUTOINCREMENT -,sender_repo_id INTEGER -,sender_login BOOLEAN -,sender_allow BOOLEAN -,sender_block BOOLEAN - -,UNIQUE(sender_repo_id,sender_login) -); - -CREATE INDEX sender_repo_ix ON senders (sender_repo_id); - --- +migrate Down - -ALTER TABLE repos DROP COLUMN repo_gated; -DROP INDEX sender_repo_ix; -DROP TABLE senders; diff --git a/store/datastore/ddl/sqlite3/15.sql b/store/datastore/ddl/sqlite3/15.sql deleted file mode 100644 index 93f5d1437..000000000 --- a/store/datastore/ddl/sqlite3/15.sql +++ /dev/null @@ -1,11 +0,0 @@ --- +migrate Up - -CREATE TABLE tasks ( - task_id TEXT PRIMARY KEY -,task_data BLOB -,task_labels BLOB -); - --- +migrate Down - -DROP TABLE tasks; diff --git a/store/datastore/ddl/sqlite3/16.sql b/store/datastore/ddl/sqlite3/16.sql deleted file mode 100644 index fc6129f49..000000000 --- a/store/datastore/ddl/sqlite3/16.sql +++ /dev/null @@ -1,17 +0,0 @@ --- +migrate Up - -CREATE TABLE config ( - config_id INTEGER PRIMARY KEY AUTOINCREMENT -,config_repo_id INTEGER -,config_hash TEXT -,config_data BLOB - -,UNIQUE(config_hash, config_repo_id) -); - -ALTER TABLE builds ADD COLUMN build_config_id INTEGER; -UPDATE builds set build_config_id = 0; - --- +migrate Down - -DROP TABLE config; diff --git a/store/datastore/ddl/sqlite3/1_init.sql b/store/datastore/ddl/sqlite3/1_init.sql deleted file mode 100644 index 281885513..000000000 --- a/store/datastore/ddl/sqlite3/1_init.sql +++ /dev/null @@ -1,135 +0,0 @@ --- +migrate Up - -CREATE TABLE users ( - user_id INTEGER PRIMARY KEY AUTOINCREMENT -,user_login TEXT -,user_token TEXT -,user_secret TEXT -,user_expiry INTEGER -,user_email TEXT -,user_avatar TEXT -,user_active BOOLEAN -,user_admin BOOLEAN -,user_hash TEXT - -,UNIQUE(user_login) -); - -CREATE TABLE repos ( - repo_id INTEGER PRIMARY KEY AUTOINCREMENT -,repo_user_id INTEGER -,repo_owner TEXT -,repo_name TEXT -,repo_full_name TEXT -,repo_avatar TEXT -,repo_link TEXT -,repo_clone TEXT -,repo_branch TEXT -,repo_timeout INTEGER -,repo_private BOOLEAN -,repo_trusted BOOLEAN -,repo_allow_pr BOOLEAN -,repo_allow_push BOOLEAN -,repo_allow_deploys BOOLEAN -,repo_allow_tags BOOLEAN -,repo_hash TEXT - -,UNIQUE(repo_full_name) -); - -CREATE TABLE stars ( - star_id INTEGER PRIMARY KEY AUTOINCREMENT -,star_repo_id INTEGER -,star_user_id INTEGER - -,UNIQUE(star_repo_id, star_user_id) -); - -CREATE INDEX ix_star_user ON stars (star_user_id); - -CREATE TABLE keys ( - key_id INTEGER PRIMARY KEY AUTOINCREMENT -,key_repo_id INTEGER -,key_public BLOB -,key_private BLOB - -,UNIQUE(key_repo_id) -); - -CREATE TABLE builds ( - build_id INTEGER PRIMARY KEY AUTOINCREMENT -,build_repo_id INTEGER -,build_number INTEGER -,build_event TEXT -,build_status TEXT -,build_enqueued INTEGER -,build_created INTEGER -,build_started INTEGER -,build_finished INTEGER -,build_commit TEXT -,build_branch TEXT -,build_ref TEXT -,build_refspec TEXT -,build_remote TEXT -,build_title TEXT -,build_message TEXT -,build_timestamp INTEGER -,build_author TEXT -,build_avatar TEXT -,build_email TEXT -,build_link TEXT - -,UNIQUE(build_number, build_repo_id) -); - -CREATE INDEX ix_build_repo ON builds (build_repo_id); -CREATE INDEX ix_build_author ON builds (build_author); - -CREATE TABLE jobs ( - job_id INTEGER PRIMARY KEY AUTOINCREMENT -,job_node_id INTEGER -,job_build_id INTEGER -,job_number INTEGER -,job_status TEXT -,job_exit_code INTEGER -,job_enqueued INTEGER -,job_started INTEGER -,job_finished INTEGER -,job_environment TEXT - -,UNIQUE(job_build_id, job_number) -); - -CREATE INDEX ix_job_build ON jobs (job_build_id); -CREATE INDEX ix_job_node ON jobs (job_node_id); - -CREATE TABLE IF NOT EXISTS logs ( - log_id INTEGER PRIMARY KEY AUTOINCREMENT -,log_job_id INTEGER -,log_data BLOB - -,UNIQUE(log_job_id) -); - -CREATE TABLE IF NOT EXISTS nodes ( - node_id INTEGER PRIMARY KEY AUTOINCREMENT -,node_addr TEXT -,node_arch TEXT -,node_cert BLOB -,node_key BLOB -,node_ca BLOB -); - -INSERT INTO nodes VALUES(null, 'unix:///var/run/docker.sock', 'linux_amd64', '', '', ''); -INSERT INTO nodes VALUES(null, 'unix:///var/run/docker.sock', 'linux_amd64', '', '', ''); - --- +migrate Down - -DROP TABLE nodes; -DROP TABLE logs; -DROP TABLE jobs; -DROP TABLE builds; -DROP TABLE keys; -DROP TABLE stars; -DROP TABLE repos; -DROP TABLE users; \ No newline at end of file diff --git a/store/datastore/ddl/sqlite3/2.sql b/store/datastore/ddl/sqlite3/2.sql deleted file mode 100644 index b9ad93ef0..000000000 --- a/store/datastore/ddl/sqlite3/2.sql +++ /dev/null @@ -1,12 +0,0 @@ --- +migrate Up - -ALTER TABLE repos ADD COLUMN repo_scm TEXT; -ALTER TABLE builds ADD COLUMN build_deploy TEXT; - -UPDATE repos SET repo_scm = 'git'; -UPDATE builds SET build_deploy = ''; - --- +migrate Down - -ALTER TABLE repos DROP COLUMN repo_scm; -ALTER TABLE builds DROP COLUMN build_deploy; diff --git a/store/datastore/ddl/sqlite3/3.sql b/store/datastore/ddl/sqlite3/3.sql deleted file mode 100644 index 88b3e1eb3..000000000 --- a/store/datastore/ddl/sqlite3/3.sql +++ /dev/null @@ -1,34 +0,0 @@ --- +migrate Up - -CREATE TABLE secrets ( - secret_id INTEGER PRIMARY KEY AUTOINCREMENT -,secret_repo_id INTEGER -,secret_name TEXT -,secret_value TEXT -,secret_images TEXT -,secret_events TEXT - -,UNIQUE(secret_name, secret_repo_id) -); - -CREATE TABLE registry ( - registry_id INTEGER PRIMARY KEY AUTOINCREMENT -,registry_repo_id INTEGER -,registry_addr TEXT -,registry_username TEXT -,registry_password TEXT -,registry_email TEXT -,registry_token TEXT - -,UNIQUE(registry_addr, registry_repo_id) -); - -CREATE INDEX ix_secrets_repo ON secrets (secret_repo_id); -CREATE INDEX ix_registry_repo ON registry (registry_repo_id); - --- +migrate Down - -DROP INDEX ix_secrets_repo; -DROP INDEX ix_registry_repo; -DROP TABLE secrets; -DROP TABLE registry; diff --git a/store/datastore/ddl/sqlite3/4.sql b/store/datastore/ddl/sqlite3/4.sql deleted file mode 100644 index a57b6b103..000000000 --- a/store/datastore/ddl/sqlite3/4.sql +++ /dev/null @@ -1,9 +0,0 @@ --- +migrate Up - -ALTER TABLE jobs ADD COLUMN job_error TEXT; - -UPDATE jobs SET job_error = ''; - --- +migrate Down - -ALTER TABLE jobs DROP COLUMN job_error; diff --git a/store/datastore/ddl/sqlite3/5.sql b/store/datastore/ddl/sqlite3/5.sql deleted file mode 100644 index 21e97c3b0..000000000 --- a/store/datastore/ddl/sqlite3/5.sql +++ /dev/null @@ -1,16 +0,0 @@ --- +migrate Up - -ALTER TABLE builds ADD COLUMN build_signed BOOLEAN; -ALTER TABLE builds ADD COLUMN build_verified BOOLEAN; - -UPDATE builds SET build_signed = 0; -UPDATE builds SET build_verified = 0; - -CREATE INDEX ix_build_status_running ON builds (build_status) - WHERE build_status IN ('pending', 'running'); - --- +migrate Down - -ALTER TABLE builds DROP COLUMN build_signed; -ALTER TABLE builds DROP COLUMN build_verified; -DROP INDEX ix_build_status_running; diff --git a/store/datastore/ddl/sqlite3/7.sql b/store/datastore/ddl/sqlite3/7.sql deleted file mode 100644 index ce3e05145..000000000 --- a/store/datastore/ddl/sqlite3/7.sql +++ /dev/null @@ -1,19 +0,0 @@ --- +migrate Up - -CREATE TABLE team_secrets ( - team_secret_id INTEGER PRIMARY KEY AUTOINCREMENT -,team_secret_key TEXT -,team_secret_name TEXT -,team_secret_value TEXT -,team_secret_images TEXT -,team_secret_events TEXT - -,UNIQUE(team_secret_name, team_secret_key) -); - -CREATE INDEX ix_team_secrets_key ON team_secrets (team_secret_key); - --- +migrate Down - -DROP INDEX ix_team_secrets_key; -DROP TABLE team_secrets; diff --git a/store/datastore/ddl/sqlite3/8.sql b/store/datastore/ddl/sqlite3/8.sql deleted file mode 100644 index ad6759b26..000000000 --- a/store/datastore/ddl/sqlite3/8.sql +++ /dev/null @@ -1,7 +0,0 @@ --- +migrate Up - -ALTER TABLE builds ADD COLUMN build_parent INTEGER DEFAULT 0; - --- +migrate Down - -ALTER TABLE builds DROP COLUMN build_parent; diff --git a/store/datastore/ddl/sqlite3/9.sql b/store/datastore/ddl/sqlite3/9.sql deleted file mode 100644 index 5cc02eb77..000000000 --- a/store/datastore/ddl/sqlite3/9.sql +++ /dev/null @@ -1,12 +0,0 @@ --- +migrate Up - -ALTER TABLE secrets ADD COLUMN secret_skip_verify BOOLEAN; -ALTER TABLE team_secrets ADD COLUMN team_secret_skip_verify BOOLEAN; - -UPDATE secrets SET secret_skip_verify = 0; -UPDATE team_secrets SET team_secret_skip_verify = 0; - --- +migrate Down - -ALTER TABLE secrets DROP COLUMN secret_skip_verify; -ALTER TABLE team_secrets DROP COLUMN team_secret_skip_verify; diff --git a/store/datastore/store.go b/store/datastore/store.go index 4e24ced3a..eb7058dc4 100644 --- a/store/datastore/store.go +++ b/store/datastore/store.go @@ -7,7 +7,6 @@ import ( "github.com/drone/drone/store" "github.com/drone/drone/store/datastore/ddl" - "github.com/rubenv/sql-migrate" "github.com/russross/meddler" "github.com/Sirupsen/logrus" @@ -116,13 +115,7 @@ func pingDatabase(db *sql.DB) (err error) { // helper function to setup the databsae by performing // automated database migration steps. func setupDatabase(driver string, db *sql.DB) error { - var migrations = &migrate.AssetMigrationSource{ - Asset: ddl.Asset, - AssetDir: ddl.AssetDir, - Dir: driver, - } - _, err := migrate.Exec(db, driver, migrations, migrate.Up) - return err + return ddl.Migrate(driver, db) } // helper function to setup the meddler default driver diff --git a/vendor/github.com/rubenv/sql-migrate/README.md b/vendor/github.com/rubenv/sql-migrate/README.md deleted file mode 100644 index 897a5f698..000000000 --- a/vendor/github.com/rubenv/sql-migrate/README.md +++ /dev/null @@ -1,245 +0,0 @@ -# sql-migrate - -> SQL Schema migration tool for [Go](http://golang.org/). Based on [gorp](https://github.com/go-gorp/gorp) and [goose](https://bitbucket.org/liamstask/goose). - -[![Build Status](https://travis-ci.org/rubenv/sql-migrate.svg?branch=master)](https://travis-ci.org/rubenv/sql-migrate) [![GoDoc](https://godoc.org/github.com/rubenv/sql-migrate?status.png)](https://godoc.org/github.com/rubenv/sql-migrate) - -Using [modl](https://github.com/jmoiron/modl)? Check out [modl-migrate](https://github.com/rubenv/modl-migrate). - -## Features - -* Usable as a CLI tool or as a library -* Supports SQLite, PostgreSQL, MySQL, MSSQL and Oracle databases (through [gorp](https://github.com/go-gorp/gorp)) -* Can embed migrations into your application -* Migrations are defined with SQL for full flexibility -* Atomic migrations -* Up/down migrations to allow rollback -* Supports multiple database types in one project - -## Installation - -To install the library and command line program, use the following: - -```bash -go get github.com/rubenv/sql-migrate/... -``` - -## Usage -### As a standalone tool -``` -$ sql-migrate --help -usage: sql-migrate [--version] [--help] [] - -Available commands are: - down Undo a database migration - redo Reapply the last migration - status Show migration status - up Migrates the database to the most recent version available -``` - -Each command requires a configuration file (which defaults to `dbconfig.yml`, but can be specified with the `-config` flag). This config file should specify one or more environments: - -```yml -development: - dialect: sqlite3 - datasource: test.db - dir: migrations/sqlite3 - -production: - dialect: postgres - datasource: dbname=myapp sslmode=disable - dir: migrations/postgres - table: migrations -``` - -The `table` setting is optional and will default to `gorp_migrations`. - -The environment that will be used can be specified with the `-env` flag (defaults to `development`). - -Use the `--help` flag in combination with any of the commands to get an overview of its usage: - -``` -$ sql-migrate up --help -Usage: sql-migrate up [options] ... - - Migrates the database to the most recent version available. - -Options: - - -config=config.yml Configuration file to use. - -env="development" Environment. - -limit=0 Limit the number of migrations (0 = unlimited). - -dryrun Don't apply migrations, just print them. -``` - -The `up` command applies all available migrations. By contrast, `down` will only apply one migration by default. This behavior can be changed for both by using the `-limit` parameter. - -The `redo` command will unapply the last migration and reapply it. This is useful during development, when you're writing migrations. - -Use the `status` command to see the state of the applied migrations: - -```bash -$ sql-migrate status -+---------------+-----------------------------------------+ -| MIGRATION | APPLIED | -+---------------+-----------------------------------------+ -| 1_initial.sql | 2014-09-13 08:19:06.788354925 +0000 UTC | -| 2_record.sql | no | -+---------------+-----------------------------------------+ -``` - -### As a library -Import sql-migrate into your application: - -```go -import "github.com/rubenv/sql-migrate" -``` - -Set up a source of migrations, this can be from memory, from a set of files or from bindata (more on that later): - -```go -// Hardcoded strings in memory: -migrations := &migrate.MemoryMigrationSource{ - Migrations: []*migrate.Migration{ - &migrate.Migration{ - Id: "123", - Up: []string{"CREATE TABLE people (id int)"}, - Down: []string{"DROP TABLE people"}, - }, - }, -} - -// OR: Read migrations from a folder: -migrations := &migrate.FileMigrationSource{ - Dir: "db/migrations", -} - -// OR: Use migrations from bindata: -migrations := &migrate.AssetMigrationSource{ - Asset: Asset, - AssetDir: AssetDir, - Dir: "migrations", -} -``` - -Then use the `Exec` function to upgrade your database: - -```go -db, err := sql.Open("sqlite3", filename) -if err != nil { - // Handle errors! -} - -n, err := migrate.Exec(db, "sqlite3", migrations, migrate.Up) -if err != nil { - // Handle errors! -} -fmt.Printf("Applied %d migrations!\n", n) -``` - -Note that `n` can be greater than `0` even if there is an error: any migration that succeeded will remain applied even if a later one fails. - -Check [the GoDoc reference](https://godoc.org/github.com/rubenv/sql-migrate) for the full documentation. - -## Writing migrations -Migrations are defined in SQL files, which contain a set of SQL statements. Special comments are used to distinguish up and down migrations. - -```sql --- +migrate Up --- SQL in section 'Up' is executed when this migration is applied -CREATE TABLE people (id int); - - --- +migrate Down --- SQL section 'Down' is executed when this migration is rolled back -DROP TABLE people; -``` - -You can put multiple statements in each block, as long as you end them with a semicolon (`;`). - -If you have complex statements which contain semicolons, use `StatementBegin` and `StatementEnd` to indicate boundaries: - -```sql --- +migrate Up -CREATE TABLE people (id int); - --- +migrate StatementBegin -CREATE OR REPLACE FUNCTION do_something() -returns void AS $$ -DECLARE - create_query text; -BEGIN - -- Do something here -END; -$$ -language plpgsql; --- +migrate StatementEnd - --- +migrate Down -DROP FUNCTION do_something(); -DROP TABLE people; -``` - -The order in which migrations are applied is defined through the filename: sql-migrate will sort migrations based on their name. It's recommended to use an increasing version number or a timestamp as the first part of the filename. - -## Embedding migrations with [bindata](https://github.com/jteeuwen/go-bindata) -If you like your Go applications self-contained (that is: a single binary): use [bindata](https://github.com/jteeuwen/go-bindata) to embed the migration files. - -Just write your migration files as usual, as a set of SQL files in a folder. - -Then use bindata to generate a `.go` file with the migrations embedded: - -```bash -go-bindata -pkg myapp -o bindata.go db/migrations/ -``` - -The resulting `bindata.go` file will contain your migrations. Remember to regenerate your `bindata.go` file whenever you add/modify a migration (`go generate` will help here, once it arrives). - -Use the `AssetMigrationSource` in your application to find the migrations: - -```go -migrations := &migrate.AssetMigrationSource{ - Asset: Asset, - AssetDir: AssetDir, - Dir: "db/migrations", -} -``` - -Both `Asset` and `AssetDir` are functions provided by bindata. - -Then proceed as usual. - -## Extending -Adding a new migration source means implementing `MigrationSource`. - -```go -type MigrationSource interface { - FindMigrations() ([]*Migration, error) -} -``` - -The resulting slice of migrations will be executed in the given order, so it should usually be sorted by the `Id` field. - -## License - - (The MIT License) - - Copyright (C) 2014-2015 by Ruben Vermeersch - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in - all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. diff --git a/vendor/github.com/rubenv/sql-migrate/doc.go b/vendor/github.com/rubenv/sql-migrate/doc.go deleted file mode 100644 index 63db39f52..000000000 --- a/vendor/github.com/rubenv/sql-migrate/doc.go +++ /dev/null @@ -1,199 +0,0 @@ -/* - -SQL Schema migration tool for Go. - -Key features: - - * Usable as a CLI tool or as a library - * Supports SQLite, PostgreSQL, MySQL, MSSQL and Oracle databases (through gorp) - * Can embed migrations into your application - * Migrations are defined with SQL for full flexibility - * Atomic migrations - * Up/down migrations to allow rollback - * Supports multiple database types in one project - -Installation - -To install the library and command line program, use the following: - - go get github.com/rubenv/sql-migrate/... - -Command-line tool - -The main command is called sql-migrate. - - $ sql-migrate --help - usage: sql-migrate [--version] [--help] [] - - Available commands are: - down Undo a database migration - redo Reapply the last migration - status Show migration status - up Migrates the database to the most recent version available - -Each command requires a configuration file (which defaults to dbconfig.yml, but can be specified with the -config flag). This config file should specify one or more environments: - - development: - dialect: sqlite3 - datasource: test.db - dir: migrations/sqlite3 - - production: - dialect: postgres - datasource: dbname=myapp sslmode=disable - dir: migrations/postgres - table: migrations - -The `table` setting is optional and will default to `gorp_migrations`. - -The environment that will be used can be specified with the -env flag (defaults to development). - -Use the --help flag in combination with any of the commands to get an overview of its usage: - - $ sql-migrate up --help - Usage: sql-migrate up [options] ... - - Migrates the database to the most recent version available. - - Options: - - -config=config.yml Configuration file to use. - -env="development" Environment. - -limit=0 Limit the number of migrations (0 = unlimited). - -dryrun Don't apply migrations, just print them. - -The up command applies all available migrations. By contrast, down will only apply one migration by default. This behavior can be changed for both by using the -limit parameter. - -The redo command will unapply the last migration and reapply it. This is useful during development, when you're writing migrations. - -Use the status command to see the state of the applied migrations: - - $ sql-migrate status - +---------------+-----------------------------------------+ - | MIGRATION | APPLIED | - +---------------+-----------------------------------------+ - | 1_initial.sql | 2014-09-13 08:19:06.788354925 +0000 UTC | - | 2_record.sql | no | - +---------------+-----------------------------------------+ - -Library - -Import sql-migrate into your application: - - import "github.com/rubenv/sql-migrate" - -Set up a source of migrations, this can be from memory, from a set of files or from bindata (more on that later): - - // Hardcoded strings in memory: - migrations := &migrate.MemoryMigrationSource{ - Migrations: []*migrate.Migration{ - &migrate.Migration{ - Id: "123", - Up: []string{"CREATE TABLE people (id int)"}, - Down: []string{"DROP TABLE people"}, - }, - }, - } - - // OR: Read migrations from a folder: - migrations := &migrate.FileMigrationSource{ - Dir: "db/migrations", - } - - // OR: Use migrations from bindata: - migrations := &migrate.AssetMigrationSource{ - Asset: Asset, - AssetDir: AssetDir, - Dir: "migrations", - } - -Then use the Exec function to upgrade your database: - - db, err := sql.Open("sqlite3", filename) - if err != nil { - // Handle errors! - } - - n, err := migrate.Exec(db, "sqlite3", migrations, migrate.Up) - if err != nil { - // Handle errors! - } - fmt.Printf("Applied %d migrations!\n", n) - -Note that n can be greater than 0 even if there is an error: any migration that succeeded will remain applied even if a later one fails. - -The full set of capabilities can be found in the API docs below. - -Writing migrations - -Migrations are defined in SQL files, which contain a set of SQL statements. Special comments are used to distinguish up and down migrations. - - -- +migrate Up - -- SQL in section 'Up' is executed when this migration is applied - CREATE TABLE people (id int); - - - -- +migrate Down - -- SQL section 'Down' is executed when this migration is rolled back - DROP TABLE people; - -You can put multiple statements in each block, as long as you end them with a semicolon (;). - -If you have complex statements which contain semicolons, use StatementBegin and StatementEnd to indicate boundaries: - - -- +migrate Up - CREATE TABLE people (id int); - - -- +migrate StatementBegin - CREATE OR REPLACE FUNCTION do_something() - returns void AS $$ - DECLARE - create_query text; - BEGIN - -- Do something here - END; - $$ - language plpgsql; - -- +migrate StatementEnd - - -- +migrate Down - DROP FUNCTION do_something(); - DROP TABLE people; - -The order in which migrations are applied is defined through the filename: sql-migrate will sort migrations based on their name. It's recommended to use an increasing version number or a timestamp as the first part of the filename. - -Embedding migrations with bindata - -If you like your Go applications self-contained (that is: a single binary): use bindata (https://github.com/jteeuwen/go-bindata) to embed the migration files. - -Just write your migration files as usual, as a set of SQL files in a folder. - -Then use bindata to generate a .go file with the migrations embedded: - - go-bindata -pkg myapp -o bindata.go db/migrations/ - -The resulting bindata.go file will contain your migrations. Remember to regenerate your bindata.go file whenever you add/modify a migration (go generate will help here, once it arrives). - -Use the AssetMigrationSource in your application to find the migrations: - - migrations := &migrate.AssetMigrationSource{ - Asset: Asset, - AssetDir: AssetDir, - Dir: "db/migrations", - } - -Both Asset and AssetDir are functions provided by bindata. - -Then proceed as usual. - -Extending - -Adding a new migration source means implementing MigrationSource. - - type MigrationSource interface { - FindMigrations() ([]*Migration, error) - } - -The resulting slice of migrations will be executed in the given order, so it should usually be sorted by the Id field. -*/ -package migrate diff --git a/vendor/github.com/rubenv/sql-migrate/migrate.go b/vendor/github.com/rubenv/sql-migrate/migrate.go deleted file mode 100644 index 4f36982f2..000000000 --- a/vendor/github.com/rubenv/sql-migrate/migrate.go +++ /dev/null @@ -1,475 +0,0 @@ -package migrate - -import ( - "bytes" - "database/sql" - "errors" - "fmt" - "io" - "os" - "path" - "regexp" - "sort" - "strconv" - "strings" - "time" - - "github.com/rubenv/sql-migrate/sqlparse" - "gopkg.in/gorp.v1" -) - -type MigrationDirection int - -const ( - Up MigrationDirection = iota - Down -) - -var tableName = "gorp_migrations" -var schemaName = "" -var numberPrefixRegex = regexp.MustCompile(`^(\d+).*$`) - -// Set the name of the table used to store migration info. -// -// Should be called before any other call such as (Exec, ExecMax, ...). -func SetTable(name string) { - if name != "" { - tableName = name - } -} - -// SetSchema sets the name of a schema that the migration table be referenced. -func SetSchema(name string) { - if name != "" { - schemaName = name - } -} - -func getTableName() string { - t := tableName - if schemaName != "" { - t = fmt.Sprintf("%s.%s", schemaName, t) - } - - return t -} - -type Migration struct { - Id string - Up []string - Down []string -} - -func (m Migration) Less(other *Migration) bool { - switch { - case m.isNumeric() && other.isNumeric(): - return m.VersionInt() < other.VersionInt() - case m.isNumeric() && !other.isNumeric(): - return true - case !m.isNumeric() && other.isNumeric(): - return false - default: - return m.Id < other.Id - } -} - -func (m Migration) isNumeric() bool { - return len(m.NumberPrefixMatches()) > 0 -} - -func (m Migration) NumberPrefixMatches() []string { - return numberPrefixRegex.FindStringSubmatch(m.Id) -} - -func (m Migration) VersionInt() int64 { - v := m.NumberPrefixMatches()[1] - value, err := strconv.ParseInt(v, 10, 64) - if err != nil { - panic(fmt.Sprintf("Could not parse %q into int64: %s", v, err)) - } - return value -} - -type PlannedMigration struct { - *Migration - Queries []string -} - -type byId []*Migration - -func (b byId) Len() int { return len(b) } -func (b byId) Swap(i, j int) { b[i], b[j] = b[j], b[i] } -func (b byId) Less(i, j int) bool { return b[i].Less(b[j]) } - -type MigrationRecord struct { - Id string `db:"id"` - AppliedAt time.Time `db:"applied_at"` -} - -var MigrationDialects = map[string]gorp.Dialect{ - "sqlite3": gorp.SqliteDialect{}, - "postgres": gorp.PostgresDialect{}, - "mysql": gorp.MySQLDialect{"InnoDB", "UTF8"}, - "mssql": gorp.SqlServerDialect{}, - "oci8": gorp.OracleDialect{}, -} - -type MigrationSource interface { - // Finds the migrations. - // - // The resulting slice of migrations should be sorted by Id. - FindMigrations() ([]*Migration, error) -} - -// A hardcoded set of migrations, in-memory. -type MemoryMigrationSource struct { - Migrations []*Migration -} - -var _ MigrationSource = (*MemoryMigrationSource)(nil) - -func (m MemoryMigrationSource) FindMigrations() ([]*Migration, error) { - // Make sure migrations are sorted - sort.Sort(byId(m.Migrations)) - - return m.Migrations, nil -} - -// A set of migrations loaded from a directory. -type FileMigrationSource struct { - Dir string -} - -var _ MigrationSource = (*FileMigrationSource)(nil) - -func (f FileMigrationSource) FindMigrations() ([]*Migration, error) { - migrations := make([]*Migration, 0) - - file, err := os.Open(f.Dir) - if err != nil { - return nil, err - } - - files, err := file.Readdir(0) - if err != nil { - return nil, err - } - - for _, info := range files { - if strings.HasSuffix(info.Name(), ".sql") { - file, err := os.Open(path.Join(f.Dir, info.Name())) - if err != nil { - return nil, err - } - - migration, err := ParseMigration(info.Name(), file) - if err != nil { - return nil, err - } - - migrations = append(migrations, migration) - } - } - - // Make sure migrations are sorted - sort.Sort(byId(migrations)) - - return migrations, nil -} - -// Migrations from a bindata asset set. -type AssetMigrationSource struct { - // Asset should return content of file in path if exists - Asset func(path string) ([]byte, error) - - // AssetDir should return list of files in the path - AssetDir func(path string) ([]string, error) - - // Path in the bindata to use. - Dir string -} - -var _ MigrationSource = (*AssetMigrationSource)(nil) - -func (a AssetMigrationSource) FindMigrations() ([]*Migration, error) { - migrations := make([]*Migration, 0) - - files, err := a.AssetDir(a.Dir) - if err != nil { - return nil, err - } - - for _, name := range files { - if strings.HasSuffix(name, ".sql") { - file, err := a.Asset(path.Join(a.Dir, name)) - if err != nil { - return nil, err - } - - migration, err := ParseMigration(name, bytes.NewReader(file)) - if err != nil { - return nil, err - } - - migrations = append(migrations, migration) - } - } - - // Make sure migrations are sorted - sort.Sort(byId(migrations)) - - return migrations, nil -} - -// Migration parsing -func ParseMigration(id string, r io.ReadSeeker) (*Migration, error) { - m := &Migration{ - Id: id, - } - - up, err := sqlparse.SplitSQLStatements(r, true) - if err != nil { - return nil, err - } - - down, err := sqlparse.SplitSQLStatements(r, false) - if err != nil { - return nil, err - } - - m.Up = up - m.Down = down - - return m, nil -} - -// Execute a set of migrations -// -// Returns the number of applied migrations. -func Exec(db *sql.DB, dialect string, m MigrationSource, dir MigrationDirection) (int, error) { - return ExecMax(db, dialect, m, dir, 0) -} - -// Execute a set of migrations -// -// Will apply at most `max` migrations. Pass 0 for no limit (or use Exec). -// -// Returns the number of applied migrations. -func ExecMax(db *sql.DB, dialect string, m MigrationSource, dir MigrationDirection, max int) (int, error) { - migrations, dbMap, err := PlanMigration(db, dialect, m, dir, max) - if err != nil { - return 0, err - } - - // Apply migrations - applied := 0 - for _, migration := range migrations { - trans, err := dbMap.Begin() - if err != nil { - return applied, err - } - - for _, stmt := range migration.Queries { - _, err := trans.Exec(stmt) - if err != nil { - trans.Rollback() - return applied, err - } - } - - if dir == Up { - err = trans.Insert(&MigrationRecord{ - Id: migration.Id, - AppliedAt: time.Now(), - }) - if err != nil { - return applied, err - } - } else if dir == Down { - _, err := trans.Delete(&MigrationRecord{ - Id: migration.Id, - }) - if err != nil { - return applied, err - } - } else { - panic("Not possible") - } - - err = trans.Commit() - if err != nil { - return applied, err - } - - applied++ - } - - return applied, nil -} - -// Plan a migration. -func PlanMigration(db *sql.DB, dialect string, m MigrationSource, dir MigrationDirection, max int) ([]*PlannedMigration, *gorp.DbMap, error) { - dbMap, err := getMigrationDbMap(db, dialect) - if err != nil { - return nil, nil, err - } - - migrations, err := m.FindMigrations() - if err != nil { - return nil, nil, err - } - - var migrationRecords []MigrationRecord - _, err = dbMap.Select(&migrationRecords, fmt.Sprintf("SELECT * FROM %s", getTableName())) - if err != nil { - return nil, nil, err - } - - // Sort migrations that have been run by Id. - var existingMigrations []*Migration - for _, migrationRecord := range migrationRecords { - existingMigrations = append(existingMigrations, &Migration{ - Id: migrationRecord.Id, - }) - } - sort.Sort(byId(existingMigrations)) - - // Get last migration that was run - record := &Migration{} - if len(existingMigrations) > 0 { - record = existingMigrations[len(existingMigrations)-1] - } - - result := make([]*PlannedMigration, 0) - - // Add missing migrations up to the last run migration. - // This can happen for example when merges happened. - if len(existingMigrations) > 0 { - result = append(result, ToCatchup(migrations, existingMigrations, record)...) - } - - // Figure out which migrations to apply - toApply := ToApply(migrations, record.Id, dir) - toApplyCount := len(toApply) - if max > 0 && max < toApplyCount { - toApplyCount = max - } - for _, v := range toApply[0:toApplyCount] { - - if dir == Up { - result = append(result, &PlannedMigration{ - Migration: v, - Queries: v.Up, - }) - } else if dir == Down { - result = append(result, &PlannedMigration{ - Migration: v, - Queries: v.Down, - }) - } - } - - return result, dbMap, nil -} - -// Filter a slice of migrations into ones that should be applied. -func ToApply(migrations []*Migration, current string, direction MigrationDirection) []*Migration { - var index = -1 - if current != "" { - for index < len(migrations)-1 { - index++ - if migrations[index].Id == current { - break - } - } - } - - if direction == Up { - return migrations[index+1:] - } else if direction == Down { - if index == -1 { - return []*Migration{} - } - - // Add in reverse order - toApply := make([]*Migration, index+1) - for i := 0; i < index+1; i++ { - toApply[index-i] = migrations[i] - } - return toApply - } - - panic("Not possible") -} - -func ToCatchup(migrations, existingMigrations []*Migration, lastRun *Migration) []*PlannedMigration { - missing := make([]*PlannedMigration, 0) - for _, migration := range migrations { - found := false - for _, existing := range existingMigrations { - if existing.Id == migration.Id { - found = true - break - } - } - if !found && migration.Less(lastRun) { - missing = append(missing, &PlannedMigration{Migration: migration, Queries: migration.Up}) - } - } - return missing -} - -func GetMigrationRecords(db *sql.DB, dialect string) ([]*MigrationRecord, error) { - dbMap, err := getMigrationDbMap(db, dialect) - if err != nil { - return nil, err - } - - var records []*MigrationRecord - query := fmt.Sprintf("SELECT * FROM %s ORDER BY id ASC", getTableName()) - _, err = dbMap.Select(&records, query) - if err != nil { - return nil, err - } - - return records, nil -} - -func getMigrationDbMap(db *sql.DB, dialect string) (*gorp.DbMap, error) { - d, ok := MigrationDialects[dialect] - if !ok { - return nil, fmt.Errorf("Unknown dialect: %s", dialect) - } - - // When using the mysql driver, make sure that the parseTime option is - // configured, otherwise it won't map time columns to time.Time. See - // https://github.com/rubenv/sql-migrate/issues/2 - if dialect == "mysql" { - var out *time.Time - err := db.QueryRow("SELECT NOW()").Scan(&out) - if err != nil { - if err.Error() == "sql: Scan error on column index 0: unsupported driver -> Scan pair: []uint8 -> *time.Time" { - return nil, errors.New(`Cannot parse dates. - -Make sure that the parseTime option is supplied to your database connection. -Check https://github.com/go-sql-driver/mysql#parsetime for more info.`) - } else { - return nil, err - } - } - } - - // Create migration database map - dbMap := &gorp.DbMap{Db: db, Dialect: d} - dbMap.AddTableWithNameAndSchema(MigrationRecord{}, schemaName, tableName).SetKeys(false, "Id") - //dbMap.TraceOn("", log.New(os.Stdout, "migrate: ", log.Lmicroseconds)) - - err := dbMap.CreateTablesIfNotExists() - if err != nil { - return nil, err - } - - return dbMap, nil -} - -// TODO: Run migration + record insert in transaction. diff --git a/vendor/github.com/rubenv/sql-migrate/sqlparse/README.md b/vendor/github.com/rubenv/sql-migrate/sqlparse/README.md deleted file mode 100644 index a41706abc..000000000 --- a/vendor/github.com/rubenv/sql-migrate/sqlparse/README.md +++ /dev/null @@ -1,28 +0,0 @@ -# SQL migration parser - -Based on the [goose](https://bitbucket.org/liamstask/goose) migration parser. - -## License - - (The MIT License) - - Copyright (C) 2014 by Ruben Vermeersch - Copyright (C) 2012-2014 by Liam Staskawicz - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in - all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. diff --git a/vendor/github.com/rubenv/sql-migrate/sqlparse/sqlparse.go b/vendor/github.com/rubenv/sql-migrate/sqlparse/sqlparse.go deleted file mode 100644 index df95011eb..000000000 --- a/vendor/github.com/rubenv/sql-migrate/sqlparse/sqlparse.go +++ /dev/null @@ -1,128 +0,0 @@ -package sqlparse - -import ( - "bufio" - "bytes" - "errors" - "io" - - "strings" -) - -const sqlCmdPrefix = "-- +migrate " - -// Checks the line to see if the line has a statement-ending semicolon -// or if the line contains a double-dash comment. -func endsWithSemicolon(line string) bool { - - prev := "" - scanner := bufio.NewScanner(strings.NewReader(line)) - scanner.Split(bufio.ScanWords) - - for scanner.Scan() { - word := scanner.Text() - if strings.HasPrefix(word, "--") { - break - } - prev = word - } - - return strings.HasSuffix(prev, ";") -} - -// Split the given sql script into individual statements. -// -// The base case is to simply split on semicolons, as these -// naturally terminate a statement. -// -// However, more complex cases like pl/pgsql can have semicolons -// within a statement. For these cases, we provide the explicit annotations -// 'StatementBegin' and 'StatementEnd' to allow the script to -// tell us to ignore semicolons. -func SplitSQLStatements(r io.ReadSeeker, direction bool) ([]string, error) { - _, err := r.Seek(0, 0) - if err != nil { - return nil, err - } - - var buf bytes.Buffer - scanner := bufio.NewScanner(r) - - // track the count of each section - // so we can diagnose scripts with no annotations - upSections := 0 - downSections := 0 - - statementEnded := false - ignoreSemicolons := false - directionIsActive := false - - stmts := make([]string, 0) - - for scanner.Scan() { - - line := scanner.Text() - - // handle any migrate-specific commands - if strings.HasPrefix(line, sqlCmdPrefix) { - cmd := strings.TrimSpace(line[len(sqlCmdPrefix):]) - switch cmd { - case "Up": - directionIsActive = (direction == true) - upSections++ - break - - case "Down": - directionIsActive = (direction == false) - downSections++ - break - - case "StatementBegin": - if directionIsActive { - ignoreSemicolons = true - } - break - - case "StatementEnd": - if directionIsActive { - statementEnded = (ignoreSemicolons == true) - ignoreSemicolons = false - } - break - } - } - - if !directionIsActive { - continue - } - - if _, err := buf.WriteString(line + "\n"); err != nil { - return nil, err - } - - // Wrap up the two supported cases: 1) basic with semicolon; 2) psql statement - // Lines that end with semicolon that are in a statement block - // do not conclude statement. - if (!ignoreSemicolons && endsWithSemicolon(line)) || statementEnded { - statementEnded = false - stmts = append(stmts, buf.String()) - buf.Reset() - } - } - - if err := scanner.Err(); err != nil { - return nil, err - } - - // diagnose likely migration script errors - if ignoreSemicolons { - return nil, errors.New("ERROR: saw '-- +migrate StatementBegin' with no matching '-- +migrate StatementEnd'") - } - - if upSections == 0 && downSections == 0 { - return nil, errors.New(`ERROR: no Up/Down annotations found, so no statements were executed. - See https://github.com/rubenv/sql-migrate for details.`) - } - - return stmts, nil -} diff --git a/vendor/gopkg.in/gorp.v1/LICENSE b/vendor/gopkg.in/gorp.v1/LICENSE deleted file mode 100644 index b661111d0..000000000 --- a/vendor/gopkg.in/gorp.v1/LICENSE +++ /dev/null @@ -1,22 +0,0 @@ -(The MIT License) - -Copyright (c) 2012 James Cooper - -Permission is hereby granted, free of charge, to any person obtaining -a copy of this software and associated documentation files (the -'Software'), to deal in the Software without restriction, including -without limitation the rights to use, copy, modify, merge, publish, -distribute, sublicense, and/or sell copies of the Software, and to -permit persons to whom the Software is furnished to do so, subject to -the following conditions: - -The above copyright notice and this permission notice shall be -included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/vendor/gopkg.in/gorp.v1/Makefile b/vendor/gopkg.in/gorp.v1/Makefile deleted file mode 100644 index edf771c17..000000000 --- a/vendor/gopkg.in/gorp.v1/Makefile +++ /dev/null @@ -1,6 +0,0 @@ -include $(GOROOT)/src/Make.inc - -TARG = github.com/coopernurse/gorp -GOFILES = gorp.go dialect.go - -include $(GOROOT)/src/Make.pkg \ No newline at end of file diff --git a/vendor/gopkg.in/gorp.v1/README.md b/vendor/gopkg.in/gorp.v1/README.md deleted file mode 100644 index c7cd5d8a2..000000000 --- a/vendor/gopkg.in/gorp.v1/README.md +++ /dev/null @@ -1,672 +0,0 @@ -# Go Relational Persistence - -[![build status](https://secure.travis-ci.org/go-gorp/gorp.png)](http://travis-ci.org/go-gorp/gorp) - -I hesitate to call gorp an ORM. Go doesn't really have objects, at least -not in the classic Smalltalk/Java sense. There goes the "O". gorp doesn't -know anything about the relationships between your structs (at least not -yet). So the "R" is questionable too (but I use it in the name because, -well, it seemed more clever). - -The "M" is alive and well. Given some Go structs and a database, gorp -should remove a fair amount of boilerplate busy-work from your code. - -I hope that gorp saves you time, minimizes the drudgery of getting data -in and out of your database, and helps your code focus on algorithms, -not infrastructure. - -* Bind struct fields to table columns via API or tag -* Support for embedded structs -* Support for transactions -* Forward engineer db schema from structs (great for unit tests) -* Pre/post insert/update/delete hooks -* Automatically generate insert/update/delete statements for a struct -* Automatic binding of auto increment PKs back to struct after insert -* Delete by primary key(s) -* Select by primary key(s) -* Optional trace sql logging -* Bind arbitrary SQL queries to a struct -* Bind slice to SELECT query results without type assertions -* Use positional or named bind parameters in custom SELECT queries -* Optional optimistic locking using a version column (for update/deletes) - -## Installation - - # install the library: - go get gopkg.in/gorp.v1 - - // use in your .go code: - import ( - "gopkg.in/gorp.v1" - ) - -## Versioning - -This project provides a stable release (v1.x tags) and a bleeding edge codebase (master). - -`gopkg.in/gorp.v1` points to the latest v1.x tag. The API's for v1 are stable and shouldn't change. Development takes place at the master branch. Althought the code in master should always compile and test successfully, it might break API's. We aim to maintain backwards compatibility, but API's and behaviour might be changed to fix a bug. Also note that API's that are new in the master branch can change until released as v2. - -If you want to use bleeding edge, use `github.com/go-gorp/gorp` as import path. - -## API Documentation - -Full godoc output from the latest v1 release is available here: - -https://godoc.org/gopkg.in/gorp.v1 - -For the latest code in master: - -https://godoc.org/github.com/go-gorp/gorp - -## Quickstart - -```go -package main - -import ( - "database/sql" - "gopkg.in/gorp.v1" - _ "github.com/mattn/go-sqlite3" - "log" - "time" -) - -func main() { - // initialize the DbMap - dbmap := initDb() - defer dbmap.Db.Close() - - // delete any existing rows - err := dbmap.TruncateTables() - checkErr(err, "TruncateTables failed") - - // create two posts - p1 := newPost("Go 1.1 released!", "Lorem ipsum lorem ipsum") - p2 := newPost("Go 1.2 released!", "Lorem ipsum lorem ipsum") - - // insert rows - auto increment PKs will be set properly after the insert - err = dbmap.Insert(&p1, &p2) - checkErr(err, "Insert failed") - - // use convenience SelectInt - count, err := dbmap.SelectInt("select count(*) from posts") - checkErr(err, "select count(*) failed") - log.Println("Rows after inserting:", count) - - // update a row - p2.Title = "Go 1.2 is better than ever" - count, err = dbmap.Update(&p2) - checkErr(err, "Update failed") - log.Println("Rows updated:", count) - - // fetch one row - note use of "post_id" instead of "Id" since column is aliased - // - // Postgres users should use $1 instead of ? placeholders - // See 'Known Issues' below - // - err = dbmap.SelectOne(&p2, "select * from posts where post_id=?", p2.Id) - checkErr(err, "SelectOne failed") - log.Println("p2 row:", p2) - - // fetch all rows - var posts []Post - _, err = dbmap.Select(&posts, "select * from posts order by post_id") - checkErr(err, "Select failed") - log.Println("All rows:") - for x, p := range posts { - log.Printf(" %d: %v\n", x, p) - } - - // delete row by PK - count, err = dbmap.Delete(&p1) - checkErr(err, "Delete failed") - log.Println("Rows deleted:", count) - - // delete row manually via Exec - _, err = dbmap.Exec("delete from posts where post_id=?", p2.Id) - checkErr(err, "Exec failed") - - // confirm count is zero - count, err = dbmap.SelectInt("select count(*) from posts") - checkErr(err, "select count(*) failed") - log.Println("Row count - should be zero:", count) - - log.Println("Done!") -} - -type Post struct { - // db tag lets you specify the column name if it differs from the struct field - Id int64 `db:"post_id"` - Created int64 - Title string - Body string -} - -func newPost(title, body string) Post { - return Post{ - Created: time.Now().UnixNano(), - Title: title, - Body: body, - } -} - -func initDb() *gorp.DbMap { - // connect to db using standard Go database/sql API - // use whatever database/sql driver you wish - db, err := sql.Open("sqlite3", "/tmp/post_db.bin") - checkErr(err, "sql.Open failed") - - // construct a gorp DbMap - dbmap := &gorp.DbMap{Db: db, Dialect: gorp.SqliteDialect{}} - - // add a table, setting the table name to 'posts' and - // specifying that the Id property is an auto incrementing PK - dbmap.AddTableWithName(Post{}, "posts").SetKeys(true, "Id") - - // create the table. in a production system you'd generally - // use a migration tool, or create the tables via scripts - err = dbmap.CreateTablesIfNotExists() - checkErr(err, "Create tables failed") - - return dbmap -} - -func checkErr(err error, msg string) { - if err != nil { - log.Fatalln(msg, err) - } -} -``` - -## Examples - -### Mapping structs to tables - -First define some types: - -```go -type Invoice struct { - Id int64 - Created int64 - Updated int64 - Memo string - PersonId int64 -} - -type Person struct { - Id int64 - Created int64 - Updated int64 - FName string - LName string -} - -// Example of using tags to alias fields to column names -// The 'db' value is the column name -// -// A hyphen will cause gorp to skip this field, similar to the -// Go json package. -// -// This is equivalent to using the ColMap methods: -// -// table := dbmap.AddTableWithName(Product{}, "product") -// table.ColMap("Id").Rename("product_id") -// table.ColMap("Price").Rename("unit_price") -// table.ColMap("IgnoreMe").SetTransient(true) -// -type Product struct { - Id int64 `db:"product_id"` - Price int64 `db:"unit_price"` - IgnoreMe string `db:"-"` -} -``` - -Then create a mapper, typically you'd do this one time at app startup: - -```go -// connect to db using standard Go database/sql API -// use whatever database/sql driver you wish -db, err := sql.Open("mymysql", "tcp:localhost:3306*mydb/myuser/mypassword") - -// construct a gorp DbMap -dbmap := &gorp.DbMap{Db: db, Dialect: gorp.MySQLDialect{"InnoDB", "UTF8"}} - -// register the structs you wish to use with gorp -// you can also use the shorter dbmap.AddTable() if you -// don't want to override the table name -// -// SetKeys(true) means we have a auto increment primary key, which -// will get automatically bound to your struct post-insert -// -t1 := dbmap.AddTableWithName(Invoice{}, "invoice_test").SetKeys(true, "Id") -t2 := dbmap.AddTableWithName(Person{}, "person_test").SetKeys(true, "Id") -t3 := dbmap.AddTableWithName(Product{}, "product_test").SetKeys(true, "Id") -``` - -### Struct Embedding - -gorp supports embedding structs. For example: - -```go -type Names struct { - FirstName string - LastName string -} - -type WithEmbeddedStruct struct { - Id int64 - Names -} - -es := &WithEmbeddedStruct{-1, Names{FirstName: "Alice", LastName: "Smith"}} -err := dbmap.Insert(es) -``` - -See the `TestWithEmbeddedStruct` function in `gorp_test.go` for a full example. - -### Create/Drop Tables ### - -Automatically create / drop registered tables. This is useful for unit tests -but is entirely optional. You can of course use gorp with tables created manually, -or with a separate migration tool (like goose: https://bitbucket.org/liamstask/goose). - -```go -// create all registered tables -dbmap.CreateTables() - -// same as above, but uses "if not exists" clause to skip tables that are -// already defined -dbmap.CreateTablesIfNotExists() - -// drop -dbmap.DropTables() -``` - -### SQL Logging - -Optionally you can pass in a logger to trace all SQL statements. -I recommend enabling this initially while you're getting the feel for what -gorp is doing on your behalf. - -Gorp defines a `GorpLogger` interface that Go's built in `log.Logger` satisfies. -However, you can write your own `GorpLogger` implementation, or use a package such -as `glog` if you want more control over how statements are logged. - -```go -// Will log all SQL statements + args as they are run -// The first arg is a string prefix to prepend to all log messages -dbmap.TraceOn("[gorp]", log.New(os.Stdout, "myapp:", log.Lmicroseconds)) - -// Turn off tracing -dbmap.TraceOff() -``` - -### Insert - -```go -// Must declare as pointers so optional callback hooks -// can operate on your data, not copies -inv1 := &Invoice{0, 100, 200, "first order", 0} -inv2 := &Invoice{0, 100, 200, "second order", 0} - -// Insert your rows -err := dbmap.Insert(inv1, inv2) - -// Because we called SetKeys(true) on Invoice, the Id field -// will be populated after the Insert() automatically -fmt.Printf("inv1.Id=%d inv2.Id=%d\n", inv1.Id, inv2.Id) -``` - -### Update - -Continuing the above example, use the `Update` method to modify an Invoice: - -```go -// count is the # of rows updated, which should be 1 in this example -count, err := dbmap.Update(inv1) -``` - -### Delete - -If you have primary key(s) defined for a struct, you can use the `Delete` -method to remove rows: - -```go -count, err := dbmap.Delete(inv1) -``` - -### Select by Key - -Use the `Get` method to fetch a single row by primary key. It returns -nil if no row is found. - -```go -// fetch Invoice with Id=99 -obj, err := dbmap.Get(Invoice{}, 99) -inv := obj.(*Invoice) -``` - -### Ad Hoc SQL - -#### SELECT - -`Select()` and `SelectOne()` provide a simple way to bind arbitrary queries to a slice -or a single struct. - -```go -// Select a slice - first return value is not needed when a slice pointer is passed to Select() -var posts []Post -_, err := dbmap.Select(&posts, "select * from post order by id") - -// You can also use primitive types -var ids []string -_, err := dbmap.Select(&ids, "select id from post") - -// Select a single row. -// Returns an error if no row found, or if more than one row is found -var post Post -err := dbmap.SelectOne(&post, "select * from post where id=?", id) -``` - -Want to do joins? Just write the SQL and the struct. gorp will bind them: - -```go -// Define a type for your join -// It *must* contain all the columns in your SELECT statement -// -// The names here should match the aliased column names you specify -// in your SQL - no additional binding work required. simple. -// -type InvoicePersonView struct { - InvoiceId int64 - PersonId int64 - Memo string - FName string -} - -// Create some rows -p1 := &Person{0, 0, 0, "bob", "smith"} -dbmap.Insert(p1) - -// notice how we can wire up p1.Id to the invoice easily -inv1 := &Invoice{0, 0, 0, "xmas order", p1.Id} -dbmap.Insert(inv1) - -// Run your query -query := "select i.Id InvoiceId, p.Id PersonId, i.Memo, p.FName " + - "from invoice_test i, person_test p " + - "where i.PersonId = p.Id" - -// pass a slice to Select() -var list []InvoicePersonView -_, err := dbmap.Select(&list, query) - -// this should test true -expected := InvoicePersonView{inv1.Id, p1.Id, inv1.Memo, p1.FName} -if reflect.DeepEqual(list[0], expected) { - fmt.Println("Woot! My join worked!") -} -``` - -#### SELECT string or int64 - -gorp provides a few convenience methods for selecting a single string or int64. - -```go -// select single int64 from db (use $1 instead of ? for postgresql) -i64, err := dbmap.SelectInt("select count(*) from foo where blah=?", blahVal) - -// select single string from db: -s, err := dbmap.SelectStr("select name from foo where blah=?", blahVal) - -``` - -#### Named bind parameters - -You may use a map or struct to bind parameters by name. This is currently -only supported in SELECT queries. - -```go -_, err := dbm.Select(&dest, "select * from Foo where name = :name and age = :age", map[string]interface{}{ - "name": "Rob", - "age": 31, -}) -``` - -#### UPDATE / DELETE - -You can execute raw SQL if you wish. Particularly good for batch operations. - -```go -res, err := dbmap.Exec("delete from invoice_test where PersonId=?", 10) -``` - -### Transactions - -You can batch operations into a transaction: - -```go -func InsertInv(dbmap *DbMap, inv *Invoice, per *Person) error { - // Start a new transaction - trans, err := dbmap.Begin() - if err != nil { - return err - } - - trans.Insert(per) - inv.PersonId = per.Id - trans.Insert(inv) - - // if the commit is successful, a nil error is returned - return trans.Commit() -} -``` - -### Hooks - -Use hooks to update data before/after saving to the db. Good for timestamps: - -```go -// implement the PreInsert and PreUpdate hooks -func (i *Invoice) PreInsert(s gorp.SqlExecutor) error { - i.Created = time.Now().UnixNano() - i.Updated = i.Created - return nil -} - -func (i *Invoice) PreUpdate(s gorp.SqlExecutor) error { - i.Updated = time.Now().UnixNano() - return nil -} - -// You can use the SqlExecutor to cascade additional SQL -// Take care to avoid cycles. gorp won't prevent them. -// -// Here's an example of a cascading delete -// -func (p *Person) PreDelete(s gorp.SqlExecutor) error { - query := "delete from invoice_test where PersonId=?" - err := s.Exec(query, p.Id); if err != nil { - return err - } - return nil -} -``` - -Full list of hooks that you can implement: - - PostGet - PreInsert - PostInsert - PreUpdate - PostUpdate - PreDelete - PostDelete - - All have the same signature. for example: - - func (p *MyStruct) PostUpdate(s gorp.SqlExecutor) error - -### Optimistic Locking - -gorp provides a simple optimistic locking feature, similar to Java's JPA, that -will raise an error if you try to update/delete a row whose `version` column -has a value different than the one in memory. This provides a safe way to do -"select then update" style operations without explicit read and write locks. - -```go -// Version is an auto-incremented number, managed by gorp -// If this property is present on your struct, update -// operations will be constrained -// -// For example, say we defined Person as: - -type Person struct { - Id int64 - Created int64 - Updated int64 - FName string - LName string - - // automatically used as the Version col - // use table.SetVersionCol("columnName") to map a different - // struct field as the version field - Version int64 -} - -p1 := &Person{0, 0, 0, "Bob", "Smith", 0} -dbmap.Insert(p1) // Version is now 1 - -obj, err := dbmap.Get(Person{}, p1.Id) -p2 := obj.(*Person) -p2.LName = "Edwards" -dbmap.Update(p2) // Version is now 2 - -p1.LName = "Howard" - -// Raises error because p1.Version == 1, which is out of date -count, err := dbmap.Update(p1) -_, ok := err.(gorp.OptimisticLockError) -if ok { - // should reach this statement - - // in a real app you might reload the row and retry, or - // you might propegate this to the user, depending on the desired - // semantics - fmt.Printf("Tried to update row with stale data: %v\n", err) -} else { - // some other db error occurred - log or return up the stack - fmt.Printf("Unknown db err: %v\n", err) -} -``` - -## Database Drivers - -gorp uses the Go 1 `database/sql` package. A full list of compliant drivers is available here: - -http://code.google.com/p/go-wiki/wiki/SQLDrivers - -Sadly, SQL databases differ on various issues. gorp provides a Dialect interface that should be -implemented per database vendor. Dialects are provided for: - -* MySQL -* PostgreSQL -* sqlite3 - -Each of these three databases pass the test suite. See `gorp_test.go` for example -DSNs for these three databases. - -Support is also provided for: - -* Oracle (contributed by @klaidliadon) -* SQL Server (contributed by @qrawl) - use driver: github.com/denisenkom/go-mssqldb - -Note that these databases are not covered by CI and I (@coopernurse) have no good way to -test them locally. So please try them and send patches as needed, but expect a bit more -unpredicability. - -## Known Issues - -### SQL placeholder portability - -Different databases use different strings to indicate variable placeholders in -prepared SQL statements. Unlike some database abstraction layers (such as JDBC), -Go's `database/sql` does not standardize this. - -SQL generated by gorp in the `Insert`, `Update`, `Delete`, and `Get` methods delegates -to a Dialect implementation for each database, and will generate portable SQL. - -Raw SQL strings passed to `Exec`, `Select`, `SelectOne`, `SelectInt`, etc will not be -parsed. Consequently you may have portability issues if you write a query like this: - -```go -// works on MySQL and Sqlite3, but not with Postgresql -err := dbmap.SelectOne(&val, "select * from foo where id = ?", 30) -``` - -In `Select` and `SelectOne` you can use named parameters to work around this. -The following is portable: - -```go -err := dbmap.SelectOne(&val, "select * from foo where id = :id", - map[string]interface{} { "id": 30}) -``` - -### time.Time and time zones - -gorp will pass `time.Time` fields through to the `database/sql` driver, but note that -the behavior of this type varies across database drivers. - -MySQL users should be especially cautious. See: https://github.com/ziutek/mymysql/pull/77 - -To avoid any potential issues with timezone/DST, consider using an integer field for time -data and storing UNIX time. - -## Running the tests - -The included tests may be run against MySQL, Postgresql, or sqlite3. -You must set two environment variables so the test code knows which driver to -use, and how to connect to your database. - -```sh -# MySQL example: -export GORP_TEST_DSN=gomysql_test/gomysql_test/abc123 -export GORP_TEST_DIALECT=mysql - -# run the tests -go test - -# run the tests and benchmarks -go test -bench="Bench" -benchtime 10 -``` - -Valid `GORP_TEST_DIALECT` values are: "mysql", "postgres", "sqlite3" -See the `test_all.sh` script for examples of all 3 databases. This is the script I run -locally to test the library. - -## Performance - -gorp uses reflection to construct SQL queries and bind parameters. See the BenchmarkNativeCrud vs BenchmarkGorpCrud in gorp_test.go for a simple perf test. On my MacBook Pro gorp is about 2-3% slower than hand written SQL. - -## Help/Support - -IRC: #gorp -Mailing list: gorp-dev@googlegroups.com -Bugs/Enhancements: Create a github issue - -## Pull requests / Contributions - -Contributions are very welcome. Please follow these guidelines: - -* Fork the `master` branch and issue pull requests targeting the `master` branch -* If you are adding an enhancement, please open an issue first with your proposed change. -* Changes that break backwards compatibility in the public API are only accepted after we - discuss on a GitHub issue for a while. - -Thanks! - -## Contributors - -* matthias-margush - column aliasing via tags -* Rob Figueiredo - @robfig -* Quinn Slack - @sqs diff --git a/vendor/gopkg.in/gorp.v1/dialect.go b/vendor/gopkg.in/gorp.v1/dialect.go deleted file mode 100644 index 5e8fdc667..000000000 --- a/vendor/gopkg.in/gorp.v1/dialect.go +++ /dev/null @@ -1,692 +0,0 @@ -package gorp - -import ( - "errors" - "fmt" - "reflect" - "strings" -) - -// The Dialect interface encapsulates behaviors that differ across -// SQL databases. At present the Dialect is only used by CreateTables() -// but this could change in the future -type Dialect interface { - - // adds a suffix to any query, usually ";" - QuerySuffix() string - - // ToSqlType returns the SQL column type to use when creating a - // table of the given Go Type. maxsize can be used to switch based on - // size. For example, in MySQL []byte could map to BLOB, MEDIUMBLOB, - // or LONGBLOB depending on the maxsize - ToSqlType(val reflect.Type, maxsize int, isAutoIncr bool) string - - // string to append to primary key column definitions - AutoIncrStr() string - - // string to bind autoincrement columns to. Empty string will - // remove reference to those columns in the INSERT statement. - AutoIncrBindValue() string - - AutoIncrInsertSuffix(col *ColumnMap) string - - // string to append to "create table" statement for vendor specific - // table attributes - CreateTableSuffix() string - - // string to truncate tables - TruncateClause() string - - // bind variable string to use when forming SQL statements - // in many dbs it is "?", but Postgres appears to use $1 - // - // i is a zero based index of the bind variable in this statement - // - BindVar(i int) string - - // Handles quoting of a field name to ensure that it doesn't raise any - // SQL parsing exceptions by using a reserved word as a field name. - QuoteField(field string) string - - // Handles building up of a schema.database string that is compatible with - // the given dialect - // - // schema - The schema that lives in - // table - The table name - QuotedTableForQuery(schema string, table string) string - - // Existance clause for table creation / deletion - IfSchemaNotExists(command, schema string) string - IfTableExists(command, schema, table string) string - IfTableNotExists(command, schema, table string) string -} - -// IntegerAutoIncrInserter is implemented by dialects that can perform -// inserts with automatically incremented integer primary keys. If -// the dialect can handle automatic assignment of more than just -// integers, see TargetedAutoIncrInserter. -type IntegerAutoIncrInserter interface { - InsertAutoIncr(exec SqlExecutor, insertSql string, params ...interface{}) (int64, error) -} - -// TargetedAutoIncrInserter is implemented by dialects that can -// perform automatic assignment of any primary key type (i.e. strings -// for uuids, integers for serials, etc). -type TargetedAutoIncrInserter interface { - // InsertAutoIncrToTarget runs an insert operation and assigns the - // automatically generated primary key directly to the passed in - // target. The target should be a pointer to the primary key - // field of the value being inserted. - InsertAutoIncrToTarget(exec SqlExecutor, insertSql string, target interface{}, params ...interface{}) error -} - -func standardInsertAutoIncr(exec SqlExecutor, insertSql string, params ...interface{}) (int64, error) { - res, err := exec.Exec(insertSql, params...) - if err != nil { - return 0, err - } - return res.LastInsertId() -} - -/////////////////////////////////////////////////////// -// sqlite3 // -///////////// - -type SqliteDialect struct { - suffix string -} - -func (d SqliteDialect) QuerySuffix() string { return ";" } - -func (d SqliteDialect) ToSqlType(val reflect.Type, maxsize int, isAutoIncr bool) string { - switch val.Kind() { - case reflect.Ptr: - return d.ToSqlType(val.Elem(), maxsize, isAutoIncr) - case reflect.Bool: - return "integer" - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - return "integer" - case reflect.Float64, reflect.Float32: - return "real" - case reflect.Slice: - if val.Elem().Kind() == reflect.Uint8 { - return "blob" - } - } - - switch val.Name() { - case "NullInt64": - return "integer" - case "NullFloat64": - return "real" - case "NullBool": - return "integer" - case "Time": - return "datetime" - } - - if maxsize < 1 { - maxsize = 255 - } - return fmt.Sprintf("varchar(%d)", maxsize) -} - -// Returns autoincrement -func (d SqliteDialect) AutoIncrStr() string { - return "autoincrement" -} - -func (d SqliteDialect) AutoIncrBindValue() string { - return "null" -} - -func (d SqliteDialect) AutoIncrInsertSuffix(col *ColumnMap) string { - return "" -} - -// Returns suffix -func (d SqliteDialect) CreateTableSuffix() string { - return d.suffix -} - -// With sqlite, there technically isn't a TRUNCATE statement, -// but a DELETE FROM uses a truncate optimization: -// http://www.sqlite.org/lang_delete.html -func (d SqliteDialect) TruncateClause() string { - return "delete from" -} - -// Returns "?" -func (d SqliteDialect) BindVar(i int) string { - return "?" -} - -func (d SqliteDialect) InsertAutoIncr(exec SqlExecutor, insertSql string, params ...interface{}) (int64, error) { - return standardInsertAutoIncr(exec, insertSql, params...) -} - -func (d SqliteDialect) QuoteField(f string) string { - return `"` + f + `"` -} - -// sqlite does not have schemas like PostgreSQL does, so just escape it like normal -func (d SqliteDialect) QuotedTableForQuery(schema string, table string) string { - return d.QuoteField(table) -} - -func (d SqliteDialect) IfSchemaNotExists(command, schema string) string { - return fmt.Sprintf("%s if not exists", command) -} - -func (d SqliteDialect) IfTableExists(command, schema, table string) string { - return fmt.Sprintf("%s if exists", command) -} - -func (d SqliteDialect) IfTableNotExists(command, schema, table string) string { - return fmt.Sprintf("%s if not exists", command) -} - -/////////////////////////////////////////////////////// -// PostgreSQL // -//////////////// - -type PostgresDialect struct { - suffix string -} - -func (d PostgresDialect) QuerySuffix() string { return ";" } - -func (d PostgresDialect) ToSqlType(val reflect.Type, maxsize int, isAutoIncr bool) string { - switch val.Kind() { - case reflect.Ptr: - return d.ToSqlType(val.Elem(), maxsize, isAutoIncr) - case reflect.Bool: - return "boolean" - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint8, reflect.Uint16, reflect.Uint32: - if isAutoIncr { - return "serial" - } - return "integer" - case reflect.Int64, reflect.Uint64: - if isAutoIncr { - return "bigserial" - } - return "bigint" - case reflect.Float64: - return "double precision" - case reflect.Float32: - return "real" - case reflect.Slice: - if val.Elem().Kind() == reflect.Uint8 { - return "bytea" - } - } - - switch val.Name() { - case "NullInt64": - return "bigint" - case "NullFloat64": - return "double precision" - case "NullBool": - return "boolean" - case "Time": - return "timestamp with time zone" - } - - if maxsize > 0 { - return fmt.Sprintf("varchar(%d)", maxsize) - } else { - return "text" - } - -} - -// Returns empty string -func (d PostgresDialect) AutoIncrStr() string { - return "" -} - -func (d PostgresDialect) AutoIncrBindValue() string { - return "default" -} - -func (d PostgresDialect) AutoIncrInsertSuffix(col *ColumnMap) string { - return " returning " + col.ColumnName -} - -// Returns suffix -func (d PostgresDialect) CreateTableSuffix() string { - return d.suffix -} - -func (d PostgresDialect) TruncateClause() string { - return "truncate" -} - -// Returns "$(i+1)" -func (d PostgresDialect) BindVar(i int) string { - return fmt.Sprintf("$%d", i+1) -} - -func (d PostgresDialect) InsertAutoIncrToTarget(exec SqlExecutor, insertSql string, target interface{}, params ...interface{}) error { - rows, err := exec.query(insertSql, params...) - if err != nil { - return err - } - defer rows.Close() - - if rows.Next() { - err := rows.Scan(target) - return err - } - - return errors.New("No serial value returned for insert: " + insertSql + " Encountered error: " + rows.Err().Error()) -} - -func (d PostgresDialect) QuoteField(f string) string { - return `"` + strings.ToLower(f) + `"` -} - -func (d PostgresDialect) QuotedTableForQuery(schema string, table string) string { - if strings.TrimSpace(schema) == "" { - return d.QuoteField(table) - } - - return schema + "." + d.QuoteField(table) -} - -func (d PostgresDialect) IfSchemaNotExists(command, schema string) string { - return fmt.Sprintf("%s if not exists", command) -} - -func (d PostgresDialect) IfTableExists(command, schema, table string) string { - return fmt.Sprintf("%s if exists", command) -} - -func (d PostgresDialect) IfTableNotExists(command, schema, table string) string { - return fmt.Sprintf("%s if not exists", command) -} - -/////////////////////////////////////////////////////// -// MySQL // -/////////// - -// Implementation of Dialect for MySQL databases. -type MySQLDialect struct { - - // Engine is the storage engine to use "InnoDB" vs "MyISAM" for example - Engine string - - // Encoding is the character encoding to use for created tables - Encoding string -} - -func (d MySQLDialect) QuerySuffix() string { return ";" } - -func (d MySQLDialect) ToSqlType(val reflect.Type, maxsize int, isAutoIncr bool) string { - switch val.Kind() { - case reflect.Ptr: - return d.ToSqlType(val.Elem(), maxsize, isAutoIncr) - case reflect.Bool: - return "boolean" - case reflect.Int8: - return "tinyint" - case reflect.Uint8: - return "tinyint unsigned" - case reflect.Int16: - return "smallint" - case reflect.Uint16: - return "smallint unsigned" - case reflect.Int, reflect.Int32: - return "int" - case reflect.Uint, reflect.Uint32: - return "int unsigned" - case reflect.Int64: - return "bigint" - case reflect.Uint64: - return "bigint unsigned" - case reflect.Float64, reflect.Float32: - return "double" - case reflect.Slice: - if val.Elem().Kind() == reflect.Uint8 { - return "mediumblob" - } - } - - switch val.Name() { - case "NullInt64": - return "bigint" - case "NullFloat64": - return "double" - case "NullBool": - return "tinyint" - case "Time": - return "datetime" - } - - if maxsize < 1 { - maxsize = 255 - } - return fmt.Sprintf("varchar(%d)", maxsize) -} - -// Returns auto_increment -func (d MySQLDialect) AutoIncrStr() string { - return "auto_increment" -} - -func (d MySQLDialect) AutoIncrBindValue() string { - return "null" -} - -func (d MySQLDialect) AutoIncrInsertSuffix(col *ColumnMap) string { - return "" -} - -// Returns engine=%s charset=%s based on values stored on struct -func (d MySQLDialect) CreateTableSuffix() string { - if d.Engine == "" || d.Encoding == "" { - msg := "gorp - undefined" - - if d.Engine == "" { - msg += " MySQLDialect.Engine" - } - if d.Engine == "" && d.Encoding == "" { - msg += "," - } - if d.Encoding == "" { - msg += " MySQLDialect.Encoding" - } - msg += ". Check that your MySQLDialect was correctly initialized when declared." - panic(msg) - } - - return fmt.Sprintf(" engine=%s charset=%s", d.Engine, d.Encoding) -} - -func (d MySQLDialect) TruncateClause() string { - return "truncate" -} - -// Returns "?" -func (d MySQLDialect) BindVar(i int) string { - return "?" -} - -func (d MySQLDialect) InsertAutoIncr(exec SqlExecutor, insertSql string, params ...interface{}) (int64, error) { - return standardInsertAutoIncr(exec, insertSql, params...) -} - -func (d MySQLDialect) QuoteField(f string) string { - return "`" + f + "`" -} - -func (d MySQLDialect) QuotedTableForQuery(schema string, table string) string { - if strings.TrimSpace(schema) == "" { - return d.QuoteField(table) - } - - return schema + "." + d.QuoteField(table) -} - -func (d MySQLDialect) IfSchemaNotExists(command, schema string) string { - return fmt.Sprintf("%s if not exists", command) -} - -func (d MySQLDialect) IfTableExists(command, schema, table string) string { - return fmt.Sprintf("%s if exists", command) -} - -func (d MySQLDialect) IfTableNotExists(command, schema, table string) string { - return fmt.Sprintf("%s if not exists", command) -} - -/////////////////////////////////////////////////////// -// Sql Server // -//////////////// - -// Implementation of Dialect for Microsoft SQL Server databases. -// Tested on SQL Server 2008 with driver: github.com/denisenkom/go-mssqldb - -type SqlServerDialect struct { - suffix string -} - -func (d SqlServerDialect) ToSqlType(val reflect.Type, maxsize int, isAutoIncr bool) string { - switch val.Kind() { - case reflect.Ptr: - return d.ToSqlType(val.Elem(), maxsize, isAutoIncr) - case reflect.Bool: - return "bit" - case reflect.Int8: - return "tinyint" - case reflect.Uint8: - return "smallint" - case reflect.Int16: - return "smallint" - case reflect.Uint16: - return "int" - case reflect.Int, reflect.Int32: - return "int" - case reflect.Uint, reflect.Uint32: - return "bigint" - case reflect.Int64: - return "bigint" - case reflect.Uint64: - return "bigint" - case reflect.Float32: - return "real" - case reflect.Float64: - return "float(53)" - case reflect.Slice: - if val.Elem().Kind() == reflect.Uint8 { - return "varbinary" - } - } - - switch val.Name() { - case "NullInt64": - return "bigint" - case "NullFloat64": - return "float(53)" - case "NullBool": - return "tinyint" - case "Time": - return "datetime" - } - - if maxsize < 1 { - maxsize = 255 - } - return fmt.Sprintf("varchar(%d)", maxsize) -} - -// Returns auto_increment -func (d SqlServerDialect) AutoIncrStr() string { - return "identity(0,1)" -} - -// Empty string removes autoincrement columns from the INSERT statements. -func (d SqlServerDialect) AutoIncrBindValue() string { - return "" -} - -func (d SqlServerDialect) AutoIncrInsertSuffix(col *ColumnMap) string { - return "" -} - -// Returns suffix -func (d SqlServerDialect) CreateTableSuffix() string { - - return d.suffix -} - -func (d SqlServerDialect) TruncateClause() string { - return "delete from" -} - -// Returns "?" -func (d SqlServerDialect) BindVar(i int) string { - return "?" -} - -func (d SqlServerDialect) InsertAutoIncr(exec SqlExecutor, insertSql string, params ...interface{}) (int64, error) { - return standardInsertAutoIncr(exec, insertSql, params...) -} - -func (d SqlServerDialect) QuoteField(f string) string { - return `"` + f + `"` -} - -func (d SqlServerDialect) QuotedTableForQuery(schema string, table string) string { - if strings.TrimSpace(schema) == "" { - return table - } - return schema + "." + table -} - -func (d SqlServerDialect) QuerySuffix() string { return ";" } - -func (d SqlServerDialect) IfSchemaNotExists(command, schema string) string { - s := fmt.Sprintf("if not exists (select name from sys.schemas where name = '%s') %s", schema, command) - return s -} - -func (d SqlServerDialect) IfTableExists(command, schema, table string) string { - var schema_clause string - if strings.TrimSpace(schema) != "" { - schema_clause = fmt.Sprintf("table_schema = '%s' and ", schema) - } - s := fmt.Sprintf("if exists (select * from information_schema.tables where %stable_name = '%s') %s", schema_clause, table, command) - return s -} - -func (d SqlServerDialect) IfTableNotExists(command, schema, table string) string { - var schema_clause string - if strings.TrimSpace(schema) != "" { - schema_clause = fmt.Sprintf("table_schema = '%s' and ", schema) - } - s := fmt.Sprintf("if not exists (select * from information_schema.tables where %stable_name = '%s') %s", schema_clause, table, command) - return s -} - -/////////////////////////////////////////////////////// -// Oracle // -/////////// - -// Implementation of Dialect for Oracle databases. -type OracleDialect struct{} - -func (d OracleDialect) QuerySuffix() string { return "" } - -func (d OracleDialect) ToSqlType(val reflect.Type, maxsize int, isAutoIncr bool) string { - switch val.Kind() { - case reflect.Ptr: - return d.ToSqlType(val.Elem(), maxsize, isAutoIncr) - case reflect.Bool: - return "boolean" - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint8, reflect.Uint16, reflect.Uint32: - if isAutoIncr { - return "serial" - } - return "integer" - case reflect.Int64, reflect.Uint64: - if isAutoIncr { - return "bigserial" - } - return "bigint" - case reflect.Float64: - return "double precision" - case reflect.Float32: - return "real" - case reflect.Slice: - if val.Elem().Kind() == reflect.Uint8 { - return "bytea" - } - } - - switch val.Name() { - case "NullInt64": - return "bigint" - case "NullFloat64": - return "double precision" - case "NullBool": - return "boolean" - case "NullTime", "Time": - return "timestamp with time zone" - } - - if maxsize > 0 { - return fmt.Sprintf("varchar(%d)", maxsize) - } else { - return "text" - } - -} - -// Returns empty string -func (d OracleDialect) AutoIncrStr() string { - return "" -} - -func (d OracleDialect) AutoIncrBindValue() string { - return "default" -} - -func (d OracleDialect) AutoIncrInsertSuffix(col *ColumnMap) string { - return " returning " + col.ColumnName -} - -// Returns suffix -func (d OracleDialect) CreateTableSuffix() string { - return "" -} - -func (d OracleDialect) TruncateClause() string { - return "truncate" -} - -// Returns "$(i+1)" -func (d OracleDialect) BindVar(i int) string { - return fmt.Sprintf(":%d", i+1) -} - -func (d OracleDialect) InsertAutoIncr(exec SqlExecutor, insertSql string, params ...interface{}) (int64, error) { - rows, err := exec.query(insertSql, params...) - if err != nil { - return 0, err - } - defer rows.Close() - - if rows.Next() { - var id int64 - err := rows.Scan(&id) - return id, err - } - - return 0, errors.New("No serial value returned for insert: " + insertSql + " Encountered error: " + rows.Err().Error()) -} - -func (d OracleDialect) QuoteField(f string) string { - return `"` + strings.ToUpper(f) + `"` -} - -func (d OracleDialect) QuotedTableForQuery(schema string, table string) string { - if strings.TrimSpace(schema) == "" { - return d.QuoteField(table) - } - - return schema + "." + d.QuoteField(table) -} - -func (d OracleDialect) IfSchemaNotExists(command, schema string) string { - return fmt.Sprintf("%s if not exists", command) -} - -func (d OracleDialect) IfTableExists(command, schema, table string) string { - return fmt.Sprintf("%s if exists", command) -} - -func (d OracleDialect) IfTableNotExists(command, schema, table string) string { - return fmt.Sprintf("%s if not exists", command) -} diff --git a/vendor/gopkg.in/gorp.v1/errors.go b/vendor/gopkg.in/gorp.v1/errors.go deleted file mode 100644 index 356d68475..000000000 --- a/vendor/gopkg.in/gorp.v1/errors.go +++ /dev/null @@ -1,26 +0,0 @@ -package gorp - -import ( - "fmt" -) - -// A non-fatal error, when a select query returns columns that do not exist -// as fields in the struct it is being mapped to -type NoFieldInTypeError struct { - TypeName string - MissingColNames []string -} - -func (err *NoFieldInTypeError) Error() string { - return fmt.Sprintf("gorp: No fields %+v in type %s", err.MissingColNames, err.TypeName) -} - -// returns true if the error is non-fatal (ie, we shouldn't immediately return) -func NonFatalError(err error) bool { - switch err.(type) { - case *NoFieldInTypeError: - return true - default: - return false - } -} diff --git a/vendor/gopkg.in/gorp.v1/gorp.go b/vendor/gopkg.in/gorp.v1/gorp.go deleted file mode 100644 index 1ad61870f..000000000 --- a/vendor/gopkg.in/gorp.v1/gorp.go +++ /dev/null @@ -1,2085 +0,0 @@ -// Copyright 2012 James Cooper. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -// Package gorp provides a simple way to marshal Go structs to and from -// SQL databases. It uses the database/sql package, and should work with any -// compliant database/sql driver. -// -// Source code and project home: -// https://github.com/coopernurse/gorp -// -package gorp - -import ( - "bytes" - "database/sql" - "database/sql/driver" - "errors" - "fmt" - "reflect" - "regexp" - "strings" - "time" - "log" - "os" -) - -// Oracle String (empty string is null) -type OracleString struct { - sql.NullString -} - -// Scan implements the Scanner interface. -func (os *OracleString) Scan(value interface{}) error { - if value == nil { - os.String, os.Valid = "", false - return nil - } - os.Valid = true - return os.NullString.Scan(value) -} - -// Value implements the driver Valuer interface. -func (os OracleString) Value() (driver.Value, error) { - if !os.Valid || os.String == "" { - return nil, nil - } - return os.String, nil -} - -// A nullable Time value -type NullTime struct { - Time time.Time - Valid bool // Valid is true if Time is not NULL -} - -// Scan implements the Scanner interface. -func (nt *NullTime) Scan(value interface{}) error { - nt.Time, nt.Valid = value.(time.Time) - return nil -} - -// Value implements the driver Valuer interface. -func (nt NullTime) Value() (driver.Value, error) { - if !nt.Valid { - return nil, nil - } - return nt.Time, nil -} - -var zeroVal reflect.Value -var versFieldConst = "[gorp_ver_field]" - -// OptimisticLockError is returned by Update() or Delete() if the -// struct being modified has a Version field and the value is not equal to -// the current value in the database -type OptimisticLockError struct { - // Table name where the lock error occurred - TableName string - - // Primary key values of the row being updated/deleted - Keys []interface{} - - // true if a row was found with those keys, indicating the - // LocalVersion is stale. false if no value was found with those - // keys, suggesting the row has been deleted since loaded, or - // was never inserted to begin with - RowExists bool - - // Version value on the struct passed to Update/Delete. This value is - // out of sync with the database. - LocalVersion int64 -} - -// Error returns a description of the cause of the lock error -func (e OptimisticLockError) Error() string { - if e.RowExists { - return fmt.Sprintf("gorp: OptimisticLockError table=%s keys=%v out of date version=%d", e.TableName, e.Keys, e.LocalVersion) - } - - return fmt.Sprintf("gorp: OptimisticLockError no row found for table=%s keys=%v", e.TableName, e.Keys) -} - -// The TypeConverter interface provides a way to map a value of one -// type to another type when persisting to, or loading from, a database. -// -// Example use cases: Implement type converter to convert bool types to "y"/"n" strings, -// or serialize a struct member as a JSON blob. -type TypeConverter interface { - // ToDb converts val to another type. Called before INSERT/UPDATE operations - ToDb(val interface{}) (interface{}, error) - - // FromDb returns a CustomScanner appropriate for this type. This will be used - // to hold values returned from SELECT queries. - // - // In particular the CustomScanner returned should implement a Binder - // function appropriate for the Go type you wish to convert the db value to - // - // If bool==false, then no custom scanner will be used for this field. - FromDb(target interface{}) (CustomScanner, bool) -} - -// CustomScanner binds a database column value to a Go type -type CustomScanner struct { - // After a row is scanned, Holder will contain the value from the database column. - // Initialize the CustomScanner with the concrete Go type you wish the database - // driver to scan the raw column into. - Holder interface{} - // Target typically holds a pointer to the target struct field to bind the Holder - // value to. - Target interface{} - // Binder is a custom function that converts the holder value to the target type - // and sets target accordingly. This function should return error if a problem - // occurs converting the holder to the target. - Binder func(holder interface{}, target interface{}) error -} - -// Bind is called automatically by gorp after Scan() -func (me CustomScanner) Bind() error { - return me.Binder(me.Holder, me.Target) -} - -// DbMap is the root gorp mapping object. Create one of these for each -// database schema you wish to map. Each DbMap contains a list of -// mapped tables. -// -// Example: -// -// dialect := gorp.MySQLDialect{"InnoDB", "UTF8"} -// dbmap := &gorp.DbMap{Db: db, Dialect: dialect} -// -type DbMap struct { - // Db handle to use with this map - Db *sql.DB - - // Dialect implementation to use with this map - Dialect Dialect - - TypeConverter TypeConverter - - tables []*TableMap - logger GorpLogger - logPrefix string -} - -// TableMap represents a mapping between a Go struct and a database table -// Use dbmap.AddTable() or dbmap.AddTableWithName() to create these -type TableMap struct { - // Name of database table. - TableName string - SchemaName string - gotype reflect.Type - Columns []*ColumnMap - keys []*ColumnMap - uniqueTogether [][]string - version *ColumnMap - insertPlan bindPlan - updatePlan bindPlan - deletePlan bindPlan - getPlan bindPlan - dbmap *DbMap -} - -// ResetSql removes cached insert/update/select/delete SQL strings -// associated with this TableMap. Call this if you've modified -// any column names or the table name itself. -func (t *TableMap) ResetSql() { - t.insertPlan = bindPlan{} - t.updatePlan = bindPlan{} - t.deletePlan = bindPlan{} - t.getPlan = bindPlan{} -} - -// SetKeys lets you specify the fields on a struct that map to primary -// key columns on the table. If isAutoIncr is set, result.LastInsertId() -// will be used after INSERT to bind the generated id to the Go struct. -// -// Automatically calls ResetSql() to ensure SQL statements are regenerated. -// -// Panics if isAutoIncr is true, and fieldNames length != 1 -// -func (t *TableMap) SetKeys(isAutoIncr bool, fieldNames ...string) *TableMap { - if isAutoIncr && len(fieldNames) != 1 { - panic(fmt.Sprintf( - "gorp: SetKeys: fieldNames length must be 1 if key is auto-increment. (Saw %v fieldNames)", - len(fieldNames))) - } - t.keys = make([]*ColumnMap, 0) - for _, name := range fieldNames { - colmap := t.ColMap(name) - colmap.isPK = true - colmap.isAutoIncr = isAutoIncr - t.keys = append(t.keys, colmap) - } - t.ResetSql() - - return t -} - -// SetUniqueTogether lets you specify uniqueness constraints across multiple -// columns on the table. Each call adds an additional constraint for the -// specified columns. -// -// Automatically calls ResetSql() to ensure SQL statements are regenerated. -// -// Panics if fieldNames length < 2. -// -func (t *TableMap) SetUniqueTogether(fieldNames ...string) *TableMap { - if len(fieldNames) < 2 { - panic(fmt.Sprintf( - "gorp: SetUniqueTogether: must provide at least two fieldNames to set uniqueness constraint.")) - } - - columns := make([]string, 0) - for _, name := range fieldNames { - columns = append(columns, name) - } - t.uniqueTogether = append(t.uniqueTogether, columns) - t.ResetSql() - - return t -} - -// ColMap returns the ColumnMap pointer matching the given struct field -// name. It panics if the struct does not contain a field matching this -// name. -func (t *TableMap) ColMap(field string) *ColumnMap { - col := colMapOrNil(t, field) - if col == nil { - e := fmt.Sprintf("No ColumnMap in table %s type %s with field %s", - t.TableName, t.gotype.Name(), field) - - panic(e) - } - return col -} - -func colMapOrNil(t *TableMap, field string) *ColumnMap { - for _, col := range t.Columns { - if col.fieldName == field || col.ColumnName == field { - return col - } - } - return nil -} - -// SetVersionCol sets the column to use as the Version field. By default -// the "Version" field is used. Returns the column found, or panics -// if the struct does not contain a field matching this name. -// -// Automatically calls ResetSql() to ensure SQL statements are regenerated. -func (t *TableMap) SetVersionCol(field string) *ColumnMap { - c := t.ColMap(field) - t.version = c - t.ResetSql() - return c -} - -type bindPlan struct { - query string - argFields []string - keyFields []string - versField string - autoIncrIdx int - autoIncrFieldName string -} - -func (plan bindPlan) createBindInstance(elem reflect.Value, conv TypeConverter) (bindInstance, error) { - bi := bindInstance{query: plan.query, autoIncrIdx: plan.autoIncrIdx, autoIncrFieldName: plan.autoIncrFieldName, versField: plan.versField} - if plan.versField != "" { - bi.existingVersion = elem.FieldByName(plan.versField).Int() - } - - var err error - - for i := 0; i < len(plan.argFields); i++ { - k := plan.argFields[i] - if k == versFieldConst { - newVer := bi.existingVersion + 1 - bi.args = append(bi.args, newVer) - if bi.existingVersion == 0 { - elem.FieldByName(plan.versField).SetInt(int64(newVer)) - } - } else { - val := elem.FieldByName(k).Interface() - if conv != nil { - val, err = conv.ToDb(val) - if err != nil { - return bindInstance{}, err - } - } - bi.args = append(bi.args, val) - } - } - - for i := 0; i < len(plan.keyFields); i++ { - k := plan.keyFields[i] - val := elem.FieldByName(k).Interface() - if conv != nil { - val, err = conv.ToDb(val) - if err != nil { - return bindInstance{}, err - } - } - bi.keys = append(bi.keys, val) - } - - return bi, nil -} - -type bindInstance struct { - query string - args []interface{} - keys []interface{} - existingVersion int64 - versField string - autoIncrIdx int - autoIncrFieldName string -} - -func (t *TableMap) bindInsert(elem reflect.Value) (bindInstance, error) { - plan := t.insertPlan - if plan.query == "" { - plan.autoIncrIdx = -1 - - s := bytes.Buffer{} - s2 := bytes.Buffer{} - s.WriteString(fmt.Sprintf("insert into %s (", t.dbmap.Dialect.QuotedTableForQuery(t.SchemaName, t.TableName))) - - x := 0 - first := true - for y := range t.Columns { - col := t.Columns[y] - if !(col.isAutoIncr && t.dbmap.Dialect.AutoIncrBindValue() == "") { - if !col.Transient { - if !first { - s.WriteString(",") - s2.WriteString(",") - } - s.WriteString(t.dbmap.Dialect.QuoteField(col.ColumnName)) - - if col.isAutoIncr { - s2.WriteString(t.dbmap.Dialect.AutoIncrBindValue()) - plan.autoIncrIdx = y - plan.autoIncrFieldName = col.fieldName - } else { - s2.WriteString(t.dbmap.Dialect.BindVar(x)) - if col == t.version { - plan.versField = col.fieldName - plan.argFields = append(plan.argFields, versFieldConst) - } else { - plan.argFields = append(plan.argFields, col.fieldName) - } - - x++ - } - first = false - } - } else { - plan.autoIncrIdx = y - plan.autoIncrFieldName = col.fieldName - } - } - s.WriteString(") values (") - s.WriteString(s2.String()) - s.WriteString(")") - if plan.autoIncrIdx > -1 { - s.WriteString(t.dbmap.Dialect.AutoIncrInsertSuffix(t.Columns[plan.autoIncrIdx])) - } - s.WriteString(t.dbmap.Dialect.QuerySuffix()) - - plan.query = s.String() - t.insertPlan = plan - } - - return plan.createBindInstance(elem, t.dbmap.TypeConverter) -} - -func (t *TableMap) bindUpdate(elem reflect.Value) (bindInstance, error) { - plan := t.updatePlan - if plan.query == "" { - - s := bytes.Buffer{} - s.WriteString(fmt.Sprintf("update %s set ", t.dbmap.Dialect.QuotedTableForQuery(t.SchemaName, t.TableName))) - x := 0 - - for y := range t.Columns { - col := t.Columns[y] - if !col.isAutoIncr && !col.Transient { - if x > 0 { - s.WriteString(", ") - } - s.WriteString(t.dbmap.Dialect.QuoteField(col.ColumnName)) - s.WriteString("=") - s.WriteString(t.dbmap.Dialect.BindVar(x)) - - if col == t.version { - plan.versField = col.fieldName - plan.argFields = append(plan.argFields, versFieldConst) - } else { - plan.argFields = append(plan.argFields, col.fieldName) - } - x++ - } - } - - s.WriteString(" where ") - for y := range t.keys { - col := t.keys[y] - if y > 0 { - s.WriteString(" and ") - } - s.WriteString(t.dbmap.Dialect.QuoteField(col.ColumnName)) - s.WriteString("=") - s.WriteString(t.dbmap.Dialect.BindVar(x)) - - plan.argFields = append(plan.argFields, col.fieldName) - plan.keyFields = append(plan.keyFields, col.fieldName) - x++ - } - if plan.versField != "" { - s.WriteString(" and ") - s.WriteString(t.dbmap.Dialect.QuoteField(t.version.ColumnName)) - s.WriteString("=") - s.WriteString(t.dbmap.Dialect.BindVar(x)) - plan.argFields = append(plan.argFields, plan.versField) - } - s.WriteString(t.dbmap.Dialect.QuerySuffix()) - - plan.query = s.String() - t.updatePlan = plan - } - - return plan.createBindInstance(elem, t.dbmap.TypeConverter) -} - -func (t *TableMap) bindDelete(elem reflect.Value) (bindInstance, error) { - plan := t.deletePlan - if plan.query == "" { - - s := bytes.Buffer{} - s.WriteString(fmt.Sprintf("delete from %s", t.dbmap.Dialect.QuotedTableForQuery(t.SchemaName, t.TableName))) - - for y := range t.Columns { - col := t.Columns[y] - if !col.Transient { - if col == t.version { - plan.versField = col.fieldName - } - } - } - - s.WriteString(" where ") - for x := range t.keys { - k := t.keys[x] - if x > 0 { - s.WriteString(" and ") - } - s.WriteString(t.dbmap.Dialect.QuoteField(k.ColumnName)) - s.WriteString("=") - s.WriteString(t.dbmap.Dialect.BindVar(x)) - - plan.keyFields = append(plan.keyFields, k.fieldName) - plan.argFields = append(plan.argFields, k.fieldName) - } - if plan.versField != "" { - s.WriteString(" and ") - s.WriteString(t.dbmap.Dialect.QuoteField(t.version.ColumnName)) - s.WriteString("=") - s.WriteString(t.dbmap.Dialect.BindVar(len(plan.argFields))) - - plan.argFields = append(plan.argFields, plan.versField) - } - s.WriteString(t.dbmap.Dialect.QuerySuffix()) - - plan.query = s.String() - t.deletePlan = plan - } - - return plan.createBindInstance(elem, t.dbmap.TypeConverter) -} - -func (t *TableMap) bindGet() bindPlan { - plan := t.getPlan - if plan.query == "" { - - s := bytes.Buffer{} - s.WriteString("select ") - - x := 0 - for _, col := range t.Columns { - if !col.Transient { - if x > 0 { - s.WriteString(",") - } - s.WriteString(t.dbmap.Dialect.QuoteField(col.ColumnName)) - plan.argFields = append(plan.argFields, col.fieldName) - x++ - } - } - s.WriteString(" from ") - s.WriteString(t.dbmap.Dialect.QuotedTableForQuery(t.SchemaName, t.TableName)) - s.WriteString(" where ") - for x := range t.keys { - col := t.keys[x] - if x > 0 { - s.WriteString(" and ") - } - s.WriteString(t.dbmap.Dialect.QuoteField(col.ColumnName)) - s.WriteString("=") - s.WriteString(t.dbmap.Dialect.BindVar(x)) - - plan.keyFields = append(plan.keyFields, col.fieldName) - } - s.WriteString(t.dbmap.Dialect.QuerySuffix()) - - plan.query = s.String() - t.getPlan = plan - } - - return plan -} - -// ColumnMap represents a mapping between a Go struct field and a single -// column in a table. -// Unique and MaxSize only inform the -// CreateTables() function and are not used by Insert/Update/Delete/Get. -type ColumnMap struct { - // Column name in db table - ColumnName string - - // If true, this column is skipped in generated SQL statements - Transient bool - - // If true, " unique" is added to create table statements. - // Not used elsewhere - Unique bool - - // Passed to Dialect.ToSqlType() to assist in informing the - // correct column type to map to in CreateTables() - // Not used elsewhere - MaxSize int - - fieldName string - gotype reflect.Type - isPK bool - isAutoIncr bool - isNotNull bool -} - -// Rename allows you to specify the column name in the table -// -// Example: table.ColMap("Updated").Rename("date_updated") -// -func (c *ColumnMap) Rename(colname string) *ColumnMap { - c.ColumnName = colname - return c -} - -// SetTransient allows you to mark the column as transient. If true -// this column will be skipped when SQL statements are generated -func (c *ColumnMap) SetTransient(b bool) *ColumnMap { - c.Transient = b - return c -} - -// SetUnique adds "unique" to the create table statements for this -// column, if b is true. -func (c *ColumnMap) SetUnique(b bool) *ColumnMap { - c.Unique = b - return c -} - -// SetNotNull adds "not null" to the create table statements for this -// column, if nn is true. -func (c *ColumnMap) SetNotNull(nn bool) *ColumnMap { - c.isNotNull = nn - return c -} - -// SetMaxSize specifies the max length of values of this column. This is -// passed to the dialect.ToSqlType() function, which can use the value -// to alter the generated type for "create table" statements -func (c *ColumnMap) SetMaxSize(size int) *ColumnMap { - c.MaxSize = size - return c -} - -// Transaction represents a database transaction. -// Insert/Update/Delete/Get/Exec operations will be run in the context -// of that transaction. Transactions should be terminated with -// a call to Commit() or Rollback() -type Transaction struct { - dbmap *DbMap - tx *sql.Tx - closed bool -} - -// SqlExecutor exposes gorp operations that can be run from Pre/Post -// hooks. This hides whether the current operation that triggered the -// hook is in a transaction. -// -// See the DbMap function docs for each of the functions below for more -// information. -type SqlExecutor interface { - Get(i interface{}, keys ...interface{}) (interface{}, error) - Insert(list ...interface{}) error - Update(list ...interface{}) (int64, error) - Delete(list ...interface{}) (int64, error) - Exec(query string, args ...interface{}) (sql.Result, error) - Select(i interface{}, query string, - args ...interface{}) ([]interface{}, error) - SelectInt(query string, args ...interface{}) (int64, error) - SelectNullInt(query string, args ...interface{}) (sql.NullInt64, error) - SelectFloat(query string, args ...interface{}) (float64, error) - SelectNullFloat(query string, args ...interface{}) (sql.NullFloat64, error) - SelectStr(query string, args ...interface{}) (string, error) - SelectNullStr(query string, args ...interface{}) (sql.NullString, error) - SelectOne(holder interface{}, query string, args ...interface{}) error - query(query string, args ...interface{}) (*sql.Rows, error) - queryRow(query string, args ...interface{}) *sql.Row -} - -// Compile-time check that DbMap and Transaction implement the SqlExecutor -// interface. -var _, _ SqlExecutor = &DbMap{}, &Transaction{} - -type GorpLogger interface { - Printf(format string, v ...interface{}) -} - -// TraceOn turns on SQL statement logging for this DbMap. After this is -// called, all SQL statements will be sent to the logger. If prefix is -// a non-empty string, it will be written to the front of all logged -// strings, which can aid in filtering log lines. -// -// Use TraceOn if you want to spy on the SQL statements that gorp -// generates. -// -// Note that the base log.Logger type satisfies GorpLogger, but adapters can -// easily be written for other logging packages (e.g., the golang-sanctioned -// glog framework). -func (m *DbMap) TraceOn(prefix string, logger GorpLogger) { - m.logger = logger - if prefix == "" { - m.logPrefix = prefix - } else { - m.logPrefix = fmt.Sprintf("%s ", prefix) - } -} - -// TraceOff turns off tracing. It is idempotent. -func (m *DbMap) TraceOff() { - m.logger = nil - m.logPrefix = "" -} - -// AddTable registers the given interface type with gorp. The table name -// will be given the name of the TypeOf(i). You must call this function, -// or AddTableWithName, for any struct type you wish to persist with -// the given DbMap. -// -// This operation is idempotent. If i's type is already mapped, the -// existing *TableMap is returned -func (m *DbMap) AddTable(i interface{}) *TableMap { - return m.AddTableWithName(i, "") -} - -// AddTableWithName has the same behavior as AddTable, but sets -// table.TableName to name. -func (m *DbMap) AddTableWithName(i interface{}, name string) *TableMap { - return m.AddTableWithNameAndSchema(i, "", name) -} - -// AddTableWithNameAndSchema has the same behavior as AddTable, but sets -// table.TableName to name. -func (m *DbMap) AddTableWithNameAndSchema(i interface{}, schema string, name string) *TableMap { - t := reflect.TypeOf(i) - if name == "" { - name = t.Name() - } - - // check if we have a table for this type already - // if so, update the name and return the existing pointer - for i := range m.tables { - table := m.tables[i] - if table.gotype == t { - table.TableName = name - return table - } - } - - tmap := &TableMap{gotype: t, TableName: name, SchemaName: schema, dbmap: m} - tmap.Columns, tmap.version = m.readStructColumns(t) - m.tables = append(m.tables, tmap) - - return tmap -} - -func (m *DbMap) readStructColumns(t reflect.Type) (cols []*ColumnMap, version *ColumnMap) { - n := t.NumField() - for i := 0; i < n; i++ { - f := t.Field(i) - if f.Anonymous && f.Type.Kind() == reflect.Struct { - // Recursively add nested fields in embedded structs. - subcols, subversion := m.readStructColumns(f.Type) - // Don't append nested fields that have the same field - // name as an already-mapped field. - for _, subcol := range subcols { - shouldAppend := true - for _, col := range cols { - if !subcol.Transient && subcol.fieldName == col.fieldName { - shouldAppend = false - break - } - } - if shouldAppend { - cols = append(cols, subcol) - } - } - if subversion != nil { - version = subversion - } - } else { - columnName := f.Tag.Get("db") - if columnName == "" { - columnName = f.Name - } - gotype := f.Type - if m.TypeConverter != nil { - // Make a new pointer to a value of type gotype and - // pass it to the TypeConverter's FromDb method to see - // if a different type should be used for the column - // type during table creation. - value := reflect.New(gotype).Interface() - scanner, useHolder := m.TypeConverter.FromDb(value) - if useHolder { - gotype = reflect.TypeOf(scanner.Holder) - } - } - cm := &ColumnMap{ - ColumnName: columnName, - Transient: columnName == "-", - fieldName: f.Name, - gotype: gotype, - } - // Check for nested fields of the same field name and - // override them. - shouldAppend := true - for index, col := range cols { - if !col.Transient && col.fieldName == cm.fieldName { - cols[index] = cm - shouldAppend = false - break - } - } - if shouldAppend { - cols = append(cols, cm) - } - if cm.fieldName == "Version" { - log.New(os.Stderr, "", log.LstdFlags).Println("Warning: Automatic mapping of Version struct members to version columns (see optimistic locking) will be deprecated in next version (V2) See: https://github.com/go-gorp/gorp/pull/214") - version = cm - } - } - } - return -} - -// CreateTables iterates through TableMaps registered to this DbMap and -// executes "create table" statements against the database for each. -// -// This is particularly useful in unit tests where you want to create -// and destroy the schema automatically. -func (m *DbMap) CreateTables() error { - return m.createTables(false) -} - -// CreateTablesIfNotExists is similar to CreateTables, but starts -// each statement with "create table if not exists" so that existing -// tables do not raise errors -func (m *DbMap) CreateTablesIfNotExists() error { - return m.createTables(true) -} - -func (m *DbMap) createTables(ifNotExists bool) error { - var err error - for i := range m.tables { - table := m.tables[i] - - s := bytes.Buffer{} - - if strings.TrimSpace(table.SchemaName) != "" { - schemaCreate := "create schema" - if ifNotExists { - s.WriteString(m.Dialect.IfSchemaNotExists(schemaCreate, table.SchemaName)) - } else { - s.WriteString(schemaCreate) - } - s.WriteString(fmt.Sprintf(" %s;", table.SchemaName)) - } - - tableCreate := "create table" - if ifNotExists { - s.WriteString(m.Dialect.IfTableNotExists(tableCreate, table.SchemaName, table.TableName)) - } else { - s.WriteString(tableCreate) - } - s.WriteString(fmt.Sprintf(" %s (", m.Dialect.QuotedTableForQuery(table.SchemaName, table.TableName))) - - x := 0 - for _, col := range table.Columns { - if !col.Transient { - if x > 0 { - s.WriteString(", ") - } - stype := m.Dialect.ToSqlType(col.gotype, col.MaxSize, col.isAutoIncr) - s.WriteString(fmt.Sprintf("%s %s", m.Dialect.QuoteField(col.ColumnName), stype)) - - if col.isPK || col.isNotNull { - s.WriteString(" not null") - } - if col.isPK && len(table.keys) == 1 { - s.WriteString(" primary key") - } - if col.Unique { - s.WriteString(" unique") - } - if col.isAutoIncr { - s.WriteString(fmt.Sprintf(" %s", m.Dialect.AutoIncrStr())) - } - - x++ - } - } - if len(table.keys) > 1 { - s.WriteString(", primary key (") - for x := range table.keys { - if x > 0 { - s.WriteString(", ") - } - s.WriteString(m.Dialect.QuoteField(table.keys[x].ColumnName)) - } - s.WriteString(")") - } - if len(table.uniqueTogether) > 0 { - for _, columns := range table.uniqueTogether { - s.WriteString(", unique (") - for i, column := range columns { - if i > 0 { - s.WriteString(", ") - } - s.WriteString(m.Dialect.QuoteField(column)) - } - s.WriteString(")") - } - } - s.WriteString(") ") - s.WriteString(m.Dialect.CreateTableSuffix()) - s.WriteString(m.Dialect.QuerySuffix()) - _, err = m.Exec(s.String()) - if err != nil { - break - } - } - return err -} - -// DropTable drops an individual table. Will throw an error -// if the table does not exist. -func (m *DbMap) DropTable(table interface{}) error { - t := reflect.TypeOf(table) - return m.dropTable(t, false) -} - -// DropTable drops an individual table. Will NOT throw an error -// if the table does not exist. -func (m *DbMap) DropTableIfExists(table interface{}) error { - t := reflect.TypeOf(table) - return m.dropTable(t, true) -} - -// DropTables iterates through TableMaps registered to this DbMap and -// executes "drop table" statements against the database for each. -func (m *DbMap) DropTables() error { - return m.dropTables(false) -} - -// DropTablesIfExists is the same as DropTables, but uses the "if exists" clause to -// avoid errors for tables that do not exist. -func (m *DbMap) DropTablesIfExists() error { - return m.dropTables(true) -} - -// Goes through all the registered tables, dropping them one by one. -// If an error is encountered, then it is returned and the rest of -// the tables are not dropped. -func (m *DbMap) dropTables(addIfExists bool) (err error) { - for _, table := range m.tables { - err = m.dropTableImpl(table, addIfExists) - if err != nil { - return - } - } - return err -} - -// Implementation of dropping a single table. -func (m *DbMap) dropTable(t reflect.Type, addIfExists bool) error { - table := tableOrNil(m, t) - if table == nil { - return errors.New(fmt.Sprintf("table %s was not registered!", table.TableName)) - } - - return m.dropTableImpl(table, addIfExists) -} - -func (m *DbMap) dropTableImpl(table *TableMap, ifExists bool) (err error) { - tableDrop := "drop table" - if ifExists { - tableDrop = m.Dialect.IfTableExists(tableDrop, table.SchemaName, table.TableName) - } - _, err = m.Exec(fmt.Sprintf("%s %s;", tableDrop, m.Dialect.QuotedTableForQuery(table.SchemaName, table.TableName))) - return err -} - -// TruncateTables iterates through TableMaps registered to this DbMap and -// executes "truncate table" statements against the database for each, or in the case of -// sqlite, a "delete from" with no "where" clause, which uses the truncate optimization -// (http://www.sqlite.org/lang_delete.html) -func (m *DbMap) TruncateTables() error { - var err error - for i := range m.tables { - table := m.tables[i] - _, e := m.Exec(fmt.Sprintf("%s %s;", m.Dialect.TruncateClause(), m.Dialect.QuotedTableForQuery(table.SchemaName, table.TableName))) - if e != nil { - err = e - } - } - return err -} - -// Insert runs a SQL INSERT statement for each element in list. List -// items must be pointers. -// -// Any interface whose TableMap has an auto-increment primary key will -// have its last insert id bound to the PK field on the struct. -// -// The hook functions PreInsert() and/or PostInsert() will be executed -// before/after the INSERT statement if the interface defines them. -// -// Panics if any interface in the list has not been registered with AddTable -func (m *DbMap) Insert(list ...interface{}) error { - return insert(m, m, list...) -} - -// Update runs a SQL UPDATE statement for each element in list. List -// items must be pointers. -// -// The hook functions PreUpdate() and/or PostUpdate() will be executed -// before/after the UPDATE statement if the interface defines them. -// -// Returns the number of rows updated. -// -// Returns an error if SetKeys has not been called on the TableMap -// Panics if any interface in the list has not been registered with AddTable -func (m *DbMap) Update(list ...interface{}) (int64, error) { - return update(m, m, list...) -} - -// Delete runs a SQL DELETE statement for each element in list. List -// items must be pointers. -// -// The hook functions PreDelete() and/or PostDelete() will be executed -// before/after the DELETE statement if the interface defines them. -// -// Returns the number of rows deleted. -// -// Returns an error if SetKeys has not been called on the TableMap -// Panics if any interface in the list has not been registered with AddTable -func (m *DbMap) Delete(list ...interface{}) (int64, error) { - return delete(m, m, list...) -} - -// Get runs a SQL SELECT to fetch a single row from the table based on the -// primary key(s) -// -// i should be an empty value for the struct to load. keys should be -// the primary key value(s) for the row to load. If multiple keys -// exist on the table, the order should match the column order -// specified in SetKeys() when the table mapping was defined. -// -// The hook function PostGet() will be executed after the SELECT -// statement if the interface defines them. -// -// Returns a pointer to a struct that matches or nil if no row is found. -// -// Returns an error if SetKeys has not been called on the TableMap -// Panics if any interface in the list has not been registered with AddTable -func (m *DbMap) Get(i interface{}, keys ...interface{}) (interface{}, error) { - return get(m, m, i, keys...) -} - -// Select runs an arbitrary SQL query, binding the columns in the result -// to fields on the struct specified by i. args represent the bind -// parameters for the SQL statement. -// -// Column names on the SELECT statement should be aliased to the field names -// on the struct i. Returns an error if one or more columns in the result -// do not match. It is OK if fields on i are not part of the SQL -// statement. -// -// The hook function PostGet() will be executed after the SELECT -// statement if the interface defines them. -// -// Values are returned in one of two ways: -// 1. If i is a struct or a pointer to a struct, returns a slice of pointers to -// matching rows of type i. -// 2. If i is a pointer to a slice, the results will be appended to that slice -// and nil returned. -// -// i does NOT need to be registered with AddTable() -func (m *DbMap) Select(i interface{}, query string, args ...interface{}) ([]interface{}, error) { - return hookedselect(m, m, i, query, args...) -} - -// Exec runs an arbitrary SQL statement. args represent the bind parameters. -// This is equivalent to running: Exec() using database/sql -func (m *DbMap) Exec(query string, args ...interface{}) (sql.Result, error) { - m.trace(query, args...) - return m.Db.Exec(query, args...) -} - -// SelectInt is a convenience wrapper around the gorp.SelectInt function -func (m *DbMap) SelectInt(query string, args ...interface{}) (int64, error) { - return SelectInt(m, query, args...) -} - -// SelectNullInt is a convenience wrapper around the gorp.SelectNullInt function -func (m *DbMap) SelectNullInt(query string, args ...interface{}) (sql.NullInt64, error) { - return SelectNullInt(m, query, args...) -} - -// SelectFloat is a convenience wrapper around the gorp.SelectFlot function -func (m *DbMap) SelectFloat(query string, args ...interface{}) (float64, error) { - return SelectFloat(m, query, args...) -} - -// SelectNullFloat is a convenience wrapper around the gorp.SelectNullFloat function -func (m *DbMap) SelectNullFloat(query string, args ...interface{}) (sql.NullFloat64, error) { - return SelectNullFloat(m, query, args...) -} - -// SelectStr is a convenience wrapper around the gorp.SelectStr function -func (m *DbMap) SelectStr(query string, args ...interface{}) (string, error) { - return SelectStr(m, query, args...) -} - -// SelectNullStr is a convenience wrapper around the gorp.SelectNullStr function -func (m *DbMap) SelectNullStr(query string, args ...interface{}) (sql.NullString, error) { - return SelectNullStr(m, query, args...) -} - -// SelectOne is a convenience wrapper around the gorp.SelectOne function -func (m *DbMap) SelectOne(holder interface{}, query string, args ...interface{}) error { - return SelectOne(m, m, holder, query, args...) -} - -// Begin starts a gorp Transaction -func (m *DbMap) Begin() (*Transaction, error) { - m.trace("begin;") - tx, err := m.Db.Begin() - if err != nil { - return nil, err - } - return &Transaction{m, tx, false}, nil -} - -// TableFor returns the *TableMap corresponding to the given Go Type -// If no table is mapped to that type an error is returned. -// If checkPK is true and the mapped table has no registered PKs, an error is returned. -func (m *DbMap) TableFor(t reflect.Type, checkPK bool) (*TableMap, error) { - table := tableOrNil(m, t) - if table == nil { - return nil, errors.New(fmt.Sprintf("No table found for type: %v", t.Name())) - } - - if checkPK && len(table.keys) < 1 { - e := fmt.Sprintf("gorp: No keys defined for table: %s", - table.TableName) - return nil, errors.New(e) - } - - return table, nil -} - -// Prepare creates a prepared statement for later queries or executions. -// Multiple queries or executions may be run concurrently from the returned statement. -// This is equivalent to running: Prepare() using database/sql -func (m *DbMap) Prepare(query string) (*sql.Stmt, error) { - m.trace(query, nil) - return m.Db.Prepare(query) -} - -func tableOrNil(m *DbMap, t reflect.Type) *TableMap { - for i := range m.tables { - table := m.tables[i] - if table.gotype == t { - return table - } - } - return nil -} - -func (m *DbMap) tableForPointer(ptr interface{}, checkPK bool) (*TableMap, reflect.Value, error) { - ptrv := reflect.ValueOf(ptr) - if ptrv.Kind() != reflect.Ptr { - e := fmt.Sprintf("gorp: passed non-pointer: %v (kind=%v)", ptr, - ptrv.Kind()) - return nil, reflect.Value{}, errors.New(e) - } - elem := ptrv.Elem() - etype := reflect.TypeOf(elem.Interface()) - t, err := m.TableFor(etype, checkPK) - if err != nil { - return nil, reflect.Value{}, err - } - - return t, elem, nil -} - -func (m *DbMap) queryRow(query string, args ...interface{}) *sql.Row { - m.trace(query, args...) - return m.Db.QueryRow(query, args...) -} - -func (m *DbMap) query(query string, args ...interface{}) (*sql.Rows, error) { - m.trace(query, args...) - return m.Db.Query(query, args...) -} - -func (m *DbMap) trace(query string, args ...interface{}) { - if m.logger != nil { - var margs = argsString(args...) - m.logger.Printf("%s%s [%s]", m.logPrefix, query, margs) - } -} - -func argsString(args ...interface{}) string { - var margs string - for i, a := range args { - var v interface{} = a - if x, ok := v.(driver.Valuer); ok { - y, err := x.Value() - if err == nil { - v = y - } - } - switch v.(type) { - case string: - v = fmt.Sprintf("%q", v) - default: - v = fmt.Sprintf("%v", v) - } - margs += fmt.Sprintf("%d:%s", i+1, v) - if i+1 < len(args) { - margs += " " - } - } - return margs -} - -/////////////// - -// Insert has the same behavior as DbMap.Insert(), but runs in a transaction. -func (t *Transaction) Insert(list ...interface{}) error { - return insert(t.dbmap, t, list...) -} - -// Update had the same behavior as DbMap.Update(), but runs in a transaction. -func (t *Transaction) Update(list ...interface{}) (int64, error) { - return update(t.dbmap, t, list...) -} - -// Delete has the same behavior as DbMap.Delete(), but runs in a transaction. -func (t *Transaction) Delete(list ...interface{}) (int64, error) { - return delete(t.dbmap, t, list...) -} - -// Get has the same behavior as DbMap.Get(), but runs in a transaction. -func (t *Transaction) Get(i interface{}, keys ...interface{}) (interface{}, error) { - return get(t.dbmap, t, i, keys...) -} - -// Select has the same behavior as DbMap.Select(), but runs in a transaction. -func (t *Transaction) Select(i interface{}, query string, args ...interface{}) ([]interface{}, error) { - return hookedselect(t.dbmap, t, i, query, args...) -} - -// Exec has the same behavior as DbMap.Exec(), but runs in a transaction. -func (t *Transaction) Exec(query string, args ...interface{}) (sql.Result, error) { - t.dbmap.trace(query, args...) - return t.tx.Exec(query, args...) -} - -// SelectInt is a convenience wrapper around the gorp.SelectInt function. -func (t *Transaction) SelectInt(query string, args ...interface{}) (int64, error) { - return SelectInt(t, query, args...) -} - -// SelectNullInt is a convenience wrapper around the gorp.SelectNullInt function. -func (t *Transaction) SelectNullInt(query string, args ...interface{}) (sql.NullInt64, error) { - return SelectNullInt(t, query, args...) -} - -// SelectFloat is a convenience wrapper around the gorp.SelectFloat function. -func (t *Transaction) SelectFloat(query string, args ...interface{}) (float64, error) { - return SelectFloat(t, query, args...) -} - -// SelectNullFloat is a convenience wrapper around the gorp.SelectNullFloat function. -func (t *Transaction) SelectNullFloat(query string, args ...interface{}) (sql.NullFloat64, error) { - return SelectNullFloat(t, query, args...) -} - -// SelectStr is a convenience wrapper around the gorp.SelectStr function. -func (t *Transaction) SelectStr(query string, args ...interface{}) (string, error) { - return SelectStr(t, query, args...) -} - -// SelectNullStr is a convenience wrapper around the gorp.SelectNullStr function. -func (t *Transaction) SelectNullStr(query string, args ...interface{}) (sql.NullString, error) { - return SelectNullStr(t, query, args...) -} - -// SelectOne is a convenience wrapper around the gorp.SelectOne function. -func (t *Transaction) SelectOne(holder interface{}, query string, args ...interface{}) error { - return SelectOne(t.dbmap, t, holder, query, args...) -} - -// Commit commits the underlying database transaction. -func (t *Transaction) Commit() error { - if !t.closed { - t.closed = true - t.dbmap.trace("commit;") - return t.tx.Commit() - } - - return sql.ErrTxDone -} - -// Rollback rolls back the underlying database transaction. -func (t *Transaction) Rollback() error { - if !t.closed { - t.closed = true - t.dbmap.trace("rollback;") - return t.tx.Rollback() - } - - return sql.ErrTxDone -} - -// Savepoint creates a savepoint with the given name. The name is interpolated -// directly into the SQL SAVEPOINT statement, so you must sanitize it if it is -// derived from user input. -func (t *Transaction) Savepoint(name string) error { - query := "savepoint " + t.dbmap.Dialect.QuoteField(name) - t.dbmap.trace(query, nil) - _, err := t.tx.Exec(query) - return err -} - -// RollbackToSavepoint rolls back to the savepoint with the given name. The -// name is interpolated directly into the SQL SAVEPOINT statement, so you must -// sanitize it if it is derived from user input. -func (t *Transaction) RollbackToSavepoint(savepoint string) error { - query := "rollback to savepoint " + t.dbmap.Dialect.QuoteField(savepoint) - t.dbmap.trace(query, nil) - _, err := t.tx.Exec(query) - return err -} - -// ReleaseSavepint releases the savepoint with the given name. The name is -// interpolated directly into the SQL SAVEPOINT statement, so you must sanitize -// it if it is derived from user input. -func (t *Transaction) ReleaseSavepoint(savepoint string) error { - query := "release savepoint " + t.dbmap.Dialect.QuoteField(savepoint) - t.dbmap.trace(query, nil) - _, err := t.tx.Exec(query) - return err -} - -// Prepare has the same behavior as DbMap.Prepare(), but runs in a transaction. -func (t *Transaction) Prepare(query string) (*sql.Stmt, error) { - t.dbmap.trace(query, nil) - return t.tx.Prepare(query) -} - -func (t *Transaction) queryRow(query string, args ...interface{}) *sql.Row { - t.dbmap.trace(query, args...) - return t.tx.QueryRow(query, args...) -} - -func (t *Transaction) query(query string, args ...interface{}) (*sql.Rows, error) { - t.dbmap.trace(query, args...) - return t.tx.Query(query, args...) -} - -/////////////// - -// SelectInt executes the given query, which should be a SELECT statement for a single -// integer column, and returns the value of the first row returned. If no rows are -// found, zero is returned. -func SelectInt(e SqlExecutor, query string, args ...interface{}) (int64, error) { - var h int64 - err := selectVal(e, &h, query, args...) - if err != nil && err != sql.ErrNoRows { - return 0, err - } - return h, nil -} - -// SelectNullInt executes the given query, which should be a SELECT statement for a single -// integer column, and returns the value of the first row returned. If no rows are -// found, the empty sql.NullInt64 value is returned. -func SelectNullInt(e SqlExecutor, query string, args ...interface{}) (sql.NullInt64, error) { - var h sql.NullInt64 - err := selectVal(e, &h, query, args...) - if err != nil && err != sql.ErrNoRows { - return h, err - } - return h, nil -} - -// SelectFloat executes the given query, which should be a SELECT statement for a single -// float column, and returns the value of the first row returned. If no rows are -// found, zero is returned. -func SelectFloat(e SqlExecutor, query string, args ...interface{}) (float64, error) { - var h float64 - err := selectVal(e, &h, query, args...) - if err != nil && err != sql.ErrNoRows { - return 0, err - } - return h, nil -} - -// SelectNullFloat executes the given query, which should be a SELECT statement for a single -// float column, and returns the value of the first row returned. If no rows are -// found, the empty sql.NullInt64 value is returned. -func SelectNullFloat(e SqlExecutor, query string, args ...interface{}) (sql.NullFloat64, error) { - var h sql.NullFloat64 - err := selectVal(e, &h, query, args...) - if err != nil && err != sql.ErrNoRows { - return h, err - } - return h, nil -} - -// SelectStr executes the given query, which should be a SELECT statement for a single -// char/varchar column, and returns the value of the first row returned. If no rows are -// found, an empty string is returned. -func SelectStr(e SqlExecutor, query string, args ...interface{}) (string, error) { - var h string - err := selectVal(e, &h, query, args...) - if err != nil && err != sql.ErrNoRows { - return "", err - } - return h, nil -} - -// SelectNullStr executes the given query, which should be a SELECT -// statement for a single char/varchar column, and returns the value -// of the first row returned. If no rows are found, the empty -// sql.NullString is returned. -func SelectNullStr(e SqlExecutor, query string, args ...interface{}) (sql.NullString, error) { - var h sql.NullString - err := selectVal(e, &h, query, args...) - if err != nil && err != sql.ErrNoRows { - return h, err - } - return h, nil -} - -// SelectOne executes the given query (which should be a SELECT statement) -// and binds the result to holder, which must be a pointer. -// -// If no row is found, an error (sql.ErrNoRows specifically) will be returned -// -// If more than one row is found, an error will be returned. -// -func SelectOne(m *DbMap, e SqlExecutor, holder interface{}, query string, args ...interface{}) error { - t := reflect.TypeOf(holder) - if t.Kind() == reflect.Ptr { - t = t.Elem() - } else { - return fmt.Errorf("gorp: SelectOne holder must be a pointer, but got: %t", holder) - } - - // Handle pointer to pointer - isptr := false - if t.Kind() == reflect.Ptr { - isptr = true - t = t.Elem() - } - - if t.Kind() == reflect.Struct { - var nonFatalErr error - - list, err := hookedselect(m, e, holder, query, args...) - if err != nil { - if !NonFatalError(err) { - return err - } - nonFatalErr = err - } - - dest := reflect.ValueOf(holder) - if isptr { - dest = dest.Elem() - } - - if list != nil && len(list) > 0 { - // check for multiple rows - if len(list) > 1 { - return fmt.Errorf("gorp: multiple rows returned for: %s - %v", query, args) - } - - // Initialize if nil - if dest.IsNil() { - dest.Set(reflect.New(t)) - } - - // only one row found - src := reflect.ValueOf(list[0]) - dest.Elem().Set(src.Elem()) - } else { - // No rows found, return a proper error. - return sql.ErrNoRows - } - - return nonFatalErr - } - - return selectVal(e, holder, query, args...) -} - -func selectVal(e SqlExecutor, holder interface{}, query string, args ...interface{}) error { - if len(args) == 1 { - switch m := e.(type) { - case *DbMap: - query, args = maybeExpandNamedQuery(m, query, args) - case *Transaction: - query, args = maybeExpandNamedQuery(m.dbmap, query, args) - } - } - rows, err := e.query(query, args...) - if err != nil { - return err - } - defer rows.Close() - - if !rows.Next() { - return sql.ErrNoRows - } - - return rows.Scan(holder) -} - -/////////////// - -func hookedselect(m *DbMap, exec SqlExecutor, i interface{}, query string, - args ...interface{}) ([]interface{}, error) { - - var nonFatalErr error - - list, err := rawselect(m, exec, i, query, args...) - if err != nil { - if !NonFatalError(err) { - return nil, err - } - nonFatalErr = err - } - - // Determine where the results are: written to i, or returned in list - if t, _ := toSliceType(i); t == nil { - for _, v := range list { - if v, ok := v.(HasPostGet); ok { - err := v.PostGet(exec) - if err != nil { - return nil, err - } - } - } - } else { - resultsValue := reflect.Indirect(reflect.ValueOf(i)) - for i := 0; i < resultsValue.Len(); i++ { - if v, ok := resultsValue.Index(i).Interface().(HasPostGet); ok { - err := v.PostGet(exec) - if err != nil { - return nil, err - } - } - } - } - return list, nonFatalErr -} - -func rawselect(m *DbMap, exec SqlExecutor, i interface{}, query string, - args ...interface{}) ([]interface{}, error) { - var ( - appendToSlice = false // Write results to i directly? - intoStruct = true // Selecting into a struct? - pointerElements = true // Are the slice elements pointers (vs values)? - ) - - var nonFatalErr error - - // get type for i, verifying it's a supported destination - t, err := toType(i) - if err != nil { - var err2 error - if t, err2 = toSliceType(i); t == nil { - if err2 != nil { - return nil, err2 - } - return nil, err - } - pointerElements = t.Kind() == reflect.Ptr - if pointerElements { - t = t.Elem() - } - appendToSlice = true - intoStruct = t.Kind() == reflect.Struct - } - - // If the caller supplied a single struct/map argument, assume a "named - // parameter" query. Extract the named arguments from the struct/map, create - // the flat arg slice, and rewrite the query to use the dialect's placeholder. - if len(args) == 1 { - query, args = maybeExpandNamedQuery(m, query, args) - } - - // Run the query - rows, err := exec.query(query, args...) - if err != nil { - return nil, err - } - defer rows.Close() - - // Fetch the column names as returned from db - cols, err := rows.Columns() - if err != nil { - return nil, err - } - - if !intoStruct && len(cols) > 1 { - return nil, fmt.Errorf("gorp: select into non-struct slice requires 1 column, got %d", len(cols)) - } - - var colToFieldIndex [][]int - if intoStruct { - if colToFieldIndex, err = columnToFieldIndex(m, t, cols); err != nil { - if !NonFatalError(err) { - return nil, err - } - nonFatalErr = err - } - } - - conv := m.TypeConverter - - // Add results to one of these two slices. - var ( - list = make([]interface{}, 0) - sliceValue = reflect.Indirect(reflect.ValueOf(i)) - ) - - for { - if !rows.Next() { - // if error occured return rawselect - if rows.Err() != nil { - return nil, rows.Err() - } - // time to exit from outer "for" loop - break - } - v := reflect.New(t) - dest := make([]interface{}, len(cols)) - - custScan := make([]CustomScanner, 0) - - for x := range cols { - f := v.Elem() - if intoStruct { - index := colToFieldIndex[x] - if index == nil { - // this field is not present in the struct, so create a dummy - // value for rows.Scan to scan into - var dummy sql.RawBytes - dest[x] = &dummy - continue - } - f = f.FieldByIndex(index) - } - target := f.Addr().Interface() - if conv != nil { - scanner, ok := conv.FromDb(target) - if ok { - target = scanner.Holder - custScan = append(custScan, scanner) - } - } - dest[x] = target - } - - err = rows.Scan(dest...) - if err != nil { - return nil, err - } - - for _, c := range custScan { - err = c.Bind() - if err != nil { - return nil, err - } - } - - if appendToSlice { - if !pointerElements { - v = v.Elem() - } - sliceValue.Set(reflect.Append(sliceValue, v)) - } else { - list = append(list, v.Interface()) - } - } - - if appendToSlice && sliceValue.IsNil() { - sliceValue.Set(reflect.MakeSlice(sliceValue.Type(), 0, 0)) - } - - return list, nonFatalErr -} - -// maybeExpandNamedQuery checks the given arg to see if it's eligible to be used -// as input to a named query. If so, it rewrites the query to use -// dialect-dependent bindvars and instantiates the corresponding slice of -// parameters by extracting data from the map / struct. -// If not, returns the input values unchanged. -func maybeExpandNamedQuery(m *DbMap, query string, args []interface{}) (string, []interface{}) { - arg := reflect.ValueOf(args[0]) - for arg.Kind() == reflect.Ptr { - arg = arg.Elem() - } - switch { - case arg.Kind() == reflect.Map && arg.Type().Key().Kind() == reflect.String: - return expandNamedQuery(m, query, func(key string) reflect.Value { - return arg.MapIndex(reflect.ValueOf(key)) - }) - // #84 - ignore time.Time structs here - there may be a cleaner way to do this - case arg.Kind() == reflect.Struct && !(arg.Type().PkgPath() == "time" && arg.Type().Name() == "Time"): - return expandNamedQuery(m, query, arg.FieldByName) - } - return query, args -} - -var keyRegexp = regexp.MustCompile(`:[[:word:]]+`) - -// expandNamedQuery accepts a query with placeholders of the form ":key", and a -// single arg of Kind Struct or Map[string]. It returns the query with the -// dialect's placeholders, and a slice of args ready for positional insertion -// into the query. -func expandNamedQuery(m *DbMap, query string, keyGetter func(key string) reflect.Value) (string, []interface{}) { - var ( - n int - args []interface{} - ) - return keyRegexp.ReplaceAllStringFunc(query, func(key string) string { - val := keyGetter(key[1:]) - if !val.IsValid() { - return key - } - args = append(args, val.Interface()) - newVar := m.Dialect.BindVar(n) - n++ - return newVar - }), args -} - -func columnToFieldIndex(m *DbMap, t reflect.Type, cols []string) ([][]int, error) { - colToFieldIndex := make([][]int, len(cols)) - - // check if type t is a mapped table - if so we'll - // check the table for column aliasing below - tableMapped := false - table := tableOrNil(m, t) - if table != nil { - tableMapped = true - } - - // Loop over column names and find field in i to bind to - // based on column name. all returned columns must match - // a field in the i struct - missingColNames := []string{} - for x := range cols { - colName := strings.ToLower(cols[x]) - field, found := t.FieldByNameFunc(func(fieldName string) bool { - field, _ := t.FieldByName(fieldName) - fieldName = field.Tag.Get("db") - - if fieldName == "-" { - return false - } else if fieldName == "" { - fieldName = field.Name - } - if tableMapped { - colMap := colMapOrNil(table, fieldName) - if colMap != nil { - fieldName = colMap.ColumnName - } - } - return colName == strings.ToLower(fieldName) - }) - if found { - colToFieldIndex[x] = field.Index - } - if colToFieldIndex[x] == nil { - missingColNames = append(missingColNames, colName) - } - } - if len(missingColNames) > 0 { - return colToFieldIndex, &NoFieldInTypeError{ - TypeName: t.Name(), - MissingColNames: missingColNames, - } - } - return colToFieldIndex, nil -} - -func fieldByName(val reflect.Value, fieldName string) *reflect.Value { - // try to find field by exact match - f := val.FieldByName(fieldName) - - if f != zeroVal { - return &f - } - - // try to find by case insensitive match - only the Postgres driver - // seems to require this - in the case where columns are aliased in the sql - fieldNameL := strings.ToLower(fieldName) - fieldCount := val.NumField() - t := val.Type() - for i := 0; i < fieldCount; i++ { - sf := t.Field(i) - if strings.ToLower(sf.Name) == fieldNameL { - f := val.Field(i) - return &f - } - } - - return nil -} - -// toSliceType returns the element type of the given object, if the object is a -// "*[]*Element" or "*[]Element". If not, returns nil. -// err is returned if the user was trying to pass a pointer-to-slice but failed. -func toSliceType(i interface{}) (reflect.Type, error) { - t := reflect.TypeOf(i) - if t.Kind() != reflect.Ptr { - // If it's a slice, return a more helpful error message - if t.Kind() == reflect.Slice { - return nil, fmt.Errorf("gorp: Cannot SELECT into a non-pointer slice: %v", t) - } - return nil, nil - } - if t = t.Elem(); t.Kind() != reflect.Slice { - return nil, nil - } - return t.Elem(), nil -} - -func toType(i interface{}) (reflect.Type, error) { - t := reflect.TypeOf(i) - - // If a Pointer to a type, follow - for t.Kind() == reflect.Ptr { - t = t.Elem() - } - - if t.Kind() != reflect.Struct { - return nil, fmt.Errorf("gorp: Cannot SELECT into this type: %v", reflect.TypeOf(i)) - } - return t, nil -} - -func get(m *DbMap, exec SqlExecutor, i interface{}, - keys ...interface{}) (interface{}, error) { - - t, err := toType(i) - if err != nil { - return nil, err - } - - table, err := m.TableFor(t, true) - if err != nil { - return nil, err - } - - plan := table.bindGet() - - v := reflect.New(t) - dest := make([]interface{}, len(plan.argFields)) - - conv := m.TypeConverter - custScan := make([]CustomScanner, 0) - - for x, fieldName := range plan.argFields { - f := v.Elem().FieldByName(fieldName) - target := f.Addr().Interface() - if conv != nil { - scanner, ok := conv.FromDb(target) - if ok { - target = scanner.Holder - custScan = append(custScan, scanner) - } - } - dest[x] = target - } - - row := exec.queryRow(plan.query, keys...) - err = row.Scan(dest...) - if err != nil { - if err == sql.ErrNoRows { - err = nil - } - return nil, err - } - - for _, c := range custScan { - err = c.Bind() - if err != nil { - return nil, err - } - } - - if v, ok := v.Interface().(HasPostGet); ok { - err := v.PostGet(exec) - if err != nil { - return nil, err - } - } - - return v.Interface(), nil -} - -func delete(m *DbMap, exec SqlExecutor, list ...interface{}) (int64, error) { - count := int64(0) - for _, ptr := range list { - table, elem, err := m.tableForPointer(ptr, true) - if err != nil { - return -1, err - } - - eval := elem.Addr().Interface() - if v, ok := eval.(HasPreDelete); ok { - err = v.PreDelete(exec) - if err != nil { - return -1, err - } - } - - bi, err := table.bindDelete(elem) - if err != nil { - return -1, err - } - - res, err := exec.Exec(bi.query, bi.args...) - if err != nil { - return -1, err - } - rows, err := res.RowsAffected() - if err != nil { - return -1, err - } - - if rows == 0 && bi.existingVersion > 0 { - return lockError(m, exec, table.TableName, - bi.existingVersion, elem, bi.keys...) - } - - count += rows - - if v, ok := eval.(HasPostDelete); ok { - err := v.PostDelete(exec) - if err != nil { - return -1, err - } - } - } - - return count, nil -} - -func update(m *DbMap, exec SqlExecutor, list ...interface{}) (int64, error) { - count := int64(0) - for _, ptr := range list { - table, elem, err := m.tableForPointer(ptr, true) - if err != nil { - return -1, err - } - - eval := elem.Addr().Interface() - if v, ok := eval.(HasPreUpdate); ok { - err = v.PreUpdate(exec) - if err != nil { - return -1, err - } - } - - bi, err := table.bindUpdate(elem) - if err != nil { - return -1, err - } - - res, err := exec.Exec(bi.query, bi.args...) - if err != nil { - return -1, err - } - - rows, err := res.RowsAffected() - if err != nil { - return -1, err - } - - if rows == 0 && bi.existingVersion > 0 { - return lockError(m, exec, table.TableName, - bi.existingVersion, elem, bi.keys...) - } - - if bi.versField != "" { - elem.FieldByName(bi.versField).SetInt(bi.existingVersion + 1) - } - - count += rows - - if v, ok := eval.(HasPostUpdate); ok { - err = v.PostUpdate(exec) - if err != nil { - return -1, err - } - } - } - return count, nil -} - -func insert(m *DbMap, exec SqlExecutor, list ...interface{}) error { - for _, ptr := range list { - table, elem, err := m.tableForPointer(ptr, false) - if err != nil { - return err - } - - eval := elem.Addr().Interface() - if v, ok := eval.(HasPreInsert); ok { - err := v.PreInsert(exec) - if err != nil { - return err - } - } - - bi, err := table.bindInsert(elem) - if err != nil { - return err - } - - if bi.autoIncrIdx > -1 { - f := elem.FieldByName(bi.autoIncrFieldName) - switch inserter := m.Dialect.(type) { - case IntegerAutoIncrInserter: - id, err := inserter.InsertAutoIncr(exec, bi.query, bi.args...) - if err != nil { - return err - } - k := f.Kind() - if (k == reflect.Int) || (k == reflect.Int16) || (k == reflect.Int32) || (k == reflect.Int64) { - f.SetInt(id) - } else if (k == reflect.Uint) || (k == reflect.Uint16) || (k == reflect.Uint32) || (k == reflect.Uint64) { - f.SetUint(uint64(id)) - } else { - return fmt.Errorf("gorp: Cannot set autoincrement value on non-Int field. SQL=%s autoIncrIdx=%d autoIncrFieldName=%s", bi.query, bi.autoIncrIdx, bi.autoIncrFieldName) - } - case TargetedAutoIncrInserter: - err := inserter.InsertAutoIncrToTarget(exec, bi.query, f.Addr().Interface(), bi.args...) - if err != nil { - return err - } - default: - return fmt.Errorf("gorp: Cannot use autoincrement fields on dialects that do not implement an autoincrementing interface") - } - } else { - _, err := exec.Exec(bi.query, bi.args...) - if err != nil { - return err - } - } - - if v, ok := eval.(HasPostInsert); ok { - err := v.PostInsert(exec) - if err != nil { - return err - } - } - } - return nil -} - -func lockError(m *DbMap, exec SqlExecutor, tableName string, - existingVer int64, elem reflect.Value, - keys ...interface{}) (int64, error) { - - existing, err := get(m, exec, elem.Interface(), keys...) - if err != nil { - return -1, err - } - - ole := OptimisticLockError{tableName, keys, true, existingVer} - if existing == nil { - ole.RowExists = false - } - return -1, ole -} - -// PostUpdate() will be executed after the GET statement. -type HasPostGet interface { - PostGet(SqlExecutor) error -} - -// PostUpdate() will be executed after the DELETE statement -type HasPostDelete interface { - PostDelete(SqlExecutor) error -} - -// PostUpdate() will be executed after the UPDATE statement -type HasPostUpdate interface { - PostUpdate(SqlExecutor) error -} - -// PostInsert() will be executed after the INSERT statement -type HasPostInsert interface { - PostInsert(SqlExecutor) error -} - -// PreDelete() will be executed before the DELETE statement. -type HasPreDelete interface { - PreDelete(SqlExecutor) error -} - -// PreUpdate() will be executed before UPDATE statement. -type HasPreUpdate interface { - PreUpdate(SqlExecutor) error -} - -// PreInsert() will be executed before INSERT statement. -type HasPreInsert interface { - PreInsert(SqlExecutor) error -} diff --git a/vendor/gopkg.in/gorp.v1/test_all.sh b/vendor/gopkg.in/gorp.v1/test_all.sh deleted file mode 100755 index f870b39a3..000000000 --- a/vendor/gopkg.in/gorp.v1/test_all.sh +++ /dev/null @@ -1,22 +0,0 @@ -#!/bin/sh - -# on macs, you may need to: -# export GOBUILDFLAG=-ldflags -linkmode=external - -set -e - -export GORP_TEST_DSN=gorptest/gorptest/gorptest -export GORP_TEST_DIALECT=mysql -go test $GOBUILDFLAG . - -export GORP_TEST_DSN=gorptest:gorptest@/gorptest -export GORP_TEST_DIALECT=gomysql -go test $GOBUILDFLAG . - -export GORP_TEST_DSN="user=gorptest password=gorptest dbname=gorptest sslmode=disable" -export GORP_TEST_DIALECT=postgres -go test $GOBUILDFLAG . - -export GORP_TEST_DSN=/tmp/gorptest.bin -export GORP_TEST_DIALECT=sqlite -go test $GOBUILDFLAG . diff --git a/vendor/vendor.json b/vendor/vendor.json index 224e76885..6a37a8aa8 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -636,16 +636,6 @@ "revision": "6ac8c5d890d415025dd5aae7595bcb2a6e7e2fad", "revisionTime": "2017-04-24T20:45:52Z" }, - { - "path": "github.com/rubenv/sql-migrate", - "revision": "53184e1edfb4f9655b0fa8dd2c23e7763f452bda", - "revisionTime": "2015-07-13T16:07:51+02:00" - }, - { - "path": "github.com/rubenv/sql-migrate/sqlparse", - "revision": "53184e1edfb4f9655b0fa8dd2c23e7763f452bda", - "revisionTime": "2015-07-13T16:07:51+02:00" - }, { "path": "github.com/russross/meddler", "revision": "308c3d0e5e45f543a2eb6c787cbfe0db3880e220", @@ -851,11 +841,6 @@ "revision": "014792cf3e266caff1e916876be12282b33059e0", "revisionTime": "2016-02-23T15:26:51-05:00" }, - { - "path": "gopkg.in/gorp.v1", - "revision": "c87af80f3cc5036b55b83d77171e156791085e2e", - "revisionTime": "2015-02-04T09:55:30+01:00" - }, { "path": "gopkg.in/yaml.v2", "revision": "5d6f7e02b7cdad63b06ab3877915532cd30073b4",