Skip to content

Commit

Permalink
Require the SQLAlchemy URI on the database model (#8720)
Browse files Browse the repository at this point in the history
* Require the SQLAlchemy URI when creating a database

* Add migration to make dbs.sqlalchemy_uri not-nullable

* Fixes for black, isort, tests

* Alter migration to use current revision from master as downgrade target

* Update tests to support new db constraint

* black
  • Loading branch information
Will Barrett authored and mistercrunch committed Dec 11, 2019
1 parent a96eae4 commit ed54f6e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# 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.
"""add_not_null_to_dbs_sqlalchemy_url
Revision ID: 817e1c9b09d0
Revises: db4b49eb0782
Create Date: 2019-12-03 10:24:16.201580
"""
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "817e1c9b09d0"
down_revision = "89115a40e8ea"


def upgrade():
with op.batch_alter_table("dbs") as batch_op:
batch_op.alter_column(
"sqlalchemy_uri", existing_type=sa.VARCHAR(length=1024), nullable=False
)


def downgrade():
with op.batch_alter_table("dbs") as batch_op:
batch_op.alter_column(
"sqlalchemy_uri", existing_type=sa.VARCHAR(length=1024), nullable=True
)
2 changes: 1 addition & 1 deletion superset/models/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ class Database(
verbose_name = Column(String(250), unique=True)
# short unique name, used in permissions
database_name = Column(String(250), unique=True, nullable=False)
sqlalchemy_uri = Column(String(1024))
sqlalchemy_uri = Column(String(1024), nullable=False)
password = Column(EncryptedType(String(1024), config["SECRET_KEY"]))
cache_timeout = Column(Integer)
select_as_create_table_as = Column(Boolean, default=False)
Expand Down
1 change: 1 addition & 0 deletions tests/base_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ def create_fake_db(self):
cls=models.Database,
criteria={"database_name": database_name},
session=db.session,
sqlalchemy_uri="sqlite://test",
id=db_id,
extra=extra,
)
Expand Down
8 changes: 6 additions & 2 deletions tests/security_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,9 @@ def test_set_perm_druid_cluster(self):

def test_set_perm_database(self):
session = db.session
database = Database(database_name="tmp_database")
database = Database(
database_name="tmp_database", sqlalchemy_uri="sqlite://test"
)
session.add(database)

stored_db = (
Expand Down Expand Up @@ -346,7 +348,9 @@ def test_set_perm_database(self):

def test_set_perm_slice(self):
session = db.session
database = Database(database_name="tmp_database")
database = Database(
database_name="tmp_database", sqlalchemy_uri="sqlite://test"
)
table = SqlaTable(table_name="tmp_perm_table", database=database)
session.add(database)
session.add(table)
Expand Down

0 comments on commit ed54f6e

Please sign in to comment.