WIP: migrations
This commit is contained in:
@@ -23,6 +23,7 @@ class DB {
|
|||||||
protected DBSettings m_settings;
|
protected DBSettings m_settings;
|
||||||
protected DataSource m_ds;
|
protected DataSource m_ds;
|
||||||
protected Connection m_conn;
|
protected Connection m_conn;
|
||||||
|
protected Statement m_stmt = null;
|
||||||
|
|
||||||
this(DBSettings settings) {
|
this(DBSettings settings) {
|
||||||
this.m_settings = settings;
|
this.m_settings = settings;
|
||||||
@@ -46,5 +47,42 @@ class DB {
|
|||||||
void connect() {
|
void connect() {
|
||||||
this.m_ds = this.getDataSource();
|
this.m_ds = this.getDataSource();
|
||||||
this.m_conn = this.m_ds.getConnection();
|
this.m_conn = this.m_ds.getConnection();
|
||||||
|
this.m_conn.setAutoCommit(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
int runUpdate(string statement) {
|
||||||
|
if (this.m_stmt is null)
|
||||||
|
this.m_stmt = this.m_conn.createStatement();
|
||||||
|
|
||||||
|
debugF!"Running update: %s"(statement);
|
||||||
|
|
||||||
|
return this.m_stmt.executeUpdate(statement);
|
||||||
|
}
|
||||||
|
|
||||||
|
ResultSet runQuery(string statement) {
|
||||||
|
if (this.m_stmt is null)
|
||||||
|
this.m_stmt = this.m_conn.createStatement();
|
||||||
|
|
||||||
|
debugF!"Running query: %s"(statement);
|
||||||
|
|
||||||
|
return this.m_stmt.executeQuery(statement);
|
||||||
|
}
|
||||||
|
|
||||||
|
void runMigrations() {
|
||||||
|
ResultSet rs = this.runQuery(
|
||||||
|
`select * from information_schema.tables where table_name = 'migrations';`,
|
||||||
|
);
|
||||||
|
|
||||||
|
bool exists = rs.next();
|
||||||
|
int ret = 0;
|
||||||
|
|
||||||
|
if (exists == false) {
|
||||||
|
ret |= this.runUpdate(
|
||||||
|
`create table migrations(
|
||||||
|
id serial,
|
||||||
|
name character varying
|
||||||
|
)`,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
25
source/db/migrations/m_001_initial.d
Normal file
25
source/db/migrations/m_001_initial.d
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
module db.migrations.m_001_initial;
|
||||||
|
|
||||||
|
import db.migrations.migration;
|
||||||
|
|
||||||
|
Migration Initial = Migration(
|
||||||
|
[
|
||||||
|
`create table users(
|
||||||
|
id uuid not null,
|
||||||
|
name character varying(255),
|
||||||
|
handle character varying(255),
|
||||||
|
local boolean not null,
|
||||||
|
ap_id character varying(255) not null,
|
||||||
|
ap_privkey text,
|
||||||
|
ap_pubkey text,
|
||||||
|
ap_shared_inbox text,
|
||||||
|
ap_inbox text,
|
||||||
|
ap_outbox text,
|
||||||
|
ap_followers_addr text,
|
||||||
|
ap_following_addr text
|
||||||
|
);`
|
||||||
|
],
|
||||||
|
[
|
||||||
|
`drop table users;`
|
||||||
|
],
|
||||||
|
);
|
||||||
31
source/db/migrations/migration.d
Normal file
31
source/db/migrations/migration.d
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
module db.migrations.migration;
|
||||||
|
|
||||||
|
import singletons;
|
||||||
|
import db.migrations.m_001_initial;
|
||||||
|
|
||||||
|
struct Migration {
|
||||||
|
string[] upStatements;
|
||||||
|
string[] downStatements;
|
||||||
|
|
||||||
|
bool up() {
|
||||||
|
int result;
|
||||||
|
|
||||||
|
foreach (string statement; upStatements)
|
||||||
|
result |= Db.runUpdate(statement);
|
||||||
|
|
||||||
|
return result == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool down() {
|
||||||
|
int result;
|
||||||
|
|
||||||
|
foreach (string statement; downStatements)
|
||||||
|
result |= Db.runUpdate(statement);
|
||||||
|
|
||||||
|
return result == 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Migration[string] migrations = [
|
||||||
|
"001_initial": Initial,
|
||||||
|
];
|
||||||
17
source/db/types.d
Normal file
17
source/db/types.d
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
module db.types;
|
||||||
|
|
||||||
|
class User {
|
||||||
|
string id;
|
||||||
|
string name;
|
||||||
|
string handle;
|
||||||
|
bool local;
|
||||||
|
|
||||||
|
string ap_id;
|
||||||
|
string ap_privkey;
|
||||||
|
string ap_pubkey;
|
||||||
|
string ap_shared_inbox;
|
||||||
|
string ap_inbox;
|
||||||
|
string ap_outbox;
|
||||||
|
string ap_followers_addr;
|
||||||
|
string ap_following_addr;
|
||||||
|
}
|
||||||
@@ -40,6 +40,7 @@ int main() {
|
|||||||
|
|
||||||
Db = DB.getDB(dbSettings);
|
Db = DB.getDB(dbSettings);
|
||||||
Db.connect();
|
Db.connect();
|
||||||
|
Db.runMigrations();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user