feat: create base class for export commands (#11463)

* Add UUID to saved_query

* Reuse function from previous migration

* Point to new head

* feat: add backend to export saved queries using new format

* Rename ImportMixin to ImportExportMixin

* Create base class for exports

* Add saved queries as well

* Add constant, small fixes

* Fix wrong import

* Fix lint
This commit is contained in:
Beto Dealmeida
2020-10-30 11:52:11 -07:00
committed by GitHub
parent ca40877640
commit fbcfaacda3
12 changed files with 199 additions and 147 deletions

View File

@@ -18,32 +18,30 @@
import json
import logging
from typing import Iterator, List, Tuple
from typing import Iterator, Tuple
import yaml
from superset.commands.base import BaseCommand
from superset.databases.commands.exceptions import DatabaseNotFoundError
from superset.databases.dao import DatabaseDAO
from superset.utils.dict_import_export import IMPORT_EXPORT_VERSION, sanitize
from superset.importexport.commands.base import ExportModelsCommand
from superset.models.core import Database
from superset.utils.dict_import_export import IMPORT_EXPORT_VERSION, sanitize
logger = logging.getLogger(__name__)
class ExportDatabasesCommand(BaseCommand):
def __init__(self, database_ids: List[int]):
self.database_ids = database_ids
class ExportDatabasesCommand(ExportModelsCommand):
# this will be set when calling validate()
self._models: List[Database] = []
dao = DatabaseDAO
not_found = DatabaseNotFoundError
@staticmethod
def export_database(database: Database) -> Iterator[Tuple[str, str]]:
database_slug = sanitize(database.database_name)
def export(model: Database) -> Iterator[Tuple[str, str]]:
database_slug = sanitize(model.database_name)
file_name = f"databases/{database_slug}.yaml"
payload = database.export_to_dict(
payload = model.export_to_dict(
recursive=False,
include_parent_ref=False,
include_defaults=True,
@@ -62,7 +60,7 @@ class ExportDatabasesCommand(BaseCommand):
file_content = yaml.safe_dump(payload, sort_keys=False)
yield file_name, file_content
for dataset in database.tables:
for dataset in model.tables:
dataset_slug = sanitize(dataset.table_name)
file_name = f"datasets/{database_slug}/{dataset_slug}.yaml"
@@ -73,18 +71,7 @@ class ExportDatabasesCommand(BaseCommand):
export_uuids=True,
)
payload["version"] = IMPORT_EXPORT_VERSION
payload["database_uuid"] = str(database.uuid)
payload["database_uuid"] = str(model.uuid)
file_content = yaml.safe_dump(payload, sort_keys=False)
yield file_name, file_content
def run(self) -> Iterator[Tuple[str, str]]:
self.validate()
for database in self._models:
yield from self.export_database(database)
def validate(self) -> None:
self._models = DatabaseDAO.find_by_ids(self.database_ids)
if len(self._models) != len(self.database_ids):
raise DatabaseNotFoundError()