From dfb2cc2fa6f00c8d0c05280923e62511363c63b4 Mon Sep 17 00:00:00 2001 From: Shiva Raisinghani Date: Tue, 2 Nov 2021 08:22:25 -0700 Subject: [PATCH 1/7] rename to schemas_allowed_for_file_upload in dbs.extra --- .../b92d69a6643c_rename_csv_to_file.py | 62 +++++++++++++++++-- 1 file changed, 58 insertions(+), 4 deletions(-) diff --git a/superset/migrations/versions/b92d69a6643c_rename_csv_to_file.py b/superset/migrations/versions/b92d69a6643c_rename_csv_to_file.py index 1f94445ff459a..96fbd38e1c16e 100644 --- a/superset/migrations/versions/b92d69a6643c_rename_csv_to_file.py +++ b/superset/migrations/versions/b92d69a6643c_rename_csv_to_file.py @@ -15,23 +15,54 @@ # specific language governing permissions and limitations # under the License. """rename_csv_to_file - Revision ID: b92d69a6643c Revises: 32646df09c64 Create Date: 2021-09-19 14:42:20.130368 - """ # revision identifiers, used by Alembic. revision = "b92d69a6643c" down_revision = "32646df09c64" -import sqlalchemy as sa +import json +import logging + from alembic import op +import sqlalchemy as sa +from sqlalchemy import Column, Integer, Text from sqlalchemy.engine.reflection import Inspector +from sqlalchemy.ext.declarative import declarative_base + +from superset import db + +Base = declarative_base() + + +class Database(Base): + + __tablename__ = "dbs" + id = Column(Integer, primary_key=True) + extra = Column(Text) def upgrade(): + bind = op.get_bind() + session = db.Session(bind=bind) + + for database in session.query(Database).all(): + try: + extra = json.loads(database.extra) + except json.decoder.JSONDecodeError as ex: + logging.warning(str(ex)) + continue + + if "schemas_allowed_for_csv_upload" in extra: + extra["schemas_allowed_for_file_upload"] = extra.pop( + "schemas_allowed_for_csv_upload" + ) + + database.extra = json.dumps(extra) + try: with op.batch_alter_table("dbs") as batch_op: batch_op.alter_column( @@ -43,7 +74,6 @@ def upgrade(): # In MySQL 8.0 renaming the column rename fails because it has # a constraint check; we can safely remove it in that case, see # https://github.com/sqlalchemy/alembic/issues/699 - bind = op.get_bind() inspector = Inspector.from_engine(bind) check_constraints = inspector.get_check_constraints("dbs") for check_constraint in check_constraints: @@ -59,11 +89,35 @@ def upgrade(): existing_type=sa.Boolean(), ) + session.commit() + session.close() + def downgrade(): + bind = op.get_bind() + session = db.Session(bind=bind) + + for database in session.query(Database).all(): + try: + extra = json.loads(database.extra) + except json.decoder.JSONDecodeError as ex: + logging.warning(str(ex)) + continue + + if "schemas_allowed_for_file_upload" in extra: + extra["schemas_allowed_for_csv_upload"] = extra.pop( + "schemas_allowed_for_file_upload" + ) + + database.extra = json.dumps(extra) + + with op.batch_alter_table("dbs") as batch_op: batch_op.alter_column( "allow_file_upload", new_column_name="allow_csv_upload", existing_type=sa.Boolean(), ) + + session.commit() + session.close() From 949799fa3fd68093e2116ffc9181118514bc71f9 Mon Sep 17 00:00:00 2001 From: shiva raisinghani Date: Tue, 2 Nov 2021 08:33:04 -0700 Subject: [PATCH 2/7] black --- superset/migrations/versions/b92d69a6643c_rename_csv_to_file.py | 1 - 1 file changed, 1 deletion(-) diff --git a/superset/migrations/versions/b92d69a6643c_rename_csv_to_file.py b/superset/migrations/versions/b92d69a6643c_rename_csv_to_file.py index 96fbd38e1c16e..7e00a7ae51af4 100644 --- a/superset/migrations/versions/b92d69a6643c_rename_csv_to_file.py +++ b/superset/migrations/versions/b92d69a6643c_rename_csv_to_file.py @@ -111,7 +111,6 @@ def downgrade(): database.extra = json.dumps(extra) - with op.batch_alter_table("dbs") as batch_op: batch_op.alter_column( "allow_file_upload", From 7e765748398a463691beff805048c19c5991c78e Mon Sep 17 00:00:00 2001 From: shiva raisinghani Date: Tue, 2 Nov 2021 08:40:35 -0700 Subject: [PATCH 3/7] I should really setup pre-commit hooks --- superset/migrations/versions/b92d69a6643c_rename_csv_to_file.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/superset/migrations/versions/b92d69a6643c_rename_csv_to_file.py b/superset/migrations/versions/b92d69a6643c_rename_csv_to_file.py index 7e00a7ae51af4..944b503b0d020 100644 --- a/superset/migrations/versions/b92d69a6643c_rename_csv_to_file.py +++ b/superset/migrations/versions/b92d69a6643c_rename_csv_to_file.py @@ -27,8 +27,8 @@ import json import logging -from alembic import op import sqlalchemy as sa +from alembic import op from sqlalchemy import Column, Integer, Text from sqlalchemy.engine.reflection import Inspector from sqlalchemy.ext.declarative import declarative_base From c605af3d875f2d363c76e9da3bb830111c4f2705 Mon Sep 17 00:00:00 2001 From: Shiva Raisinghani Date: Tue, 2 Nov 2021 09:17:07 -0700 Subject: [PATCH 4/7] Apply suggestions Co-authored-by: John Bodley <4567245+john-bodley@users.noreply.github.com> --- .../migrations/versions/b92d69a6643c_rename_csv_to_file.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/superset/migrations/versions/b92d69a6643c_rename_csv_to_file.py b/superset/migrations/versions/b92d69a6643c_rename_csv_to_file.py index 944b503b0d020..2e7d124ef7229 100644 --- a/superset/migrations/versions/b92d69a6643c_rename_csv_to_file.py +++ b/superset/migrations/versions/b92d69a6643c_rename_csv_to_file.py @@ -61,7 +61,7 @@ def upgrade(): "schemas_allowed_for_csv_upload" ) - database.extra = json.dumps(extra) + database.extra = json.dumps(extra) try: with op.batch_alter_table("dbs") as batch_op: @@ -109,7 +109,7 @@ def downgrade(): "schemas_allowed_for_file_upload" ) - database.extra = json.dumps(extra) + database.extra = json.dumps(extra) with op.batch_alter_table("dbs") as batch_op: batch_op.alter_column( From 4964992f6f52641482e31cea10a36497c29f6e63 Mon Sep 17 00:00:00 2001 From: shiva raisinghani Date: Thu, 11 Nov 2021 04:24:53 -0800 Subject: [PATCH 5/7] move changes to a seperate migration --- ...acd_rename_to_schemas_allowed_for_file_.py | 89 +++++++++++++++++++ .../b92d69a6643c_rename_csv_to_file.py | 57 +----------- 2 files changed, 90 insertions(+), 56 deletions(-) create mode 100644 superset/migrations/versions/0ca9e5f1dacd_rename_to_schemas_allowed_for_file_.py diff --git a/superset/migrations/versions/0ca9e5f1dacd_rename_to_schemas_allowed_for_file_.py b/superset/migrations/versions/0ca9e5f1dacd_rename_to_schemas_allowed_for_file_.py new file mode 100644 index 0000000000000..6c41cfd282c4c --- /dev/null +++ b/superset/migrations/versions/0ca9e5f1dacd_rename_to_schemas_allowed_for_file_.py @@ -0,0 +1,89 @@ +# 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. +"""rename to schemas_allowed_for_file_upload in dbs.extra + +Revision ID: 0ca9e5f1dacd +Revises: b92d69a6643c +Create Date: 2021-11-11 04:18:26.171851 + +""" + +# revision identifiers, used by Alembic. +revision = '0ca9e5f1dacd' +down_revision = 'b92d69a6643c' + +import json +import logging + +from alembic import op +from sqlalchemy import Column, Integer, Text +from sqlalchemy.ext.declarative import declarative_base + +from superset import db + +Base = declarative_base() + + +class Database(Base): + + __tablename__ = "dbs" + id = Column(Integer, primary_key=True) + extra = Column(Text) + + +def upgrade(): + bind = op.get_bind() + session = db.Session(bind=bind) + + for database in session.query(Database).all(): + try: + extra = json.loads(database.extra) + except json.decoder.JSONDecodeError as ex: + logging.warning(str(ex)) + continue + + if "schemas_allowed_for_csv_upload" in extra: + extra["schemas_allowed_for_file_upload"] = extra.pop( + "schemas_allowed_for_csv_upload" + ) + + database.extra = json.dumps(extra) + + session.commit() + session.close() + + +def downgrade(): + bind = op.get_bind() + session = db.Session(bind=bind) + + for database in session.query(Database).all(): + try: + extra = json.loads(database.extra) + except json.decoder.JSONDecodeError as ex: + logging.warning(str(ex)) + continue + + if "schemas_allowed_for_file_upload" in extra: + extra["schemas_allowed_for_csv_upload"] = extra.pop( + "schemas_allowed_for_file_upload" + ) + + database.extra = json.dumps(extra) + + session.commit() + session.close() diff --git a/superset/migrations/versions/b92d69a6643c_rename_csv_to_file.py b/superset/migrations/versions/b92d69a6643c_rename_csv_to_file.py index 2e7d124ef7229..980d40ac6fdb9 100644 --- a/superset/migrations/versions/b92d69a6643c_rename_csv_to_file.py +++ b/superset/migrations/versions/b92d69a6643c_rename_csv_to_file.py @@ -24,45 +24,12 @@ revision = "b92d69a6643c" down_revision = "32646df09c64" -import json -import logging - import sqlalchemy as sa from alembic import op -from sqlalchemy import Column, Integer, Text from sqlalchemy.engine.reflection import Inspector -from sqlalchemy.ext.declarative import declarative_base - -from superset import db - -Base = declarative_base() - - -class Database(Base): - - __tablename__ = "dbs" - id = Column(Integer, primary_key=True) - extra = Column(Text) def upgrade(): - bind = op.get_bind() - session = db.Session(bind=bind) - - for database in session.query(Database).all(): - try: - extra = json.loads(database.extra) - except json.decoder.JSONDecodeError as ex: - logging.warning(str(ex)) - continue - - if "schemas_allowed_for_csv_upload" in extra: - extra["schemas_allowed_for_file_upload"] = extra.pop( - "schemas_allowed_for_csv_upload" - ) - - database.extra = json.dumps(extra) - try: with op.batch_alter_table("dbs") as batch_op: batch_op.alter_column( @@ -74,6 +41,7 @@ def upgrade(): # In MySQL 8.0 renaming the column rename fails because it has # a constraint check; we can safely remove it in that case, see # https://github.com/sqlalchemy/alembic/issues/699 + bind = op.get_bind() inspector = Inspector.from_engine(bind) check_constraints = inspector.get_check_constraints("dbs") for check_constraint in check_constraints: @@ -89,34 +57,11 @@ def upgrade(): existing_type=sa.Boolean(), ) - session.commit() - session.close() - def downgrade(): - bind = op.get_bind() - session = db.Session(bind=bind) - - for database in session.query(Database).all(): - try: - extra = json.loads(database.extra) - except json.decoder.JSONDecodeError as ex: - logging.warning(str(ex)) - continue - - if "schemas_allowed_for_file_upload" in extra: - extra["schemas_allowed_for_csv_upload"] = extra.pop( - "schemas_allowed_for_file_upload" - ) - - database.extra = json.dumps(extra) - with op.batch_alter_table("dbs") as batch_op: batch_op.alter_column( "allow_file_upload", new_column_name="allow_csv_upload", existing_type=sa.Boolean(), ) - - session.commit() - session.close() From 055b7631bc32e48b823edd17110579f174ab0b5a Mon Sep 17 00:00:00 2001 From: Shiva Raisinghani Date: Thu, 11 Nov 2021 04:28:33 -0800 Subject: [PATCH 6/7] fix spaces --- superset/migrations/versions/b92d69a6643c_rename_csv_to_file.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/superset/migrations/versions/b92d69a6643c_rename_csv_to_file.py b/superset/migrations/versions/b92d69a6643c_rename_csv_to_file.py index 980d40ac6fdb9..1f94445ff459a 100644 --- a/superset/migrations/versions/b92d69a6643c_rename_csv_to_file.py +++ b/superset/migrations/versions/b92d69a6643c_rename_csv_to_file.py @@ -15,9 +15,11 @@ # specific language governing permissions and limitations # under the License. """rename_csv_to_file + Revision ID: b92d69a6643c Revises: 32646df09c64 Create Date: 2021-09-19 14:42:20.130368 + """ # revision identifiers, used by Alembic. From 0f375da0c1eeda5f78d90004d7fac2138d297eac Mon Sep 17 00:00:00 2001 From: shiva raisinghani Date: Thu, 11 Nov 2021 04:30:23 -0800 Subject: [PATCH 7/7] black --- .../0ca9e5f1dacd_rename_to_schemas_allowed_for_file_.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/superset/migrations/versions/0ca9e5f1dacd_rename_to_schemas_allowed_for_file_.py b/superset/migrations/versions/0ca9e5f1dacd_rename_to_schemas_allowed_for_file_.py index 6c41cfd282c4c..5a2a4b94f4208 100644 --- a/superset/migrations/versions/0ca9e5f1dacd_rename_to_schemas_allowed_for_file_.py +++ b/superset/migrations/versions/0ca9e5f1dacd_rename_to_schemas_allowed_for_file_.py @@ -23,8 +23,8 @@ """ # revision identifiers, used by Alembic. -revision = '0ca9e5f1dacd' -down_revision = 'b92d69a6643c' +revision = "0ca9e5f1dacd" +down_revision = "b92d69a6643c" import json import logging