| 130 | * Create "votes" table in the Cloud SQL database |
| 131 | */ |
| 132 | const createTable = async () => { |
| 133 | if (!knex) knex = await connect(); |
| 134 | const exists = await knex.schema.hasTable(TABLE); |
| 135 | if (!exists) { |
| 136 | try { |
| 137 | await knex.schema.createTable(TABLE, table => { |
| 138 | table.bigIncrements('vote_id').notNull(); |
| 139 | table.timestamp('time_cast').notNull(); |
| 140 | table.specificType('candidate', 'CHAR(6) NOT NULL'); |
| 141 | table.specificType('uid', 'VARCHAR(128) NOT NULL'); |
| 142 | }); |
| 143 | logger.info(`Successfully created ${TABLE} table.`); |
| 144 | } catch (err) { |
| 145 | const message = `Failed to create ${TABLE} table: ${err}`; |
| 146 | logger.error(message); |
| 147 | } |
| 148 | } |
| 149 | }; |
| 150 | |
| 151 | const closeConnection = () => { |
| 152 | if (!knex) knex.destroy(); |