Skip to content

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

Add TiDB support

天舟231306f89886
diff --git a/frontend/src/assets/db-mysql.png b/frontend/src/assets/db-mysql.png
new file mode 100644
index 0000000000..03e234d45e
Binary files /dev/null and b/frontend/src/assets/db-mysql.png differ
diff --git a/frontend/src/assets/db-mysql.svg b/frontend/src/assets/db-mysql.svg
deleted file mode 100644
index 4109e78f77..0000000000
--- a/frontend/src/assets/db-mysql.svg
+++ /dev/null
@@ -1 +0,0 @@
-<?xml version="1.0" ?><svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title/><path d="M16.405 5.676c-.115 0-.193.014-.274.033v.013h.014c.054.104.146.18.214.273.054.107.1.214.154.32l.014-.015c.094-.066.14-.172.14-.333-.04-.047-.046-.094-.08-.14-.04-.067-.126-.1-.18-.153zM5.77 18.87h-.927c-.03-1.562-.123-3.03-.27-4.41h-.008l-1.41 4.41H2.45l-1.4-4.41h-.01c-.103 1.323-.168 2.793-.195 4.41H0c.055-1.966.192-3.81.41-5.53h1.15l1.335 4.064h.008L4.25 13.34h1.095c.242 2.015.384 3.86.428 5.53zm4.017-4.08c-.378 2.045-.876 3.533-1.492 4.46-.482.716-1.01 1.073-1.583 1.073-.153 0-.34-.046-.566-.138v-.494c.11.017.24.026.386.026.268 0 .483-.075.647-.222.197-.18.295-.382.295-.605 0-.155-.077-.47-.23-.944L6.23 14.79h.91l.727 2.36c.164.536.233.91.205 1.123.4-1.064.678-2.227.835-3.483h.88zM22.112 18.87h-2.63v-5.53h.885v4.85h1.745zM18.792 19.005l-1.016-.5c.09-.076.177-.158.255-.25.433-.506.648-1.258.648-2.253 0-1.83-.718-2.746-2.155-2.746-.704 0-1.254.232-1.65.697-.43.508-.646 1.256-.646 2.245 0 .972.19 1.686.574 2.14.35.41.877.615 1.583.615.264 0 .506-.033.725-.098l1.325.772.36-.622zM15.5 17.763c-.225-.36-.337-.94-.337-1.736 0-1.393.424-2.09 1.27-2.09.443 0 .77.167.977.5.224.362.336.936.336 1.723 0 1.404-.424 2.108-1.27 2.108-.445 0-.77-.167-.978-.5zM13.842 17.338c0 .47-.172.856-.516 1.156s-.803.45-1.384.45c-.543 0-1.064-.172-1.573-.515l.237-.476c.438.22.833.328 1.19.328.332 0 .593-.073.783-.22.188-.147.3-.354.3-.615 0-.33-.23-.61-.648-.845-.388-.213-1.163-.657-1.163-.657-.422-.307-.632-.636-.632-1.177 0-.45.157-.81.47-1.085.315-.278.72-.415 1.22-.415.512 0 .98.136 1.4.41l-.213.476c-.36-.152-.715-.23-1.064-.23-.283 0-.502.068-.654.206-.153.136-.248.31-.248.524 0 .328.234.61.666.85.393.215 1.187.67 1.187.67.433.305.648.63.648 1.168zM23.224 11.486c-.535-.014-.95.04-1.297.188-.1.04-.26.04-.274.167.055.053.063.14.11.214.08.134.218.313.346.407.14.11.28.216.427.31.26.16.555.255.81.416.145.094.293.213.44.313.073.05.12.14.214.172v-.02c-.046-.06-.06-.147-.105-.214-.067-.067-.134-.127-.2-.193-.194-.26-.435-.487-.695-.675-.214-.146-.682-.35-.77-.595l-.013-.014c.146-.013.32-.066.46-.106.227-.06.435-.047.67-.106.106-.027.213-.06.32-.094v-.06c-.12-.12-.21-.283-.334-.395-.34-.295-.717-.582-1.104-.823-.21-.134-.476-.22-.697-.334-.08-.04-.214-.06-.26-.127-.12-.146-.19-.34-.275-.514-.192-.368-.38-.775-.547-1.163-.12-.262-.193-.523-.34-.763-.69-1.137-1.437-1.826-2.586-2.5-.247-.14-.543-.2-.856-.274-.167-.008-.334-.02-.5-.027-.11-.047-.216-.174-.31-.235-.38-.24-1.364-.76-1.644-.072-.18.434.267.862.422 1.082.115.153.26.328.34.5.047.116.06.235.107.356.106.294.207.622.347.897.073.14.153.287.247.413.054.073.146.107.167.227-.094.136-.1.334-.154.5-.24.757-.146 1.693.194 2.25.107.166.362.534.703.393.3-.12.234-.5.32-.835.02-.08.007-.133.048-.187v.015c.094.188.188.367.274.555.206.328.566.668.867.895.16.12.287.328.487.402v-.02h-.015c-.043-.058-.1-.086-.154-.133-.12-.12-.255-.267-.35-.4-.28-.377-.527-.79-.747-1.218-.11-.21-.202-.436-.29-.643-.04-.08-.04-.2-.107-.24-.1.146-.247.273-.32.453-.127.288-.14.642-.188 1.01-.027.007-.014 0-.027.014-.214-.052-.287-.274-.367-.46-.2-.475-.233-1.238-.06-1.785.047-.14.247-.582.167-.716-.042-.127-.174-.2-.247-.303-.087-.124-.18-.285-.24-.427-.16-.374-.24-.788-.414-1.162-.08-.173-.22-.354-.334-.513-.127-.18-.267-.307-.368-.52-.033-.073-.08-.194-.027-.274.014-.054.042-.075.094-.09.088-.072.335.022.422.062.247.1.455.194.662.334.094.066.195.193.315.226h.14c.214.047.455.014.655.073.355.114.675.28.962.46.876.556 1.596 1.345 2.085 2.286.08.154.115.295.188.455.14.33.313.663.455.982.14.315.275.636.476.897.1.14.502.213.682.286.133.06.34.115.46.188.23.14.454.3.67.454.11.076.443.243.463.378z"/></svg>
\ No newline at end of file
diff --git a/frontend/src/assets/db-tidb.png b/frontend/src/assets/db-tidb.png
new file mode 100644
index 0000000000..7f02ca5ab4
Binary files /dev/null and b/frontend/src/assets/db-tidb.png differ
diff --git a/frontend/src/components/InstanceForm.vue b/frontend/src/components/InstanceForm.vue
index c31561516d..3161f6fdb1 100644
--- a/frontend/src/components/InstanceForm.vue
+++ b/frontend/src/components/InstanceForm.vue
@@ -15,8 +15,78 @@ input[type="number"] {
<template>
<form class="space-y-6 divide-y divide-block-border">
<div class="space-y-6 divide-y divide-block-border px-1">
+ <div v-if="create" class="grid grid-cols-1 gap-4 sm:grid-cols-6">
+ <template v-for="(engine, index) in ['MYSQL', 'TIDB']" :key="index">
+ <div
+ class="
+ flex
+ justify-center
+ px-2
+ py-4
+ border border-control-border
+ hover:bg-control-bg-hover
+ cursor-pointer
+ "
+ @click.capture="state.instance.engine = engine"
+ >
+ <div class="flex flex-col items-center">
+ <!-- This awkward code is author couldn't figure out proper way to use dynamic src under vite
+ https://github.com/vitejs/vite/issues/1265 -->
+ <template v-if="engine == 'MYSQL'">
+ <img class="h-8 w-auto" src="../assets/db-mysql.png" alt="" />
+ </template>
+ <template v-else-if="engine == 'TIDB'">
+ <img class="h-8 w-auto" src="../assets/db-tidb.png" />
+ </template>
+ <p class="mt-1 text-center textlabel">
+ {{ engineName(engine) }}
+ </p>
+ <div class="mt-3 radio text-sm">
+ <input
+ type="radio"
+ class="btn"
+ :checked="state.instance.engine == engine"
+ />
+ </div>
+ </div>
+ </div>
+ </template>
+ </div>
<!-- Instance Name -->
- <div class="grid grid-cols-1 gap-y-6 gap-x-4 sm:grid-cols-4">
+ <div
+ class="grid grid-cols-1 gap-y-6 gap-x-4 sm:grid-cols-4"
+ :class="create ? 'pt-4' : ''"
+ >
+ <div class="sm:col-span-3 sm:col-start-1">
+ <label for="name" class="textlabel flex flex-row items-center">
+ <template v-if="!create">
+ <img
+ v-if="state.instance.engine == 'MYSQL'"
+ class="h-6 w-auto mr-2"
+ src="../assets/db-mysql.png"
+ alt=""
+ />
+ <img
+ v-else-if="state.instance.engine == 'TIDB'"
+ class="h-6 w-auto mr-2"
+ src="../assets/db-tidb.png"
+ alt=""
+ />
+ </template>
+ Instance Name&nbsp;<span style="color: red">*</span>
+ </label>
+ <input
+ required
+ id="name"
+ name="name"
+ type="text"
+ class="textfield mt-1 w-full"
+ :disabled="!allowEdit"
+ :value="state.instance.name"
+ @input="updateInstance('name', $event.target.value)"
+ />
+ </div>
+
<div class="sm:col-span-3 sm:col-start-1">
<label for="environment" class="textlabel">
Environment <span v-if="create" style="color: red">*</span>
@@ -44,22 +114,6 @@ input[type="number"] {
/>
</div>
- <div class="sm:col-span-3 sm:col-start-1">
- <label for="name" class="textlabel">
- Instance Name <span style="color: red">*</span>
- </label>
- <input
- required
- id="name"
- name="name"
- type="text"
- class="textfield mt-1 w-full"
- :disabled="!allowEdit"
- :value="state.instance.name"
- @input="updateInstance('name', $event.target.value)"
- />
- </div>
-
<div class="sm:col-span-3 sm:col-start-1">
<label for="host" class="textlabel block">
Host or Socket <span style="color: red">*</span>
@@ -343,6 +397,7 @@ import {
InstancePatch,
ConnectionInfo,
SqlResultSet,
+ EngineType,
} from "../types";
import isEmpty from "lodash-es/isEmpty";
@@ -431,6 +486,15 @@ export default {
);
});
+ const engineName = (type: EngineType): string => {
+ switch (type) {
+ case "MYSQL":
+ return "MySQL";
+ case "TIDB":
+ return "TiDB";
+ }
+ };
+
const toggleCreateUserExample = () => {
state.showCreateUserExample = !state.showCreateUserExample;
};
@@ -578,6 +642,7 @@ export default {
allowEdit,
showTestConnection,
valueChanged,
+ engineName,
toggleCreateUserExample,
updateInstance,
cancel,
diff --git a/frontend/src/components/InstanceTable.vue b/frontend/src/components/InstanceTable.vue
index 00c4acd67d..832816df2a 100644
--- a/frontend/src/components/InstanceTable.vue
+++ b/frontend/src/components/InstanceTable.vue
@@ -8,11 +8,16 @@
@click-row="clickInstance"
>
<template v-slot:body="{ rowData: instance }">
- <BBTableCell :leftPadding="4" class="w-8">
+ <BBTableCell :leftPadding="4" class="w-4">
<img
v-if="instance.engine == 'MYSQL'"
- class="h-10 w-auto"
- src="../assets/db-mysql.svg"
+ class="h-6 w-auto"
+ src="../assets/db-mysql.png"
+ />
+ <img
+ v-else-if="instance.engine == 'TIDB'"
+ class="h-6 w-auto"
+ src="../assets/db-tidb.png"
/>
</BBTableCell>
<BBTableCell class="w-32">
diff --git a/frontend/src/types/instance.ts b/frontend/src/types/instance.ts
index bfc3aaa778..7229f9b52f 100644
--- a/frontend/src/types/instance.ts
+++ b/frontend/src/types/instance.ts
@@ -4,7 +4,7 @@ import { EnvironmentId, InstanceId, MigrationHistoryId } from "./id";
import { Principal } from "./principal";
import { VCSPushEvent } from "./vcs";
-export type EngineType = "MYSQL";
+export type EngineType = "MYSQL" | "TIDB";
export type Instance = {
id: InstanceId;
diff --git a/plugin/db/driver.go b/plugin/db/driver.go
index 6ca8ad9b60..c496a4a881 100644
--- a/plugin/db/driver.go
+++ b/plugin/db/driver.go
@@ -18,6 +18,7 @@ type Type string
const (
MySQL Type = "MYSQL"
Postgres Type = "POSTGRES"
+ TiDB Type = "TIDB"
)
func (e Type) String() string {
@@ -26,6 +27,8 @@ func (e Type) String() string {
return "MYSQL"
case Postgres:
return "POSTGRES"
+ case TiDB:
+ return "TIDB"
}
return "UNKNOWN"
@@ -269,7 +272,9 @@ type ConnectionContext struct {
}
type Driver interface {
- Open(config ConnectionConfig, ctx ConnectionContext) (Driver, error)
+ // A driver might support multiple engines (e.g. MySQL driver can support both MySQL and TiDB),
+ // So we pass the dbType to tell the exact engine.
+ Open(dbType Type, config ConnectionConfig, ctx ConnectionContext) (Driver, error)
// Remember to call Close to avoid connection leak
Close(ctx context.Context) error
Ping(ctx context.Context) error
@@ -318,7 +323,7 @@ func Open(dbType Type, driverConfig DriverConfig, connectionConfig ConnectionCon
return nil, fmt.Errorf("db: unknown driver %v", dbType)
}
- driver, err := f(driverConfig).Open(connectionConfig, ctx)
+ driver, err := f(driverConfig).Open(dbType, connectionConfig, ctx)
if err != nil {
return nil, err
}
diff --git a/plugin/db/mysql/mysql.go b/plugin/db/mysql/mysql.go
index b7dc82994f..13a5a1ea30 100644
--- a/plugin/db/mysql/mysql.go
+++ b/plugin/db/mysql/mysql.go
@@ -34,11 +34,13 @@ var (
func init() {
db.Register(db.MySQL, newDriver)
+ db.Register(db.TiDB, newDriver)
}
type Driver struct {
l *zap.Logger
connectionCtx db.ConnectionContext
+ dbType db.Type
db *sql.DB
}
@@ -49,7 +51,7 @@ func newDriver(config db.DriverConfig) db.Driver {
}
}
-func (driver *Driver) Open(config db.ConnectionConfig, ctx db.ConnectionContext) (db.Driver, error) {
+func (driver *Driver) Open(dbType db.Type, config db.ConnectionConfig, ctx db.ConnectionContext) (db.Driver, error) {
protocol := "tcp"
if strings.HasPrefix(config.Host, "/") {
protocol = "unix"
@@ -88,6 +90,7 @@ func (driver *Driver) Open(config db.ConnectionConfig, ctx db.ConnectionContext)
if err != nil {
panic(err)
}
+ driver.dbType = dbType
driver.db = db
driver.connectionCtx = ctx
@@ -861,7 +864,12 @@ const (
func (driver *Driver) Dump(ctx context.Context, database string, out io.Writer, schemaOnly bool) error {
// mysqldump -u root --databases dbName --no-data --routines --events --triggers --compact
- txn, err := driver.db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
+ options := sql.TxOptions{}
+ // TiDB does not support readonly, so we only set for MySQL.
+ if driver.dbType == "MYSQL" {
+ options.ReadOnly = true
+ }
+ txn, err := driver.db.BeginTx(ctx, &options)
if err != nil {
return err
}
diff --git a/plugin/db/pg/pg.go b/plugin/db/pg/pg.go
index d944062432..281c9f1ce3 100644
--- a/plugin/db/pg/pg.go
+++ b/plugin/db/pg/pg.go
@@ -197,7 +197,7 @@ func newDriver(config db.DriverConfig) db.Driver {
}
}
-func (driver *Driver) Open(config db.ConnectionConfig, ctx db.ConnectionContext) (db.Driver, error) {
+func (driver *Driver) Open(dbType db.Type, config db.ConnectionConfig, ctx db.ConnectionContext) (db.Driver, error) {
if (config.TlsConfig.SslCert == "" && config.TlsConfig.SslKey != "") ||
(config.TlsConfig.SslCert != "" && config.TlsConfig.SslKey == "") {
return nil, fmt.Errorf("ssl-cert and ssl-key must be both set or unset")
diff --git a/store/migration/10001__init_schema.sql b/store/migration/10001__init_schema.sql
index c98f30d628..18570f0fe5 100644
--- a/store/migration/10001__init_schema.sql
+++ b/store/migration/10001__init_schema.sql
@@ -307,7 +307,7 @@ CREATE TABLE instance (
updated_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
environment_id INTEGER NOT NULL REFERENCES environment (id),
name TEXT NOT NULL,
- `engine` TEXT NOT NULL CHECK (`engine` IN ('MYSQL', 'POSTGRES')),
+ `engine` TEXT NOT NULL CHECK (`engine` IN ('MYSQL', 'POSTGRES', 'TIDB')),
host TEXT NOT NULL,
port TEXT NOT NULL,
external_link TEXT NOT NULL DEFAULT ''