Browse Source

PostgreSQL迁移插件

master
lidongy 4 years ago
commit
74767a05c4
  1. 6
      .gitignore
  2. 130
      build.xml
  3. BIN
      lib/finekit-10.0-20200630.jar
  4. BIN
      lib/postgresql-42.1.4.jar
  5. 24
      plugin.xml
  6. 23
      pom.xml
  7. 10
      readme.md
  8. 20
      src/main/java/com/fr/plugin/migration/pgsql/FRPGSQLDialect.java
  9. 26
      src/main/java/com/fr/plugin/migration/pgsql/PGSQLClassLoaderHandler.java
  10. 21
      src/main/java/com/fr/plugin/migration/pgsql/PGSQLDialect.java
  11. 42
      src/main/java/com/fr/plugin/migration/pgsql/PGSQLDialectCreator.java
  12. 69
      src/main/java/com/fr/plugin/migration/pgsql/PGSQLLifecycleMonitor.java
  13. 35
      src/main/java/com/fr/plugin/migration/pgsql/PGSQLMigrationConfig.java
  14. 20
      src/main/java/com/fr/plugin/migration/pgsql/web/PGSQLComponent.java
  15. 22
      src/main/java/com/fr/plugin/migration/pgsql/web/PGSQLWebResource.java
  16. BIN
      src/main/resources/FRPGSQLDialect.class
  17. 22
      src/main/resources/com/fr/plugin/migration/pgsql/web/js/plugin.min.js
  18. BIN
      src/main/resources/postgresql-42.1.4.jar

6
.gitignore vendored

@ -0,0 +1,6 @@
*.iml
.idea/
.DS_Store
.classpath
target/
lib/report/*.jar

130
build.xml

@ -0,0 +1,130 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="jar" name="websocketToken">
<!-- JDK路径,根据自己机器上实际位置修改-->
<property name="jdk.home" value="C:\Program Files\Java\jdk1.8.0_121"/>
<property name="libs" value="${basedir}/lib"/>
<property name="publicLibs" value=""/>
<property name="reportLibs" value="${basedir}/../webroot/WEB-INF/lib"/>
<property name="destLoc" value="."/>
<property name="classes" value="classes"/>
<xmlproperty file="${basedir}/plugin.xml"/>
<property name="current-version" value="${plugin.version}"/>
<!-- 插件版本-->
<property name="plugin-version" value="${current-version}"/>
<!-- 插件名字-->
<property name="plugin-name" value="migration-pgsql"/>
<property name="plugin-jar" value="fr-plugin-${plugin-name}-${plugin-version}.jar"/>
<target name="prepare">
<delete dir="${classes}"/>
<delete dir="fr-plugin-${plugin-name}-${plugin-version}"/>
<xmlproperty file="${basedir}/plugin.xml"/>
<delete dir="${destLoc}/${plugin.name}"/>
</target>
<path id="compile.classpath">
<fileset dir="${libs}">
<include name="**/*.jar"/>
</fileset>
<fileset dir="${publicLibs}">
<include name="**/*.jar"/>
</fileset>
<fileset dir="${reportLibs}">
<include name="**/*.jar"/>
</fileset>
</path>
<patternset id="resources4Jar">
<exclude name="**/.settings/**"/>
<exclude name=".classpath"/>
<exclude name=".project"/>
<exclude name="**/*.java"/>
<exclude name="**/*.db"/>
<exclude name="**/*.g"/>
<exclude name="**/package.html"/>
</patternset>
<target name="copy_resources">
<echo message="从${resources_from}拷贝图片,JS,CSS等资源文件"/>
<delete dir="tmp"/>
<copy todir="tmp">
<fileset dir="${resources_from}/src/main/resources">
<patternset refid="resources4Jar"/>
</fileset>
</copy>
<copy todir="${classes}">
<fileset dir="tmp"/>
</copy>
<delete dir="tmp"/>
</target>
<target name="compile_javas">
<echo message="编译${compile_files}下的Java文件"/>
<javac destdir="${classes}" debug="false" optimize="on" source="${source_jdk_version}"
target="${target_jdk_version}"
fork="true" memoryMaximumSize="512m" listfiles="false" srcdir="${basedir}"
executable="${compile_jdk_version}/bin/javac">
<src path="${basedir}/src/main/java"/>
<exclude name="**/.svn/**"/>
<compilerarg line="-encoding UTF8 "/>
<classpath refid="compile.classpath"/>
</javac>
<taskdef name="pretreatment" classname="com.fr.plugin.pack.PluginPretreatmentTask">
<classpath refid="compile.classpath"/>
</taskdef>
<pretreatment baseDir="${basedir}"/>
</target>
<target name="jar_classes">
<echo message="打Jar包:${jar_name}"/>
<delete file="${basedir}/${jar_name}"/>
<jar jarfile="${basedir}/${jar_name}">
<fileset dir="${classes}">
</fileset>
</jar>
</target>
<target name="super_jar" depends="prepare">
<antcall target="copy_resources">
<param name="resources_from" value="${basedir}"/>
</antcall>
<antcall target="compile_javas">
<param name="source_jdk_version" value="1.6"/>
<param name="target_jdk_version" value="1.6"/>
<param name="compile_jdk_version" value="${jdk.home}"/>
<param name="compile_files" value="${basedir}/src"/>
</antcall>
<echo message="compile plugin success!"/>
<antcall target="jar_classes">
<param name="jar_name" value="${plugin-jar}"/>
</antcall>
<delete dir="${classes}"/>
</target>
<target name="jar" depends="super_jar">
<antcall target="zip"/>
</target>
<target name="zip">
<property name="plugin-folder" value="fr-plugin-${plugin-name}-${plugin-version}"/>
<echo message="----------zip files----------"/>
<mkdir dir="${plugin-folder}"/>
<copy todir="${plugin-folder}">
<fileset dir=".">
<include name="${plugin-jar}"/>
<include name="plugin.xml"/>
</fileset>
<fileset dir="${libs}">
<include name="*.jar"/>
<include name="*.dll"/>
</fileset>
</copy>
<zip destfile="${basedir}/${plugin-folder}.zip" basedir=".">
<include name="${plugin-folder}/*.jar"/>
<include name="${plugin-folder}/*.dll"/>
<include name="${plugin-folder}/plugin.xml"/>
</zip>
<move file="${plugin-folder}.zip" todir="${destLoc}/install"/>
</target>
</project>

BIN
lib/finekit-10.0-20200630.jar

Binary file not shown.

BIN
lib/postgresql-42.1.4.jar

Binary file not shown.

24
plugin.xml

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<plugin>
<id>com.fr.plugin.migration.pgsql</id>
<name><![CDATA[PostgreSQL外置数据库迁移]]></name>
<active>yes</active>
<hidden>no</hidden>
<version>1.0.0</version>
<env-version>10.0</env-version>
<jartime>2020-11-18</jartime>
<vendor>dec.lidongy</vendor>
<description><![CDATA[PostgreSQL外置数据库迁移]]></description>
<change-notes><![CDATA[
<p>[2020-11-18]第一版</p>
]]></change-notes>
<extra-core>
<DataSourceDriverLoader class="com.fr.plugin.migration.pgsql.PGSQLClassLoaderHandler"/>
<DialectCreator class="com.fr.plugin.migration.pgsql.PGSQLDialectCreator"/>
</extra-core>
<extra-decision>
<WebResourceProvider class="com.fr.plugin.migration.pgsql.web.PGSQLWebResource"/>
</extra-decision>
<lifecycle-monitor class="com.fr.plugin.migration.pgsql.PGSQLLifecycleMonitor"/>
<function-recorder class="com.fr.plugin.migration.pgsql.PGSQLDialectCreator"/>
</plugin>

23
pom.xml

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.fr.plugin</groupId>
<artifactId>starter</artifactId>
<version>10.0</version>
</parent>
<packaging>jar</packaging>
<artifactId>decision-migration-pgsql</artifactId>
<dependencies>
<dependency>
<groupId>com.fanruan.api</groupId>
<artifactId>finekit</artifactId>
<version>10.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/finekit-10.0.jar</systemPath>
</dependency>
</dependencies>
</project>

10
readme.md

@ -0,0 +1,10 @@
# PostgreSQL外置数据库迁移插件
## 作用
将工程迁移至PostgreSQL外置数据库
## 插件使用简介
点击平台/系统管理,选择PostgreSQL,开始迁移

20
src/main/java/com/fr/plugin/migration/pgsql/FRPGSQLDialect.java

@ -0,0 +1,20 @@
package com.fr.plugin.migration.pgsql;
import com.fr.third.org.hibernate.dialect.PostgresPlusDialect;
import java.sql.Types;
/**
* hibernate方言具体实现
*
* @author lidongy
* @version 10.0
* Created by lidongy on 2020/7/30
*/
public class FRPGSQLDialect extends PostgresPlusDialect {
public FRPGSQLDialect() {
super();
registerColumnType(Types.VARCHAR, 65536, "varchar($l)");
registerColumnType(Types.VARCHAR, "text");
}
}

26
src/main/java/com/fr/plugin/migration/pgsql/PGSQLClassLoaderHandler.java

@ -0,0 +1,26 @@
package com.fr.plugin.migration.pgsql;
import com.fanruan.api.log.LogKit;
import com.fr.stable.fun.impl.AbstractDataSourceDriverLoader;
/**
* 迁移前数据库测试连接处的过滤
*
* @author lidongy
* @version 10.0
* Created by lidongy on 2020/11/18
*/
public class PGSQLClassLoaderHandler extends AbstractDataSourceDriverLoader {
public boolean isInterceptAllowed(String databaseType, String url) {
if (url.startsWith("jdbc:postgresql")) {
LogKit.info("[com.fr.plugin.connection.mysql8] The connection has been intercepted.");
return true;
}
return false;
}
public ClassLoader getClassLoader() {
return this.getClass().getClassLoader();
}
}

21
src/main/java/com/fr/plugin/migration/pgsql/PGSQLDialect.java

@ -0,0 +1,21 @@
package com.fr.plugin.migration.pgsql;
import com.fr.data.core.db.dialect.PostgreSQLDialect;
import com.fr.data.core.db.dialect.base.DialectKeyConstants;
import com.fr.data.core.db.dialect.base.key.hibernate.HibernateDialectClassExecutor;
/**
* 我们自己的方言框架非hibernate的实现内部反射调用hibernate方言
*
* @author lidongy
* @version 10.0
* Created by lidongy on 2020/7/31
*/
public class PGSQLDialect extends PostgreSQLDialect {
public PGSQLDialect() {
super();
putExecutor(DialectKeyConstants.HIBERNATE_DIALECT_CLASS_KEY, new HibernateDialectClassExecutor(FRPGSQLDialect.class.getName()));
}
}

42
src/main/java/com/fr/plugin/migration/pgsql/PGSQLDialectCreator.java

@ -0,0 +1,42 @@
package com.fr.plugin.migration.pgsql;
import com.fanruan.api.log.LogKit;
import com.fanruan.api.util.StringKit;
import com.fr.intelli.record.Focus;
import com.fr.intelli.record.Original;
import com.fr.record.analyzer.EnableMetrics;
import com.fr.stable.UrlDriver;
import com.fr.stable.fun.impl.AbstractDialectCreator;
import java.sql.Connection;
/**
* 迁移时代码走到的的方言过滤
*
* @author lidongy
* @version 10.0
* Created by lidongy on 2020/7/31
*/
@EnableMetrics
public class PGSQLDialectCreator extends AbstractDialectCreator {
@Override
@Focus(id = "com.fr.plugin.migration.pgsql", text = "", source = Original.PLUGIN)
public Class<?> generate(UrlDriver urlDriver) {
if (StringKit.equals(urlDriver.getDriver(), "org.postgresql.Driver")) {
return PGSQLDialect.class;
}
return null;
}
@Override
public Class<?> generate(Connection connection) {
try {
if (connection.getMetaData().getURL().startsWith("jdbc:postgresql")) {
return PGSQLDialect.class;
}
} catch (Exception e) {
LogKit.error(e, e.getMessage());
}
return null;
}
}

69
src/main/java/com/fr/plugin/migration/pgsql/PGSQLLifecycleMonitor.java

@ -0,0 +1,69 @@
package com.fr.plugin.migration.pgsql;
import com.fanruan.api.util.GeneralKit;
import com.fanruan.api.util.IOKit;
import com.fr.config.FineDBDriverClassLoaderStore;
import com.fr.log.FineLoggerFactory;
import com.fr.plugin.context.PluginContext;
import com.fr.plugin.observer.inner.AbstractPluginLifecycleMonitor;
import com.fr.third.guava.io.ByteStreams;
import com.fr.workspace.WorkContext;
import java.io.IOException;
import java.io.InputStream;
/**
* 生命周期
*
* @author lidongy
* @version 10.0
* Created by lidongy on 2020/11/5
*/
public class PGSQLLifecycleMonitor extends AbstractPluginLifecycleMonitor {
@Override
public void afterRun(PluginContext pluginContext) {
//用配置项,而不放在afterInstall的原因:afterInstall的时候还读不到resource里的内容
if (!PGSQLMigrationConfig.getInstance().isInit()) {
// 移动方言、jar包,防止重启后,外置数据库模块在插件模块之前启动,依赖不到方言和jar。
// 此处不放在生命周期类afterInstall方法中的原因是要考虑 1,此插件有可能升级的情况 2,在afterInstall处读不到resource中的内容
try {
moveDialectClass();
moveDriverLib();
} catch (IOException e) {
FineLoggerFactory.getLogger().error(e, e.getMessage());
}
PGSQLMigrationConfig.getInstance().setInit(true);
}
// 产品内部逻辑实现是,注册之后的类加载器,在连接外置数据库时,都会传给druid。
FineDBDriverClassLoaderStore.getInstance().add(this.getClass().getClassLoader());
}
@Override
public void beforeStop(PluginContext pluginContext) {
FineDBDriverClassLoaderStore.getInstance().remove(this.getClass().getClassLoader());
}
@Override
public void beforeUninstall(PluginContext pluginContext) {
PGSQLMigrationConfig.getInstance().setInit(false);
}
private void moveDialectClass() throws IOException {
InputStream is = IOKit.readResource("FRPGSQLDialect.class");
String path = GeneralKit.pathJoin("/classes", FRPGSQLDialect.class.getName().replace('.', '/') + ".class");
if (!WorkContext.getWorkResource().exist(path)) {
WorkContext.getWorkResource().write(path, ByteStreams.toByteArray(is));
}
}
private void moveDriverLib() throws IOException {
InputStream is = IOKit.readResource("postgresql-42.1.4.jar");
String path = "/lib/postgresql-42.1.4.jar";
if (!WorkContext.getWorkResource().exist(path)) {
WorkContext.getWorkResource().write(path, ByteStreams.toByteArray(is));
}
}
}

35
src/main/java/com/fr/plugin/migration/pgsql/PGSQLMigrationConfig.java

@ -0,0 +1,35 @@
package com.fr.plugin.migration.pgsql;
import com.fr.config.ConfigContext;
import com.fr.config.DefaultConfiguration;
import com.fr.config.Identifier;
import com.fr.config.holder.Conf;
import com.fr.config.holder.factory.Holders;
/**
* @author lidongy
* @version 10.0
* Created by lidongy on 2020/11/27
*/
public class PGSQLMigrationConfig extends DefaultConfiguration {
@Identifier(value = "init")
private Conf<Boolean> init = Holders.simple(false);
private static volatile PGSQLMigrationConfig config = null;
public static PGSQLMigrationConfig getInstance() {
if (config == null) {
config = ConfigContext.getConfigInstance(PGSQLMigrationConfig.class);
}
return config;
}
public Boolean isInit() {
return init.get();
}
public void setInit(Boolean init) {
this.init.set(init);
}
}

20
src/main/java/com/fr/plugin/migration/pgsql/web/PGSQLComponent.java

@ -0,0 +1,20 @@
package com.fr.plugin.migration.pgsql.web;
import com.fr.web.struct.Component;
import com.fr.web.struct.browser.RequestClient;
import com.fr.web.struct.category.ScriptPath;
/**
* @author lidongy
* @version 10.0
* Created by lidongy on 2020/11/5
*/
public class PGSQLComponent extends Component {
public static PGSQLComponent KEY = new PGSQLComponent();
@Override
public ScriptPath script(RequestClient req) {
return ScriptPath.build("com/fr/plugin/migration/pgsql/web/js/plugin.min.js");
}
}

22
src/main/java/com/fr/plugin/migration/pgsql/web/PGSQLWebResource.java

@ -0,0 +1,22 @@
package com.fr.plugin.migration.pgsql.web;
import com.fr.decision.fun.impl.AbstractWebResourceProvider;
import com.fr.decision.web.MainComponent;
import com.fr.web.struct.Atom;
/**
* @author lidongy
* @version 10.0
* Created by lidongy on 2020/11/5
*/
public class PGSQLWebResource extends AbstractWebResourceProvider {
@Override
public Atom attach() {
return MainComponent.KEY;
}
@Override
public Atom client() {
return PGSQLComponent.KEY;
}
}

BIN
src/main/resources/FRPGSQLDialect.class

Binary file not shown.

22
src/main/resources/com/fr/plugin/migration/pgsql/web/js/plugin.min.js vendored

@ -0,0 +1,22 @@
!(function () {
BI.config('dec.provider.migration', provider => {
provider.registerDatabase({
value: 'postgresql',
driver: ['org.postgresql.Driver'],
url: 'jdbc:postgresql://{host}:{port}/{database}',
schema: true,
port: 5432,
}, url => {
const result = url.match(/^jdbc:postgresql:\/\/([0-9a-zA-Z_\\.-]+)(:([0-9]+))?\/([0-9a-zA-Z_\\.]+)(.*)/i);
if (result) {
return {
host: result[1],
port: result[3],
databaseName: result[4],
urlInfo: result[5],
};
}
});
});
}());

BIN
src/main/resources/postgresql-42.1.4.jar

Binary file not shown.
Loading…
Cancel
Save