Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

Add rollback sql panel

天舟5215ac593957
diff --git a/frontend/src/components/Quickstart.vue b/frontend/src/components/Quickstart.vue
index 20f0f4062c..e0475bccff 100644
--- a/frontend/src/components/Quickstart.vue
+++ b/frontend/src/components/Quickstart.vue
@@ -127,7 +127,7 @@ export default {
{
name: "Create a table",
link:
- "/task/new?template=bytebase.database.schema.update&name=Create employee table&description=Stores employee basic info&sql=CREATE TABLE employee (\n id INT NOT NULL,\n name TEXT,\n age INT,\n PRIMARY KEY (name)\n);",
+ "/task/new?template=bytebase.database.schema.update&name=Create employee table&description=Stores employee basic info&sql=CREATE TABLE employee (\n id INT NOT NULL,\n name TEXT,\n age INT,\n PRIMARY KEY (name)\n);&rollbacksql=DROP TABLE employee;",
allowDeveloper: true,
done: computed(() =>
store.getters["uistate/introStateByKey"]("table.create")
diff --git a/frontend/src/miragejs/factories/task.js b/frontend/src/miragejs/factories/task.js
index 6faea2f868..e86984a8da 100644
--- a/frontend/src/miragejs/factories/task.js
+++ b/frontend/src/miragejs/factories/task.js
@@ -22,7 +22,10 @@ export default {
return faker.fake("{{lorem.paragraphs}}");
},
sql() {
- return "SHOW TABLES";
+ return "CREATE TABLE t1 (id INT NOT NULL);";
+ },
+ rollbackSql() {
+ return "DROP TABLE t1;";
},
stageList() {
return [
diff --git a/frontend/src/miragejs/routes/index.js b/frontend/src/miragejs/routes/index.js
index 51e429c387..47856ba6e6 100644
--- a/frontend/src/miragejs/routes/index.js
+++ b/frontend/src/miragejs/routes/index.js
@@ -287,6 +287,16 @@ export default function routes() {
}
}
+ if (attrs.rollbackSql !== undefined) {
+ if (task.rollbackSql != attrs.rollbackSql) {
+ changeList.push({
+ fieldId: TaskBuiltinFieldId.ROLLBACK_SQL,
+ oldValue: task.rollbackSql,
+ newValue: attrs.rollbackSql,
+ });
+ }
+ }
+
for (const fieldId in attrs.payload) {
const oldValue = task.payload[fieldId];
const newValue = attrs.payload[fieldId];
diff --git a/frontend/src/plugins/types.ts b/frontend/src/plugins/types.ts
index 8b3ba236f0..ae41349e8a 100644
--- a/frontend/src/plugins/types.ts
+++ b/frontend/src/plugins/types.ts
@@ -22,6 +22,7 @@ export enum TaskBuiltinFieldId {
DATA_SOURCE = "8",
STAGE = "9", // The full id is concatenated with the actual stage id e.g. "8".<<stage id>>
SQL = "10",
+ ROLLBACK_SQL = "11",
}
export const CUSTOM_FIELD_ID_BEGIN = "100";
diff --git a/frontend/src/types/bytebase.ts b/frontend/src/types/bytebase.ts
index 489f056528..c90d545112 100644
--- a/frontend/src/types/bytebase.ts
+++ b/frontend/src/types/bytebase.ts
@@ -308,6 +308,7 @@ export type Task = {
assignee?: Principal;
subscriberList: Principal[];
sql?: string;
+ rollbackSql?: string;
payload: TaskPayload;
};
@@ -320,6 +321,7 @@ export type TaskNew = {
assigneeId?: PrincipalId;
subscriberIdList: PrincipalId[];
sql?: string;
+ rollbackSql?: string;
payload: TaskPayload;
};
@@ -328,6 +330,7 @@ export type TaskPatch = {
status?: TaskStatus;
description?: string;
sql?: string;
+ rollbackSql?: string;
assigneeId?: PrincipalId;
stage?: StageProgressPatch;
payload?: TaskPayload;
diff --git a/frontend/src/views/TaskActivityPanel.vue b/frontend/src/views/TaskActivityPanel.vue
index 41db1abd51..c45b7c7a13 100644
--- a/frontend/src/views/TaskActivityPanel.vue
+++ b/frontend/src/views/TaskActivityPanel.vue
@@ -483,6 +483,10 @@ export default {
name = "SQL";
oldValue = update.oldValue;
newValue = update.newValue;
+ } else if (update.fieldId == TaskBuiltinFieldId.ROLLBACK_SQL) {
+ name = "Rollback SQL";
+ oldValue = update.oldValue;
+ newValue = update.newValue;
} else {
const field = fieldFromId(props.taskTemplate, update.fieldId);
name = field.name;
diff --git a/frontend/src/views/TaskDetail.vue b/frontend/src/views/TaskDetail.vue
index 3e22298bf5..af8403347f 100644
--- a/frontend/src/views/TaskDetail.vue
+++ b/frontend/src/views/TaskDetail.vue
@@ -103,10 +103,20 @@
<TaskSqlPanel
:task="state.task"
:new="state.new"
+ :rollback="false"
:allowEdit="allowEditFields"
@update-sql="updateSql"
/>
</section>
+ <section v-if="showTaskRollbackSqlPanel" class="border-b mb-4">
+ <TaskSqlPanel
+ :task="state.task"
+ :new="state.new"
+ :rollback="true"
+ :allowEdit="allowEditFields"
+ @update-sql="updateRollbackSql"
+ />
+ </section>
<TaskDescriptionPanel
:task="state.task"
:new="state.new"
@@ -338,6 +348,10 @@ export default {
if (router.currentRoute.value.query.sql) {
newTask.sql = router.currentRoute.value.query.sql as string;
}
+ if (router.currentRoute.value.query.rollbacksql) {
+ newTask.rollbackSql = router.currentRoute.value.query
+ .rollbacksql as string;
+ }
if (router.currentRoute.value.query.assignee) {
newTask.assigneeId = router.currentRoute.value.query
.assignee as PrincipalId;
@@ -421,6 +435,22 @@ export default {
}
};
+ const updateRollbackSql = (
+ newSql: string,
+ postUpdated: (updatedTask: Task) => void
+ ) => {
+ if (state.new) {
+ (state.task as TaskNew).rollbackSql = newSql;
+ } else {
+ patchTask(
+ {
+ rollbackSql: newSql,
+ },
+ postUpdated
+ );
+ }
+ };
+
const updateDescription = (
newDescription: string,
postUpdated: (updatedTask: Task) => void
@@ -644,6 +674,10 @@ export default {
);
});
+ const showTaskRollbackSqlPanel = computed(() => {
+ return state.task.type == "bytebase.database.schema.update";
+ });
+
const applicableStatusTransitionList = computed(
(): TaskStatusTransition[] => {
const list: TaskStatusTransitionType[] = [];
@@ -674,6 +708,7 @@ export default {
updateName,
updateDescription,
updateSql,
+ updateRollbackSql,
allowTransition,
tryStartTaskStatusTransition,
doTaskStatusTransition,
@@ -690,6 +725,7 @@ export default {
showTaskStageFlowBar,
showTaskOutputPanel,
showTaskSqlPanel,
+ showTaskRollbackSqlPanel,
applicableStatusTransitionList,
};
},
diff --git a/frontend/src/views/TaskSqlPanel.vue b/frontend/src/views/TaskSqlPanel.vue
index d822ed4bc7..454ae12435 100644
--- a/frontend/src/views/TaskSqlPanel.vue
+++ b/frontend/src/views/TaskSqlPanel.vue
@@ -1,6 +1,6 @@
<template>
<div class="flex justify-between">
- <div class="textlabel">SQL</div>
+ <div class="textlabel">{{ rollback ? "Rollback SQL" : "SQL" }}</div>
<div v-if="!$props.new" class="space-x-2">
<button
v-if="allowEdit && !state.editing"
@@ -34,7 +34,7 @@
v-if="state.editing"
type="button"
class="mt-0.5 px-3 border border-control-border rounded-sm text-control bg-control-bg hover:bg-control-bg-hover disabled:bg-control-bg disabled:opacity-50 disabled:cursor-not-allowed text-sm leading-5 font-normal focus:ring-control focus:outline-none focus-visible:ring-2 focus:ring-offset-2"
- :disabled="state.editSql == task.sql"
+ :disabled="!allowSave"
@click.prevent="saveEdit"
>
Save
@@ -84,9 +84,11 @@ import {
ref,
reactive,
watch,
+ computed,
} from "vue";
import { Task } from "../types";
import { sizeToFit } from "../utils";
+import command from "../store/modules/command";
interface LocalState {
editing: boolean;
@@ -105,6 +107,10 @@ export default {
required: true,
type: Boolean,
},
+ rollback: {
+ required: true,
+ type: Boolean,
+ },
allowEdit: {
required: true,
type: Boolean,
@@ -114,9 +120,13 @@ export default {
setup(props, { emit }) {
const editSqlTextArea = ref();
+ const effectiveSql = (task: Task): string => {
+ return (props.rollback ? task.rollbackSql : task.sql) || "";
+ };
+
const state = reactive<LocalState>({
editing: false,
- editSql: props.task.sql || "",
+ editSql: effectiveSql(props.task),
});
const keyboardHandler = (e: KeyboardEvent) => {
@@ -124,8 +134,14 @@ export default {
if (e.code == "Escape") {
cancelEdit();
} else if (e.code == "Enter" && e.metaKey) {
- if (state.editSql != props.task.sql) {
- saveEdit();
+ if (props.rollback) {
+ if (state.editSql != props.task.rollbackSql) {
+ saveEdit();
+ }
+ } else {
+ if (state.editSql != props.task.sql) {
+ saveEdit();
+ }
}
}
}
@@ -163,8 +179,14 @@ export default {
}
);
+ const allowSave = computed(() => {
+ return props.rollback
+ ? state.editSql != props.task.rollbackSql
+ : state.editSql != props.task.sql;
+ });
+
const beginEdit = () => {
- state.editSql = props.task.sql || "";
+ state.editSql = effectiveSql(props.task);
state.editing = true;
nextTick(() => {
editSqlTextArea.value.focus();
@@ -173,17 +195,24 @@ export default {
const saveEdit = () => {
emit("update-sql", state.editSql, (updatedTask: Task) => {
- state.editSql = updatedTask.sql || "";
+ state.editSql = effectiveSql(updatedTask);
state.editing = false;
});
};
const cancelEdit = () => {
- state.editSql = props.task.sql || "";
+ state.editSql = effectiveSql(props.task);
state.editing = false;
};
- return { editSqlTextArea, state, beginEdit, saveEdit, cancelEdit };
+ return {
+ editSqlTextArea,
+ state,
+ allowSave,
+ beginEdit,
+ saveEdit,
+ cancelEdit,
+ };
},
};
</script>