mirror of
https://github.com/morten-olsen/homelab-operator.git
synced 2026-02-08 01:36:28 +01:00
stuff
This commit is contained in:
@@ -8,7 +8,7 @@ type PostgresInstanceOptions = {
|
||||
services: Services;
|
||||
host: string;
|
||||
port?: number;
|
||||
username: string;
|
||||
user: string;
|
||||
password: string;
|
||||
database?: string;
|
||||
};
|
||||
@@ -20,10 +20,10 @@ class PostgresInstance {
|
||||
this.#db = knex({
|
||||
client: 'pg',
|
||||
connection: {
|
||||
host: process.env.FORCE_PG_HOST ?? options.host,
|
||||
user: process.env.FORCE_PG_USER ?? options.username,
|
||||
password: process.env.FORCE_PG_PASSWORD ?? options.password,
|
||||
port: process.env.FORCE_PG_PORT ? parseInt(process.env.FORCE_PG_PORT) : options.port,
|
||||
host: options.host,
|
||||
user: options.user,
|
||||
password: options.password,
|
||||
port: options.port,
|
||||
database: options.database,
|
||||
},
|
||||
});
|
||||
@@ -32,29 +32,32 @@ class PostgresInstance {
|
||||
public ping = async () => {
|
||||
try {
|
||||
await this.#db.raw('SELECT 1');
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
return;
|
||||
} catch (err) {
|
||||
return err;
|
||||
}
|
||||
};
|
||||
|
||||
public upsertRole = async (role: PostgresRole) => {
|
||||
const existingRole = await this.#db.raw('SELECT 1 FROM pg_roles WHERE rolname = ?', [role.name]);
|
||||
const name = role.name;
|
||||
const existingRole = await this.#db.raw('SELECT 1 FROM pg_roles WHERE rolname = ?', [name]);
|
||||
|
||||
if (existingRole.rows.length === 0) {
|
||||
await this.#db.raw(`CREATE ROLE "${role.name}" WITH LOGIN PASSWORD '${role.password}'`);
|
||||
await this.#db.raw(`CREATE ROLE "${name}" WITH LOGIN PASSWORD '${role.password}'`);
|
||||
} else {
|
||||
await this.#db.raw(`ALTER ROLE "${role.name}" WITH PASSWORD '${role.password}'`);
|
||||
await this.#db.raw(`ALTER ROLE "${name}" WITH PASSWORD '${role.password}'`);
|
||||
}
|
||||
};
|
||||
|
||||
public upsertDatabase = async (database: PostgresDatabase) => {
|
||||
const existingDatabase = await this.#db.raw('SELECT * FROM pg_database WHERE datname = ?', [database.name]);
|
||||
const owner = database.owner;
|
||||
const name = database.name;
|
||||
const existingDatabase = await this.#db.raw('SELECT * FROM pg_database WHERE datname = ?', [name]);
|
||||
|
||||
if (existingDatabase.rows.length === 0) {
|
||||
await this.#db.raw(`CREATE DATABASE "${database.name}" OWNER "${database.owner}"`);
|
||||
await this.#db.raw(`CREATE DATABASE "${name}" OWNER "${owner}"`);
|
||||
} else {
|
||||
await this.#db.raw(`ALTER DATABASE "${database.name}" OWNER TO "${database.owner}"`);
|
||||
await this.#db.raw(`ALTER DATABASE "${name}" OWNER TO "${owner}"`);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user