aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2018-11-09 02:39:27 +0100
committerTeddy Wing2018-11-09 02:39:27 +0100
commitd3fa3ce3712e6a46b1e9d1a2f3c8ac0ca6d5e25b (patch)
treeedb9a4361a55b574f76665c352a0d1d5d2ebb775
parent99092d0cddd5f6cf8a5294a666999d885e430ba3 (diff)
downloaddome-key-web-d3fa3ce3712e6a46b1e9d1a2f3c8ac0ca6d5e25b.tar.bz2
Add migrations for `purchasers` table
A database table to hold purchaser information so we can re-generate licenses in case purchasers lose their license keys. Needed to use a trigger to update the `updated_at` field on `UPDATE` as you can't do `ON UPDATE UTC_TIMESTAMP()` in the column definition > You can not specify UTC_TIMESTAMP as default to specify automatic > properties (https://dba.stackexchange.com/questions/20217/mysql-set-utc-time-as-default-timestamp/21619#21619) Weirdly, the trigger isn't working when applying the migration with $ migrant apply but it is working when running $ mysql -u user dome_key < migrations/20181108234653_create-purchasers/up.sql Not sure what's going on there, but 'migrant' appears to have trouble realising there are errors coming back from MySQL and executes the migrations regardless. It also doesn't print syntax error messages from MySQL which is very inconvenient. Migrant seemed to be the most advanced migration CLI on crates.io, and I was hoping to use a Rust program for this, but for simplicity, I'm thinking I'll have to go with a different migration runner. Considering https://github.com/golang-migrate/migrate.
-rw-r--r--license-generator/migrations/20181108234653_create-purchasers/down.sql6
-rw-r--r--license-generator/migrations/20181108234653_create-purchasers/up.sql21
2 files changed, 27 insertions, 0 deletions
diff --git a/license-generator/migrations/20181108234653_create-purchasers/down.sql b/license-generator/migrations/20181108234653_create-purchasers/down.sql
new file mode 100644
index 0000000..d17db9c
--- /dev/null
+++ b/license-generator/migrations/20181108234653_create-purchasers/down.sql
@@ -0,0 +1,6 @@
+BEGIN;
+
+DROP TRIGGER purchasers_updated_at;
+DROP TABLE purchasers;
+
+COMMIT;
diff --git a/license-generator/migrations/20181108234653_create-purchasers/up.sql b/license-generator/migrations/20181108234653_create-purchasers/up.sql
new file mode 100644
index 0000000..14927c2
--- /dev/null
+++ b/license-generator/migrations/20181108234653_create-purchasers/up.sql
@@ -0,0 +1,21 @@
+BEGIN;
+
+CREATE TABLE purchasers (
+ id INT AUTO_INCREMENT PRIMARY KEY,
+ name VARCHAR(255) NOT NULL,
+ email VARCHAR(255) NOT NULL,
+ secret VARCHAR(255),
+ created_at DATETIME NOT NULL DEFAULT UTC_TIMESTAMP(),
+ updated_at DATETIME NOT NULL DEFAULT UTC_TIMESTAMP()
+);
+
+DELIMITER $$
+ CREATE TRIGGER purchasers_updated_at
+ BEFORE UPDATE
+ ON purchasers FOR EACH ROW
+ BEGIN
+ SET NEW.updated_at = UTC_TIMESTAMP();
+ END$$
+DELIMITER ;
+
+COMMIT;