WIP: migrations

This commit is contained in:
2024-01-28 23:34:13 -03:00
parent fbf487051e
commit 1aa9f6992b
5 changed files with 112 additions and 0 deletions

View File

@@ -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
)`,
);
}
}
}