Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(import-datasources): Use "admin" user as default for importing datasources #27154

Merged
merged 7 commits into from
Feb 27, 2024
39 changes: 24 additions & 15 deletions superset/cli/importexport.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

from superset import security_manager
from superset.extensions import db
from superset.utils.core import override_user

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -170,26 +171,34 @@
"-p",
help="Path to a single ZIP file",
)
def import_datasources(path: str) -> None:
@click.option(
"--username",
"-u",
required=False,
default="admin",
help="Specify the user name to assign datasources to",
)
def import_datasources(path: str, username: Optional[str] = "admin") -> None:
"""Import datasources from ZIP file"""
# pylint: disable=import-outside-toplevel
from superset.commands.dataset.importers.dispatcher import ImportDatasetsCommand
from superset.commands.importers.v1.utils import get_contents_from_bundle

if is_zipfile(path):
with ZipFile(path) as bundle:
contents = get_contents_from_bundle(bundle)
else:
with open(path) as file:
contents = {path: file.read()}
try:
ImportDatasetsCommand(contents, overwrite=True).run()
except Exception: # pylint: disable=broad-except
logger.exception(
"There was an error when importing the dataset(s), please check the "
"exception traceback in the log"
)
sys.exit(1)
with override_user(user=security_manager.find_user(username=username)):
if is_zipfile(path):
with ZipFile(path) as bundle:
contents = get_contents_from_bundle(bundle)

Check warning on line 190 in superset/cli/importexport.py

View check run for this annotation

Codecov / codecov/patch

superset/cli/importexport.py#L187-L190

Added lines #L187 - L190 were not covered by tests
else:
with open(path) as file:
contents = {path: file.read()}
try:
ImportDatasetsCommand(contents, overwrite=True).run()
except Exception: # pylint: disable=broad-except
logger.exception(

Check warning on line 197 in superset/cli/importexport.py

View check run for this annotation

Codecov / codecov/patch

superset/cli/importexport.py#L192-L197

Added lines #L192 - L197 were not covered by tests
"There was an error when importing the dataset(s), please check the "
"exception traceback in the log"
)
sys.exit(1)

Check warning on line 201 in superset/cli/importexport.py

View check run for this annotation

Codecov / codecov/patch

superset/cli/importexport.py#L201

Added line #L201 was not covered by tests


@click.command()
Expand Down
Loading