Skip to content

Commit

Permalink
Merge pull request #1620 from running-elephant/dev
Browse files Browse the repository at this point in the history
Sync Dev into Master
  • Loading branch information
tianlu-root authored Jul 12, 2022
2 parents a8228e4 + eee0cfa commit 796c7ca
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 17 deletions.
Binary file modified bin/h2/datart.demo.mv.db
Binary file not shown.
2 changes: 2 additions & 0 deletions config/profiles/application-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ server:
keyAlias: tomcat

datart:
migration:
enable: true # 是否开启数据库自动升级
server:
address: ${datart.address:http://127.0.0.1:8080}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ private static Context createContext(JdbcDriverInfo driverInfo) {
public void unparseOffsetFetch(SqlWriter writer, SqlNode offset, SqlNode fetch) {
if (driverInfo.getSupportSqlLimit() != null && driverInfo.getSupportSqlLimit()) {
unparseFetchUsingLimit(writer, offset, fetch);
} else {
super.unparseOffsetFetch(writer, offset, fetch);
}
super.unparseOffsetFetch(writer, offset, fetch);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.parser.SqlParseException;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest(classes = DataProviderTestApplication.class)
@Slf4j
@Disabled
public class SqlScriptRenderTest {

private static final String T = "DATART_VTABLE";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.data.util.ReflectionUtils;

Expand Down Expand Up @@ -101,21 +102,42 @@ private String getConfigName(Class clazz, String fieldName) {
}

private void switchProfile(ConfigurableEnvironment environment) {
String url = getDefaultDBUrl(environment);
if (url == null || (url.contains("null") && environment.getProperty(CONFIG_DATABASE_URL) == null)) {
environment.setActiveProfiles("demo");
System.err.println("【********* Invalid database configuration. Datart is running in demo mode *********】");
try {
String url = getDefaultDBUrl(environment);
if (url == null || (url.contains("null") && environment.getProperty(CONFIG_DATABASE_URL) == null)) {
environment.setActiveProfiles("demo");
// remove default config propertySource
String defaultConfig = null;
for (PropertySource<?> propertySource : environment.getPropertySources()) {
if (propertySource.getName().contains("application-config")) {
defaultConfig = propertySource.getName();
}
}
if (defaultConfig != null) {
environment.getPropertySources().remove(defaultConfig);
}
// add demo propertySource
List<PropertySource<?>> propertySources = new YamlPropertySourceLoader().load("demo", new ClassPathResource("application-demo.yml"));
if (propertySources != null && propertySources.size() > 0) {
for (PropertySource<?> propertySource : propertySources) {
environment.getPropertySources().addFirst(propertySource);
}
}
System.err.println("【********* Invalid database configuration. Datart is running in demo mode *********】");
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}

private String processDBUrl(ConfigurableEnvironment environment){
private String processDBUrl(ConfigurableEnvironment environment) {
String jdbcUrl = environment.getProperty(DATABASE_URL);
if (!StringUtils.startsWith(jdbcUrl, "jdbc:mysql")) {
return "";
}
Boolean isModify = false;
String[] split = StringUtils.split(jdbcUrl, "?");
if (split.length>1) {
if (split.length > 1) {
Map<String, Object> urlParams = UrlUtils.getParamsMap(split[1]);
if (!"true".equals(urlParams.getOrDefault("allowMultiQueries", "false"))) {
isModify = true;
Expand All @@ -128,7 +150,7 @@ private String processDBUrl(ConfigurableEnvironment environment){
jdbcUrl = split[0] + "?" + UrlUtils.covertMapToUrlParams(urlParams);
} else {
isModify = true;
jdbcUrl = jdbcUrl+"?allowMultiQueries=true&characterEncoding=utf-8";
jdbcUrl = jdbcUrl + "?allowMultiQueries=true&characterEncoding=utf-8";
}
if (isModify) {
return jdbcUrl;
Expand All @@ -148,7 +170,7 @@ private String getDefaultDBUrl(ConfigurableEnvironment environment) {
System.err.println("Default config application-config not found ");
return null;
}
return (String) propertySources.get(0).getProperty(DATABASE_URL);
return environment.getProperty(DATABASE_URL);
} catch (Exception e) {
e.printStackTrace();
System.err.println("Default config application-config not found ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import datart.core.migration.DatabaseMigration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.Ordered;
Expand All @@ -31,12 +30,10 @@
@Component
public class DatabaseMigrationAware implements ApplicationContextAware, Ordered {

@Value("${datart.migration.enable:true}")
private boolean migrationEnable;

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if (!migrationEnable) {
String migrationEnable = applicationContext.getEnvironment().getProperty("datart.migration.enable");
if (migrationEnable == null || "false".equals(migrationEnable)) {
return;
}
DatabaseMigration databaseMigration = applicationContext.getBean(DatabaseMigration.class);
Expand All @@ -51,4 +48,5 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
public int getOrder() {
return 0;
}

}
1 change: 1 addition & 0 deletions server/src/main/resources/application-demo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ server:
mime-types: application/javascript,application/json,application/xml,text/html,text/xml,text/plain,text/css,image/*

datart:

migration:
enable: false

Expand Down
6 changes: 3 additions & 3 deletions server/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ shiro:
enabled: false

datart:
migration:
enable: false

server:
path-prefix: /api/v1

migration:
enable: true

server:
port: 8080
address: 0.0.0.0
Expand Down

0 comments on commit 796c7ca

Please sign in to comment.