87 lines
2.0 KiB
Python
87 lines
2.0 KiB
Python
"""PostgreSQL database command construction."""
|
|
|
|
from core.database import DatabaseAdapter
|
|
|
|
|
|
class PostgreSQLAdapter(DatabaseAdapter):
|
|
|
|
backup_extension = ".dump"
|
|
|
|
def client_command(
|
|
self,
|
|
executable,
|
|
interactive=False,
|
|
):
|
|
|
|
return (
|
|
self.docker_exec(interactive=interactive)
|
|
+ [
|
|
"env",
|
|
"PGPASSFILE=/run/secrets/absdev-pgpass",
|
|
executable,
|
|
"--no-password",
|
|
f"--host={self.database['host']}",
|
|
f"--port={self.database['port']}",
|
|
f"--username={self.database['username']}",
|
|
]
|
|
)
|
|
|
|
def query_command(self, query):
|
|
|
|
return (
|
|
self.client_command("psql")
|
|
+ [
|
|
"--no-psqlrc",
|
|
"--tuples-only",
|
|
"--no-align",
|
|
"--set=ON_ERROR_STOP=1",
|
|
f"--dbname={self.database['name']}",
|
|
"--command",
|
|
query,
|
|
]
|
|
)
|
|
|
|
def ping_command(self):
|
|
|
|
return self.query_command("SELECT 1")
|
|
|
|
def version_command(self):
|
|
|
|
return self.query_command("SHOW server_version")
|
|
|
|
def identity_command(self):
|
|
|
|
return self.query_command(
|
|
"SELECT current_database(), current_user"
|
|
)
|
|
|
|
def backup_command(self):
|
|
|
|
return (
|
|
self.client_command("pg_dump")
|
|
+ [
|
|
"--format=custom",
|
|
"--no-owner",
|
|
"--no-privileges",
|
|
f"--dbname={self.database['name']}",
|
|
]
|
|
)
|
|
|
|
def restore_command(self, target):
|
|
|
|
target = self.validate_restore_target(target)
|
|
|
|
return (
|
|
self.client_command(
|
|
"pg_restore",
|
|
interactive=True,
|
|
)
|
|
+ [
|
|
"--exit-on-error",
|
|
"--no-owner",
|
|
"--no-privileges",
|
|
"--single-transaction",
|
|
f"--dbname={target}",
|
|
]
|
|
)
|