mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-11-02 15:30:25 +08:00
Merge pull request #55 from kanzihuang/feature-instance
feature: 从数据库管理模块中拆分数据库实例管理功能
This commit is contained in:
@@ -1,102 +1,41 @@
|
||||
<template>
|
||||
<div>
|
||||
<el-dialog :title="title" v-model="dialogVisible" :before-close="cancel" :close-on-click-modal="false" :destroy-on-close="true" width="38%">
|
||||
<el-dialog :title="title" v-model="dialogVisible" @open="open" :before-close="cancel" :close-on-click-modal="false" :destroy-on-close="true" width="38%">
|
||||
<el-form :model="form" ref="dbForm" :rules="rules" label-width="auto">
|
||||
<el-tabs v-model="tabActiveName">
|
||||
<el-tab-pane label="基础信息" name="basic">
|
||||
<el-form-item prop="tagId" label="标签:" required>
|
||||
<tag-select v-model="form.tagId" v-model:tag-path="form.tagPath" style="width: 100%" />
|
||||
</el-form-item>
|
||||
<el-form-item prop="tagId" label="标签:" required>
|
||||
<tag-select v-model="form.tagId" v-model:tag-path="form.tagPath" style="width: 100%" />
|
||||
</el-form-item>
|
||||
|
||||
<el-form-item prop="name" label="别名:" required>
|
||||
<el-input v-model.trim="form.name" placeholder="请输入数据库别名" auto-complete="off"></el-input>
|
||||
</el-form-item>
|
||||
<el-form-item prop="type" label="类型:" required>
|
||||
<el-select style="width: 100%" v-model="form.type" placeholder="请选择数据库类型">
|
||||
<el-option key="item.id" label="mysql" value="mysql"> </el-option>
|
||||
<el-option key="item.id" label="postgres" value="postgres"> </el-option>
|
||||
</el-select>
|
||||
</el-form-item>
|
||||
<el-form-item prop="host" label="host:" required>
|
||||
<el-col :span="18">
|
||||
<el-input :disabled="form.id !== undefined" v-model.trim="form.host" placeholder="请输入主机ip" auto-complete="off"></el-input>
|
||||
</el-col>
|
||||
<el-col style="text-align: center" :span="1">:</el-col>
|
||||
<el-col :span="5">
|
||||
<el-input type="number" v-model.number="form.port" placeholder="请输入端口"></el-input>
|
||||
</el-col>
|
||||
</el-form-item>
|
||||
<el-form-item prop="username" label="用户名:" required>
|
||||
<el-input v-model.trim="form.username" placeholder="请输入用户名"></el-input>
|
||||
</el-form-item>
|
||||
<el-form-item prop="password" label="密码:">
|
||||
<el-input
|
||||
type="password"
|
||||
show-password
|
||||
v-model.trim="form.password"
|
||||
placeholder="请输入密码,修改操作可不填"
|
||||
autocomplete="new-password"
|
||||
>
|
||||
<template v-if="form.id && form.id != 0" #suffix>
|
||||
<el-popover @hide="pwd = ''" placement="right" title="原密码" :width="200" trigger="click" :content="pwd">
|
||||
<template #reference>
|
||||
<el-link @click="getDbPwd" :underline="false" type="primary" class="mr5">原密码 </el-link>
|
||||
</template>
|
||||
</el-popover>
|
||||
</template>
|
||||
</el-input>
|
||||
</el-form-item>
|
||||
<el-form-item prop="database" label="数据库名:" required>
|
||||
<el-col :span="19">
|
||||
<el-select
|
||||
@change="changeDatabase"
|
||||
v-model="databaseList"
|
||||
multiple
|
||||
clearable
|
||||
collapse-tags
|
||||
collapse-tags-tooltip
|
||||
filterable
|
||||
allow-create
|
||||
placeholder="请确保数据库实例信息填写完整后获取库名"
|
||||
style="width: 100%"
|
||||
>
|
||||
<el-option v-for="db in allDatabases" :key="db" :label="db" :value="db" />
|
||||
</el-select>
|
||||
</el-col>
|
||||
<el-col style="text-align: center" :span="1">
|
||||
<el-divider direction="vertical" border-style="dashed" />
|
||||
</el-col>
|
||||
<el-col :span="4">
|
||||
<el-link @click="getAllDatabase" :underline="false" type="success">获取库名</el-link>
|
||||
</el-col>
|
||||
</el-form-item>
|
||||
<el-form-item prop="instanceId" label="数据库实例:" required>
|
||||
<el-select :disabled="form.id !== undefined" @change="getAllDatabase" v-model="form.instanceId" placeholder="请选择实例" filterable clearable style="width: 200px">
|
||||
<el-option v-for="item in state.instances" :key="item.id" :label="item.name" :value="item.id"> </el-option>
|
||||
</el-select>
|
||||
</el-form-item>
|
||||
|
||||
<el-form-item prop="remark" label="备注:">
|
||||
<el-input v-model.trim="form.remark" auto-complete="off" type="textarea"></el-input>
|
||||
</el-form-item>
|
||||
</el-tab-pane>
|
||||
<el-form-item prop="name" label="别名:" required>
|
||||
<el-input v-model.trim="form.name" placeholder="请输入数据库别名" auto-complete="off"></el-input>
|
||||
</el-form-item>
|
||||
|
||||
<el-tab-pane label="其他配置" name="other">
|
||||
<el-form-item prop="params" label="连接参数:">
|
||||
<el-input v-model.trim="form.params" placeholder="其他连接参数,形如: key1=value1&key2=value2">
|
||||
<template #suffix>
|
||||
<el-link
|
||||
target="_blank"
|
||||
href="https://github.com/go-sql-driver/mysql#parameters"
|
||||
:underline="false"
|
||||
type="primary"
|
||||
class="mr5"
|
||||
>参数参考</el-link
|
||||
>
|
||||
</template>
|
||||
</el-input>
|
||||
</el-form-item>
|
||||
<el-form-item prop="database" label="数据库名:" required>
|
||||
<el-select
|
||||
@change="changeDatabase"
|
||||
v-model="databaseList"
|
||||
multiple
|
||||
clearable
|
||||
collapse-tags
|
||||
collapse-tags-tooltip
|
||||
filterable
|
||||
allow-create
|
||||
placeholder="请确保数据库实例信息填写完整后获取库名"
|
||||
style="width: 100%"
|
||||
>
|
||||
<el-option v-for="db in allDatabases" :key="db" :label="db" :value="db" />
|
||||
</el-select>
|
||||
</el-form-item>
|
||||
|
||||
<el-form-item prop="sshTunnelMachineId" label="SSH隧道:">
|
||||
<ssh-tunnel-select v-model="form.sshTunnelMachineId" />
|
||||
</el-form-item>
|
||||
</el-tab-pane>
|
||||
</el-tabs>
|
||||
<el-form-item prop="remark" label="备注:">
|
||||
<el-input v-model.trim="form.remark" auto-complete="off" type="textarea"></el-input>
|
||||
</el-form-item>
|
||||
</el-form>
|
||||
|
||||
<template #footer>
|
||||
@@ -113,10 +52,7 @@
|
||||
import { toRefs, reactive, watch, ref } from 'vue';
|
||||
import { dbApi } from './api';
|
||||
import { ElMessage } from 'element-plus';
|
||||
import { notBlank } from '@/common/assert';
|
||||
import { RsaEncrypt } from '@/common/rsa';
|
||||
import TagSelect from '../component/TagSelect.vue';
|
||||
import SshTunnelSelect from '../component/SshTunnelSelect.vue';
|
||||
|
||||
const props = defineProps({
|
||||
visible: {
|
||||
@@ -141,6 +77,15 @@ const rules = {
|
||||
trigger: ['change', 'blur'],
|
||||
},
|
||||
],
|
||||
|
||||
instanceId: [
|
||||
{
|
||||
required: true,
|
||||
message: '请选择数据库实例',
|
||||
trigger: ['change', 'blur'],
|
||||
},
|
||||
],
|
||||
|
||||
name: [
|
||||
{
|
||||
required: true,
|
||||
@@ -148,27 +93,6 @@ const rules = {
|
||||
trigger: ['change', 'blur'],
|
||||
},
|
||||
],
|
||||
type: [
|
||||
{
|
||||
required: true,
|
||||
message: '请选择数据库类型',
|
||||
trigger: ['change', 'blur'],
|
||||
},
|
||||
],
|
||||
host: [
|
||||
{
|
||||
required: true,
|
||||
message: '请输入主机ip和port',
|
||||
trigger: ['change', 'blur'],
|
||||
},
|
||||
],
|
||||
username: [
|
||||
{
|
||||
required: true,
|
||||
message: '请输入用户名',
|
||||
trigger: ['change', 'blur'],
|
||||
},
|
||||
],
|
||||
database: [
|
||||
{
|
||||
required: true,
|
||||
@@ -182,43 +106,34 @@ const dbForm: any = ref(null);
|
||||
|
||||
const state = reactive({
|
||||
dialogVisible: false,
|
||||
tabActiveName: 'basic',
|
||||
allDatabases: [] as any,
|
||||
databaseList: [] as any,
|
||||
form: {
|
||||
id: null,
|
||||
tagId: null as any,
|
||||
tagPath: null as any,
|
||||
type: null,
|
||||
name: null,
|
||||
host: '',
|
||||
port: 3306,
|
||||
username: null,
|
||||
password: null,
|
||||
params: null,
|
||||
database: '',
|
||||
remark: '',
|
||||
sshTunnelMachineId: null as any,
|
||||
instanceId: null as any,
|
||||
},
|
||||
// 原密码
|
||||
pwd: '',
|
||||
btnLoading: false,
|
||||
instances: [] as any,
|
||||
});
|
||||
|
||||
const { dialogVisible, tabActiveName, allDatabases, databaseList, form, pwd, btnLoading } = toRefs(state);
|
||||
const { dialogVisible, allDatabases, databaseList, form, btnLoading } = toRefs(state);
|
||||
|
||||
watch(props, (newValue: any) => {
|
||||
state.dialogVisible = newValue.visible;
|
||||
if (!state.dialogVisible) {
|
||||
return;
|
||||
}
|
||||
state.tabActiveName = 'basic';
|
||||
if (newValue.db) {
|
||||
state.form = { ...newValue.db };
|
||||
// 将数据库名使用空格切割,获取所有数据库列表
|
||||
state.databaseList = newValue.db.database.split(' ');
|
||||
} else {
|
||||
state.form = { port: 3306 } as any;
|
||||
state.form = {} as any;
|
||||
state.databaseList = [];
|
||||
}
|
||||
});
|
||||
@@ -232,26 +147,26 @@ const changeDatabase = () => {
|
||||
|
||||
const getAllDatabase = async () => {
|
||||
const reqForm = { ...state.form };
|
||||
reqForm.password = await RsaEncrypt(reqForm.password);
|
||||
state.allDatabases = await dbApi.getAllDatabase.request(reqForm);
|
||||
ElMessage.success('获取成功, 请选择需要管理操作的数据库');
|
||||
if (state.form.instanceId > 0) {
|
||||
state.allDatabases = await dbApi.getAllDatabase.request(reqForm);
|
||||
}
|
||||
};
|
||||
|
||||
const getDbPwd = async () => {
|
||||
state.pwd = await dbApi.getDbPwd.request({ id: state.form.id });
|
||||
const getAllInstances = async () => {
|
||||
const data = await dbApi.instances.request(null);
|
||||
if (data) {
|
||||
state.instances = data.list;
|
||||
}
|
||||
}
|
||||
const open = async () => {
|
||||
await getAllInstances()
|
||||
await getAllDatabase()
|
||||
};
|
||||
|
||||
const btnOk = async () => {
|
||||
if (!state.form.id) {
|
||||
notBlank(state.form.password, '新增操作,密码不可为空');
|
||||
}
|
||||
dbForm.value.validate(async (valid: boolean) => {
|
||||
if (valid) {
|
||||
const reqForm = { ...state.form };
|
||||
reqForm.password = await RsaEncrypt(reqForm.password);
|
||||
if (!state.form.sshTunnelMachineId) {
|
||||
reqForm.sshTunnelMachineId = -1;
|
||||
}
|
||||
dbApi.saveDb.request(reqForm).then(() => {
|
||||
ElMessage.success('保存成功');
|
||||
emit('val-change', state.form);
|
||||
@@ -272,6 +187,7 @@ const btnOk = async () => {
|
||||
const resetInputDb = () => {
|
||||
state.databaseList = [];
|
||||
state.allDatabases = [];
|
||||
state.instances = [];
|
||||
};
|
||||
|
||||
const cancel = () => {
|
||||
|
||||
@@ -14,11 +14,17 @@
|
||||
@pageChange="search()"
|
||||
>
|
||||
<template #tagPathSelect>
|
||||
<el-select @focus="getTags" v-model="query.tagPath" placeholder="请选择标签" @clear="search" filterable clearable style="width: 200px">
|
||||
<el-select @focus="getTags" v-model="query.tagPath" placeholder="请选择标签" filterable clearable style="width: 200px">
|
||||
<el-option v-for="item in tags" :key="item" :label="item" :value="item"> </el-option>
|
||||
</el-select>
|
||||
</template>
|
||||
|
||||
<template #instanceSelect>
|
||||
<el-select @focus="getInstances" v-model="query.instanceId" placeholder="请选择实例" filterable clearable style="width: 200px">
|
||||
<el-option v-for="item in instances" :key="item.id" :label="item.name" :value="item.id"> </el-option>
|
||||
</el-select>
|
||||
</template>
|
||||
|
||||
<template #queryRight>
|
||||
<el-button v-auth="perms.saveDb" type="primary" icon="plus" @click="editDb(false)">添加</el-button>
|
||||
<el-button v-auth="perms.delDb" :disabled="selectionData.length < 1" @click="deleteDb()" type="danger" icon="delete">删除</el-button>
|
||||
@@ -226,30 +232,23 @@
|
||||
<el-input disabled type="textarea" :autosize="{ minRows: 15, maxRows: 30 }" v-model="ddlDialog.ddl" size="small"> </el-input>
|
||||
</el-dialog>
|
||||
|
||||
<el-dialog v-model="infoDialog.visible">
|
||||
<el-dialog v-model="infoDialog.visible" :before-close="onBeforeCloseInfoDialog" :close-on-click-modal="false">
|
||||
<el-descriptions title="详情" :column="3" border>
|
||||
<el-descriptions-item :span="1.5" label="id">{{ infoDialog.data.id }}</el-descriptions-item>
|
||||
<el-descriptions-item :span="1.5" label="名称">{{ infoDialog.data.name }}</el-descriptions-item>
|
||||
|
||||
<el-descriptions-item :span="3" label="标签路径">{{ infoDialog.data.tagPath }}</el-descriptions-item>
|
||||
|
||||
<el-descriptions-item :span="2" label="主机">{{ infoDialog.data.host }}</el-descriptions-item>
|
||||
<el-descriptions-item :span="1" label="端口">{{ infoDialog.data.port }}</el-descriptions-item>
|
||||
|
||||
<el-descriptions-item :span="2" label="用户名">{{ infoDialog.data.username }}</el-descriptions-item>
|
||||
<el-descriptions-item :span="1" label="类型">{{ infoDialog.data.type }}</el-descriptions-item>
|
||||
|
||||
<el-descriptions-item :span="3" label="连接参数">{{ infoDialog.data.params }}</el-descriptions-item>
|
||||
<el-descriptions-item :span="3" label="备注">{{ infoDialog.data.remark }}</el-descriptions-item>
|
||||
<el-descriptions-item :span="2" label="名称">{{ infoDialog.data.name }}</el-descriptions-item>
|
||||
<el-descriptions-item :span="1" label="id">{{ infoDialog.data.id }}</el-descriptions-item>
|
||||
<el-descriptions-item :span="3" label="数据库">{{ infoDialog.data.database }}</el-descriptions-item>
|
||||
|
||||
<el-descriptions-item :span="3" label="SSH隧道">{{ infoDialog.data.sshTunnelMachineId > 0 ? '是' : '否' }} </el-descriptions-item>
|
||||
|
||||
<el-descriptions-item :span="3" label="备注">{{ infoDialog.data.remark }}</el-descriptions-item>
|
||||
<el-descriptions-item :span="2" label="创建时间">{{ dateFormat(infoDialog.data.createTime) }} </el-descriptions-item>
|
||||
<el-descriptions-item :span="1" label="创建者">{{ infoDialog.data.creator }}</el-descriptions-item>
|
||||
|
||||
<el-descriptions-item :span="2" label="更新时间">{{ dateFormat(infoDialog.data.updateTime) }} </el-descriptions-item>
|
||||
<el-descriptions-item :span="1" label="修改者">{{ infoDialog.data.modifier }}</el-descriptions-item>
|
||||
|
||||
<el-descriptions-item :span="3" label="数据库实例名称">{{ infoDialog.instance.name }}</el-descriptions-item>
|
||||
<el-descriptions-item :span="2" label="主机">{{ infoDialog.instance.host }}</el-descriptions-item>
|
||||
<el-descriptions-item :span="1" label="端口">{{ infoDialog.instance.port }}</el-descriptions-item>
|
||||
<el-descriptions-item :span="2" label="用户名">{{ infoDialog.instance.username }}</el-descriptions-item>
|
||||
<el-descriptions-item :span="1" label="类型">{{ infoDialog.instance.type }}</el-descriptions-item>
|
||||
</el-descriptions>
|
||||
</el-dialog>
|
||||
|
||||
@@ -292,15 +291,15 @@ const perms = {
|
||||
delDb: 'db:del',
|
||||
};
|
||||
|
||||
const queryConfig = [TableQuery.slot('tagPath', '标签', 'tagPathSelect')];
|
||||
const queryConfig = [
|
||||
TableQuery.slot('tagPath', '标签', 'tagPathSelect'),
|
||||
TableQuery.slot('instanceId', '实例', 'instanceSelect'),
|
||||
];
|
||||
|
||||
const columns = ref([
|
||||
TableColumn.new('tagPath', '标签路径').isSlot().setAddWidth(20),
|
||||
TableColumn.new('name', '名称'),
|
||||
TableColumn.new('host', 'host:port').setFormatFunc((data: any, _prop: string) => `${data.host}:${data.port}`),
|
||||
TableColumn.new('type', '类型'),
|
||||
TableColumn.new('database', '数据库').isSlot().setMinWidth(70),
|
||||
TableColumn.new('username', '用户名'),
|
||||
TableColumn.new('remark', '备注'),
|
||||
TableColumn.new('more', '更多').isSlot().setMinWidth(165).fixedRight(),
|
||||
]);
|
||||
@@ -316,6 +315,7 @@ const state = reactive({
|
||||
dbId: 0,
|
||||
db: '',
|
||||
tags: [],
|
||||
instances: [],
|
||||
/**
|
||||
* 选中的数据
|
||||
*/
|
||||
@@ -325,6 +325,7 @@ const state = reactive({
|
||||
*/
|
||||
query: {
|
||||
tagPath: null,
|
||||
instanceId: null,
|
||||
pageNum: 1,
|
||||
pageSize: 10,
|
||||
},
|
||||
@@ -333,6 +334,10 @@ const state = reactive({
|
||||
infoDialog: {
|
||||
visible: false,
|
||||
data: null as any,
|
||||
instance: null as any,
|
||||
query: {
|
||||
instanceId: 0,
|
||||
}
|
||||
},
|
||||
showDumpInfo: false,
|
||||
dumpInfo: {
|
||||
@@ -427,6 +432,7 @@ const {
|
||||
dbId,
|
||||
db,
|
||||
tags,
|
||||
instances,
|
||||
selectionData,
|
||||
query,
|
||||
datas,
|
||||
@@ -489,15 +495,31 @@ const search = async () => {
|
||||
}
|
||||
};
|
||||
|
||||
const showInfo = (info: any) => {
|
||||
const showInfo = async (info: any) => {
|
||||
state.infoDialog.data = info;
|
||||
state.infoDialog.query.instanceId = info.instanceId;
|
||||
const res = await dbApi.getInstance.request(state.infoDialog.query);
|
||||
state.infoDialog.instance = res;
|
||||
state.infoDialog.visible = true;
|
||||
};
|
||||
|
||||
const onBeforeCloseInfoDialog = () => {
|
||||
state.infoDialog.visible = false;
|
||||
state.infoDialog.data = null;
|
||||
state.infoDialog.instance = null;
|
||||
};
|
||||
|
||||
const getTags = async () => {
|
||||
state.tags = await dbApi.dbTags.request(null);
|
||||
};
|
||||
|
||||
const getInstances = async () => {
|
||||
const data = await dbApi.instances.request(null);
|
||||
if (data) {
|
||||
state.instances = data.list;
|
||||
}
|
||||
};
|
||||
|
||||
const editDb = async (data: any) => {
|
||||
if (!data) {
|
||||
state.dbEditDialog.data = null;
|
||||
@@ -527,7 +549,7 @@ const deleteDb = async () => {
|
||||
};
|
||||
|
||||
const onShowSqlExec = async (row: any) => {
|
||||
state.sqlExecLogDialog.title = `${row.name}[${row.host}:${row.port}]`;
|
||||
state.sqlExecLogDialog.title = `${row.name}`;
|
||||
state.sqlExecLogDialog.query.dbId = row.id;
|
||||
state.sqlExecLogDialog.dbs = row.database.split(' ');
|
||||
searchSqlExecLog();
|
||||
|
||||
212
mayfly_go_web/src/views/ops/db/InstanceEdit.vue
Normal file
212
mayfly_go_web/src/views/ops/db/InstanceEdit.vue
Normal file
@@ -0,0 +1,212 @@
|
||||
<template>
|
||||
<div>
|
||||
<el-dialog :title="title" v-model="dialogVisible" :before-close="cancel" :close-on-click-modal="false" :destroy-on-close="true" width="38%">
|
||||
<el-form :model="form" ref="dbForm" :rules="rules" label-width="auto">
|
||||
<el-tabs v-model="tabActiveName">
|
||||
<el-tab-pane label="基础信息" name="basic">
|
||||
<el-form-item prop="name" label="别名:" required>
|
||||
<el-input v-model.trim="form.name" placeholder="请输入数据库别名" auto-complete="off"></el-input>
|
||||
</el-form-item>
|
||||
<el-form-item prop="type" label="类型:" required>
|
||||
<el-select style="width: 100%" v-model="form.type" placeholder="请选择数据库类型">
|
||||
<el-option key="item.id" label="mysql" value="mysql"> </el-option>
|
||||
<el-option key="item.id" label="postgres" value="postgres"> </el-option>
|
||||
</el-select>
|
||||
</el-form-item>
|
||||
<el-form-item prop="host" label="host:" required>
|
||||
<el-col :span="18">
|
||||
<el-input :disabled="form.id !== undefined" v-model.trim="form.host" placeholder="请输入主机ip" auto-complete="off"></el-input>
|
||||
</el-col>
|
||||
<el-col style="text-align: center" :span="1">:</el-col>
|
||||
<el-col :span="5">
|
||||
<el-input type="number" v-model.number="form.port" placeholder="请输入端口"></el-input>
|
||||
</el-col>
|
||||
</el-form-item>
|
||||
<el-form-item prop="username" label="用户名:" required>
|
||||
<el-input v-model.trim="form.username" placeholder="请输入用户名"></el-input>
|
||||
</el-form-item>
|
||||
<el-form-item prop="password" label="密码:">
|
||||
<el-input
|
||||
type="password"
|
||||
show-password
|
||||
v-model.trim="form.password"
|
||||
placeholder="请输入密码"
|
||||
autocomplete="new-password"
|
||||
/>
|
||||
</el-form-item>
|
||||
|
||||
<el-form-item prop="remark" label="备注:">
|
||||
<el-input v-model.trim="form.remark" auto-complete="off" type="textarea"></el-input>
|
||||
</el-form-item>
|
||||
</el-tab-pane>
|
||||
|
||||
<el-tab-pane label="其他配置" name="other">
|
||||
<el-form-item prop="params" label="连接参数:">
|
||||
<el-input v-model.trim="form.params" placeholder="其他连接参数,形如: key1=value1&key2=value2">
|
||||
<template #suffix>
|
||||
<el-link
|
||||
target="_blank"
|
||||
href="https://github.com/go-sql-driver/mysql#parameters"
|
||||
:underline="false"
|
||||
type="primary"
|
||||
class="mr5"
|
||||
>参数参考</el-link
|
||||
>
|
||||
</template>
|
||||
</el-input>
|
||||
</el-form-item>
|
||||
|
||||
<el-form-item prop="sshTunnelMachineId" label="SSH隧道:">
|
||||
<ssh-tunnel-select v-model="form.sshTunnelMachineId" />
|
||||
</el-form-item>
|
||||
</el-tab-pane>
|
||||
</el-tabs>
|
||||
</el-form>
|
||||
|
||||
<template #footer>
|
||||
<div class="dialog-footer">
|
||||
<el-button @click="cancel()">取 消</el-button>
|
||||
<el-button type="primary" :loading="btnLoading" @click="btnOk">确 定</el-button>
|
||||
</div>
|
||||
</template>
|
||||
</el-dialog>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import { toRefs, reactive, watch, ref } from 'vue';
|
||||
import { dbApi } from './api';
|
||||
import { ElMessage } from 'element-plus';
|
||||
import { notBlank } from '@/common/assert';
|
||||
import { RsaEncrypt } from '@/common/rsa';
|
||||
import TagSelect from '../component/TagSelect.vue';
|
||||
import SshTunnelSelect from '../component/SshTunnelSelect.vue';
|
||||
|
||||
const props = defineProps({
|
||||
visible: {
|
||||
type: Boolean,
|
||||
},
|
||||
data: {
|
||||
type: [Boolean, Object],
|
||||
},
|
||||
title: {
|
||||
type: String,
|
||||
},
|
||||
});
|
||||
|
||||
//定义事件
|
||||
const emit = defineEmits(['update:visible', 'cancel', 'val-change']);
|
||||
|
||||
const rules = {
|
||||
name: [
|
||||
{
|
||||
required: true,
|
||||
message: '请输入别名',
|
||||
trigger: ['change', 'blur'],
|
||||
},
|
||||
],
|
||||
type: [
|
||||
{
|
||||
required: true,
|
||||
message: '请选择数据库类型',
|
||||
trigger: ['change', 'blur'],
|
||||
},
|
||||
],
|
||||
host: [
|
||||
{
|
||||
required: true,
|
||||
message: '请输入主机ip和port',
|
||||
trigger: ['change', 'blur'],
|
||||
},
|
||||
],
|
||||
username: [
|
||||
{
|
||||
required: true,
|
||||
message: '请输入用户名',
|
||||
trigger: ['change', 'blur'],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const dbForm: any = ref(null);
|
||||
|
||||
const state = reactive({
|
||||
dialogVisible: false,
|
||||
tabActiveName: 'basic',
|
||||
form: {
|
||||
id: null,
|
||||
type: null,
|
||||
name: null,
|
||||
host: '',
|
||||
port: 3306,
|
||||
username: null,
|
||||
password: null,
|
||||
params: null,
|
||||
remark: '',
|
||||
sshTunnelMachineId: null as any,
|
||||
},
|
||||
// 原密码
|
||||
pwd: '',
|
||||
// 原用户名
|
||||
oldUserName: null,
|
||||
btnLoading: false,
|
||||
});
|
||||
|
||||
const { dialogVisible, tabActiveName, form, pwd, btnLoading } = toRefs(state);
|
||||
|
||||
watch(props, (newValue: any) => {
|
||||
state.dialogVisible = newValue.visible;
|
||||
if (!state.dialogVisible) {
|
||||
return;
|
||||
}
|
||||
state.tabActiveName = 'basic';
|
||||
if (newValue.data) {
|
||||
state.form = { ...newValue.data};
|
||||
state.oldUserName = state.form.username
|
||||
} else {
|
||||
state.form = { port: 3306 } as any;
|
||||
state.oldUserName = null
|
||||
}
|
||||
});
|
||||
|
||||
const getDbPwd = async () => {
|
||||
state.pwd = await dbApi.getInstancePwd.request({ id: state.form.id });
|
||||
};
|
||||
|
||||
const btnOk = async () => {
|
||||
if (!state.form.id) {
|
||||
notBlank(state.form.password, '新增操作,密码不可为空');
|
||||
} else if (state.form.username != state.oldUserName) {
|
||||
notBlank(state.form.password, '已修改用户名,请输入密码');
|
||||
}
|
||||
|
||||
dbForm.value.validate(async (valid: boolean) => {
|
||||
if (valid) {
|
||||
const reqForm = { ...state.form };
|
||||
reqForm.password = await RsaEncrypt(reqForm.password);
|
||||
if (!state.form.sshTunnelMachineId) {
|
||||
reqForm.sshTunnelMachineId = -1;
|
||||
}
|
||||
dbApi.saveInstance.request(reqForm).then(() => {
|
||||
ElMessage.success('保存成功');
|
||||
emit('val-change', state.form);
|
||||
state.btnLoading = true;
|
||||
setTimeout(() => {
|
||||
state.btnLoading = false;
|
||||
}, 1000);
|
||||
|
||||
cancel();
|
||||
});
|
||||
} else {
|
||||
ElMessage.error('请正确填写信息');
|
||||
return false;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const cancel = () => {
|
||||
emit('update:visible', false);
|
||||
emit('cancel');
|
||||
};
|
||||
</script>
|
||||
<style lang="scss"></style>
|
||||
182
mayfly_go_web/src/views/ops/db/InstanceList.vue
Normal file
182
mayfly_go_web/src/views/ops/db/InstanceList.vue
Normal file
@@ -0,0 +1,182 @@
|
||||
<template>
|
||||
<div class="db-list">
|
||||
<page-table
|
||||
ref="pageTableRef"
|
||||
:query="queryConfig"
|
||||
v-model:query-form="query"
|
||||
:show-selection="true"
|
||||
v-model:selection-data="state.selectionData"
|
||||
:data="datas"
|
||||
:columns="columns"
|
||||
:total="total"
|
||||
v-model:page-size="query.pageSize"
|
||||
v-model:page-num="query.pageNum"
|
||||
@pageChange="search()"
|
||||
>
|
||||
<template #queryRight>
|
||||
<el-button v-auth="perms.saveInstance" type="primary" icon="plus" @click="editInstance(false)">添加</el-button>
|
||||
<el-button v-auth="perms.delInstance" :disabled="selectionData.length < 1" @click="deleteInstance()" type="danger" icon="delete">删除</el-button>
|
||||
</template>
|
||||
|
||||
<template #more="{ data }">
|
||||
<el-button @click="showInfo(data)" link>详情</el-button>
|
||||
</template>
|
||||
|
||||
<template #action="{ data }">
|
||||
<el-button v-if="actionBtns[perms.saveInstance]" @click="editInstance(data)" type="primary" link>编辑</el-button>
|
||||
</template>
|
||||
</page-table>
|
||||
|
||||
<el-dialog v-model="infoDialog.visible">
|
||||
<el-descriptions title="详情" :column="3" border>
|
||||
<el-descriptions-item :span="2" label="名称">{{ infoDialog.data.name }}</el-descriptions-item>
|
||||
<el-descriptions-item :span="1" label="id">{{ infoDialog.data.id }}</el-descriptions-item>
|
||||
<el-descriptions-item :span="2" label="主机">{{ infoDialog.data.host }}</el-descriptions-item>
|
||||
<el-descriptions-item :span="1" label="端口">{{ infoDialog.data.port }}</el-descriptions-item>
|
||||
|
||||
<el-descriptions-item :span="2" label="用户名">{{ infoDialog.data.username }}</el-descriptions-item>
|
||||
<el-descriptions-item :span="1" label="类型">{{ infoDialog.data.type }}</el-descriptions-item>
|
||||
|
||||
<el-descriptions-item :span="3" label="连接参数">{{ infoDialog.data.params }}</el-descriptions-item>
|
||||
<el-descriptions-item :span="3" label="备注">{{ infoDialog.data.remark }}</el-descriptions-item>
|
||||
|
||||
<el-descriptions-item :span="3" label="SSH隧道">{{ infoDialog.data.sshTunnelMachineId > 0 ? '是' : '否' }} </el-descriptions-item>
|
||||
|
||||
<el-descriptions-item :span="2" label="创建时间">{{ dateFormat(infoDialog.data.createTime) }} </el-descriptions-item>
|
||||
<el-descriptions-item :span="1" label="创建者">{{ infoDialog.data.creator }}</el-descriptions-item>
|
||||
|
||||
<el-descriptions-item :span="2" label="更新时间">{{ dateFormat(infoDialog.data.updateTime) }} </el-descriptions-item>
|
||||
<el-descriptions-item :span="1" label="修改者">{{ infoDialog.data.modifier }}</el-descriptions-item>
|
||||
</el-descriptions>
|
||||
</el-dialog>
|
||||
|
||||
<instance-edit @val-change="valChange" :title="instanceEditDialog.title" v-model:visible="instanceEditDialog.visible" v-model:data="instanceEditDialog.data"></instance-edit>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import { ref, toRefs, reactive, computed, onMounted, defineAsyncComponent } from 'vue';
|
||||
import { ElMessage, ElMessageBox } from 'element-plus';
|
||||
import { dbApi } from './api';
|
||||
import { dateFormat } from '@/common/utils/date';
|
||||
import PageTable from '@/components/pagetable/PageTable.vue';
|
||||
import { TableColumn, TableQuery } from '@/components/pagetable';
|
||||
import { hasPerms } from '@/components/auth/auth';
|
||||
|
||||
const InstanceEdit = defineAsyncComponent(() => import('./InstanceEdit.vue'));
|
||||
|
||||
const perms = {
|
||||
saveInstance: 'instance:save',
|
||||
delInstance: 'instance:del',
|
||||
};
|
||||
|
||||
const queryConfig = [TableQuery.text('name', '名称')];
|
||||
|
||||
const columns = ref([
|
||||
TableColumn.new('name', '名称'),
|
||||
TableColumn.new('host', 'host:port').setFormatFunc((data: any, _prop: string) => `${data.host}:${data.port}`),
|
||||
TableColumn.new('type', '类型'),
|
||||
TableColumn.new('username', '用户名'),
|
||||
TableColumn.new('remark', '备注'),
|
||||
TableColumn.new('more', '更多').isSlot().setMinWidth(50).fixedRight(),
|
||||
]);
|
||||
|
||||
// 该用户拥有的的操作列按钮权限
|
||||
const actionBtns = hasPerms([perms.saveInstance]);
|
||||
const actionColumn = TableColumn.new('action', '操作').isSlot().setMinWidth(65).fixedRight().alignCenter();
|
||||
|
||||
const pageTableRef: any = ref(null);
|
||||
|
||||
const state = reactive({
|
||||
row: {},
|
||||
dbId: 0,
|
||||
db: '',
|
||||
/**
|
||||
* 选中的数据
|
||||
*/
|
||||
selectionData: [],
|
||||
/**
|
||||
* 查询条件
|
||||
*/
|
||||
query: {
|
||||
name: null,
|
||||
pageNum: 1,
|
||||
pageSize: 10,
|
||||
},
|
||||
datas: [],
|
||||
total: 0,
|
||||
infoDialog: {
|
||||
visible: false,
|
||||
data: null as any,
|
||||
},
|
||||
instanceEditDialog: {
|
||||
visible: false,
|
||||
data: null as any,
|
||||
title: '新增数据库实例',
|
||||
},
|
||||
});
|
||||
|
||||
const {
|
||||
dbId,
|
||||
db,
|
||||
selectionData,
|
||||
query,
|
||||
datas,
|
||||
total,
|
||||
infoDialog,
|
||||
instanceEditDialog,
|
||||
} = toRefs(state);
|
||||
|
||||
onMounted(async () => {
|
||||
if (Object.keys(actionBtns).length > 0) {
|
||||
columns.value.push(actionColumn);
|
||||
}
|
||||
search();
|
||||
});
|
||||
|
||||
const search = async () => {
|
||||
try {
|
||||
pageTableRef.value.loading(true);
|
||||
let res: any = await dbApi.instances.request(state.query);
|
||||
state.datas = res.list;
|
||||
state.total = res.total;
|
||||
} finally {
|
||||
pageTableRef.value.loading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const showInfo = (info: any) => {
|
||||
state.infoDialog.data = info;
|
||||
state.infoDialog.visible = true;
|
||||
};
|
||||
|
||||
const editInstance = async (data: any) => {
|
||||
if (!data) {
|
||||
state.instanceEditDialog.data = null;
|
||||
state.instanceEditDialog.title = '新增数据库实例';
|
||||
} else {
|
||||
state.instanceEditDialog.data = data;
|
||||
state.instanceEditDialog.title = '修改数据库实例';
|
||||
}
|
||||
state.instanceEditDialog.visible = true;
|
||||
};
|
||||
|
||||
const valChange = () => {
|
||||
search();
|
||||
};
|
||||
|
||||
const deleteInstance = async () => {
|
||||
try {
|
||||
await ElMessageBox.confirm(`确定删除数据库实例【${state.selectionData.map((x: any) => x.name).join(', ')}】?`, '提示', {
|
||||
confirmButtonText: '确定',
|
||||
cancelButtonText: '取消',
|
||||
type: 'warning',
|
||||
});
|
||||
await dbApi.deleteInstance.request({ id: state.selectionData.map((x: any) => x.id).join(',') });
|
||||
ElMessage.success('删除成功');
|
||||
search();
|
||||
} catch (err) {}
|
||||
};
|
||||
|
||||
</script>
|
||||
<style lang="scss"></style>
|
||||
@@ -5,8 +5,6 @@ export const dbApi = {
|
||||
dbs: Api.newGet('/dbs'),
|
||||
dbTags: Api.newGet('/dbs/tags'),
|
||||
saveDb: Api.newPost('/dbs'),
|
||||
getAllDatabase: Api.newPost('/dbs/databases'),
|
||||
getDbPwd: Api.newGet('/dbs/{id}/pwd'),
|
||||
deleteDb: Api.newDelete('/dbs/{id}'),
|
||||
dumpDb: Api.newPost('/dbs/{id}/dump'),
|
||||
tableInfos: Api.newGet('/dbs/{id}/t-infos'),
|
||||
@@ -26,4 +24,12 @@ export const dbApi = {
|
||||
deleteDbSql: Api.newDelete('/dbs/{id}/sql'),
|
||||
// 获取数据库sql执行记录
|
||||
getSqlExecs: Api.newGet('/dbs/{dbId}/sql-execs'),
|
||||
|
||||
// 获取权限列表
|
||||
instances: Api.newGet('/instances'),
|
||||
getInstance: Api.newGet("/instances/{instanceId}"),
|
||||
getAllDatabase: Api.newGet('/instances/{instanceId}/databases'),
|
||||
saveInstance: Api.newPost('/instances'),
|
||||
getInstancePwd: Api.newGet('/instances/{id}/pwd'),
|
||||
deleteInstance: Api.newDelete('/instances/{id}'),
|
||||
};
|
||||
|
||||
@@ -14,9 +14,7 @@ import (
|
||||
"mayfly-go/pkg/gormx"
|
||||
"mayfly-go/pkg/model"
|
||||
"mayfly-go/pkg/req"
|
||||
"mayfly-go/pkg/utils/cryptox"
|
||||
"mayfly-go/pkg/utils/stringx"
|
||||
"mayfly-go/pkg/utils/structx"
|
||||
"mayfly-go/pkg/ws"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -27,6 +25,7 @@ import (
|
||||
)
|
||||
|
||||
type Db struct {
|
||||
InstanceApp application.Instance
|
||||
DbApp application.Db
|
||||
DbSqlExecApp application.DbSqlExec
|
||||
MsgApp msgapp.Msg
|
||||
@@ -58,46 +57,22 @@ func (d *Db) Save(rc *req.Ctx) {
|
||||
form := &form.DbForm{}
|
||||
db := ginx.BindJsonAndCopyTo[*entity.Db](rc.GinCtx, form, new(entity.Db))
|
||||
|
||||
// 密码解密,并使用解密后的赋值
|
||||
originPwd, err := cryptox.DefaultRsaDecrypt(form.Password, true)
|
||||
biz.ErrIsNilAppendErr(err, "解密密码错误: %s")
|
||||
db.Password = originPwd
|
||||
|
||||
// 密码脱敏记录日志
|
||||
form.Password = "****"
|
||||
rc.ReqParam = form
|
||||
|
||||
db.SetBaseInfo(rc.LoginAccount)
|
||||
d.DbApp.Save(db)
|
||||
}
|
||||
|
||||
// 获取数据库实例密码,由于数据库是加密存储,故提供该接口展示原文密码
|
||||
func (d *Db) GetDbPwd(rc *req.Ctx) {
|
||||
dbId := GetDbId(rc.GinCtx)
|
||||
dbEntity := d.DbApp.GetById(dbId, "Password")
|
||||
dbEntity.PwdDecrypt()
|
||||
rc.ResData = dbEntity.Password
|
||||
}
|
||||
|
||||
// 获取数据库实例的所有数据库名
|
||||
func (d *Db) GetDatabaseNames(rc *req.Ctx) {
|
||||
form := &form.DbForm{}
|
||||
ginx.BindJsonAndValid(rc.GinCtx, form)
|
||||
|
||||
db := new(entity.Db)
|
||||
structx.Copy(db, form)
|
||||
|
||||
// 密码解密,并使用解密后的赋值
|
||||
originPwd, err := cryptox.DefaultRsaDecrypt(form.Password, true)
|
||||
biz.ErrIsNilAppendErr(err, "解密密码错误: %s")
|
||||
db.Password = originPwd
|
||||
|
||||
// 如果id不为空,并且密码为空则从数据库查询
|
||||
if form.Id != 0 && db.Password == "" {
|
||||
db = d.DbApp.GetById(form.Id)
|
||||
db.PwdDecrypt()
|
||||
}
|
||||
rc.ResData = d.DbApp.GetDatabases(db)
|
||||
instance := d.InstanceApp.GetById(form.InstanceId, "Password")
|
||||
biz.NotNil(instance, "获取数据库实例错误")
|
||||
instance.PwdDecrypt()
|
||||
rc.ResData = d.InstanceApp.GetDatabases(instance)
|
||||
rc.ResData = d.InstanceApp.GetDatabases(instance)
|
||||
}
|
||||
|
||||
func (d *Db) DeleteDb(rc *req.Ctx) {
|
||||
@@ -115,20 +90,29 @@ func (d *Db) DeleteDb(rc *req.Ctx) {
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Db) getDbConnection(g *gin.Context) *application.DbConnection {
|
||||
dbName := g.Query("db")
|
||||
biz.NotEmpty(dbName, "db不能为空")
|
||||
dbId := getDbId(g)
|
||||
db := d.DbApp.GetById(dbId)
|
||||
instance := d.InstanceApp.GetById(db.InstanceId)
|
||||
return d.DbApp.GetDbConnection(db, instance, dbName)
|
||||
}
|
||||
|
||||
func (d *Db) TableInfos(rc *req.Ctx) {
|
||||
rc.ResData = d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx)).GetMeta().GetTableInfos()
|
||||
rc.ResData = d.getDbConnection(rc.GinCtx).GetMeta().GetTableInfos()
|
||||
}
|
||||
|
||||
func (d *Db) TableIndex(rc *req.Ctx) {
|
||||
tn := rc.GinCtx.Query("tableName")
|
||||
biz.NotEmpty(tn, "tableName不能为空")
|
||||
rc.ResData = d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx)).GetMeta().GetTableIndex(tn)
|
||||
rc.ResData = d.getDbConnection(rc.GinCtx).GetMeta().GetTableIndex(tn)
|
||||
}
|
||||
|
||||
func (d *Db) GetCreateTableDdl(rc *req.Ctx) {
|
||||
tn := rc.GinCtx.Query("tableName")
|
||||
biz.NotEmpty(tn, "tableName不能为空")
|
||||
rc.ResData = d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx)).GetMeta().GetCreateTableDdl(tn)
|
||||
rc.ResData = d.getDbConnection(rc.GinCtx).GetMeta().GetCreateTableDdl(tn)
|
||||
}
|
||||
|
||||
func (d *Db) ExecSql(rc *req.Ctx) {
|
||||
@@ -136,9 +120,10 @@ func (d *Db) ExecSql(rc *req.Ctx) {
|
||||
form := &form.DbSqlExecForm{}
|
||||
ginx.BindJsonAndValid(g, form)
|
||||
|
||||
id := GetDbId(g)
|
||||
db := form.Db
|
||||
dbInstance := d.DbApp.GetDbInstance(id, db)
|
||||
dbId := getDbId(g)
|
||||
db := d.DbApp.GetById(dbId)
|
||||
instance := d.InstanceApp.GetById(db.InstanceId)
|
||||
dbInstance := d.DbApp.GetDbConnection(db, instance, form.Db)
|
||||
biz.ErrIsNilAppendErr(d.TagApp.CanAccess(rc.LoginAccount.Id, dbInstance.Info.TagPath), "%s")
|
||||
|
||||
rc.ReqParam = fmt.Sprintf("%s\n-> %s", dbInstance.Info.GetLogDesc(), form.Sql)
|
||||
@@ -148,8 +133,8 @@ func (d *Db) ExecSql(rc *req.Ctx) {
|
||||
sql := stringx.TrimSpaceAndBr(form.Sql)
|
||||
|
||||
execReq := &application.DbSqlExecReq{
|
||||
DbId: id,
|
||||
Db: db,
|
||||
DbId: dbId,
|
||||
Db: form.Db,
|
||||
Remark: form.Remark,
|
||||
DbInstance: dbInstance,
|
||||
LoginAccount: rc.LoginAccount,
|
||||
@@ -192,9 +177,10 @@ func (d *Db) ExecSqlFile(rc *req.Ctx) {
|
||||
|
||||
file, _ := fileheader.Open()
|
||||
filename := fileheader.Filename
|
||||
dbId, db := GetIdAndDb(g)
|
||||
dbId := getDbId(g)
|
||||
dbName := getDbName(g)
|
||||
|
||||
dbInstance := d.DbApp.GetDbInstance(dbId, db)
|
||||
dbInstance := d.getDbConnection(rc.GinCtx)
|
||||
biz.ErrIsNilAppendErr(d.TagApp.CanAccess(rc.LoginAccount.Id, dbInstance.Info.TagPath), "%s")
|
||||
rc.ReqParam = fmt.Sprintf("%s -> filename: %s", dbInstance.Info.GetLogDesc(), filename)
|
||||
|
||||
@@ -216,7 +202,7 @@ func (d *Db) ExecSqlFile(rc *req.Ctx) {
|
||||
|
||||
execReq := &application.DbSqlExecReq{
|
||||
DbId: dbId,
|
||||
Db: db,
|
||||
Db: dbName,
|
||||
Remark: fileheader.Filename,
|
||||
DbInstance: dbInstance,
|
||||
LoginAccount: rc.LoginAccount,
|
||||
@@ -249,7 +235,7 @@ func (d *Db) ExecSqlFile(rc *req.Ctx) {
|
||||
// 数据库dump
|
||||
func (d *Db) DumpSql(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
dbId, db := GetIdAndDb(g)
|
||||
db := getDbName(g)
|
||||
dumpType := g.Query("type")
|
||||
tablesStr := g.Query("tables")
|
||||
biz.NotEmpty(tablesStr, "请选择要导出的表")
|
||||
@@ -260,7 +246,7 @@ func (d *Db) DumpSql(rc *req.Ctx) {
|
||||
// 是否需要导出数据
|
||||
needData := dumpType == "2" || dumpType == "3"
|
||||
|
||||
dbInstance := d.DbApp.GetDbInstance(dbId, db)
|
||||
dbInstance := d.getDbConnection(rc.GinCtx)
|
||||
biz.ErrIsNilAppendErr(d.TagApp.CanAccess(rc.LoginAccount.Id, dbInstance.Info.TagPath), "%s")
|
||||
|
||||
now := time.Now()
|
||||
@@ -275,7 +261,7 @@ func (d *Db) DumpSql(rc *req.Ctx) {
|
||||
writer.WriteString(fmt.Sprintf("\n-- 导出数据库: %s ", db))
|
||||
writer.WriteString("\n-- ----------------------------\n")
|
||||
|
||||
dbmeta := d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx)).GetMeta()
|
||||
dbmeta := d.getDbConnection(rc.GinCtx).GetMeta()
|
||||
for _, table := range tables {
|
||||
if needStruct {
|
||||
writer.WriteString(fmt.Sprintf("\n-- ----------------------------\n-- 表结构: %s \n-- ----------------------------\n", table))
|
||||
@@ -329,7 +315,7 @@ func (d *Db) DumpSql(rc *req.Ctx) {
|
||||
|
||||
// @router /api/db/:dbId/t-metadata [get]
|
||||
func (d *Db) TableMA(rc *req.Ctx) {
|
||||
dbi := d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx))
|
||||
dbi := d.getDbConnection(rc.GinCtx)
|
||||
rc.ResData = dbi.GetMeta().GetTables()
|
||||
}
|
||||
|
||||
@@ -339,13 +325,13 @@ func (d *Db) ColumnMA(rc *req.Ctx) {
|
||||
tn := g.Query("tableName")
|
||||
biz.NotEmpty(tn, "tableName不能为空")
|
||||
|
||||
dbi := d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx))
|
||||
dbi := d.getDbConnection(rc.GinCtx)
|
||||
rc.ResData = dbi.GetMeta().GetColumns(tn)
|
||||
}
|
||||
|
||||
// @router /api/db/:dbId/hint-tables [get]
|
||||
func (d *Db) HintTables(rc *req.Ctx) {
|
||||
dbi := d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx))
|
||||
dbi := d.getDbConnection(rc.GinCtx)
|
||||
|
||||
dm := dbi.GetMeta()
|
||||
// 获取所有表
|
||||
@@ -391,7 +377,7 @@ func (d *Db) SaveSql(rc *req.Ctx) {
|
||||
ginx.BindJsonAndValid(g, dbSqlForm)
|
||||
rc.ReqParam = dbSqlForm
|
||||
|
||||
dbId := GetDbId(g)
|
||||
dbId := getDbId(g)
|
||||
// 判断dbId是否存在
|
||||
err := gormx.GetById(new(entity.Db), dbId)
|
||||
biz.ErrIsNil(err, "该数据库信息不存在")
|
||||
@@ -413,9 +399,10 @@ func (d *Db) SaveSql(rc *req.Ctx) {
|
||||
|
||||
// 获取所有保存的sql names
|
||||
func (d *Db) GetSqlNames(rc *req.Ctx) {
|
||||
id, db := GetIdAndDb(rc.GinCtx)
|
||||
dbId := getDbId(rc.GinCtx)
|
||||
dbName := getDbName(rc.GinCtx)
|
||||
// 获取用于是否有该dbsql的保存记录,有则更改,否则新增
|
||||
dbSql := &entity.DbSql{Type: 1, DbId: id, Db: db}
|
||||
dbSql := &entity.DbSql{Type: 1, DbId: dbId, Db: dbName}
|
||||
dbSql.CreatorId = rc.LoginAccount.Id
|
||||
var sqls []entity.DbSql
|
||||
gormx.ListBy(dbSql, &sqls, "id", "name")
|
||||
@@ -425,7 +412,7 @@ func (d *Db) GetSqlNames(rc *req.Ctx) {
|
||||
|
||||
// 删除保存的sql
|
||||
func (d *Db) DeleteSql(rc *req.Ctx) {
|
||||
dbSql := &entity.DbSql{Type: 1, DbId: GetDbId(rc.GinCtx)}
|
||||
dbSql := &entity.DbSql{Type: 1, DbId: getDbId(rc.GinCtx)}
|
||||
dbSql.CreatorId = rc.LoginAccount.Id
|
||||
dbSql.Name = rc.GinCtx.Query("name")
|
||||
dbSql.Db = rc.GinCtx.Query("db")
|
||||
@@ -436,9 +423,10 @@ func (d *Db) DeleteSql(rc *req.Ctx) {
|
||||
|
||||
// @router /api/db/:dbId/sql [get]
|
||||
func (d *Db) GetSql(rc *req.Ctx) {
|
||||
id, db := GetIdAndDb(rc.GinCtx)
|
||||
dbId := getDbId(rc.GinCtx)
|
||||
dbName := getDbName(rc.GinCtx)
|
||||
// 根据创建者id, 数据库id,以及sql模板名称查询保存的sql信息
|
||||
dbSql := &entity.DbSql{Type: 1, DbId: id, Db: db}
|
||||
dbSql := &entity.DbSql{Type: 1, DbId: dbId, Db: dbName}
|
||||
dbSql.CreatorId = rc.LoginAccount.Id
|
||||
dbSql.Name = rc.GinCtx.Query("name")
|
||||
|
||||
@@ -449,14 +437,14 @@ func (d *Db) GetSql(rc *req.Ctx) {
|
||||
rc.ResData = dbSql
|
||||
}
|
||||
|
||||
func GetDbId(g *gin.Context) uint64 {
|
||||
func getDbId(g *gin.Context) uint64 {
|
||||
dbId, _ := strconv.Atoi(g.Param("dbId"))
|
||||
biz.IsTrue(dbId > 0, "dbId错误")
|
||||
return uint64(dbId)
|
||||
}
|
||||
|
||||
func GetIdAndDb(g *gin.Context) (uint64, string) {
|
||||
func getDbName(g *gin.Context) string {
|
||||
db := g.Query("db")
|
||||
biz.NotEmpty(db, "db不能为空")
|
||||
return GetDbId(g), db
|
||||
return db
|
||||
}
|
||||
|
||||
@@ -1,19 +1,13 @@
|
||||
package form
|
||||
|
||||
type DbForm struct {
|
||||
Id uint64 `json:"id"`
|
||||
Name string `binding:"required" json:"name"`
|
||||
Type string `binding:"required" json:"type"` // 类型,mysql oracle等
|
||||
Host string `binding:"required" json:"host"`
|
||||
Port int `binding:"required" json:"port"`
|
||||
Username string `binding:"required" json:"username"`
|
||||
Password string `json:"password"`
|
||||
Params string `json:"params"`
|
||||
Database string `json:"database"`
|
||||
Remark string `json:"remark"`
|
||||
TagId uint64 `binding:"required" json:"tagId"`
|
||||
TagPath string `binding:"required" json:"tagPath"`
|
||||
SshTunnelMachineId int `json:"sshTunnelMachineId"`
|
||||
Id uint64 `json:"id"`
|
||||
Name string `binding:"required" json:"name"`
|
||||
Database string `json:"database"`
|
||||
Remark string `json:"remark"`
|
||||
TagId uint64 `binding:"required" json:"tagId"`
|
||||
TagPath string `binding:"required" json:"tagPath"`
|
||||
InstanceId uint64 `binding:"required" json:"instanceId"`
|
||||
}
|
||||
|
||||
type DbSqlSaveForm struct {
|
||||
|
||||
14
server/internal/db/api/form/instance.go
Normal file
14
server/internal/db/api/form/instance.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package form
|
||||
|
||||
type InstanceForm struct {
|
||||
Id uint64 `json:"id"`
|
||||
Name string `binding:"required" json:"name"`
|
||||
Type string `binding:"required" json:"type"` // 类型,mysql oracle等
|
||||
Host string `binding:"required" json:"host"`
|
||||
Port int `binding:"required" json:"port"`
|
||||
Username string `binding:"required" json:"username"`
|
||||
Password string `json:"password"`
|
||||
Params string `json:"params"`
|
||||
Remark string `json:"remark"`
|
||||
SshTunnelMachineId int `json:"sshTunnelMachineId"`
|
||||
}
|
||||
103
server/internal/db/api/instance.go
Normal file
103
server/internal/db/api/instance.go
Normal file
@@ -0,0 +1,103 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"mayfly-go/internal/db/api/form"
|
||||
"mayfly-go/internal/db/api/vo"
|
||||
"mayfly-go/internal/db/application"
|
||||
"mayfly-go/internal/db/domain/entity"
|
||||
msgapp "mayfly-go/internal/msg/application"
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/ginx"
|
||||
"mayfly-go/pkg/req"
|
||||
"mayfly-go/pkg/utils/cryptox"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Instance struct {
|
||||
InstanceApp application.Instance
|
||||
DbApp application.Db
|
||||
MsgApp msgapp.Msg
|
||||
}
|
||||
|
||||
// Instances 获取数据库实例信息
|
||||
// @router /api/instances [get]
|
||||
func (d *Instance) Instances(rc *req.Ctx) {
|
||||
queryCond, page := ginx.BindQueryAndPage[*entity.InstanceQuery](rc.GinCtx, new(entity.InstanceQuery))
|
||||
rc.ResData = d.InstanceApp.GetPageList(queryCond, page, new([]vo.SelectDataInstanceVO))
|
||||
}
|
||||
|
||||
// SaveInstance 保存数据库实例信息
|
||||
// @router /api/instances [post]
|
||||
func (d *Instance) SaveInstance(rc *req.Ctx) {
|
||||
form := &form.InstanceForm{}
|
||||
instance := ginx.BindJsonAndCopyTo[*entity.Instance](rc.GinCtx, form, new(entity.Instance))
|
||||
|
||||
// 密码解密,并使用解密后的赋值
|
||||
originPwd, err := cryptox.DefaultRsaDecrypt(form.Password, true)
|
||||
biz.ErrIsNilAppendErr(err, "解密密码错误: %s")
|
||||
instance.Password = originPwd
|
||||
|
||||
// 密码脱敏记录日志
|
||||
form.Password = "****"
|
||||
rc.ReqParam = form
|
||||
|
||||
instance.SetBaseInfo(rc.LoginAccount)
|
||||
d.InstanceApp.Save(instance)
|
||||
}
|
||||
|
||||
// GetInstance 获取数据库实例密码,由于数据库是加密存储,故提供该接口展示原文密码
|
||||
// @router /api/instances/:instance [GET]
|
||||
func (d *Instance) GetInstance(rc *req.Ctx) {
|
||||
dbId := getInstanceId(rc.GinCtx)
|
||||
dbEntity := d.InstanceApp.GetById(dbId)
|
||||
biz.IsTrue(dbEntity != nil, "获取数据库实例错误")
|
||||
dbEntity.Password = ""
|
||||
rc.ResData = dbEntity
|
||||
}
|
||||
|
||||
// GetInstancePwd 获取数据库实例密码,由于数据库是加密存储,故提供该接口展示原文密码
|
||||
// @router /api/instances/:instance/pwd [GET]
|
||||
func (d *Instance) GetInstancePwd(rc *req.Ctx) {
|
||||
instanceId := getInstanceId(rc.GinCtx)
|
||||
instanceEntity := d.InstanceApp.GetById(instanceId, "Password")
|
||||
biz.IsTrue(instanceEntity != nil, "获取数据库实例错误")
|
||||
instanceEntity.PwdDecrypt()
|
||||
rc.ResData = instanceEntity.Password
|
||||
}
|
||||
|
||||
// DeleteInstance 删除数据库实例信息
|
||||
// @router /api/instances/:instance [DELETE]
|
||||
func (d *Instance) DeleteInstance(rc *req.Ctx) {
|
||||
idsStr := ginx.PathParam(rc.GinCtx, "instanceId")
|
||||
rc.ReqParam = idsStr
|
||||
ids := strings.Split(idsStr, ",")
|
||||
|
||||
for _, v := range ids {
|
||||
value, err := strconv.Atoi(v)
|
||||
biz.ErrIsNilAppendErr(err, "string类型转换为int异常: %s")
|
||||
instanceId := uint64(value)
|
||||
if d.DbApp.Count(&entity.DbQuery{InstanceId: instanceId}) != 0 {
|
||||
instance := d.InstanceApp.GetById(instanceId, "name")
|
||||
biz.NotNil(instance, "获取数据库实例错误,数据库实例ID为:%d", instance.Id)
|
||||
biz.IsTrue(false, "不能删除数据库实例【%s】,请先删除其关联的数据库资源。", instance.Name)
|
||||
}
|
||||
d.InstanceApp.Delete(instanceId)
|
||||
}
|
||||
}
|
||||
|
||||
func getInstanceId(g *gin.Context) uint64 {
|
||||
instanceId, _ := strconv.Atoi(g.Param("instanceId"))
|
||||
biz.IsTrue(instanceId > 0, "instanceId 错误")
|
||||
return uint64(instanceId)
|
||||
}
|
||||
|
||||
// 获取数据库实例的所有数据库名
|
||||
func (d *Instance) GetDatabaseNames(rc *req.Ctx) {
|
||||
instanceId := getInstanceId(rc.GinCtx)
|
||||
instance := d.InstanceApp.GetById(instanceId, "Password")
|
||||
biz.IsTrue(instance != nil, "获取数据库实例错误")
|
||||
instance.PwdDecrypt()
|
||||
rc.ResData = d.InstanceApp.GetDatabases(instance)
|
||||
}
|
||||
@@ -4,24 +4,21 @@ import "time"
|
||||
|
||||
type SelectDataDbVO struct {
|
||||
//models.BaseModel
|
||||
Id *int64 `json:"id"`
|
||||
Name *string `json:"name"`
|
||||
Host *string `json:"host"`
|
||||
Port *int `json:"port"`
|
||||
Type *string `json:"type"`
|
||||
Params *string `json:"params"`
|
||||
Database *string `json:"database"`
|
||||
Username *string `json:"username"`
|
||||
Remark *string `json:"remark"`
|
||||
TagId *int64 `json:"tagId"`
|
||||
TagPath *string `json:"tagPath"`
|
||||
Id *int64 `json:"id"`
|
||||
Name *string `json:"name"`
|
||||
Database *string `json:"database"`
|
||||
Remark *string `json:"remark"`
|
||||
TagId *int64 `json:"tagId"`
|
||||
TagPath *string `json:"tagPath"`
|
||||
|
||||
InstanceId *int64 `json:"instanceId"`
|
||||
InstanceName *string `json:"instanceName"`
|
||||
InstanceType *string `json:"type"`
|
||||
|
||||
CreateTime *time.Time `json:"createTime"`
|
||||
Creator *string `json:"creator"`
|
||||
CreatorId *int64 `json:"creatorId"`
|
||||
|
||||
UpdateTime *time.Time `json:"updateTime"`
|
||||
Modifier *string `json:"modifier"`
|
||||
ModifierId *int64 `json:"modifierId"`
|
||||
|
||||
SshTunnelMachineId int `json:"sshTunnelMachineId"`
|
||||
}
|
||||
|
||||
24
server/internal/db/api/vo/instance.go
Normal file
24
server/internal/db/api/vo/instance.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package vo
|
||||
|
||||
import "time"
|
||||
|
||||
type SelectDataInstanceVO struct {
|
||||
//models.BaseModel
|
||||
Id *int64 `json:"id"`
|
||||
Name *string `json:"name"`
|
||||
Host *string `json:"host"`
|
||||
Port *int `json:"port"`
|
||||
Type *string `json:"type"`
|
||||
Params *string `json:"params"`
|
||||
Username *string `json:"username"`
|
||||
Remark *string `json:"remark"`
|
||||
CreateTime *time.Time `json:"createTime"`
|
||||
Creator *string `json:"creator"`
|
||||
CreatorId *int64 `json:"creatorId"`
|
||||
|
||||
UpdateTime *time.Time `json:"updateTime"`
|
||||
Modifier *string `json:"modifier"`
|
||||
ModifierId *int64 `json:"modifierId"`
|
||||
|
||||
SshTunnelMachineId int `json:"sshTunnelMachineId"`
|
||||
}
|
||||
@@ -5,10 +5,15 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
instanceApp Instance = newInstanceApp(persistence.GetInstanceRepo())
|
||||
dbApp Db = newDbApp(persistence.GetDbRepo(), persistence.GetDbSqlRepo())
|
||||
dbSqlExecApp DbSqlExec = newDbSqlExecApp(persistence.GetDbSqlExecRepo())
|
||||
)
|
||||
|
||||
func GetInstanceApp() Instance {
|
||||
return instanceApp
|
||||
}
|
||||
|
||||
func GetDbApp() Db {
|
||||
return dbApp
|
||||
}
|
||||
|
||||
@@ -12,7 +12,6 @@ import (
|
||||
"mayfly-go/pkg/logx"
|
||||
"mayfly-go/pkg/model"
|
||||
"mayfly-go/pkg/utils/collx"
|
||||
"mayfly-go/pkg/utils/structx"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -40,10 +39,7 @@ type Db interface {
|
||||
// 获取数据库连接实例
|
||||
// @param id 数据库实例id
|
||||
// @param db 数据库
|
||||
GetDbInstance(id uint64, db string) *DbInstance
|
||||
|
||||
// 获取数据库实例的所有数据库列表
|
||||
GetDatabases(entity *entity.Db) []string
|
||||
GetDbConnection(db *entity.Db, instance *entity.Instance, dbName string) *DbConnection
|
||||
}
|
||||
|
||||
func newDbApp(dbRepo repository.Db, dbSqlRepo repository.DbSql) Db {
|
||||
@@ -78,32 +74,19 @@ func (d *dbAppImpl) GetById(id uint64, cols ...string) *entity.Db {
|
||||
}
|
||||
|
||||
func (d *dbAppImpl) Save(dbEntity *entity.Db) {
|
||||
// 默认tcp连接
|
||||
dbEntity.Network = dbEntity.GetNetwork()
|
||||
|
||||
// 测试连接
|
||||
if dbEntity.Password != "" {
|
||||
TestConnection(dbEntity)
|
||||
}
|
||||
|
||||
// 查找是否存在该库
|
||||
oldDb := &entity.Db{Host: dbEntity.Host, Port: dbEntity.Port, Username: dbEntity.Username}
|
||||
if dbEntity.SshTunnelMachineId > 0 {
|
||||
oldDb.SshTunnelMachineId = dbEntity.SshTunnelMachineId
|
||||
}
|
||||
oldDb := &entity.Db{Name: dbEntity.Name}
|
||||
err := d.GetDbBy(oldDb)
|
||||
|
||||
if dbEntity.Id == 0 {
|
||||
biz.NotEmpty(dbEntity.Password, "密码不能为空")
|
||||
biz.IsTrue(err != nil, "该数据库实例已存在")
|
||||
dbEntity.PwdEncrypt()
|
||||
biz.IsTrue(err != nil, "该数据库资源已存在")
|
||||
d.dbRepo.Insert(dbEntity)
|
||||
return
|
||||
}
|
||||
|
||||
// 如果存在该库,则校验修改的库是否为该库
|
||||
if err == nil {
|
||||
biz.IsTrue(oldDb.Id == dbEntity.Id, "该数据库实例已存在")
|
||||
biz.IsTrue(oldDb.Id == dbEntity.Id, "该数据库资源已存在")
|
||||
}
|
||||
|
||||
dbId := dbEntity.Id
|
||||
@@ -129,7 +112,6 @@ func (d *dbAppImpl) Save(dbEntity *entity.Db) {
|
||||
d.dbSqlRepo.DeleteBy(&entity.DbSql{DbId: dbId, Db: v.(string)})
|
||||
}
|
||||
|
||||
dbEntity.PwdEncrypt()
|
||||
d.dbRepo.Update(dbEntity)
|
||||
}
|
||||
|
||||
@@ -145,75 +127,48 @@ func (d *dbAppImpl) Delete(id uint64) {
|
||||
d.dbSqlRepo.DeleteBy(&entity.DbSql{DbId: id})
|
||||
}
|
||||
|
||||
func (d *dbAppImpl) GetDatabases(ed *entity.Db) []string {
|
||||
ed.Network = ed.GetNetwork()
|
||||
databases := make([]string, 0)
|
||||
var dbConn *sql.DB
|
||||
var metaDb string
|
||||
var getDatabasesSql string
|
||||
if ed.Type == entity.DbTypeMysql {
|
||||
metaDb = "information_schema"
|
||||
getDatabasesSql = "SELECT SCHEMA_NAME AS dbname FROM SCHEMATA"
|
||||
} else {
|
||||
metaDb = "postgres"
|
||||
getDatabasesSql = "SELECT datname AS dbname FROM pg_database"
|
||||
}
|
||||
|
||||
dbConn, err := GetDbConn(ed, metaDb)
|
||||
biz.ErrIsNilAppendErr(err, "数据库连接失败: %s")
|
||||
defer dbConn.Close()
|
||||
|
||||
_, res, err := SelectDataByDb(dbConn, getDatabasesSql)
|
||||
biz.ErrIsNilAppendErr(err, "获取数据库列表失败")
|
||||
for _, re := range res {
|
||||
databases = append(databases, re["dbname"].(string))
|
||||
}
|
||||
return databases
|
||||
}
|
||||
|
||||
var mutex sync.Mutex
|
||||
|
||||
func (da *dbAppImpl) GetDbInstance(id uint64, db string) *DbInstance {
|
||||
cacheKey := GetDbCacheKey(id, db)
|
||||
func (d *dbAppImpl) GetDbConnection(db *entity.Db, instance *entity.Instance, dbName string) *DbConnection {
|
||||
cacheKey := GetDbCacheKey(db.Id, dbName)
|
||||
|
||||
// Id不为0,则为需要缓存
|
||||
needCache := id != 0
|
||||
needCache := db.Id != 0
|
||||
if needCache {
|
||||
load, ok := dbCache.Get(cacheKey)
|
||||
if ok {
|
||||
return load.(*DbInstance)
|
||||
return load.(*DbConnection)
|
||||
}
|
||||
}
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
|
||||
d := da.GetById(id)
|
||||
biz.NotNil(d, "数据库信息不存在")
|
||||
biz.IsTrue(strings.Contains(d.Database, db), "未配置该库的操作权限")
|
||||
biz.NotNil(db, "数据库信息不存在")
|
||||
biz.IsTrue(strings.Contains(" "+db.Database+" ", " "+dbName+" "), "未配置该库的操作权限")
|
||||
|
||||
// 密码解密
|
||||
d.PwdDecrypt()
|
||||
instance.PwdDecrypt()
|
||||
|
||||
dbInfo := new(DbInfo)
|
||||
structx.Copy(dbInfo, d)
|
||||
dbInfo.Database = db
|
||||
dbi := &DbInstance{Id: cacheKey, Info: dbInfo}
|
||||
dbInfo := NewDbInfo(db, instance)
|
||||
dbInfo.Database = dbName
|
||||
dbi := &DbConnection{Id: cacheKey, Info: dbInfo}
|
||||
|
||||
DB, err := GetDbConn(d, db)
|
||||
conn, err := getInstanceConn(instance, dbName)
|
||||
if err != nil {
|
||||
dbi.Close()
|
||||
logx.Errorf("连接db失败: %s:%d/%s", d.Host, d.Port, db)
|
||||
logx.Errorf("连接db失败: %s:%d/%s", dbInfo.Host, dbInfo.Port, dbName)
|
||||
panic(biz.NewBizErr(fmt.Sprintf("数据库连接失败: %s", err.Error())))
|
||||
}
|
||||
|
||||
// 最大连接周期,超过时间的连接就close
|
||||
// DB.SetConnMaxLifetime(100 * time.Second)
|
||||
// conn.SetConnMaxLifetime(100 * time.Second)
|
||||
// 设置最大连接数
|
||||
DB.SetMaxOpenConns(5)
|
||||
conn.SetMaxOpenConns(5)
|
||||
// 设置闲置连接数
|
||||
DB.SetMaxIdleConns(1)
|
||||
conn.SetMaxIdleConns(1)
|
||||
|
||||
dbi.db = DB
|
||||
logx.Infof("连接db: %s:%d/%s", d.Host, d.Port, db)
|
||||
dbi.db = conn
|
||||
logx.Infof("连接db: %s:%d/%s", dbInfo.Host, dbInfo.Port, dbName)
|
||||
if needCache {
|
||||
dbCache.Put(cacheKey, dbi)
|
||||
}
|
||||
@@ -235,13 +190,26 @@ type DbInfo struct {
|
||||
SshTunnelMachineId int
|
||||
}
|
||||
|
||||
func NewDbInfo(db *entity.Db, instance *entity.Instance) *DbInfo {
|
||||
return &DbInfo{
|
||||
Id: db.Id,
|
||||
Name: db.Name,
|
||||
Type: instance.Type,
|
||||
Host: instance.Host,
|
||||
Port: instance.Port,
|
||||
Username: instance.Username,
|
||||
TagPath: db.TagPath,
|
||||
SshTunnelMachineId: instance.SshTunnelMachineId,
|
||||
}
|
||||
}
|
||||
|
||||
// 获取记录日志的描述
|
||||
func (d *DbInfo) GetLogDesc() string {
|
||||
return fmt.Sprintf("DB[id=%d, tag=%s, name=%s, ip=%s:%d, database=%s]", d.Id, d.TagPath, d.Name, d.Host, d.Port, d.Database)
|
||||
}
|
||||
|
||||
// db实例
|
||||
type DbInstance struct {
|
||||
type DbConnection struct {
|
||||
Id string
|
||||
Info *DbInfo
|
||||
|
||||
@@ -250,18 +218,18 @@ type DbInstance struct {
|
||||
|
||||
// 执行查询语句
|
||||
// 依次返回 列名数组,结果map,错误
|
||||
func (d *DbInstance) SelectData(execSql string) ([]string, []map[string]any, error) {
|
||||
func (d *DbConnection) SelectData(execSql string) ([]string, []map[string]any, error) {
|
||||
return SelectDataByDb(d.db, execSql)
|
||||
}
|
||||
|
||||
// 将查询结果映射至struct,可具体参考sqlx库
|
||||
func (d *DbInstance) SelectData2Struct(execSql string, dest any) error {
|
||||
func (d *DbConnection) SelectData2Struct(execSql string, dest any) error {
|
||||
return Select2StructByDb(d.db, execSql, dest)
|
||||
}
|
||||
|
||||
// 执行 update, insert, delete,建表等sql
|
||||
// 返回影响条数和错误
|
||||
func (d *DbInstance) Exec(sql string) (int64, error) {
|
||||
func (d *DbConnection) Exec(sql string) (int64, error) {
|
||||
res, err := d.db.Exec(sql)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
@@ -270,7 +238,7 @@ func (d *DbInstance) Exec(sql string) (int64, error) {
|
||||
}
|
||||
|
||||
// 获取数据库元信息实现接口
|
||||
func (di *DbInstance) GetMeta() DbMetadata {
|
||||
func (di *DbConnection) GetMeta() DbMetadata {
|
||||
dbType := di.Info.Type
|
||||
if dbType == entity.DbTypeMysql {
|
||||
return &MysqlMetadata{di: di}
|
||||
@@ -282,7 +250,7 @@ func (di *DbInstance) GetMeta() DbMetadata {
|
||||
}
|
||||
|
||||
// 关闭连接
|
||||
func (d *DbInstance) Close() {
|
||||
func (d *DbConnection) Close() {
|
||||
if d.db != nil {
|
||||
if err := d.db.Close(); err != nil {
|
||||
logx.Errorf("关闭数据库实例[%s]连接失败: %s", d.Id, err.Error())
|
||||
@@ -298,7 +266,7 @@ var dbCache = cache.NewTimedCache(consts.DbConnExpireTime, 5*time.Second).
|
||||
WithUpdateAccessTime(true).
|
||||
OnEvicted(func(key any, value any) {
|
||||
logx.Info(fmt.Sprintf("删除db连接缓存 id = %s", key))
|
||||
value.(*DbInstance).Close()
|
||||
value.(*DbConnection).Close()
|
||||
})
|
||||
|
||||
func init() {
|
||||
@@ -306,7 +274,7 @@ func init() {
|
||||
// 遍历所有db连接实例,若存在db实例使用该ssh隧道机器,则返回true,表示还在使用中...
|
||||
items := dbCache.Items()
|
||||
for _, v := range items {
|
||||
if v.Value.(*DbInstance).Info.SshTunnelMachineId == machineId {
|
||||
if v.Value.(*DbConnection).Info.SshTunnelMachineId == machineId {
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -318,42 +286,13 @@ func GetDbCacheKey(dbId uint64, db string) string {
|
||||
return fmt.Sprintf("%d:%s", dbId, db)
|
||||
}
|
||||
|
||||
func GetDbInstanceByCache(id string) *DbInstance {
|
||||
func GetDbInstanceByCache(id string) *DbConnection {
|
||||
if load, ok := dbCache.Get(id); ok {
|
||||
return load.(*DbInstance)
|
||||
return load.(*DbConnection)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestConnection(d *entity.Db) {
|
||||
// 验证第一个库是否可以连接即可
|
||||
DB, err := GetDbConn(d, strings.Split(d.Database, " ")[0])
|
||||
biz.ErrIsNilAppendErr(err, "数据库连接失败: %s")
|
||||
defer DB.Close()
|
||||
}
|
||||
|
||||
// 获取数据库连接
|
||||
func GetDbConn(d *entity.Db, db string) (*sql.DB, error) {
|
||||
var DB *sql.DB
|
||||
var err error
|
||||
if d.Type == entity.DbTypeMysql {
|
||||
DB, err = getMysqlDB(d, db)
|
||||
} else if d.Type == entity.DbTypePostgres {
|
||||
DB, err = getPgsqlDB(d, db)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = DB.Ping()
|
||||
if err != nil {
|
||||
DB.Close()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return DB, nil
|
||||
}
|
||||
|
||||
func SelectDataByDb(db *sql.DB, selectSql string) ([]string, []map[string]any, error) {
|
||||
rows, err := db.Query(selectSql)
|
||||
if err != nil {
|
||||
|
||||
@@ -20,7 +20,7 @@ type DbSqlExecReq struct {
|
||||
Sql string
|
||||
Remark string
|
||||
LoginAccount *model.LoginAccount
|
||||
DbInstance *DbInstance
|
||||
DbInstance *DbConnection
|
||||
}
|
||||
|
||||
type DbSqlExecRes struct {
|
||||
@@ -252,7 +252,7 @@ func doInsert(insert *sqlparser.Insert, execSqlReq *DbSqlExecReq, dbSqlExec *ent
|
||||
return doExec(execSqlReq.Sql, execSqlReq.DbInstance)
|
||||
}
|
||||
|
||||
func doExec(sql string, dbInstance *DbInstance) (*DbSqlExecRes, error) {
|
||||
func doExec(sql string, dbInstance *DbConnection) (*DbSqlExecRes, error) {
|
||||
rowsAffected, err := dbInstance.Exec(sql)
|
||||
execRes := "success"
|
||||
if err != nil {
|
||||
|
||||
149
server/internal/db/application/instance.go
Normal file
149
server/internal/db/application/instance.go
Normal file
@@ -0,0 +1,149 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"mayfly-go/internal/db/domain/entity"
|
||||
"mayfly-go/internal/db/domain/repository"
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/model"
|
||||
)
|
||||
|
||||
type Instance interface {
|
||||
// GetPageList 分页获取数据库实例
|
||||
GetPageList(condition *entity.InstanceQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any]
|
||||
|
||||
Count(condition *entity.InstanceQuery) int64
|
||||
|
||||
// GetInstanceBy 根据条件获取数据库实例
|
||||
GetInstanceBy(condition *entity.Instance, cols ...string) error
|
||||
|
||||
// GetById 根据id获取数据库实例
|
||||
GetById(id uint64, cols ...string) *entity.Instance
|
||||
|
||||
Save(instanceEntity *entity.Instance)
|
||||
|
||||
// Delete 删除数据库信息
|
||||
Delete(id uint64)
|
||||
|
||||
// GetDatabases 获取数据库实例的所有数据库列表
|
||||
GetDatabases(entity *entity.Instance) []string
|
||||
}
|
||||
|
||||
func newInstanceApp(InstanceRepo repository.Instance) Instance {
|
||||
return &instanceAppImpl{
|
||||
instanceRepo: InstanceRepo,
|
||||
}
|
||||
}
|
||||
|
||||
type instanceAppImpl struct {
|
||||
instanceRepo repository.Instance
|
||||
}
|
||||
|
||||
// GetPageList 分页获取数据库实例
|
||||
func (app *instanceAppImpl) GetPageList(condition *entity.InstanceQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] {
|
||||
return app.instanceRepo.GetInstanceList(condition, pageParam, toEntity, orderBy...)
|
||||
}
|
||||
|
||||
func (app *instanceAppImpl) Count(condition *entity.InstanceQuery) int64 {
|
||||
return app.instanceRepo.Count(condition)
|
||||
}
|
||||
|
||||
// GetInstanceBy 根据条件获取数据库实例
|
||||
func (app *instanceAppImpl) GetInstanceBy(condition *entity.Instance, cols ...string) error {
|
||||
return app.instanceRepo.GetInstance(condition, cols...)
|
||||
}
|
||||
|
||||
// GetById 根据id获取数据库实例
|
||||
func (app *instanceAppImpl) GetById(id uint64, cols ...string) *entity.Instance {
|
||||
return app.instanceRepo.GetById(id, cols...)
|
||||
}
|
||||
|
||||
func (app *instanceAppImpl) Save(instanceEntity *entity.Instance) {
|
||||
// 默认tcp连接
|
||||
instanceEntity.Network = instanceEntity.GetNetwork()
|
||||
|
||||
// 测试连接
|
||||
if instanceEntity.Password != "" {
|
||||
testConnection(instanceEntity)
|
||||
}
|
||||
|
||||
// 查找是否存在该库
|
||||
oldInstance := &entity.Instance{Host: instanceEntity.Host, Port: instanceEntity.Port, Username: instanceEntity.Username}
|
||||
if instanceEntity.SshTunnelMachineId > 0 {
|
||||
oldInstance.SshTunnelMachineId = instanceEntity.SshTunnelMachineId
|
||||
}
|
||||
|
||||
err := app.GetInstanceBy(oldInstance)
|
||||
if instanceEntity.Id == 0 {
|
||||
biz.NotEmpty(instanceEntity.Password, "密码不能为空")
|
||||
biz.IsTrue(err != nil, "该数据库实例已存在")
|
||||
instanceEntity.PwdEncrypt()
|
||||
app.instanceRepo.Insert(instanceEntity)
|
||||
} else {
|
||||
// 如果存在该库,则校验修改的库是否为该库
|
||||
if err == nil {
|
||||
biz.IsTrue(oldInstance.Id == instanceEntity.Id, "该数据库实例已存在")
|
||||
}
|
||||
instanceEntity.PwdEncrypt()
|
||||
app.instanceRepo.Update(instanceEntity)
|
||||
}
|
||||
}
|
||||
|
||||
func (app *instanceAppImpl) Delete(id uint64) {
|
||||
app.instanceRepo.Delete(id)
|
||||
}
|
||||
|
||||
// getInstanceConn 获取数据库连接数据库实例
|
||||
func getInstanceConn(instance *entity.Instance, db string) (*sql.DB, error) {
|
||||
var conn *sql.DB
|
||||
var err error
|
||||
if instance.Type == entity.DbTypeMysql {
|
||||
conn, err = getMysqlDB(instance, db)
|
||||
} else if instance.Type == entity.DbTypePostgres {
|
||||
conn, err = getPgsqlDB(instance, db)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = conn.Ping()
|
||||
if err != nil {
|
||||
conn.Close()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
func testConnection(d *entity.Instance) {
|
||||
// 不指定数据库名称
|
||||
conn, err := getInstanceConn(d, "")
|
||||
biz.ErrIsNilAppendErr(err, "数据库连接失败: %s")
|
||||
defer conn.Close()
|
||||
}
|
||||
|
||||
func (app *instanceAppImpl) GetDatabases(ed *entity.Instance) []string {
|
||||
ed.Network = ed.GetNetwork()
|
||||
databases := make([]string, 0)
|
||||
var dbConn *sql.DB
|
||||
var metaDb string
|
||||
var getDatabasesSql string
|
||||
if ed.Type == entity.DbTypeMysql {
|
||||
metaDb = "information_schema"
|
||||
getDatabasesSql = "SELECT SCHEMA_NAME AS dbname FROM SCHEMATA"
|
||||
} else {
|
||||
metaDb = "postgres"
|
||||
getDatabasesSql = "SELECT datname AS dbname FROM pg_database"
|
||||
}
|
||||
|
||||
dbConn, err := getInstanceConn(ed, metaDb)
|
||||
biz.ErrIsNilAppendErr(err, "数据库连接失败: %s")
|
||||
defer dbConn.Close()
|
||||
|
||||
_, res, err := SelectDataByDb(dbConn, getDatabasesSql)
|
||||
biz.ErrIsNilAppendErr(err, "获取数据库列表失败")
|
||||
for _, re := range res {
|
||||
databases = append(databases, re["dbname"].(string))
|
||||
}
|
||||
return databases
|
||||
}
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
"github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
func getMysqlDB(d *entity.Db, db string) (*sql.DB, error) {
|
||||
func getMysqlDB(d *entity.Instance, db string) (*sql.DB, error) {
|
||||
// SSH Conect
|
||||
if d.SshTunnelMachineId > 0 {
|
||||
sshTunnelMachine := machineapp.GetMachineApp().GetSshTunnelMachine(d.SshTunnelMachineId)
|
||||
@@ -39,7 +39,7 @@ const (
|
||||
)
|
||||
|
||||
type MysqlMetadata struct {
|
||||
di *DbInstance
|
||||
di *DbConnection
|
||||
}
|
||||
|
||||
// 获取表基础元信息, 如表名等
|
||||
|
||||
@@ -16,7 +16,7 @@ import (
|
||||
"github.com/lib/pq"
|
||||
)
|
||||
|
||||
func getPgsqlDB(d *entity.Db, db string) (*sql.DB, error) {
|
||||
func getPgsqlDB(d *entity.Instance, db string) (*sql.DB, error) {
|
||||
driverName := d.Type
|
||||
// SSH Conect
|
||||
if d.SshTunnelMachineId > 0 {
|
||||
@@ -28,7 +28,11 @@ func getPgsqlDB(d *entity.Db, db string) (*sql.DB, error) {
|
||||
sql.Drivers()
|
||||
}
|
||||
|
||||
dsn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", d.Host, d.Port, d.Username, d.Password, db)
|
||||
var dbParam string
|
||||
if db != "" {
|
||||
dbParam = "dbname=" + db
|
||||
}
|
||||
dsn := fmt.Sprintf("host=%s port=%d user=%s password=%s %s sslmode=disable", d.Host, d.Port, d.Username, d.Password, dbParam)
|
||||
if d.Params != "" {
|
||||
dsn = fmt.Sprintf("%s %s", dsn, strings.Join(strings.Split(d.Params, "&"), " "))
|
||||
}
|
||||
@@ -67,7 +71,7 @@ const (
|
||||
)
|
||||
|
||||
type PgsqlMetadata struct {
|
||||
di *DbInstance
|
||||
di *DbConnection
|
||||
}
|
||||
|
||||
// 获取表基础元信息, 如表名等
|
||||
|
||||
@@ -1,53 +1,16 @@
|
||||
package entity
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"mayfly-go/internal/common/utils"
|
||||
"mayfly-go/pkg/model"
|
||||
)
|
||||
|
||||
type Db struct {
|
||||
model.Model
|
||||
|
||||
Name string `orm:"column(name)" json:"name"`
|
||||
Type string `orm:"column(type)" json:"type"` // 类型,mysql oracle等
|
||||
Host string `orm:"column(host)" json:"host"`
|
||||
Port int `orm:"column(port)" json:"port"`
|
||||
Network string `orm:"column(network)" json:"network"`
|
||||
Username string `orm:"column(username)" json:"username"`
|
||||
Password string `orm:"column(password)" json:"-"`
|
||||
Database string `orm:"column(database)" json:"database"`
|
||||
Params string `json:"params"`
|
||||
Remark string `json:"remark"`
|
||||
TagId uint64
|
||||
TagPath string
|
||||
SshTunnelMachineId int `orm:"column(ssh_tunnel_machine_id)" json:"sshTunnelMachineId"` // ssh隧道机器id
|
||||
Name string `orm:"column(name)" json:"name"`
|
||||
Database string `orm:"column(database)" json:"database"`
|
||||
Remark string `json:"remark"`
|
||||
TagId uint64
|
||||
TagPath string
|
||||
InstanceId uint64
|
||||
}
|
||||
|
||||
// 获取数据库连接网络, 若没有使用ssh隧道,则直接返回。否则返回拼接的网络需要注册至指定dial
|
||||
func (d *Db) GetNetwork() string {
|
||||
network := d.Network
|
||||
if d.SshTunnelMachineId <= 0 {
|
||||
if network == "" {
|
||||
return "tcp"
|
||||
} else {
|
||||
return network
|
||||
}
|
||||
}
|
||||
return fmt.Sprintf("%s+ssh:%d", d.Type, d.SshTunnelMachineId)
|
||||
}
|
||||
|
||||
func (d *Db) PwdEncrypt() {
|
||||
// 密码替换为加密后的密码
|
||||
d.Password = utils.PwdAesEncrypt(d.Password)
|
||||
}
|
||||
|
||||
func (d *Db) PwdDecrypt() {
|
||||
// 密码替换为解密后的密码
|
||||
d.Password = utils.PwdAesDecrypt(d.Password)
|
||||
}
|
||||
|
||||
const (
|
||||
DbTypeMysql = "mysql"
|
||||
DbTypePostgres = "postgres"
|
||||
)
|
||||
|
||||
54
server/internal/db/domain/entity/instance.go
Normal file
54
server/internal/db/domain/entity/instance.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package entity
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"mayfly-go/internal/common/utils"
|
||||
"mayfly-go/pkg/model"
|
||||
)
|
||||
|
||||
type Instance struct {
|
||||
model.Model
|
||||
|
||||
Name string `orm:"column(name)" json:"name"`
|
||||
Type string `orm:"column(type)" json:"type"` // 类型,mysql oracle等
|
||||
Host string `orm:"column(host)" json:"host"`
|
||||
Port int `orm:"column(port)" json:"port"`
|
||||
Network string `orm:"column(network)" json:"network"`
|
||||
Username string `orm:"column(username)" json:"username"`
|
||||
Password string `orm:"column(password)" json:"-"`
|
||||
Params string `orm:"column(params)" json:"params"`
|
||||
Remark string `orm:"column(remark)" json:"remark"`
|
||||
SshTunnelMachineId int `orm:"column(ssh_tunnel_machine_id)" json:"sshTunnelMachineId"` // ssh隧道机器id
|
||||
}
|
||||
|
||||
func (d *Instance) TableName() string {
|
||||
return "t_db_instance"
|
||||
}
|
||||
|
||||
// 获取数据库连接网络, 若没有使用ssh隧道,则直接返回。否则返回拼接的网络需要注册至指定dial
|
||||
func (d *Instance) GetNetwork() string {
|
||||
network := d.Network
|
||||
if d.SshTunnelMachineId <= 0 {
|
||||
if network == "" {
|
||||
return "tcp"
|
||||
} else {
|
||||
return network
|
||||
}
|
||||
}
|
||||
return fmt.Sprintf("%s+ssh:%d", d.Type, d.SshTunnelMachineId)
|
||||
}
|
||||
|
||||
func (d *Instance) PwdEncrypt() {
|
||||
// 密码替换为加密后的密码
|
||||
d.Password = utils.PwdAesEncrypt(d.Password)
|
||||
}
|
||||
|
||||
func (d *Instance) PwdDecrypt() {
|
||||
// 密码替换为解密后的密码
|
||||
d.Password = utils.PwdAesDecrypt(d.Password)
|
||||
}
|
||||
|
||||
const (
|
||||
DbTypeMysql = "mysql"
|
||||
DbTypePostgres = "postgres"
|
||||
)
|
||||
@@ -2,23 +2,33 @@ package entity
|
||||
|
||||
import "mayfly-go/pkg/model"
|
||||
|
||||
// 数据库查询实体,不与数据库表字段一一对应
|
||||
type DbQuery struct {
|
||||
// 数据库实例查询
|
||||
type InstanceQuery struct {
|
||||
model.Model
|
||||
|
||||
Name string `orm:"column(name)" json:"name"`
|
||||
Name string `orm:"column(name)" json:"name" form:"name"`
|
||||
Type string `orm:"column(type)" json:"type"` // 类型,mysql oracle等
|
||||
Host string `orm:"column(host)" json:"host"`
|
||||
Port int `orm:"column(port)" json:"port"`
|
||||
Network string `orm:"column(network)" json:"network"`
|
||||
Username string `orm:"column(username)" json:"username"`
|
||||
Password string `orm:"column(password)" json:"-"`
|
||||
Params string `orm:"column(params)" json:"params"`
|
||||
Remark string `orm:"column(remark)" json:"remark"`
|
||||
}
|
||||
|
||||
// 数据库查询实体,不与数据库表字段一一对应
|
||||
type DbQuery struct {
|
||||
model.Model
|
||||
|
||||
Name string `orm:"column(name)" json:"name"`
|
||||
Database string `orm:"column(database)" json:"database"`
|
||||
Params string `json:"params"`
|
||||
Remark string `json:"remark"`
|
||||
|
||||
TagIds []uint64
|
||||
TagPath string `form:"tagPath"`
|
||||
TagIds []uint64 `orm:"column(tag_id)"`
|
||||
TagPath string `form:"tagPath"`
|
||||
|
||||
InstanceId uint64 `form:"instanceId"`
|
||||
}
|
||||
|
||||
type DbSqlExecQuery struct {
|
||||
|
||||
@@ -6,12 +6,12 @@ import (
|
||||
)
|
||||
|
||||
type Db interface {
|
||||
// 分页获取机器信息列表
|
||||
// 分页获取数据信息列表
|
||||
GetDbList(condition *entity.DbQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any]
|
||||
|
||||
Count(condition *entity.DbQuery) int64
|
||||
|
||||
// 根据条件获取账号信息
|
||||
// 根据条件获取数据库信息
|
||||
GetDb(condition *entity.Db, cols ...string) error
|
||||
|
||||
// 根据id获取
|
||||
|
||||
25
server/internal/db/domain/repository/instance.go
Normal file
25
server/internal/db/domain/repository/instance.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"mayfly-go/internal/db/domain/entity"
|
||||
"mayfly-go/pkg/model"
|
||||
)
|
||||
|
||||
type Instance interface {
|
||||
// 分页获取数据库实例信息列表
|
||||
GetInstanceList(condition *entity.InstanceQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any]
|
||||
|
||||
Count(condition *entity.InstanceQuery) int64
|
||||
|
||||
// 根据条件获取实例信息
|
||||
GetInstance(condition *entity.Instance, cols ...string) error
|
||||
|
||||
// 根据id获取
|
||||
GetById(id uint64, cols ...string) *entity.Instance
|
||||
|
||||
Insert(db *entity.Instance)
|
||||
|
||||
Update(db *entity.Instance)
|
||||
|
||||
Delete(id uint64)
|
||||
}
|
||||
@@ -16,12 +16,17 @@ func newDbRepo() repository.Db {
|
||||
|
||||
// 分页获取数据库信息列表
|
||||
func (d *dbRepoImpl) GetDbList(condition *entity.DbQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] {
|
||||
qd := gormx.NewQuery(new(entity.Db)).
|
||||
Like("host", condition.Host).
|
||||
Like("database", condition.Database).
|
||||
In("tag_id", condition.TagIds).
|
||||
RLike("tag_path", condition.TagPath).
|
||||
OrderByAsc("tag_path")
|
||||
qd := gormx.NewQueryWithTableName("t_db db").
|
||||
Select("db.*, inst.name instance_name, inst.type instance_type").
|
||||
Joins("JOIN t_db_instance inst ON db.instance_id = inst.id").
|
||||
Eq("db.instance_id", condition.InstanceId).
|
||||
Like("db.database", condition.Database).
|
||||
In("db.tag_id", condition.TagIds).
|
||||
RLike("db.tag_path", condition.TagPath).
|
||||
Eq0("db."+model.DeletedColumn, model.ModelUndeleted).
|
||||
Eq0("inst."+model.DeletedColumn, model.ModelUndeleted).
|
||||
OrderByAsc("db.tag_path")
|
||||
|
||||
return gormx.PageQuery(qd, pageParam, toEntity)
|
||||
}
|
||||
|
||||
@@ -30,6 +35,9 @@ func (d *dbRepoImpl) Count(condition *entity.DbQuery) int64 {
|
||||
if len(condition.TagIds) > 0 {
|
||||
where["tag_id"] = condition.TagIds
|
||||
}
|
||||
if condition.InstanceId > 0 {
|
||||
where["instance_id"] = condition.InstanceId
|
||||
}
|
||||
return gormx.CountByCond(new(entity.Db), where)
|
||||
}
|
||||
|
||||
|
||||
56
server/internal/db/infrastructure/persistence/instance.go
Normal file
56
server/internal/db/infrastructure/persistence/instance.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package persistence
|
||||
|
||||
import (
|
||||
"mayfly-go/internal/db/domain/entity"
|
||||
"mayfly-go/internal/db/domain/repository"
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/gormx"
|
||||
"mayfly-go/pkg/model"
|
||||
)
|
||||
|
||||
type instanceRepoImpl struct{}
|
||||
|
||||
func newInstanceRepo() repository.Instance {
|
||||
return new(instanceRepoImpl)
|
||||
}
|
||||
|
||||
// 分页获取数据库信息列表
|
||||
func (d *instanceRepoImpl) GetInstanceList(condition *entity.InstanceQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] {
|
||||
qd := gormx.NewQuery(new(entity.Instance)).
|
||||
Eq("host", condition.Host).
|
||||
Eq("port", condition.Port).
|
||||
Eq("username", condition.Username).
|
||||
Like("name", condition.Name)
|
||||
return gormx.PageQuery(qd, pageParam, toEntity)
|
||||
}
|
||||
|
||||
func (d *instanceRepoImpl) Count(condition *entity.InstanceQuery) int64 {
|
||||
where := make(map[string]any)
|
||||
return gormx.CountByCond(new(entity.Instance), where)
|
||||
}
|
||||
|
||||
// 根据条件获数据库实例信息
|
||||
func (d *instanceRepoImpl) GetInstance(condition *entity.Instance, cols ...string) error {
|
||||
return gormx.GetBy(condition, cols...)
|
||||
}
|
||||
|
||||
// 根据id获取数据库实例
|
||||
func (d *instanceRepoImpl) GetById(id uint64, cols ...string) *entity.Instance {
|
||||
instance := new(entity.Instance)
|
||||
if err := gormx.GetById(instance, id, cols...); err != nil {
|
||||
return nil
|
||||
}
|
||||
return instance
|
||||
}
|
||||
|
||||
func (d *instanceRepoImpl) Insert(db *entity.Instance) {
|
||||
biz.ErrIsNil(gormx.Insert(db), "新增数据库实例失败")
|
||||
}
|
||||
|
||||
func (d *instanceRepoImpl) Update(db *entity.Instance) {
|
||||
biz.ErrIsNil(gormx.UpdateById(db), "更新数据库实例失败")
|
||||
}
|
||||
|
||||
func (d *instanceRepoImpl) Delete(id uint64) {
|
||||
gormx.DeleteById(new(entity.Instance), id)
|
||||
}
|
||||
@@ -3,11 +3,16 @@ package persistence
|
||||
import "mayfly-go/internal/db/domain/repository"
|
||||
|
||||
var (
|
||||
instanceRepo repository.Instance = newInstanceRepo()
|
||||
dbRepo repository.Db = newDbRepo()
|
||||
dbSqlRepo repository.DbSql = newDbSqlRepo()
|
||||
dbSqlExecRepo repository.DbSqlExec = newDbSqlExecRepo()
|
||||
)
|
||||
|
||||
func GetInstanceRepo() repository.Instance {
|
||||
return instanceRepo
|
||||
}
|
||||
|
||||
func GetDbRepo() repository.Db {
|
||||
return dbRepo
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ func InitDbRouter(router *gin.RouterGroup) {
|
||||
db := router.Group("dbs")
|
||||
|
||||
d := &api.Db{
|
||||
InstanceApp: application.GetInstanceApp(),
|
||||
DbApp: application.GetDbApp(),
|
||||
DbSqlExecApp: application.GetDbSqlExecApp(),
|
||||
MsgApp: msgapp.GetMsgApp(),
|
||||
@@ -31,8 +32,6 @@ func InitDbRouter(router *gin.RouterGroup) {
|
||||
// 获取数据库实例的所有数据库名
|
||||
req.NewPost("/databases", d.GetDatabaseNames),
|
||||
|
||||
req.NewGet(":dbId/pwd", d.GetDbPwd),
|
||||
|
||||
req.NewDelete(":dbId", d.DeleteDb).Log(req.NewLogSave("db-删除数据库信息")),
|
||||
|
||||
req.NewGet(":dbId/t-infos", d.TableInfos),
|
||||
|
||||
36
server/internal/db/router/instance.go
Normal file
36
server/internal/db/router/instance.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"mayfly-go/internal/db/api"
|
||||
"mayfly-go/internal/db/application"
|
||||
msgapp "mayfly-go/internal/msg/application"
|
||||
"mayfly-go/pkg/req"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func InitInstanceRouter(router *gin.RouterGroup) {
|
||||
instances := router.Group("/instances")
|
||||
|
||||
d := &api.Instance{
|
||||
InstanceApp: application.GetInstanceApp(),
|
||||
DbApp: application.GetDbApp(),
|
||||
MsgApp: msgapp.GetMsgApp(),
|
||||
}
|
||||
|
||||
reqs := [...]*req.Conf{
|
||||
// 获取数据库列表
|
||||
req.NewGet("", d.Instances),
|
||||
|
||||
req.NewPost("", d.SaveInstance).Log(req.NewLogSave("db-保存数据库实例信息")),
|
||||
|
||||
req.NewGet(":instanceId", d.GetInstance),
|
||||
req.NewGet(":instanceId/pwd", d.GetInstancePwd),
|
||||
// 获取数据库实例的所有数据库名
|
||||
req.NewGet(":instanceId/databases", d.GetDatabaseNames),
|
||||
|
||||
req.NewDelete(":instanceId", d.DeleteInstance).Log(req.NewLogSave("db-删除数据库实例")),
|
||||
}
|
||||
|
||||
req.BatchSetGroup(instances, reqs[:])
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package router
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
func Init(router *gin.RouterGroup) {
|
||||
InitInstanceRouter(router)
|
||||
InitDbRouter(router)
|
||||
InitDbSqlExecRouter(router)
|
||||
}
|
||||
|
||||
@@ -1,36 +1,61 @@
|
||||
SET NAMES utf8mb4;
|
||||
SET FOREIGN_KEY_CHECKS = 0;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for t_db_instance
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `t_db_instance`;
|
||||
CREATE TABLE `t_db_instance` (
|
||||
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
|
||||
`name` varchar(32) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '数据库实例名称',
|
||||
`host` varchar(100) COLLATE utf8mb4_bin NOT NULL,
|
||||
`port` int(8) NOT NULL,
|
||||
`username` varchar(255) COLLATE utf8mb4_bin NOT NULL,
|
||||
`password` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL,
|
||||
`type` varchar(20) COLLATE utf8mb4_bin NOT NULL COMMENT '数据库实例类型(mysql...)',
|
||||
`params` varchar(125) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '其他连接参数',
|
||||
`network` varchar(20) COLLATE utf8mb4_bin DEFAULT NULL,
|
||||
`ssh_tunnel_machine_id` bigint(20) DEFAULT NULL COMMENT 'ssh隧道的机器id',
|
||||
`remark` varchar(125) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '备注,描述等',
|
||||
`create_time` datetime DEFAULT NULL,
|
||||
`creator_id` bigint(20) DEFAULT NULL,
|
||||
`creator` varchar(32) COLLATE utf8mb4_bin DEFAULT NULL,
|
||||
`update_time` datetime DEFAULT NULL,
|
||||
`modifier_id` bigint(20) DEFAULT NULL,
|
||||
`modifier` varchar(32) COLLATE utf8mb4_bin DEFAULT NULL,
|
||||
`is_deleted` tinyint(8) NOT NULL DEFAULT '0',
|
||||
`delete_time` datetime DEFAULT NULL,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='数据库实例信息表';
|
||||
|
||||
-- ----------------------------
|
||||
-- Records of t_db_instance
|
||||
-- ----------------------------
|
||||
BEGIN;
|
||||
COMMIT;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for t_db
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `t_db`;
|
||||
CREATE TABLE `t_db` (
|
||||
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
|
||||
`name` varchar(32) DEFAULT NULL COMMENT '数据库实例名称',
|
||||
`host` varchar(100) NOT NULL,
|
||||
`port` int(8) NOT NULL,
|
||||
`username` varchar(255) NOT NULL,
|
||||
`password` varchar(255) DEFAULT NULL,
|
||||
`type` varchar(20) NOT NULL COMMENT '数据库实例类型(mysql...)',
|
||||
`database` varchar(1000) DEFAULT NULL COMMENT '数据库,空格分割多个数据库',
|
||||
`params` varchar(125) DEFAULT NULL COMMENT '其他连接参数',
|
||||
`network` varchar(20) DEFAULT NULL,
|
||||
`ssh_tunnel_machine_id` bigint(20) DEFAULT NULL COMMENT 'ssh隧道的机器id',
|
||||
`remark` varchar(125) DEFAULT NULL COMMENT '备注,描述等',
|
||||
`tag_id` bigint(20) DEFAULT NULL COMMENT '标签id',
|
||||
`tag_path` varchar(255) DEFAULT NULL COMMENT '标签路径',
|
||||
`create_time` datetime DEFAULT NULL,
|
||||
`creator_id` bigint(20) DEFAULT NULL,
|
||||
`creator` varchar(32) DEFAULT NULL,
|
||||
`update_time` datetime DEFAULT NULL,
|
||||
`modifier_id` bigint(20) DEFAULT NULL,
|
||||
`modifier` varchar(32) DEFAULT NULL,
|
||||
`is_deleted` tinyint(8) NOT NULL DEFAULT 0,
|
||||
`delete_time` datetime DEFAULT NULL,
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `idx_path` (`tag_path`) USING BTREE
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='数据库资源信息表';
|
||||
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
|
||||
`name` varchar(32) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '数据库实例名称',
|
||||
`database` varchar(1000) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '数据库,空格分割多个数据库',
|
||||
`remark` varchar(125) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '备注,描述等',
|
||||
`tag_id` bigint(20) DEFAULT NULL COMMENT '标签id',
|
||||
`tag_path` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '标签路径',
|
||||
`instance_id` bigint(20) NOT NULL COMMENT '数据库实例 ID',
|
||||
`create_time` datetime DEFAULT NULL,
|
||||
`creator_id` bigint(20) DEFAULT NULL,
|
||||
`creator` varchar(32) COLLATE utf8mb4_bin DEFAULT NULL,
|
||||
`update_time` datetime DEFAULT NULL,
|
||||
`modifier_id` bigint(20) DEFAULT NULL,
|
||||
`modifier` varchar(32) COLLATE utf8mb4_bin DEFAULT NULL,
|
||||
`is_deleted` tinyint(8) NOT NULL DEFAULT '0',
|
||||
`delete_time` datetime DEFAULT NULL,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='数据库资源信息表';
|
||||
|
||||
-- ----------------------------
|
||||
-- Records of t_db
|
||||
@@ -609,6 +634,10 @@ INSERT INTO t_sys_resource (id, pid, ui_path, `type`, status, name, code, weight
|
||||
INSERT INTO t_sys_resource (id, pid, ui_path, `type`, status, name, code, weight, meta, creator_id, creator, modifier_id, modifier, create_time, update_time, is_deleted, delete_time) VALUES(130, 2, '12sSjal1/W9XKiabq/', 1, 1, '计划任务', '/machine/cron-job', 1689646396, '{"component":"ops/machine/cronjob/CronJobList","icon":"AlarmClock","isKeepAlive":true,"routeName":"CronJobList"}', 1, 'admin', 1, 'admin', '2023-07-18 10:13:16', '2023-07-18 10:14:06', 0, NULL);
|
||||
INSERT INTO t_sys_resource (id, pid, ui_path, type, status, name, code, weight, meta, creator_id, creator, modifier_id, modifier, create_time, update_time, is_deleted, delete_time) VALUES(134, 80, 'Mongo452/eggago31/3sblw1Wb/', 2, 1, '删除数据', 'mongo:data:del', 1692674964, 'null', 1, 'admin', 1, 'admin', '2023-08-22 11:29:24', '2023-08-22 11:29:24', 0, NULL);
|
||||
INSERT INTO t_sys_resource (id, pid, ui_path, type, status, name, code, weight, meta, creator_id, creator, modifier_id, modifier, create_time, update_time, is_deleted, delete_time) VALUES(133, 80, 'Mongo452/eggago31/xvpKk36u/', 2, 1, '保存数据', 'mongo:data:save', 1692674943, 'null', 1, 'admin', 1, 'admin', '2023-08-22 11:29:04', '2023-08-22 11:29:11', 0, NULL);
|
||||
INSERT INTO t_sys_resource (id, pid, ui_path, `type`, status, name, code, weight, meta, creator_id, creator, modifier_id, modifier, create_time, update_time, is_deleted, delete_time) VALUES (135, 36, 'dbms23ax/X0f4BxT0/', 1, 1, '数据库实例管理', 'instances', 1693040706, '{\"component\":\"ops/db/InstanceList\",\"icon\":\"Coin\",\"isKeepAlive\":true,\"routeName\":\"InstanceList\"}', 1, 'admin', 1, 'admin', '2023-08-26 09:05:07', '2023-08-29 22:35:11', 0, NULL);
|
||||
INSERT INTO t_sys_resource (id, pid, ui_path, `type`, status, name, code, weight, meta, creator_id, creator, modifier_id, modifier, create_time, update_time, is_deleted, delete_time) VALUES (136, 133, 'dbms23ax/X0f4BxT0/D23fUiBr/', 2, 1, '实例保存', 'instance:save', 1693041001, 'null', 1, 'admin', 1, 'admin', '2023-08-26 09:10:02', '2023-08-26 09:10:02', 0, NULL);
|
||||
INSERT INTO t_sys_resource (id, pid, ui_path, `type`, status, name, code, weight, meta, creator_id, creator, modifier_id, modifier, create_time, update_time, is_deleted, delete_time) VALUES (137, 133, 'dbms23ax/X0f4BxT0/mJlBeTCs/', 2, 1, '基本权限', 'instance', 1693041055, 'null', 1, 'admin', 1, 'admin', '2023-08-26 09:10:55', '2023-08-26 09:10:55', 0, NULL);
|
||||
INSERT INTO t_sys_resource (id, pid, ui_path, `type`, status, name, code, weight, meta, creator_id, creator, modifier_id, modifier, create_time, update_time, is_deleted, delete_time) VALUES (138, 133, 'dbms23ax/X0f4BxT0/Sgg8uPwz/', 2, 1, '实例删除', 'instance:del', 1693041084, 'null', 1, 'admin', 1, 'admin', '2023-08-26 09:11:24', '2023-08-26 09:11:24', 0, NULL);
|
||||
COMMIT;
|
||||
|
||||
-- ----------------------------
|
||||
@@ -830,7 +859,13 @@ INSERT INTO `t_sys_role_resource` (role_id,resource_id,creator_id,creator,create
|
||||
(1,128,1,'admin','2023-03-16 16:11:25', 0, NULL),
|
||||
(1,130,1,'admin','2023-03-16 16:11:25', 0, NULL),
|
||||
(1,131,1,'admin','2023-03-16 16:11:25', 0, NULL),
|
||||
(1,132,1,'admin','2023-03-16 16:11:25', 0, NULL);
|
||||
(1,132,1,'admin','2023-03-16 16:11:25', 0, NULL),
|
||||
(1,133,1,'admin','2023-08-30 20:17:00', 0, NULL),
|
||||
(1,134,1,'admin','2023-08-30 20:17:00', 0, NULL),
|
||||
(1,135,1,'admin','2023-08-30 20:17:00', 0, NULL),
|
||||
(1,136,1,'admin','2023-08-30 20:17:00', 0, NULL),
|
||||
(1,137,1,'admin','2023-08-30 20:17:00', 0, NULL),
|
||||
(1,138,1,'admin','2023-08-30 20:17:00', 0, NULL);
|
||||
COMMIT;
|
||||
|
||||
-- ----------------------------
|
||||
|
||||
@@ -43,6 +43,9 @@ func T2022() *gormigrate.Migration {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := tx.AutoMigrate(&entity2.Instance{}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.AutoMigrate(&entity2.Db{}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
67
server/migrations/migrate-database-instance.sql
Normal file
67
server/migrations/migrate-database-instance.sql
Normal file
@@ -0,0 +1,67 @@
|
||||
CREATE TABLE `t_db_instance` (
|
||||
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
|
||||
`name` varchar(32) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '数据库实例名称',
|
||||
`host` varchar(100) COLLATE utf8mb4_bin NOT NULL,
|
||||
`port` int(8) NOT NULL,
|
||||
`username` varchar(255) COLLATE utf8mb4_bin NOT NULL,
|
||||
`password` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL,
|
||||
`type` varchar(20) COLLATE utf8mb4_bin NOT NULL COMMENT '数据库实例类型(mysql...)',
|
||||
`params` varchar(125) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '其他连接参数',
|
||||
`network` varchar(20) COLLATE utf8mb4_bin DEFAULT NULL,
|
||||
`ssh_tunnel_machine_id` bigint(20) DEFAULT NULL COMMENT 'ssh隧道的机器id',
|
||||
`remark` varchar(125) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '备注,描述等',
|
||||
`create_time` datetime DEFAULT NULL,
|
||||
`creator_id` bigint(20) DEFAULT NULL,
|
||||
`creator` varchar(32) COLLATE utf8mb4_bin DEFAULT NULL,
|
||||
`update_time` datetime DEFAULT NULL,
|
||||
`modifier_id` bigint(20) DEFAULT NULL,
|
||||
`modifier` varchar(32) COLLATE utf8mb4_bin DEFAULT NULL,
|
||||
`is_deleted` tinyint(8) NOT NULL DEFAULT '0',
|
||||
`delete_time` datetime DEFAULT NULL,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='数据库实例信息表';
|
||||
|
||||
ALTER TABLE t_db
|
||||
ADD COLUMN instance_id bigint(20) UNSIGNED NULL AFTER tag_path;
|
||||
|
||||
BEGIN;
|
||||
|
||||
INSERT INTO t_sys_resource (id, pid, ui_path, `type`, status, name, code, weight, meta, creator_id, creator, modifier_id, modifier, create_time, update_time, is_deleted, delete_time) VALUES (135, 36, 'dbms23ax/X0f4BxT0/', 1, 1, '数据库实例管理', 'instances', 1693040706, '{\"component\":\"ops/db/InstanceList\",\"icon\":\"Coin\",\"isKeepAlive\":true,\"routeName\":\"InstanceList\"}', 1, 'admin', 1, 'admin', '2023-08-26 09:05:07', '2023-08-29 22:35:11', 0, NULL);
|
||||
|
||||
INSERT INTO t_sys_resource (id, pid, ui_path, `type`, status, name, code, weight, meta, creator_id, creator, modifier_id, modifier, create_time, update_time, is_deleted, delete_time) VALUES (136, 133, 'dbms23ax/X0f4BxT0/D23fUiBr/', 2, 1, '实例保存', 'instance:save', 1693041001, 'null', 1, 'admin', 1, 'admin', '2023-08-26 09:10:02', '2023-08-26 09:10:02', 0, NULL);
|
||||
|
||||
INSERT INTO t_sys_resource (id, pid, ui_path, `type`, status, name, code, weight, meta, creator_id, creator, modifier_id, modifier, create_time, update_time, is_deleted, delete_time) VALUES (137, 133, 'dbms23ax/X0f4BxT0/mJlBeTCs/', 2, 1, '基本权限', 'instance', 1693041055, 'null', 1, 'admin', 1, 'admin', '2023-08-26 09:10:55', '2023-08-26 09:10:55', 0, NULL);
|
||||
|
||||
INSERT INTO t_sys_resource (id, pid, ui_path, `type`, status, name, code, weight, meta, creator_id, creator, modifier_id, modifier, create_time, update_time, is_deleted, delete_time) VALUES (138, 133, 'dbms23ax/X0f4BxT0/Sgg8uPwz/', 2, 1, '实例删除', 'instance:del', 1693041084, 'null', 1, 'admin', 1, 'admin', '2023-08-26 09:11:24', '2023-08-26 09:11:24', 0, NULL);
|
||||
|
||||
INSERT INTO `t_sys_role_resource` (role_id,resource_id,creator_id,creator,create_time,is_deleted,delete_time) VALUES
|
||||
(1,135,1,'admin','2023-08-30 20:17:00', 0, NULL),
|
||||
(1,136,1,'admin','2023-08-30 20:17:00', 0, NULL),
|
||||
(1,137,1,'admin','2023-08-30 20:17:00', 0, NULL),
|
||||
(1,138,1,'admin','2023-08-30 20:17:00', 0, NULL);
|
||||
|
||||
INSERT INTO t_db_instance (`host`, `port`, `username`, `password`, `type`, `params`, `network`, `ssh_tunnel_machine_id`, `remark`, `create_time`, `creator_id`, `creator`, `update_time`, `modifier_id`, `modifier`, `is_deleted`, `delete_time`)
|
||||
SELECT DISTINCT `host`, `port`, `username`, `password`, `type`, `params`, `network`, `ssh_tunnel_machine_id`, '', '2023-08-30 15:04:07', 1, 'admin', '2023-08-30 15:04:07', 1, 'admin', 0, NULL
|
||||
FROM t_db
|
||||
WHERE is_deleted = 0;
|
||||
|
||||
UPDATE t_db_instance SET name = CONCAT('instance_', id)
|
||||
WHERE name is NULL;
|
||||
|
||||
UPDATE t_db a, t_db_instance b SET a.instance_id = b.id
|
||||
WHERE a.`host`=b.`host` and a.`port`=b.`port` and a.`username`=b.`username` and a.`password`=b.`password` and a.`type`=b.`type` and a.`params`=b.`params` and a.`network`=b.`network` and a.`ssh_tunnel_machine_id`=b.`ssh_tunnel_machine_id`;
|
||||
|
||||
COMMIT;
|
||||
|
||||
ALTER TABLE t_db
|
||||
MODIFY COLUMN instance_id bigint(20) UNSIGNED NOT NULL AFTER tag_path;
|
||||
|
||||
ALTER TABLE t_db
|
||||
DROP COLUMN `host`,
|
||||
DROP COLUMN `port`,
|
||||
DROP COLUMN `username`,
|
||||
DROP COLUMN `password`,
|
||||
DROP COLUMN `type`,
|
||||
DROP COLUMN `params`,
|
||||
DROP COLUMN `network`,
|
||||
DROP COLUMN `ssh_tunnel_machine_id`;
|
||||
Reference in New Issue
Block a user