github.com/marinho/drone@v0.2.1-0.20140504195434-d3ba962e89a7/pkg/database/schema/schema.go (about)

     1  package schema
     2  
     3  import (
     4  	"database/sql"
     5  )
     6  
     7  // SQL statement to create the User Table.
     8  var userTableStmt = `
     9  CREATE TABLE users (
    10     id                INTEGER PRIMARY KEY AUTOINCREMENT
    11    ,email             VARCHAR(255) UNIQUE
    12    ,password          VARCHAR(255)
    13    ,token             VARCHAR(255) UNIQUE
    14    ,name              VARCHAR(255)
    15    ,gravatar          VARCHAR(255)
    16    ,created           TIMESTAMP
    17    ,updated           TIMESTAMP
    18    ,admin             BOOLEAN
    19    ,github_login      VARCHAR(255)
    20    ,github_token      VARCHAR(255)
    21    ,bitbucket_login   VARCHAR(255)
    22    ,bitbucket_token   VARCHAR(255)
    23    ,bitbucket_secret  VARCHAR(255)
    24  );
    25  `
    26  
    27  // SQL statement to create the Team Table.
    28  var teamTableStmt = `
    29  CREATE TABLE teams (
    30    id        INTEGER PRIMARY KEY AUTOINCREMENT
    31    ,slug     VARCHAR(255) UNIQUE
    32    ,name     VARCHAR(255)
    33    ,email    VARCHAR(255)
    34    ,gravatar VARCHAR(255)
    35    ,created  TIMESTAMP
    36    ,updated  TIMESTAMP
    37  );
    38  `
    39  
    40  // SQL statement to create the Member Table.
    41  var memberTableStmt = `
    42  CREATE TABLE members (
    43     id      INTEGER PRIMARY KEY AUTOINCREMENT
    44    ,team_id INTEGER
    45    ,user_id INTEGER
    46    ,role    INTEGER
    47  );
    48  `
    49  
    50  // SQL statement to create the Repo Table.
    51  var repoTableStmt = `
    52  CREATE TABLE repos (
    53     id          INTEGER PRIMARY KEY AUTOINCREMENT
    54    ,slug        VARCHAR(1024) UNIQUE
    55    ,host        VARCHAR(255)
    56    ,owner       VARCHAR(255)
    57    ,name        VARCHAR(255)
    58    ,private     BOOLEAN
    59    ,disabled    BOOLEAN
    60    ,disabled_pr BOOLEAN
    61    ,priveleged  BOOLEAN
    62    ,timeout     INTEGER
    63    ,scm         VARCHAR(25)
    64    ,url         VARCHAR(1024)
    65    ,username    VARCHAR(255)
    66    ,password    VARCHAR(255)
    67    ,public_key  VARCHAR(1024)
    68    ,private_key VARCHAR(1024)
    69    ,params      VARCHAR(2000)
    70    ,created     TIMESTAMP
    71    ,updated     TIMESTAMP
    72    ,user_id     INTEGER
    73    ,team_id     INTEGER
    74  );
    75  `
    76  
    77  // SQL statement to create the Commit Table.
    78  var commitTableStmt = `
    79  CREATE TABLE commits (
    80     id           INTEGER PRIMARY KEY AUTOINCREMENT
    81    ,repo_id      INTEGER
    82    ,status       VARCHAR(255)
    83    ,started      TIMESTAMP
    84    ,finished     TIMESTAMP
    85    ,duration     INTEGER
    86    ,attempts     INTEGER
    87    ,hash         VARCHAR(255)
    88    ,branch       VARCHAR(255)
    89    ,pull_request VARCHAR(255)
    90    ,author       VARCHAR(255)
    91    ,gravatar     VARCHAR(255)
    92    ,timestamp    VARCHAR(255)
    93    ,message      VARCHAR(255)
    94    ,created      TIMESTAMP
    95    ,updated      TIMESTAMP
    96  );
    97  `
    98  
    99  // SQL statement to create the Build Table.
   100  var buildTableStmt = `
   101  CREATE TABLE builds (
   102     id        INTEGER PRIMARY KEY AUTOINCREMENT
   103    ,commit_id INTEGER
   104    ,slug      VARCHAR(255)
   105    ,status    VARCHAR(255)
   106    ,started   TIMESTAMP
   107    ,finished  TIMESTAMP
   108    ,duration  INTEGER
   109    ,created   TIMESTAMP
   110    ,updated   TIMESTAMP
   111    ,stdout    BLOB
   112  );
   113  `
   114  
   115  // SQL statement to create the Settings
   116  var settingsTableStmt = `
   117  CREATE TABLE settings (
   118     id               INTEGER PRIMARY KEY
   119    ,github_key       VARCHAR(255)
   120    ,github_secret    VARCHAR(255)
   121    ,bitbucket_key    VARCHAR(255)
   122    ,bitbucket_secret VARCHAR(255)
   123    ,smtp_server      VARCHAR(1024)
   124    ,smtp_port        VARCHAR(5)
   125    ,smtp_address     VARCHAR(1024)
   126    ,smtp_username    VARCHAR(1024)
   127    ,smtp_password    VARCHAR(1024)
   128    ,hostname         VARCHAR(1024)
   129    ,scheme           VARCHAR(5)
   130    ,open_invitations BOOLEAN
   131  );
   132  `
   133  
   134  var memberUniqueIndex = `
   135  CREATE UNIQUE INDEX member_uix ON members (team_id, user_id);
   136  `
   137  
   138  var memberTeamIndex = `
   139  CREATE INDEX member_team_ix ON members (team_id);
   140  `
   141  
   142  var memberUserIndex = `
   143  CREATE INDEX member_user_ix ON members (user_id);
   144  `
   145  
   146  var commitUniqueIndex = `
   147  CREATE UNIQUE INDEX commits_uix ON commits  (repo_id, hash, branch);
   148  `
   149  
   150  var commitRepoIndex = `
   151  CREATE INDEX commits_repo_ix ON commits (repo_id);
   152  `
   153  
   154  var commitBranchIndex = `
   155  CREATE INDEX commits_repo_ix ON commits (repo_id, branch);
   156  `
   157  
   158  var repoTeamIndex = `
   159  CREATE INDEX repo_team_ix ON repos (team_id);
   160  `
   161  
   162  var repoUserIndex = `
   163  CREATE INDEX repo_user_ix ON repos (user_id);
   164  `
   165  
   166  var buildCommitIndex = `
   167  CREATE INDEX builds_commit_ix ON builds (commit_id);
   168  `
   169  
   170  var buildSlugIndex = `
   171  CREATE INDEX builds_commit_slug_ix ON builds (commit_id, slug);
   172  `
   173  
   174  // Load will apply the DDL commands to
   175  // the provided database.
   176  func Load(db *sql.DB) error {
   177  
   178  	// created tables
   179  	db.Exec(userTableStmt)
   180  	db.Exec(teamTableStmt)
   181  	db.Exec(memberTableStmt)
   182  	db.Exec(repoTableStmt)
   183  	db.Exec(commitTableStmt)
   184  	db.Exec(buildTableStmt)
   185  	db.Exec(settingsTableStmt)
   186  
   187  	db.Exec(memberUniqueIndex)
   188  	db.Exec(memberTeamIndex)
   189  	db.Exec(memberUserIndex)
   190  	db.Exec(commitUniqueIndex)
   191  	db.Exec(commitRepoIndex)
   192  	db.Exec(commitBranchIndex)
   193  	db.Exec(repoTeamIndex)
   194  	db.Exec(repoUserIndex)
   195  	db.Exec(buildCommitIndex)
   196  	db.Exec(buildSlugIndex)
   197  
   198  	// migrations for backward compatibility
   199  	db.Exec("ALTER TABLE settings ADD COLUMN open_invitations BOOLEAN")
   200  	db.Exec("UPDATE settings SET open_invitations=0 WHERE open_invitations IS NULL")
   201  
   202  	return nil
   203  }