Skip to content

feat(plugin): Add Key-Value Storage Support for Plugins #1265

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 9 commits into from
May 9, 2025
32 changes: 32 additions & 0 deletions internal/entity/plugin_kv_storage_entity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package entity

type PluginKVStorage struct {
ID int `xorm:"not null pk autoincr INT(11) id"`
PluginSlugName string `xorm:"not null VARCHAR(128) UNIQUE(uk_psg) plugin_slug_name"`
Group string `xorm:"not null VARCHAR(128) UNIQUE(uk_psg) 'group'"`
Key string `xorm:"not null VARCHAR(128) UNIQUE(uk_psg) 'key'"`
Value string `xorm:"not null TEXT value"`
}

func (PluginKVStorage) TableName() string {
return "plugin_kv_storage"
}
1 change: 1 addition & 0 deletions internal/migrations/init_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ var (
&entity.Badge{},
&entity.BadgeGroup{},
&entity.BadgeAward{},
&entity.PluginKVStorage{},
}

roles = []*entity.Role{
Expand Down
1 change: 1 addition & 0 deletions internal/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ var migrations = []Migration{
NewMigration("v1.4.1", "add question link", addQuestionLink, true),
NewMigration("v1.4.2", "add the number of question links", addQuestionLinkedCount, true),
NewMigration("v1.4.5", "add file record", addFileRecord, true),
NewMigration("v1.5.1", "add plugin kv storage", addPluginKVStorage, true),
}

func GetMigrations() []Migration {
Expand Down
31 changes: 31 additions & 0 deletions internal/migrations/v26.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package migrations

import (
"context"

"github.com/apache/answer/internal/entity"
"xorm.io/xorm"
)

func addPluginKVStorage(ctx context.Context, x *xorm.Engine) error {
return x.Context(ctx).Sync(new(entity.PluginKVStorage))
}
9 changes: 9 additions & 0 deletions internal/service/plugin_common/plugin_common_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,15 @@ func (ps *PluginCommonService) GetUserPluginConfig(ctx context.Context, req *sch
}

func (ps *PluginCommonService) initPluginData() {
_ = plugin.CallKVStorage(func(k plugin.KVStorage) error {
k.SetOperator(plugin.NewKVOperator(
ps.data.DB,
ps.data.Cache,
k.Info().SlugName,
))
return nil
})

// init plugin status
pluginStatus, err := ps.configService.GetStringValue(context.TODO(), constant.PluginStatus)
if err != nil {
Expand Down
Loading