Initial ABSDEV deployment framework development preview
ABSDEV Framework CI / framework-validation (push) Canceled after 0s
ABSDEV Framework CI / framework-validation (push) Canceled after 0s
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
"""Database adapter interface and validated adapter factory."""
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from types import MappingProxyType
|
||||
import re
|
||||
|
||||
from core.configuration import (
|
||||
ConfigurationError,
|
||||
validate_config,
|
||||
)
|
||||
|
||||
|
||||
class DatabaseError(RuntimeError):
|
||||
"""Invalid or unsafe database operation."""
|
||||
|
||||
|
||||
class DatabaseAdapter(ABC):
|
||||
|
||||
def __init__(self, config):
|
||||
|
||||
try:
|
||||
|
||||
validated = validate_config(config)
|
||||
|
||||
except ConfigurationError as error:
|
||||
|
||||
raise DatabaseError(
|
||||
"Invalid database configuration"
|
||||
) from error
|
||||
|
||||
self.database = MappingProxyType(
|
||||
validated["database"]
|
||||
)
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def backup_extension(self):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def ping_command(self):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def version_command(self):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def identity_command(self):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def backup_command(self):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def restore_command(self, target):
|
||||
pass
|
||||
|
||||
def docker_exec(self, interactive=False):
|
||||
|
||||
command = ["docker", "exec"]
|
||||
|
||||
if interactive:
|
||||
command.append("-i")
|
||||
|
||||
command.append(
|
||||
self.database["container"]
|
||||
)
|
||||
|
||||
return command
|
||||
|
||||
def validate_restore_target(self, target):
|
||||
|
||||
if (
|
||||
not isinstance(target, str)
|
||||
or re.fullmatch(
|
||||
r"isolated_restore_[a-z0-9_]{1,45}",
|
||||
target,
|
||||
) is None
|
||||
):
|
||||
|
||||
raise DatabaseError(
|
||||
"Only isolated restoration targets are permitted"
|
||||
)
|
||||
|
||||
if target.lower() == self.database["name"].lower():
|
||||
|
||||
raise DatabaseError(
|
||||
"Restoration into the configured database is prohibited"
|
||||
)
|
||||
|
||||
return target
|
||||
|
||||
|
||||
def get_adapter(config):
|
||||
|
||||
try:
|
||||
|
||||
validated = validate_config(config)
|
||||
|
||||
except ConfigurationError as error:
|
||||
|
||||
raise DatabaseError(
|
||||
"Invalid database configuration"
|
||||
) from error
|
||||
|
||||
driver = validated["database"]["driver"]
|
||||
|
||||
if driver == "mysql":
|
||||
|
||||
from adapters.mysql.adapter import MySQLAdapter
|
||||
|
||||
return MySQLAdapter(validated)
|
||||
|
||||
if driver == "pgsql":
|
||||
|
||||
from adapters.postgresql.adapter import PostgreSQLAdapter
|
||||
|
||||
return PostgreSQLAdapter(validated)
|
||||
|
||||
raise DatabaseError("Unsupported database driver")
|
||||
Reference in New Issue
Block a user