WIP: migrations
This commit is contained in:
@@ -23,6 +23,7 @@ class DB {
|
||||
protected DBSettings m_settings;
|
||||
protected DataSource m_ds;
|
||||
protected Connection m_conn;
|
||||
protected Statement m_stmt = null;
|
||||
|
||||
this(DBSettings settings) {
|
||||
this.m_settings = settings;
|
||||
@@ -46,5 +47,42 @@ class DB {
|
||||
void connect() {
|
||||
this.m_ds = this.getDataSource();
|
||||
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
|
||||
)`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user