Skip to content

Commit

Permalink
upgrade(version): 0.0.3 适配 nacos 2.3.2
Browse files Browse the repository at this point in the history
注意更新SQL , users 表字段不兼容
  • Loading branch information
lltx committed Aug 25, 2024
1 parent b162a1a commit 6a8be6f
Show file tree
Hide file tree
Showing 13 changed files with 431 additions and 618 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@ Nacos 从 2.2.0 版本开始,可通过 SPI 机制注入多数据源实现插件,

> 依赖已上传 maven 中央仓库,请勿使用阿里云代理
| NACOS 版本 | 插件版本 |
|---------------|-------|
| 2.2.0 - 2.3.0 | 0.0.2 |
| 2.3.1 - 2.3.2 | 0.0.3 |

```xml
<dependency>
<groupId>com.pig4cloud.plugin</groupId>
<artifactId>nacos-datasource-plugin-pg</artifactId>
<version>0.0.2</version>
<version>${nacos.plugin.version}</version>
</dependency>

<dependency>
Expand Down
41 changes: 18 additions & 23 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,31 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.15</version>
<version>2.7.18</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.pig4cloud.plugin</groupId>
<artifactId>nacos-datasource-plugin-pg</artifactId>
<version>0.0.2</version>
<version>0.0.3</version>
<name>nacos-datasource-plugin-pg</name>
<description>nacos-datasource-plugin-pg</description>
<url>https://pig4cloud.com</url>

<licenses>
<license>
<name>The ApacheSoftware License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>

<properties>
<nacos.version>2.3.2</nacos.version>
<spring.checkstyle.plugin>0.0.32</spring.checkstyle.plugin>
<maven.compiler.version>3.8.1</maven.compiler.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.checkstyle.plugin>0.0.32</spring.checkstyle.plugin>
<git.commit.plugin>2.2.5</git.commit.plugin>
<commons-compress>1.21</commons-compress>
<maven.compiler.version>3.8.1</maven.compiler.version>
</properties>

<developers>
Expand All @@ -37,6 +38,7 @@
<email>[email protected]</email>
</developer>
</developers>

<scm>
<tag>master</tag>
<connection>https://gitee.wang/lengleng/pig</connection>
Expand All @@ -45,42 +47,34 @@
</scm>

<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-datasource-plugin</artifactId>
<version>2.2.4</version>
<version>${nacos.version}</version>
</dependency>

<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-common</artifactId>
<version>2.2.4</version>
<version>${nacos.version}</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<!--打包jar 与git commit 关联插件-->
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<version>${git.commit.plugin}</version>
</plugin>
<!--代码格式插件,默认使用spring 规则-->
<plugin>
<groupId>io.spring.javaformat</groupId>
Expand Down Expand Up @@ -272,4 +266,5 @@
</distributionManagement>
</profile>
</profiles>

</project>
29 changes: 19 additions & 10 deletions sql/nacos-pg.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE SEQUENCE "config_info_aggr_id_seq"
CREATE SEQUENCE "config_info_aggr_id_seq"
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
Expand All @@ -8,7 +8,7 @@ CACHE 1;
-- ----------------------------
-- Sequence structure for config_info_beta_id_seq
-- ----------------------------
CREATE SEQUENCE "config_info_beta_id_seq"
CREATE SEQUENCE "config_info_beta_id_seq"
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
Expand All @@ -18,7 +18,7 @@ CACHE 1;
-- ----------------------------
-- Sequence structure for config_info_id_seq
-- ----------------------------
CREATE SEQUENCE "config_info_id_seq"
CREATE SEQUENCE "config_info_id_seq"
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
Expand All @@ -28,7 +28,7 @@ CACHE 1;
-- ----------------------------
-- Sequence structure for config_info_tag_id_seq
-- ----------------------------
CREATE SEQUENCE "config_info_tag_id_seq"
CREATE SEQUENCE "config_info_tag_id_seq"
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
Expand All @@ -38,7 +38,7 @@ CACHE 1;
-- ----------------------------
-- Sequence structure for config_tags_relation_id_seq
-- ----------------------------
CREATE SEQUENCE "config_tags_relation_id_seq"
CREATE SEQUENCE "config_tags_relation_id_seq"
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
Expand All @@ -48,7 +48,7 @@ CACHE 1;
-- ----------------------------
-- Sequence structure for group_capacity_id_seq
-- ----------------------------
CREATE SEQUENCE "group_capacity_id_seq"
CREATE SEQUENCE "group_capacity_id_seq"
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
Expand All @@ -58,7 +58,7 @@ CACHE 1;
-- ----------------------------
-- Sequence structure for his_config_info_nid_seq
-- ----------------------------
CREATE SEQUENCE "his_config_info_nid_seq"
CREATE SEQUENCE "his_config_info_nid_seq"
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
Expand All @@ -68,7 +68,7 @@ CACHE 1;
-- ----------------------------
-- Sequence structure for tenant_capacity_id_seq
-- ----------------------------
CREATE SEQUENCE "tenant_capacity_id_seq"
CREATE SEQUENCE "tenant_capacity_id_seq"
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
Expand All @@ -78,7 +78,7 @@ CACHE 1;
-- ----------------------------
-- Sequence structure for tenant_info_id_seq
-- ----------------------------
CREATE SEQUENCE "tenant_info_id_seq"
CREATE SEQUENCE "tenant_info_id_seq"
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
Expand Down Expand Up @@ -547,4 +547,13 @@ CREATE INDEX "uk_tenant_info_kptenantid" ON "tenant_info" USING btree (
-- ----------------------------
-- Primary Key structure for table tenant_info
-- ----------------------------
ALTER TABLE "tenant_info" ADD CONSTRAINT "tenant_info_pkey" PRIMARY KEY ("id");
ALTER TABLE "tenant_info" ADD CONSTRAINT "tenant_info_pkey" PRIMARY KEY ("id");

-- 新版这个字段是boolean类型,旧版是int类型,需要转换
ALTER TABLE "users"
ALTER COLUMN "enabled" TYPE boolean
USING CASE
WHEN "enabled" = 1 THEN true
WHEN "enabled" = 0 THEN false
ELSE NULL
END;
Original file line number Diff line number Diff line change
@@ -1,75 +1,35 @@
package com.pig4cloud.plugin.impl.postgresql;

import com.alibaba.nacos.plugin.datasource.constants.TableConstant;
import com.alibaba.nacos.plugin.datasource.mapper.AbstractMapper;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.alibaba.nacos.plugin.datasource.constants.FieldConstant;
import com.alibaba.nacos.plugin.datasource.mapper.ConfigInfoAggrMapper;
import com.alibaba.nacos.plugin.datasource.model.MapperContext;
import com.alibaba.nacos.plugin.datasource.model.MapperResult;
import com.pig4cloud.plugin.constants.DataSourceConstant;

import java.util.List;

public class ConfigInfoAggrMapperByPostgresql extends AbstractMapper implements ConfigInfoAggrMapper {

/**
* 批量删除
* @param datumList
* @return
*/
public String batchRemoveAggr(List<String> datumList) {
final StringBuilder datumString = new StringBuilder();
for (String datum : datumList) {
datumString.append('\'').append(datum).append("',");
}
datumString.deleteCharAt(datumString.length() - 1);
return "DELETE FROM config_info_aggr WHERE data_id = ? AND group_id = ? AND tenant_id = ? AND datum_id IN ("
+ datumString + ")";
}

@Override
public String aggrConfigInfoCount(int size, boolean isIn) {
StringBuilder sql = new StringBuilder(
"SELECT count(*) FROM config_info_aggr WHERE data_id = ? AND group_id = ? AND tenant_id = ? AND datum_id");
if (isIn) {
sql.append(" IN (");
}
else {
sql.append(" NOT IN (");
}
for (int i = 0; i < size; i++) {
if (i > 0) {
sql.append(", ");
}
sql.append('?');
}
sql.append(')');

return sql.toString();
}

@Override
public String findConfigInfoAggrIsOrdered() {
return "SELECT data_id,group_id,tenant_id,datum_id,app_name,content FROM "
+ "config_info_aggr WHERE data_id = ? AND group_id = ? AND tenant_id = ? ORDER BY datum_id";
}

@Override
public String findConfigInfoAggrByPageFetchRows(int startRow, int pageSize) {
return "SELECT data_id,group_id,tenant_id,datum_id,app_name,content FROM config_info_aggr WHERE data_id= ? AND "
+ "group_id= ? AND tenant_id= ? ORDER BY datum_id LIMIT " + pageSize + " OFFSET " + startRow;
}
public class ConfigInfoAggrMapperByPostgresql extends PostgresqlAbstractMapper implements ConfigInfoAggrMapper {

@Override
public String findAllAggrGroupByDistinct() {
return "SELECT DISTINCT data_id, group_id, tenant_id FROM config_info_aggr";
}

@Override
public String getTableName() {
return TableConstant.CONFIG_INFO_AGGR;
public MapperResult findConfigInfoAggrByPageFetchRows(MapperContext context) {
int startRow = context.getStartRow();
int pageSize = context.getPageSize();
String dataId = (String) context.getWhereParameter(FieldConstant.DATA_ID);
String groupId = (String) context.getWhereParameter(FieldConstant.GROUP_ID);
String tenantId = (String) context.getWhereParameter(FieldConstant.TENANT_ID);

String sql = "SELECT data_id,group_id,tenant_id,datum_id,app_name,content FROM config_info_aggr WHERE data_id= ? AND "
+ "group_id= ? AND tenant_id= ? ORDER BY datum_id LIMIT " + pageSize + " offset "
+ startRow;
List<Object> paramList = CollectionUtils.list(dataId, groupId, tenantId);
return new MapperResult(sql, paramList);
}

@Override
public String getDataSource() {
return DataSourceConstant.POSTGRESQL;
}


}
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
package com.pig4cloud.plugin.impl.postgresql;

import com.alibaba.nacos.plugin.datasource.constants.TableConstant;
import com.alibaba.nacos.plugin.datasource.mapper.AbstractMapper;
import com.alibaba.nacos.plugin.datasource.mapper.ConfigInfoBetaMapper;
import com.alibaba.nacos.plugin.datasource.model.MapperContext;
import com.alibaba.nacos.plugin.datasource.model.MapperResult;
import com.pig4cloud.plugin.constants.DataSourceConstant;

public class ConfigInfoBetaMapperByPostgresql extends AbstractMapper implements ConfigInfoBetaMapper {
import java.util.ArrayList;
import java.util.List;

@Override
public String updateConfigInfo4BetaCas() {
return "UPDATE config_info_beta SET content = ?,md5 = ?,beta_ips = ?,src_ip = ?,src_user = ?,gmt_modified = ?,app_name = ? "
+ "WHERE data_id = ? AND group_id = ? AND tenant_id = ? AND (md5 = ? or md5 is null or md5 = '')";
}
public class ConfigInfoBetaMapperByPostgresql extends PostgresqlAbstractMapper implements ConfigInfoBetaMapper {

@Override
public String findAllConfigInfoBetaForDumpAllFetchRows(int startRow, int pageSize) {
return " SELECT t.id,data_id,group_id,tenant_id,app_name,content,md5,gmt_modified,beta_ips,encrypted_data_key "
+ " FROM ( SELECT id FROM config_info_beta ORDER BY id LIMIT " + pageSize + " OFFSET " + startRow
+ " )" + " g, config_info_beta t WHERE g.id = t.id ";
}
public MapperResult findAllConfigInfoBetaForDumpAllFetchRows(MapperContext context) {
int startRow = context.getStartRow();
int pageSize = context.getPageSize();
String sql = " SELECT t.id,data_id,group_id,tenant_id,app_name,content,md5,gmt_modified,beta_ips,encrypted_data_key "
+ " FROM ( SELECT id FROM config_info_beta ORDER BY id LIMIT " + pageSize + " offset "
+ startRow + " )" + " g, config_info_beta t WHERE g.id = t.id ";
List<Object> paramList = new ArrayList<>();
paramList.add(startRow);
paramList.add(pageSize);

@Override
public String getTableName() {
return TableConstant.CONFIG_INFO_BETA;
return new MapperResult(sql, paramList);
}

@Override
Expand Down
Loading

0 comments on commit 6a8be6f

Please sign in to comment.