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: Users being able to update datasets across DBs #17348

Merged
merged 4 commits into from
Nov 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion superset-frontend/src/SqlLab/components/ResultSet/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ const ResultSetErrorMessage = styled.div`
`;

const updateDataset = async (
dbId: number,
datasetId: number,
sql: string,
columns: Array<Record<string, any>>,
Expand All @@ -159,6 +160,7 @@ const updateDataset = async (
sql,
columns,
owners,
database_id: dbId,
});

const data: JsonResponse = await SupersetClient.put({
Expand Down Expand Up @@ -272,10 +274,11 @@ export default class ResultSet extends React.PureComponent<
};

handleOverwriteDataset = async () => {
const { sql, results } = this.props.query;
const { sql, results, dbId } = this.props.query;
const { datasetToOverwrite } = this.state;

await updateDataset(
dbId,
datasetToOverwrite.datasetId,
sql,
results.selected_columns.map(d => ({ column_name: d.name })),
Expand Down
3 changes: 1 addition & 2 deletions superset/connectors/sqla/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1734,14 +1734,13 @@ def before_update(

for attr in ["database_id", "schema", "table_name"]:
history = state.get_history(attr, True)

if history.has_changes():
break
else:
return None

if not DatasetDAO.validate_uniqueness(
target.database_id, target.schema, target.table_name
target.database_id, target.schema, target.table_name, target.id
):
raise Exception(get_dataset_exist_error_msg(target.full_name))

Expand Down
12 changes: 11 additions & 1 deletion superset/datasets/dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,22 @@ def validate_table_exists(
return False

@staticmethod
def validate_uniqueness(database_id: int, schema: Optional[str], name: str) -> bool:
def validate_uniqueness(
database_id: int,
schema: Optional[str],
name: str,
dataset_id: Optional[int] = None,
) -> bool:
dataset_query = db.session.query(SqlaTable).filter(
SqlaTable.table_name == name,
SqlaTable.schema == schema,
SqlaTable.database_id == database_id,
)

if dataset_id:
# make sure the dataset found is different from the target (if any)
dataset_query = dataset_query.filter(SqlaTable.id != dataset_id)

return not db.session.query(dataset_query.exists()).scalar()

@staticmethod
Expand Down