Skip to content

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

feat: instance count limit (#517)

ecmadao25511a3ffd83
diff --git a/api/instance.go b/api/instance.go
index 92d4d84b94..de4be270bd 100644
--- a/api/instance.go
+++ b/api/instance.go
@@ -157,5 +157,6 @@ type InstanceService interface {
CreateInstance(ctx context.Context, create *InstanceCreate) (*Instance, error)
FindInstanceList(ctx context.Context, find *InstanceFind) ([]*Instance, error)
FindInstance(ctx context.Context, find *InstanceFind) (*Instance, error)
+ CountInstance(ctx context.Context, find *InstanceFind) (int, error)
PatchInstance(ctx context.Context, patch *InstancePatch) (*Instance, error)
}
diff --git a/frontend/src/components/QuickActionPanel.vue b/frontend/src/components/QuickActionPanel.vue
index 2554ac5d73..3e3e43dfae 100644
--- a/frontend/src/components/QuickActionPanel.vue
+++ b/frontend/src/components/QuickActionPanel.vue
@@ -1,5 +1,7 @@
<template>
- <div class="pt-1 overflow-hidden grid grid-cols-4 gap-x-2 gap-y-4 md:inline-flex md:gap-x-0">
+ <div
+ class="pt-1 overflow-hidden grid grid-cols-4 gap-x-2 gap-y-4 md:inline-flex md:gap-x-0"
+ >
<template v-for="(quickAction, index) in quickActionList" :key="index">
<div
v-if="quickAction == 'quickaction.bb.instance.create'"
@@ -8,9 +10,9 @@
<button class="btn-icon-primary p-3" @click.prevent="createInstance">
<heroicons-outline:plus-sm class="w-6 h-6" />
</button>
- <h3
- class="mt-1 text-base font-normal text-main tracking-tight"
- >{{ $t("quick-action.add-instance") }}</h3>
+ <h3 class="mt-1 text-base font-normal text-main tracking-tight">
+ {{ $t("quick-action.add-instance") }}
+ </h3>
</div>
<div
@@ -22,7 +24,9 @@
</router-link>
<h3
class="mt-1 text-center text-base font-normal text-main tracking-tight"
- >{{ $t("quick-action.manage-user") }}</h3>
+ >
+ {{ $t("quick-action.manage-user") }}
+ </h3>
</div>
<div
@@ -34,7 +38,9 @@
</button>
<h3
class="mt-1 text-center text-base font-normal text-main tracking-tight"
- >{{ $t("common.query") }}</h3>
+ >
+ {{ $t("common.query") }}
+ </h3>
</div>
<div
@@ -46,7 +52,9 @@
</button>
<h3
class="mt-1 text-center text-base font-normal text-main tracking-tight"
- >{{ $t("quick-action.edit-data") }}</h3>
+ >
+ {{ $t("quick-action.edit-data") }}
+ </h3>
</div>
<div
@@ -58,7 +66,9 @@
</button>
<h3
class="mt-1 text-base text-center font-normal text-main tracking-tight"
- >{{ $t("quick-action.new-db") }}</h3>
+ >
+ {{ $t("quick-action.new-db") }}
+ </h3>
</div>
<div
@@ -70,7 +80,9 @@
</button>
<h3
class="mt-1 text-base text-center font-normal text-main tracking-tight"
- >{{ $t("quick-action.request-db") }}</h3>
+ >
+ {{ $t("quick-action.request-db") }}
+ </h3>
</div>
<div
@@ -80,9 +92,9 @@
<button class="btn-icon-primary p-3" @click.prevent="alterSchema">
<heroicons-outline:pencil-alt class="w-6 h-6" />
</button>
- <h3
- class="mt-1 text-center text-base font-normal text-main"
- >{{ $t("database.alter-schema") }}</h3>
+ <h3 class="mt-1 text-center text-base font-normal text-main">
+ {{ $t("database.alter-schema") }}
+ </h3>
</div>
<div
@@ -92,9 +104,9 @@
<button class="btn-icon-primary p-3" @click.prevent="changeData">
<heroicons-outline:pencil class="w-6 h-6" />
</button>
- <h3
- class="mt-1 text-center text-base font-normal text-main"
- >{{ $t("database.change-data") }}</h3>
+ <h3 class="mt-1 text-center text-base font-normal text-main">
+ {{ $t("database.change-data") }}
+ </h3>
</div>
<div
@@ -104,9 +116,9 @@
<router-link to="/issue/new" class="btn-icon-primary p-3">
<heroicons-outline:hand class="w-6 h-6" />
</router-link>
- <h3
- class="mt-1 text-center text-base font-normal text-main"
- >{{ $t("quick-action.troubleshoot") }}</h3>
+ <h3 class="mt-1 text-center text-base font-normal text-main">
+ {{ $t("quick-action.troubleshoot") }}
+ </h3>
</div>
<div
@@ -116,17 +128,24 @@
<button class="btn-icon-primary p-3" @click.prevent="createEnvironment">
<heroicons-outline:plus-sm class="w-6 h-6" />
</button>
- <h3 class="mt-1 text-center text-base font-normal text-main">{{ $t("environment.create") }}</h3>
+ <h3 class="mt-1 text-center text-base font-normal text-main">
+ {{ $t("environment.create") }}
+ </h3>
</div>
<div
v-if="quickAction == 'quickaction.bb.environment.reorder'"
class="flex flex-col items-center w-28"
>
- <button class="btn-icon-primary p-3" @click.prevent="reorderEnvironment">
+ <button
+ class="btn-icon-primary p-3"
+ @click.prevent="reorderEnvironment"
+ >
<heroicons-outline:selector class="transform rotate-90 w-6 h-6" />
</button>
- <h3 class="mt-1 text-center text-base font-normal text-main">{{ $t("common.reorder") }}</h3>
+ <h3 class="mt-1 text-center text-base font-normal text-main">
+ {{ $t("common.reorder") }}
+ </h3>
</div>
<div
@@ -136,9 +155,9 @@
<button class="btn-icon-primary p-3" @click.prevent="createProject">
<heroicons-outline:template class="w-6 h-6" />
</button>
- <h3
- class="mt-1 text-center text-base font-normal text-main"
- >{{ $t("quick-action.new-project") }}</h3>
+ <h3 class="mt-1 text-center text-base font-normal text-main">
+ {{ $t("quick-action.new-project") }}
+ </h3>
</div>
<div
@@ -148,9 +167,9 @@
<button class="btn-icon-primary p-3" @click.prevent="goDefaultProject">
<heroicons-outline:cube class="w-6 h-6" />
</button>
- <h3
- class="mt-1 text-center text-base font-normal text-main"
- >{{ $t("quick-action.default-project") }}</h3>
+ <h3 class="mt-1 text-center text-base font-normal text-main">
+ {{ $t("quick-action.default-project") }}
+ </h3>
</div>
<div
@@ -160,9 +179,9 @@
<button class="btn-icon-primary p-3" @click.prevent="transferDatabase">
<heroicons-outline:chevron-double-down class="w-6 h-6" />
</button>
- <h3
- class="mt-1 text-center text-base font-normal text-main"
- >{{ $t("quick-action.transfer-in-db") }}</h3>
+ <h3 class="mt-1 text-center text-base font-normal text-main">
+ {{ $t("quick-action.transfer-in-db") }}
+ </h3>
</div>
</template>
</div>
@@ -175,7 +194,9 @@
<template v-if="state.quickActionType == 'quickaction.bb.project.create'">
<ProjectCreate @dismiss="state.showModal = false" />
</template>
- <template v-else-if="state.quickActionType == 'quickaction.bb.instance.create'">
+ <template
+ v-else-if="state.quickActionType == 'quickaction.bb.instance.create'"
+ >
<InstanceForm :create="true" @dismiss="state.showModal = false" />
</template>
<template
@@ -191,9 +212,7 @@
/>
</template>
<template
- v-else-if="
- state.quickActionType == 'quickaction.bb.database.data.update'
- "
+ v-else-if="state.quickActionType == 'quickaction.bb.database.data.update'"
>
<!-- eslint-disable vue/attribute-hyphenation -->
<AlterSchemaPrepForm
@@ -202,11 +221,18 @@
@dismiss="state.showModal = false"
/>
</template>
- <template v-else-if="state.quickActionType == 'quickaction.bb.database.create'">
+ <template
+ v-else-if="state.quickActionType == 'quickaction.bb.database.create'"
+ >
<!-- eslint-disable vue/attribute-hyphenation -->
- <CreateDatabasePrepForm :projectId="projectId" @dismiss="state.showModal = false" />
+ <CreateDatabasePrepForm
+ :projectId="projectId"
+ @dismiss="state.showModal = false"
+ />
</template>
- <template v-else-if="state.quickActionType == 'quickaction.bb.database.request'">
+ <template
+ v-else-if="state.quickActionType == 'quickaction.bb.database.request'"
+ >
<RequestDatabasePrepForm @dismiss="state.showModal = false" />
</template>
<template
@@ -215,9 +241,17 @@
"
>
<!-- eslint-disable vue/attribute-hyphenation -->
- <TransferDatabaseForm :projectId="projectId" @dismiss="state.showModal = false" />
+ <TransferDatabaseForm
+ :projectId="projectId"
+ @dismiss="state.showModal = false"
+ />
</template>
</BBModal>
+ <FeatureModal
+ v-if="state.showFeatureModal"
+ feature="bb.feature.instance-count"
+ @cancel="state.showFeatureModal = false"
+ />
</template>
<script lang="ts">
@@ -234,9 +268,11 @@ import { DEFAULT_PROJECT_ID, ProjectId, QuickActionType } from "../types";
import { idFromSlug } from "../utils";
import { Action, defineAction, useRegisterActions } from "@bytebase/vue-kbar";
import { useI18n } from "vue-i18n";
+import { Subscription } from "../types";
interface LocalState {
showModal: boolean;
+ showFeatureModal: boolean;
modalTitle: string;
modalSubtitle: string;
quickActionType: QuickActionType;
@@ -265,6 +301,7 @@ export default defineComponent({
const state = reactive<LocalState>({
showModal: false,
+ showFeatureModal: false,
modalTitle: "",
modalSubtitle: "",
quickActionType: "quickaction.bb.instance.create",
@@ -302,6 +339,13 @@ export default defineComponent({
};
const createInstance = () => {
+ const subscription: Subscription | undefined =
+ store.getters["subscription/subscription"]();
+ const instanceList = store.getters["instance/instanceList"]();
+ if ((subscription?.instanceCount ?? 5) <= instanceList.length) {
+ state.showFeatureModal = true;
+ return;
+ }
state.modalTitle = t("quick-action.create-instance");
state.modalSubtitle = "";
state.quickActionType = "quickaction.bb.instance.create";
diff --git a/frontend/src/locales/en.yml b/frontend/src/locales/en.yml
index 75f7b4b620..d0c48656cf 100644
--- a/frontend/src/locales/en.yml
+++ b/frontend/src/locales/en.yml
@@ -1138,10 +1138,17 @@ subscription:
disabled-feature: This is a premium feature in subscription plan
trial: You can start a trial with $7 for 7 days.
trial-comment: Only enabled for the first payment. You can cancel the subscription at any time.
+ upgrade: Upgrade subscription
features:
- bb.feature.task-schedule-time:
+ bb-feature-task-schedule-time:
name: Set task schedule time
desc: Set task schedule time allows you to set a specific time to run your tasks.
+ bb-feature-instance-count:
+ title: Instance count limit
+ desc: You have reached the maximum instance count limit. Please upgrade to get more instance quota.
+ remaining: Your total instance quota is {total}, only have {count} instance remained.
+ runoutof: Your have run out of your {total} instance quota.
+ upgrade: '{upgrade} to get more instance quota.'
bb-feature-multi-tenancy:
title: Multi-tenancy
desc: Intelligent database management for tenants using identical schemas.
diff --git a/frontend/src/locales/zh-CN.yml b/frontend/src/locales/zh-CN.yml
index c682140bee..7a5a45c455 100644
--- a/frontend/src/locales/zh-CN.yml
+++ b/frontend/src/locales/zh-CN.yml
@@ -1007,10 +1007,17 @@ subscription:
disabled-feature: 这是一个订阅版高级功能
trial: 您可以以 $7 的价格试用 7 天
trial-comment: 仅首次付费可以试用,可随时退款。
+ upgrade: 升级订阅
features:
bb-feature-task-schedule-time:
title: 设定任务执行时间
desc: 「设定任务执行时间」可以将任务设定在特定时间执行
+ bb-feature-instance-count:
+ title: 实例数量限制
+ desc: 您已达最大实例数量限制,请付费升级来获取更多实例额度。
+ remaining: 当前总实例额度为 {total},目前仅剩 {count} 个份额可用。
+ runoutof: 已用尽全部 {total} 个实例额度。
+ upgrade: '{upgrade}来获取更多实例额度。'
bb-feature-multi-tenancy:
title: 多租户
desc: 「多租户功能」可以同时管理多个属于不同租户的同构数据库,帮您更统一和方便地对多个数据库进行管理和表结构变更。
diff --git a/frontend/src/views/InstanceDashboard.vue b/frontend/src/views/InstanceDashboard.vue
index 15c0176339..04bfce8012 100644
--- a/frontend/src/views/InstanceDashboard.vue
+++ b/frontend/src/views/InstanceDashboard.vue
@@ -1,5 +1,40 @@
<template>
<div class="flex flex-col">
+ <div class="m-5 p-2 rounded bg-yellow-300" v-if="remainingInstanceCount <= 3">
+ <i18n-t keypath="subscription.features.bb-feature-instance-count.remaining" v-if="remainingInstanceCount > 0">
+ <template v-slot:total>
+ <span class="text-accent">{{ instanceQuota }}</span>
+ </template>
+ <template v-slot:count>
+ <span class="text-accent">{{ remainingInstanceCount }}</span>
+ </template>
+ <template v-slot:upgrade>
+ <router-link
+ to="/setting/subscription"
+ class="normal-link"
+ >
+ {{ $t("subscription.upgrade")}}
+ </router-link>
+ </template>
+ </i18n-t>
+ <i18n-t v-else keypath="subscription.features.bb-feature-instance-count.runoutof">
+ <template v-slot:total>
+ <span class="text-accent">{{ instanceQuota }}</span>
+ </template>
+ </i18n-t>
+ <span class="pl-1">
+ <i18n-t keypath="subscription.features.bb-feature-instance-count.upgrade">
+ <template v-slot:upgrade>
+ <router-link
+ to="/setting/subscription"
+ class="normal-link"
+ >
+ {{ $t("subscription.upgrade")}}
+ </router-link>
+ </template>
+ </i18n-t>
+ </span>
+ </div>
<div class="px-5 py-2 flex justify-between items-center">
<!-- eslint-disable vue/attribute-hyphenation -->
<EnvironmentTabFilter
@@ -38,7 +73,7 @@ import { useRouter } from "vue-router";
import EnvironmentTabFilter from "../components/EnvironmentTabFilter.vue";
import InstanceTable from "../components/InstanceTable.vue";
import { useStore } from "vuex";
-import { Environment, Instance } from "../types";
+import { Environment, Subscription, Instance } from "../types";
import { cloneDeep } from "lodash-es";
import { sortInstanceList } from "../utils";
@@ -141,6 +176,17 @@ export default {
});
};
+ const instanceQuota = computed((): number => {
+ const subscription: Subscription | undefined =
+ store.getters["subscription/subscription"]();
+ return subscription?.instanceCount ?? 5
+ })
+
+ const remainingInstanceCount = computed((): number => {
+ const instanceList: Instance[] = store.getters["instance/instanceList"](["NORMAL"]);
+ return Math.max(0, instanceQuota.value - instanceList.length)
+ })
+
return {
searchField,
state,
@@ -149,6 +195,8 @@ export default {
selectEnvironment,
changeSearchText,
doDismissGuide,
+ instanceQuota,
+ remainingInstanceCount,
};
},
};
diff --git a/frontend/src/views/InstanceDetail.vue b/frontend/src/views/InstanceDetail.vue
index eb22086fc0..d2665419dd 100644
--- a/frontend/src/views/InstanceDetail.vue
+++ b/frontend/src/views/InstanceDetail.vue
@@ -131,6 +131,11 @@
@dismiss="state.showCreateDatabaseModal = false"
/>
</BBModal>
+ <FeatureModal
+ v-if="state.showFeatureModal"
+ feature="bb.feature.instance-count"
+ @cancel="state.showFeatureModal = false"
+ />
</template>
<script lang="ts">
@@ -152,6 +157,7 @@ import {
} from "../types";
import { BBTabFilterItem } from "../bbkit/types";
import { useI18n } from "vue-i18n";
+import { Subscription } from "../types";
const DATABASE_TAB = 0;
const USER_TAB = 1;
@@ -163,6 +169,7 @@ interface LocalState {
creatingMigrationSchema: boolean;
showCreateDatabaseModal: boolean;
syncingSchema: boolean;
+ showFeatureModal: boolean;
}
export default {
@@ -194,6 +201,7 @@ export default {
creatingMigrationSchema: false,
showCreateDatabaseModal: false,
syncingSchema: false,
+ showFeatureModal: false,
});
const instance = computed((): Instance => {
@@ -337,6 +345,13 @@ export default {
};
const doRestore = () => {
+ const subscription: Subscription | undefined =
+ store.getters["subscription/subscription"]();
+ const instanceList = store.getters["instance/instanceList"](["NORMAL"]);
+ if ((subscription?.instanceCount ?? 0) <= instanceList.length) {
+ state.showFeatureModal = true;
+ return;
+ }
store
.dispatch("instance/patchInstance", {
instanceId: instance.value.id,
diff --git a/server/instance.go b/server/instance.go
index d431589402..5ce97a67df 100644
--- a/server/instance.go
+++ b/server/instance.go
@@ -17,6 +17,10 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
// Besides adding the instance to Bytebase, it will also try to create a "bytebase" db in the newly added instance.
g.POST("/instance", func(c echo.Context) error {
ctx := context.Background()
+ if err := s.instanceCountGuard(ctx); err != nil {
+ return err
+ }
+
instanceCreate := &api.InstanceCreate{}
if err := jsonapi.UnmarshalPayload(c.Request().Body, instanceCreate); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted create instance request").SetInternal(err)
@@ -118,6 +122,13 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
var instance *api.Instance
if instancePatch.RowStatus != nil || instancePatch.Name != nil || instancePatch.ExternalLink != nil || instancePatch.Host != nil || instancePatch.Port != nil {
+ // Users can switch instance status from ARCHIVED to NORMAL.
+ // So we need to check the current instance count with NORMAL status for quota limitation.
+ if instancePatch.RowStatus != nil && *instancePatch.RowStatus == api.Normal.String() {
+ if err := s.instanceCountGuard(ctx); err != nil {
+ return err
+ }
+ }
instance, err = s.InstanceService.PatchInstance(ctx, instancePatch)
if err != nil {
if common.ErrorCode(err) == common.NotFound {
@@ -528,3 +539,22 @@ func (s *Server) findInstanceAdminPasswordByID(ctx context.Context, instanceID i
}
return "", &common.Error{Code: common.NotFound, Err: fmt.Errorf("missing admin password for instance: %d", instanceID)}
}
+
+// instanceCountGuard is a feature guard for instance count.
+// We only count instances with NORMAL status since users cannot make any operations for ARCHIVED one.
+func (s *Server) instanceCountGuard(ctx context.Context) error {
+ status := api.Normal
+ count, err := s.InstanceService.CountInstance(ctx, &api.InstanceFind{
+ RowStatus: &status,
+ })
+
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to count instance").SetInternal(err)
+ }
+ subscription := s.loadSubscription()
+ if count >= subscription.InstanceCount {
+ return echo.NewHTTPError(http.StatusForbidden, fmt.Sprintf("You have reached the maximum instance count %d.", subscription.InstanceCount))
+ }
+
+ return nil
+}
diff --git a/server/plan.go b/server/plan.go
index c33e6fdb76..c8bc7bee8e 100644
--- a/server/plan.go
+++ b/server/plan.go
@@ -40,11 +40,6 @@ func (s *Server) registerPlanRoutes(g *echo.Group) {
}
func (s *Server) feature(feature api.FeatureType) bool {
- plan := api.TEAM
- license, _ := s.loadLicense()
- if license != nil {
- plan = license.Plan
- }
-
- return api.FeatureMatrix[feature][plan]
+ subscription := s.loadSubscription()
+ return api.FeatureMatrix[feature][subscription.Plan]
}
diff --git a/server/subscription.go b/server/subscription.go
index db0cf6bed4..b23a4f8322 100644
--- a/server/subscription.go
+++ b/server/subscription.go
@@ -15,23 +15,7 @@ import (
func (s *Server) registerSubscriptionRoutes(g *echo.Group) {
g.GET("/subscription", func(c echo.Context) error {
- subscription := &enterpriseAPI.Subscription{
- Plan: api.FREE,
- // -1 means not expire, just for free plan
- ExpiresTs: -1,
- InstanceCount: 5,
- }
-
- license, err := s.loadLicense()
- if err != nil {
- s.l.Error("Failed to get valid license for subscription", zap.String("error", err.Error()))
- } else {
- subscription = &enterpriseAPI.Subscription{
- Plan: license.Plan,
- ExpiresTs: license.ExpiresTs,
- InstanceCount: license.InstanceCount,
- }
- }
+ subscription := s.loadSubscription()
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, subscription); err != nil {
@@ -68,6 +52,32 @@ func (s *Server) registerSubscriptionRoutes(g *echo.Group) {
})
}
+// loadLicense will load current subscription by license.
+// Return subscription with free plan if no license found.
+func (server *Server) loadSubscription() *enterprise.Subscription {
+ subscription := &enterpriseAPI.Subscription{
+ Plan: api.TEAM,
+ // -1 means not expire, just for free plan
+ ExpiresTs: -1,
+ InstanceCount: 5,
+ }
+ license, _ := server.loadLicense()
+ if license != nil {
+ subscription = &enterpriseAPI.Subscription{
+ Plan: license.Plan,
+ ExpiresTs: license.ExpiresTs,
+ InstanceCount: license.InstanceCount,
+ }
+ } else {
+ // increase instance quota for dev environment.
+ if server.mode == "dev" {
+ subscription.InstanceCount = subscription.InstanceCount * 2
+ }
+ }
+
+ return subscription
+}
+
// loadLicense will get and parse valid license from file.
func (server *Server) loadLicense() (*enterprise.License, error) {
license, err := server.LicenseService.LoadLicense()
diff --git a/store/instance.go b/store/instance.go
index 2abb8085db..aacb7636df 100644
--- a/store/instance.go
+++ b/store/instance.go
@@ -108,6 +108,36 @@ func (s *InstanceService) FindInstanceList(ctx context.Context, find *api.Instan
return list, nil
}
+func (s *InstanceService) CountInstance(ctx context.Context, find *api.InstanceFind) (int, error) {
+ tx, err := s.db.BeginTx(ctx, nil)
+ if err != nil {
+ return 0, FormatError(err)
+ }
+ defer tx.Rollback()
+
+ where, args := findInstanceQuery(find)
+
+ row, err := tx.QueryContext(ctx, `
+ SELECT COUNT(*)
+ FROM instance
+ WHERE `+where,
+ args...,
+ )
+ if err != nil {
+ return 0, FormatError(err)
+ }
+ defer row.Close()
+
+ count := 0
+ if row.Next() {
+ if err := row.Scan(&count); err != nil {
+ return 0, FormatError(err)
+ }
+ }
+
+ return count, nil
+}
+
// FindInstance retrieves a single instance based on find.
// Returns ECONFLICT if finding more than 1 matching records.
func (s *InstanceService) FindInstance(ctx context.Context, find *api.InstanceFind) (*api.Instance, error) {
@@ -225,14 +255,7 @@ func createInstance(ctx context.Context, tx *Tx, create *api.InstanceCreate) (*a
}
func findInstanceList(ctx context.Context, tx *Tx, find *api.InstanceFind) (_ []*api.Instance, err error) {
- // Build WHERE clause.
- where, args := []string{"1 = 1"}, []interface{}{}
- if v := find.ID; v != nil {
- where, args = append(where, "id = ?"), append(args, *v)
- }
- if v := find.RowStatus; v != nil {
- where, args = append(where, "row_status = ?"), append(args, *v)
- }
+ where, args := findInstanceQuery(find)
rows, err := tx.QueryContext(ctx, `
SELECT
@@ -250,7 +273,7 @@ func findInstanceList(ctx context.Context, tx *Tx, find *api.InstanceFind) (_ []
host,
port
FROM instance
- WHERE `+strings.Join(where, " AND "),
+ WHERE `+where,
args...,
)
if err != nil {
@@ -353,3 +376,16 @@ func patchInstance(ctx context.Context, tx *Tx, patch *api.InstancePatch) (*api.
return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("instance ID not found: %d", patch.ID)}
}
+
+func findInstanceQuery(find *api.InstanceFind) (string, []interface{}) {
+ // Build WHERE clause.
+ where, args := []string{"1 = 1"}, []interface{}{}
+ if v := find.ID; v != nil {
+ where, args = append(where, "id = ?"), append(args, *v)
+ }
+ if v := find.RowStatus; v != nil {
+ where, args = append(where, "row_status = ?"), append(args, *v)
+ }
+
+ return strings.Join(where, " AND "), args
+}