dpgaspar commented on a change in pull request #10741: URL: https://github.com/apache/incubator-superset/pull/10741#discussion_r481487133
########## File path: superset/databases/commands/create.py ########## @@ -0,0 +1,84 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +import logging +from typing import Any, Dict, List, Optional + +from flask_appbuilder.models.sqla import Model +from flask_appbuilder.security.sqla.models import User +from marshmallow import ValidationError + +from superset.commands.base import BaseCommand +from superset.dao.exceptions import DAOCreateFailedError +from superset.databases.commands.exceptions import ( + DatabaseConnectionFailedError, + DatabaseCreateFailedError, + DatabaseExistsValidationError, + DatabaseInvalidError, + DatabaseRequiredFieldValidationError, +) +from superset.databases.dao import DatabaseDAO +from superset.extensions import db, security_manager + +logger = logging.getLogger(__name__) + + +class CreateDatabaseCommand(BaseCommand): + def __init__(self, user: User, data: Dict[str, Any]): + self._actor = user + self._properties = data.copy() + + def run(self) -> Model: + self.validate() + try: + database = DatabaseDAO.create(self._properties, commit=False) + database.set_sqlalchemy_uri(database.sqlalchemy_uri) + # adding a new database we always want to force refresh schema list + # TODO Improve this simplistic implementation for catching DB conn fails Review comment: Note: Since we need to fetch db schemas to create schema permissions, we need valid/working connections when creating a database, made a simplistic approach to this, since this PR was already big. @lilykuang we could move db test connection logic into a command ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
