"""MySQL database command construction.""" from core.database import DatabaseAdapter class MySQLAdapter(DatabaseAdapter): backup_extension = ".sql" client_configuration = ( "--defaults-file=/run/secrets/absdev-client.cnf" ) def connection_arguments(self): return [ "--protocol=TCP", f"--host={self.database['host']}", f"--port={self.database['port']}", f"--user={self.database['username']}", ] def query_command(self, query): return ( self.docker_exec() + [ "mysql", self.client_configuration, ] + self.connection_arguments() + [ "--batch", "--skip-column-names", f"--database={self.database['name']}", "--execute", query, ] ) def ping_command(self): return self.query_command("SELECT 1") def version_command(self): return self.query_command("SELECT VERSION()") def identity_command(self): return self.query_command( "SELECT DATABASE(), CURRENT_USER(), @@server_uuid" ) def backup_command(self): return ( self.docker_exec() + [ "mysqldump", self.client_configuration, ] + self.connection_arguments() + [ "--single-transaction", "--quick", "--no-tablespaces", "--skip-lock-tables", "--set-gtid-purged=OFF", "--hex-blob", "--triggers", "--routines", "--events", self.database["name"], ] ) def restore_command(self, target): target = self.validate_restore_target(target) return ( self.docker_exec(interactive=True) + [ "mysql", self.client_configuration, ] + self.connection_arguments() + [ f"--database={target}", ] )