migrateDB creates the votes table if it does not already exist.
(db *sql.DB)
| 48 | |
| 49 | // migrateDB creates the votes table if it does not already exist. |
| 50 | func migrateDB(db *sql.DB) error { |
| 51 | // Drop the votes table if it already exists. |
| 52 | if _, err := db.Exec("DROP TABLE IF EXISTS votes;"); err != nil { |
| 53 | log.Fatalf("DB.Exec: unable to drop votes table: %v", err) |
| 54 | } |
| 55 | // Create the votes table. |
| 56 | createVotes := `CREATE TABLE votes ( |
| 57 | id int IDENTITY(1,1) PRIMARY KEY, |
| 58 | created_at DATETIME NOT NULL, |
| 59 | candidate CHAR(6) NOT NULL |
| 60 | );` |
| 61 | _, err := db.Exec(createVotes) |
| 62 | return err |
| 63 | } |
| 64 | |
| 65 | // recentVotes returns the last five votes cast. |
| 66 | func recentVotes(db *sql.DB) ([]vote, error) { |