You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by hu...@apache.org on 2022/04/01 14:05:47 UTC

[superset] branch add-owners-new-dataset created (now c1a5e5a)

This is an automated email from the ASF dual-hosted git repository.

hugh pushed a change to branch add-owners-new-dataset
in repository https://gitbox.apache.org/repos/asf/superset.git.


      at c1a5e5a  working migration and need to fill in owners values

This branch includes the following new commits:

     new a0df608  create table for sl_dataset_users table
     new c1a5e5a  working migration and need to fill in owners values

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


[superset] 01/02: create table for sl_dataset_users table

Posted by hu...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

hugh pushed a commit to branch add-owners-new-dataset
in repository https://gitbox.apache.org/repos/asf/superset.git

commit a0df608e5d98dcf482fd9375cdd207ceace0fd3e
Author: hughhhh <hu...@gmail.com>
AuthorDate: Thu Mar 31 22:26:48 2022 -0400

    create table for sl_dataset_users table
---
 superset/datasets/models.py                        | 12 +++++
 .../89f17d951737_add_owners_dataset_model.py       | 51 ++++++++++++++++++++++
 2 files changed, 63 insertions(+)

diff --git a/superset/datasets/models.py b/superset/datasets/models.py
index 56a6fbf..bcd8add 100644
--- a/superset/datasets/models.py
+++ b/superset/datasets/models.py
@@ -30,6 +30,7 @@ import sqlalchemy as sa
 from flask_appbuilder import Model
 from sqlalchemy.orm import relationship
 
+from superset import security_manager
 from superset.columns.models import Column
 from superset.models.helpers import (
     AuditMixinNullable,
@@ -52,6 +53,13 @@ table_association_table = sa.Table(
     sa.Column("table_id", sa.ForeignKey("sl_tables.id")),
 )
 
+dataset_user = sa.Table(
+    "sl_dataset_users",
+    Model.metadata,
+    sa.Column("user_id", sa.ForeignKey("ab_user.id")),
+    sa.Column("dataset_id", sa.ForeignKey("sl_datasets.id")),
+)
+
 
 class Dataset(Model, AuditMixinNullable, ExtraJSONMixin, ImportExportMixin):
     """
@@ -90,3 +98,7 @@ class Dataset(Model, AuditMixinNullable, ExtraJSONMixin, ImportExportMixin):
     # Column is managed externally and should be read-only inside Superset
     is_managed_externally = sa.Column(sa.Boolean, nullable=False, default=False)
     external_url = sa.Column(sa.Text, nullable=True)
+
+    owners = relationship(
+        security_manager.user_model, secondary=dataset_user, backref="sl_datasets"
+    )
diff --git a/superset/migrations/versions/89f17d951737_add_owners_dataset_model.py b/superset/migrations/versions/89f17d951737_add_owners_dataset_model.py
new file mode 100644
index 0000000..5b83609
--- /dev/null
+++ b/superset/migrations/versions/89f17d951737_add_owners_dataset_model.py
@@ -0,0 +1,51 @@
+# 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_owners_dataset_model
+
+Revision ID: 89f17d951737
+Revises: 2ed890b36b94
+Create Date: 2022-03-31 22:22:43.831122
+
+"""
+
+# revision identifiers, used by Alembic.
+revision = "89f17d951737"
+down_revision = "2ed890b36b94"
+
+import sqlalchemy as sa
+from alembic import op
+from sqlalchemy.dialects import postgresql
+
+
+def upgrade():
+    op.create_table(
+        "sl_dataset_users",
+        sa.Column("user_id", sa.Integer(), nullable=True),
+        sa.Column("dataset_id", sa.Integer(), nullable=True),
+        sa.ForeignKeyConstraint(
+            ["dataset_id"],
+            ["sl_datasets.id"],
+        ),
+        sa.ForeignKeyConstraint(
+            ["user_id"],
+            ["ab_user.id"],
+        ),
+    )
+
+
+def downgrade():
+    op.drop_table("sl_dataset_users")

[superset] 02/02: working migration and need to fill in owners values

Posted by hu...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

hugh pushed a commit to branch add-owners-new-dataset
in repository https://gitbox.apache.org/repos/asf/superset.git

commit c1a5e5adb8e771d0481db4b5a4ea7dc0d602c449
Author: hughhhh <hu...@gmail.com>
AuthorDate: Thu Mar 31 23:06:36 2022 -0400

    working migration and need to fill in owners values
---
 .../89f17d951737_add_owners_dataset_model.py       | 67 ++++++++++++++++++++++
 1 file changed, 67 insertions(+)

diff --git a/superset/migrations/versions/89f17d951737_add_owners_dataset_model.py b/superset/migrations/versions/89f17d951737_add_owners_dataset_model.py
index 5b83609..fe2f38e 100644
--- a/superset/migrations/versions/89f17d951737_add_owners_dataset_model.py
+++ b/superset/migrations/versions/89f17d951737_add_owners_dataset_model.py
@@ -29,11 +29,63 @@ down_revision = "2ed890b36b94"
 import sqlalchemy as sa
 from alembic import op
 from sqlalchemy.dialects import postgresql
+from sqlalchemy.ext.declarative import declarative_base
+from sqlalchemy.orm import relationship
+
+from superset import db, security_manager
+
+Base = declarative_base()
+
+
+class User(Base):
+    """Declarative class to do query in upgrade"""
+
+    __tablename__ = "ab_user"
+    id = sa.Column(sa.Integer, primary_key=True)
+
+
+class SqlaTable(Base):
+    __tablename__ = "tables"
+
+    id = sa.Column(sa.Integer, primary_key=True)
+    owners = relationship(
+        "User",
+        secondary=sa.Table(
+            "sqlatable_user",
+            Base.metadata,
+            sa.Column("id", sa.Integer, primary_key=True),
+            sa.Column("user_id", sa.Integer, sa.ForeignKey("ab_user.id")),
+            sa.Column("table_id", sa.Integer, sa.ForeignKey("tables.id")),
+        ),
+        backref="tables",
+    )
+
+
+class Dataset(Base):
+    __tablename__ = "sl_datasets"
+
+    id = sa.Column(sa.Integer, primary_key=True)
+    sqlatable_id = sa.Column(sa.Integer, nullable=True, unique=True)
+    owners = relationship(
+        "User",
+        secondary=sa.Table(
+            "sl_dataset_users",
+            Base.metadata,
+            sa.Column("id", sa.Integer, primary_key=True),
+            sa.Column("user_id", sa.ForeignKey("ab_user.id")),
+            sa.Column("dataset_id", sa.ForeignKey("sl_datasets.id")),
+        ),
+        backref="sl_datasets",
+    )
 
 
 def upgrade():
+    bind = op.get_bind()
+    session = db.Session(bind=bind)
+
     op.create_table(
         "sl_dataset_users",
+        sa.Column("id", sa.Integer(), primary_key=True),
         sa.Column("user_id", sa.Integer(), nullable=True),
         sa.Column("dataset_id", sa.Integer(), nullable=True),
         sa.ForeignKeyConstraint(
@@ -46,6 +98,21 @@ def upgrade():
         ),
     )
 
+    for sqlatable in session.query(SqlaTable).all():
+        try:
+            ds = (
+                session.query(Dataset)
+                .filter(Dataset.sqlatable_id == sqlatable.id)
+                .one()
+            )
+        except sa.orm.exc.NoResultFound:
+            continue
+
+        ds.owners = sqlatable.owners
+
+    session.commit()
+    session.close()
+
 
 def downgrade():
     op.drop_table("sl_dataset_users")