Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
Add sql field to task and add TaskSqlPanel
天舟72d80a77b61a
diff --git a/frontend/src/miragejs/factories/task.js b/frontend/src/miragejs/factories/task.jsindex f7855779c1..7e33221e51 100644--- a/frontend/src/miragejs/factories/task.js+++ b/frontend/src/miragejs/factories/task.js@@ -21,6 +21,9 @@ export default {description() {return faker.fake("{{lorem.paragraphs}}");},+ sql() {+ return "SHOW TABLES";+ },stageList() {return [{diff --git a/frontend/src/miragejs/routes/index.js b/frontend/src/miragejs/routes/index.jsindex 2cfa9840f1..b8509d9f05 100644--- a/frontend/src/miragejs/routes/index.js+++ b/frontend/src/miragejs/routes/index.js@@ -265,9 +265,7 @@ export default function routes() {}if (attrs.stage !== undefined) {- const stage = task.stageList.find(- (item) => item.id == attrs.stage.id- );+ const stage = task.stageList.find((item) => item.id == attrs.stage.id);if (stage) {changeList.push({fieldId: [TaskBuiltinFieldId.STAGE, stage.id].join("."),@@ -279,6 +277,16 @@ export default function routes() {}}+ if (attrs.sql !== undefined) {+ if (task.sql != attrs.sql) {+ changeList.push({+ fieldId: TaskBuiltinFieldId.SQL,+ oldValue: task.sql,+ newValue: attrs.sql,+ });+ }+ }+for (const fieldId in attrs.payload) {const oldValue = task.payload[fieldId];const newValue = attrs.payload[fieldId];diff --git a/frontend/src/miragejs/seeds/index.ts b/frontend/src/miragejs/seeds/index.tsindex b0f11859cf..5b43dbbdba 100644--- a/frontend/src/miragejs/seeds/index.ts+++ b/frontend/src/miragejs/seeds/index.ts@@ -168,8 +168,9 @@ const workspacesSeeder = (server: any) => {};for (let i = 0; i < 5; i++) {+ const sql = randomUpdateSchemaTaskName();task = server.create("task", {- name: randomUpdateSchemaTaskName(),+ name: sql,type: "bytebase.database.schema.update",creatorId: ws1Dev1.id,assigneeId: ws1Owner.id,@@ -177,6 +178,7 @@ const workspacesSeeder = (server: any) => {id: ws1Dev1.id,name: ws1Dev1.name,},+ sql,subscriberIdList: [ws1DBA.id, ws1Dev2.id],...fillTaskAndStageStatus(environmentList1),workspace: workspace1,@@ -202,11 +204,13 @@ const workspacesSeeder = (server: any) => {}for (let i = 0; i < 15; i++) {+ const sql = randomUpdateSchemaTaskName();task = server.create("task", {- name: randomUpdateSchemaTaskName(),+ name: sql,type: "bytebase.database.schema.update",creatorId: ws1Owner.id,assigneeId: ws1DBA.id,+ sql,subscriberIdList: [ws1Dev2.id],...fillTaskAndStageStatus(environmentList1),workspace: workspace1,@@ -235,11 +239,13 @@ const workspacesSeeder = (server: any) => {}for (let i = 0; i < 15; i++) {+ const sql = randomUpdateSchemaTaskName();task = server.create("task", {- name: randomUpdateSchemaTaskName(),+ name: sql,type: "bytebase.database.schema.update",creatorId: ws1Dev2.id,assigneeId: ws1DBA.id,+ sql,subscriberIdList: [ws1Owner.id, ws1Dev1.id],...fillTaskAndStageStatus(environmentList1),workspace: workspace1,@@ -263,12 +269,13 @@ const workspacesSeeder = (server: any) => {});}}-+ const sql = randomUpdateSchemaTaskName();task = server.create("task", {- name: randomUpdateSchemaTaskName(),+ name: sql,type: "bytebase.database.schema.update",creatorId: ws2Dev.id,assigneeId: ws2DBA.id,+ sql,...fillTaskAndStageStatus(environmentList2),workspace: workspace2,});diff --git a/frontend/src/plugins/types.ts b/frontend/src/plugins/types.tsindex 96812f687c..8b3ba236f0 100644--- a/frontend/src/plugins/types.ts+++ b/frontend/src/plugins/types.ts@@ -21,6 +21,7 @@ export enum TaskBuiltinFieldId {DATABASE = "7",DATA_SOURCE = "8",STAGE = "9", // The full id is concatenated with the actual stage id e.g. "8".<<stage id>>+ SQL = "10",}export const CUSTOM_FIELD_ID_BEGIN = "100";diff --git a/frontend/src/types/bytebase.ts b/frontend/src/types/bytebase.tsindex 17d35506d8..20c8613fc8 100644--- a/frontend/src/types/bytebase.ts+++ b/frontend/src/types/bytebase.ts@@ -302,6 +302,7 @@ export type Task = {creator: Principal;assignee?: Principal;subscriberList: Principal[];+ sql?: string;payload: TaskPayload;};@@ -313,6 +314,7 @@ export type TaskNew = {creatorId: PrincipalId;assigneeId?: PrincipalId;subscriberIdList: PrincipalId[];+ sql?: string;payload: TaskPayload;};@@ -320,6 +322,7 @@ export type TaskPatch = {name?: string;status?: TaskStatus;description?: string;+ sql?: string;assigneeId?: PrincipalId;stage?: StageProgressPatch;payload?: TaskPayload;diff --git a/frontend/src/views/TaskActivityPanel.vue b/frontend/src/views/TaskActivityPanel.vueindex 7f3590f162..acdb68d6d7 100644--- a/frontend/src/views/TaskActivityPanel.vue+++ b/frontend/src/views/TaskActivityPanel.vue@@ -479,6 +479,10 @@ export default {name = stageName(props.task, stageId);oldValue = update.oldValue;newValue = update.newValue;+ } else if (update.fieldId == TaskBuiltinFieldId.SQL) {+ name = "SQL";+ oldValue = update.oldValue;+ newValue = update.newValue;} else {const field = fieldFromId(props.taskTemplate, update.fieldId);name = field.name;diff --git a/frontend/src/views/TaskDescriptionPanel.vue b/frontend/src/views/TaskDescriptionPanel.vueindex 1740d9b438..9f0c88a3f2 100644--- a/frontend/src/views/TaskDescriptionPanel.vue+++ b/frontend/src/views/TaskDescriptionPanel.vue@@ -77,10 +77,10 @@ import {nextTick,onMounted,onUnmounted,- watch,PropType,ref,reactive,+ watch,} from "vue";import { Task } from "../types";import { sizeToFit } from "../utils";diff --git a/frontend/src/views/TaskDetail.vue b/frontend/src/views/TaskDetail.vueindex 4d99a21e93..1f7c637f89 100644--- a/frontend/src/views/TaskDetail.vue+++ b/frontend/src/views/TaskDetail.vue@@ -98,6 +98,14 @@</div><div class="lg:hidden border-t border-block-border" /><div class="w-full py-6 pr-4">+ <section class="border-b mb-4">+ <TaskSqlPanel+ :task="state.task"+ :new="state.new"+ :allowEdit="allowEditFields"+ @update-sql="updateSql"+ />+ </section><TaskDescriptionPanel:task="state.task":new="state.new"@@ -167,6 +175,7 @@ import { idFromSlug, taskSlug } from "../utils";import TaskHighlightPanel from "../views/TaskHighlightPanel.vue";import TaskStageFlow from "./TaskStageFlow.vue";import TaskOutputPanel from "../views/TaskOutputPanel.vue";+import TaskSqlPanel from "../views/TaskSqlPanel.vue";import TaskDescriptionPanel from "./TaskDescriptionPanel.vue";import TaskActivityPanel from "../views/TaskActivityPanel.vue";import TaskSidebar from "../views/TaskSidebar.vue";@@ -240,6 +249,7 @@ export default {TaskHighlightPanel,TaskStageFlow,TaskOutputPanel,+ TaskSqlPanel,TaskDescriptionPanel,TaskActivityPanel,TaskSidebar,@@ -368,6 +378,22 @@ export default {}};+ const updateSql = (+ newSql: string,+ postUpdated: (updatedTask: Task) => void+ ) => {+ if (state.new) {+ (state.task as TaskNew).sql = newSql;+ } else {+ patchTask(+ {+ sql: newSql,+ },+ postUpdated+ );+ }+ };+const updateDescription = (newDescription: string,postUpdated: (updatedTask: Task) => void@@ -597,6 +623,7 @@ export default {state,updateName,updateDescription,+ updateSql,tryStartTaskStatusTransition,doTaskStatusTransition,updateAssigneeId,diff --git a/frontend/src/views/TaskSqlPanel.vue b/frontend/src/views/TaskSqlPanel.vuenew file mode 100644index 0000000000..d822ed4bc7--- /dev/null+++ b/frontend/src/views/TaskSqlPanel.vue@@ -0,0 +1,189 @@+<template>+ <div class="flex justify-between">+ <div class="textlabel">SQL</div>+ <div v-if="!$props.new" class="space-x-2">+ <button+ v-if="allowEdit && !state.editing"+ type="button"+ class="btn-icon"+ @click.prevent="beginEdit"+ >+ <!-- Heroicon name: solid/pencil -->+ <svg+ class="h-6 w-6"+ xmlns="http://www.w3.org/2000/svg"+ viewBox="0 0 20 20"+ fill="currentColor"+ aria-hidden="true"+ >+ <path+ d="M13.586 3.586a2 2 0 112.828 2.828l-.793.793-2.828-2.828.793-.793zM11.379 5.793L3 14.172V17h2.828l8.38-8.379-2.83-2.828z"+ />+ </svg>+ </button>+ <!-- mt-0.5 is to prevent jiggling betweening switching edit/none-edit -->+ <button+ v-if="state.editing"+ type="button"+ class="mt-0.5 px-3 rounded-sm text-control 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"+ @click.prevent="cancelEdit"+ >+ Cancel+ </button>+ <button+ 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"+ @click.prevent="saveEdit"+ >+ Save+ </button>+ </div>+ </div>+ <label class="sr-only">SQL statement</label>+ <template v-if="state.editing">+ <textarea+ ref="editSqlTextArea"+ class="whitespace-pre-wrap mt-2 w-full resize-none border-white focus:border-white outline-none"+ :class="state.editing ? 'focus:ring-control focus-visible:ring-2' : ''"+ placeholder="Add SQL statement..."+ v-model="state.editSql"+ @input="+ (e) => {+ sizeToFit(e.target);+ // When creating the task, we will emit the event on keystroke to update the in-memory state.+ if ($props.new) {+ $emit('update-sql', state.editSql);+ }+ }+ "+ @focus="+ (e) => {+ sizeToFit(e.target);+ }+ "+ ></textarea>+ </template>+ <!-- Margin value is to prevent flickering when switching between edit/non-edit mode -->+ <!-- TODO: There is still flickering between edit/non-edit mode depending on the line height -->+ <div v-else style="margin-left: 5px; margin-top: 8.5px; margin-bottom: 31px">+ <div v-if="state.editSql" v-highlight class="whitespace-pre-wrap">+ {{ state.editSql }}+ </div>+ <div v-else class="ml-2 text-control-light">Add SQL statement...</div>+ </div>+</template>++<script lang="ts">+import {+ nextTick,+ onMounted,+ onUnmounted,+ PropType,+ ref,+ reactive,+ watch,+} from "vue";+import { Task } from "../types";+import { sizeToFit } from "../utils";++interface LocalState {+ editing: boolean;+ editSql: string;+}++export default {+ name: "TaskSqlPanel",+ emits: ["update-sql"],+ props: {+ task: {+ required: true,+ type: Object as PropType<Task>,+ },+ new: {+ required: true,+ type: Boolean,+ },+ allowEdit: {+ required: true,+ type: Boolean,+ },+ },+ components: {},+ setup(props, { emit }) {+ const editSqlTextArea = ref();++ const state = reactive<LocalState>({+ editing: false,+ editSql: props.task.sql || "",+ });++ const keyboardHandler = (e: KeyboardEvent) => {+ if (state.editing && editSqlTextArea.value === document.activeElement) {+ if (e.code == "Escape") {+ cancelEdit();+ } else if (e.code == "Enter" && e.metaKey) {+ if (state.editSql != props.task.sql) {+ saveEdit();+ }+ }+ }+ };++ const resizeTextAreaHandler = () => {+ if (state.editing) {+ sizeToFit(editSqlTextArea.value);+ }+ };++ onMounted(() => {+ document.addEventListener("keydown", keyboardHandler);+ window.addEventListener("resize", resizeTextAreaHandler);+ if (props.new) {+ state.editing = true;+ nextTick(() => {+ sizeToFit(editSqlTextArea.value);+ });+ }+ });++ onUnmounted(() => {+ document.removeEventListener("keydown", keyboardHandler);+ window.removeEventListener("resize", resizeTextAreaHandler);+ });++ // Reset the edit state after creating the task.+ watch(+ () => props.new,+ (curNew, prevNew) => {+ if (!curNew && prevNew) {+ state.editing = false;+ }+ }+ );++ const beginEdit = () => {+ state.editSql = props.task.sql || "";+ state.editing = true;+ nextTick(() => {+ editSqlTextArea.value.focus();+ });+ };++ const saveEdit = () => {+ emit("update-sql", state.editSql, (updatedTask: Task) => {+ state.editSql = updatedTask.sql || "";+ state.editing = false;+ });+ };++ const cancelEdit = () => {+ state.editSql = props.task.sql || "";+ state.editing = false;+ };++ return { editSqlTextArea, state, beginEdit, saveEdit, cancelEdit };+ },+};+</script>