1 module migrations.models; 2 3 import hunt.database; 4 import std.format; 5 6 // struct Version { 7 // ubyte id; 8 // uint version_; 9 // } 10 11 struct Migrations { 12 private string migrationsTable; 13 public Database db; 14 15 this(Database _db, string _migrationsTable = "migrations") { 16 db = _db; 17 migrationsTable = _migrationsTable; 18 } 19 20 void initDatabase() { 21 /* block */ { 22 immutable sql = format!"CREATE TABLE %s (id TINYINT UNSIGNED NOT NULL PRIMARY KEY, version INT UNSIGNED NOT NULL)"(migrationsTable); 23 db.execute(sql); 24 } 25 /* block */ { 26 immutable sql = format!"INSERT %s SET id=1, version=0"(migrationsTable); 27 db.execute(sql); 28 } 29 } 30 31 @property uint version_() { 32 immutable sql = format!"SELECT version FROM %s LIMIT 1"(migrationsTable); 33 return db.query(sql).iterator.front["version"].get!uint; 34 } 35 36 @property version_(uint value) { 37 immutable sql = format!"UPDATE %s SET version=%d"(migrationsTable, value); 38 db.execute(sql); 39 } 40 }