Skip to content

Commit

Permalink
mr offical dev/4.2.1
Browse files Browse the repository at this point in the history
Merge remote-tracking branch 'offical/dev/4.2.1' into yaobin_fix_osc_foreign_key_ddl_failed
  • Loading branch information
krihy committed Sep 4, 2023
2 parents d2a91b4 + 66bb388 commit 496b8e1
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,30 @@ public class OscFactoryWrapperGeneratorTest {
private static String oraclePrefix;
private static String mysqlPrefix;
private static String newTableSuffix;
private static String newTableSuffixOBOracle;
private static String renamedTableSuffix;
private static String renamedTableSuffixOBOracle;

@BeforeClass
public static void before() {
oraclePrefix = DdlConstants.OSC_TABLE_NAME_PREFIX_OB_ORACLE;
mysqlPrefix = DdlConstants.OSC_TABLE_NAME_PREFIX;
newTableSuffix = DdlConstants.NEW_TABLE_NAME_SUFFIX;
newTableSuffixOBOracle = DdlConstants.NEW_TABLE_NAME_SUFFIX_OB_ORACLE;
renamedTableSuffix = DdlConstants.RENAMED_TABLE_NAME_SUFFIX;
renamedTableSuffixOBOracle = DdlConstants.RENAMED_TABLE_NAME_SUFFIX_OB_ORACLE;
}

@Test
public void test_ob_oracle_table_name() {
TableNameDescriptorFactory tableNameDescriptorFactory = getTableNameDescriptorFactory(DialectType.OB_ORACLE);
TableNameDescriptor descriptor = tableNameDescriptorFactory.getTableNameDescriptor("t");
Assert.equals("t", descriptor.getOriginTableNameUnwrapped());
Assert.equals(oraclePrefix + "t" + newTableSuffix,
Assert.equals(oraclePrefix + "t" + newTableSuffixOBOracle,
descriptor.getNewTableName());
Assert.equals(oraclePrefix + "t" + newTableSuffix,
Assert.equals(oraclePrefix + "t" + newTableSuffixOBOracle,
descriptor.getNewTableNameUnWrapped());
Assert.equals(oraclePrefix + "t" + renamedTableSuffix,
Assert.equals(oraclePrefix + "t" + renamedTableSuffixOBOracle,
descriptor.getRenamedTableName());

}
Expand All @@ -66,12 +70,12 @@ public void test_ob_oracle_table_name_quote() {
TableNameDescriptorFactory tableNameDescriptorFactory = getTableNameDescriptorFactory(DialectType.OB_ORACLE);
TableNameDescriptor descriptor = tableNameDescriptorFactory.getTableNameDescriptor("\"t\"");
Assert.equals("t", descriptor.getOriginTableNameUnwrapped());
Assert.equals(quote(oraclePrefix + "t" + newTableSuffix),
Assert.equals(quote(oraclePrefix + "t" + newTableSuffixOBOracle),
descriptor.getNewTableName());
Assert.equals(oraclePrefix + "t" + newTableSuffix,
Assert.equals(oraclePrefix + "t" + newTableSuffixOBOracle,
descriptor.getNewTableNameUnWrapped());
Assert.equals(
quote(oraclePrefix + "t" + renamedTableSuffix),
quote(oraclePrefix + "t" + renamedTableSuffixOBOracle),
descriptor.getRenamedTableName());
}

Expand All @@ -92,15 +96,15 @@ public void test_ob_mysql_table_name() {

@Test
public void test_ob_mysql_table_name_accent() {
TableNameDescriptorFactory tableNameDescriptorFactory = getTableNameDescriptorFactory(DialectType.OB_ORACLE);
TableNameDescriptorFactory tableNameDescriptorFactory = getTableNameDescriptorFactory(DialectType.OB_MYSQL);
TableNameDescriptor descriptor = tableNameDescriptorFactory.getTableNameDescriptor("`t`");
Assert.equals("t", descriptor.getOriginTableNameUnwrapped());
Assert.equals(accent(oraclePrefix + "t" + newTableSuffix),
Assert.equals(accent(mysqlPrefix + "t" + newTableSuffix),
descriptor.getNewTableName());
Assert.equals(oraclePrefix + "t" + newTableSuffix,
Assert.equals(mysqlPrefix + "t" + newTableSuffix,
descriptor.getNewTableNameUnWrapped());
Assert.equals(
accent(oraclePrefix + "t" + renamedTableSuffix),
accent(mysqlPrefix + "t" + renamedTableSuffix),
descriptor.getRenamedTableName());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ public interface ScheduleRepository extends OdcJpaRepository<ScheduleEntity, Lon

default Page<ScheduleEntity> find(@NotNull Pageable pageable, @NotNull QueryScheduleParams params) {
Specification<ScheduleEntity> specification = Specification
.where(OdcJpaRepository.between(ScheduleEntity_.createTime, params.getStartTime(), params.getEndTime())
.and(OdcJpaRepository.eq(ScheduleEntity_.jobType, params.getType()))
.and(OdcJpaRepository.in(ScheduleEntity_.projectId, params.getProjectIds()))
.and(OdcJpaRepository.in(ScheduleEntity_.creatorId, params.getCreatorIds()))
.and(OdcJpaRepository.eq(ScheduleEntity_.id, params.getId()))
.and(OdcJpaRepository.in(ScheduleEntity_.status, params.getStatuses())))
.where(OdcJpaRepository.between(ScheduleEntity_.createTime, params.getStartTime(), params.getEndTime()))
.and(OdcJpaRepository.eq(ScheduleEntity_.jobType, params.getType()))
.and(OdcJpaRepository.in(ScheduleEntity_.projectId, params.getProjectIds()))
.and(OdcJpaRepository.in(ScheduleEntity_.creatorId, params.getCreatorIds()))
.and(OdcJpaRepository.eq(ScheduleEntity_.id, params.getId()))
.and(OdcJpaRepository.in(ScheduleEntity_.status, params.getStatuses()))
.and(OdcJpaRepository.eq(ScheduleEntity_.organizationId, params.getOrganizationId()));
return findAll(specification, pageable);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ private void prepareSchema(OnlineSchemaChangeParameters param, OnlineSchemaChang
// update new table ddl for display
String finalTableDdl = DdlUtils.queryOriginTableCreateDdl(session, taskParam.getNewTableName());
String ddlForDisplay = DdlUtils.replaceTableName(finalTableDdl, taskParam.getOriginTableName(),
session.getDialectType(), param.getSqlType());
session.getDialectType(), OnlineSchemaChangeSqlType.CREATE);
taskParam.setNewTableCreateDdlForDisplay(ddlForDisplay);
scheduleTaskRepository.updateTaskResult(scheduleTaskId,
JsonUtils.toJson(new OnlineSchemaChangeScheduleTaskResult(taskParam)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,22 @@ public TableNameDescriptor getTableNameDescriptor(String tableName) {
nameDescriptor.setOriginTableName(tableName);
nameDescriptor.setOriginTableNameUnwrapped(DdlUtils.getUnwrappedName(tableName));

String newTableName = DdlUtils.getNewNameWithSuffix(tableName, tablePrefix(),
DdlConstants.NEW_TABLE_NAME_SUFFIX);
String newTableName = DdlUtils.getNewNameWithSuffix(tableName, tablePrefix(), newTableSuffix());
nameDescriptor.setNewTableName(newTableName);
nameDescriptor.setNewTableNameUnWrapped(DdlUtils.getUnwrappedName(newTableName));

String renamedTableName = DdlUtils.getNewNameWithSuffix(tableName, tablePrefix(),
DdlConstants.RENAMED_TABLE_NAME_SUFFIX);
String renamedTableName = DdlUtils.getNewNameWithSuffix(tableName, tablePrefix(), renamedTableSuffix());
nameDescriptor.setRenamedTableName(renamedTableName);
nameDescriptor.setRenamedTableNameUnWrapped(DdlUtils.getUnwrappedName(renamedTableName));
return nameDescriptor;
}

protected abstract String tablePrefix();

protected abstract String newTableSuffix();

protected abstract String renamedTableSuffix();



}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public class DdlConstants {

public static final String RENAMED_TABLE_NAME_SUFFIX = "_osc_old_";

public static final String NEW_TABLE_NAME_SUFFIX_OB_ORACLE = "_OSC_NEW_";

public static final String RENAMED_TABLE_NAME_SUFFIX_OB_ORACLE = "_OSC_OLD_";

public static final String TABLE_NAME_WRAPPER = "`";

public static final String TABLE_NAME_WRAPPED_QUOTE = "\"";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,14 @@ public class OBMySqlTableNameDescriptorFactory extends BaseTableNameDescriptorFa
protected String tablePrefix() {
return DdlConstants.OSC_TABLE_NAME_PREFIX;
}

@Override
protected String newTableSuffix() {
return DdlConstants.NEW_TABLE_NAME_SUFFIX;
}

@Override
protected String renamedTableSuffix() {
return DdlConstants.RENAMED_TABLE_NAME_SUFFIX;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,14 @@ public class OBOracleTableNameDescriptorFactory extends BaseTableNameDescriptorF
protected String tablePrefix() {
return DdlConstants.OSC_TABLE_NAME_PREFIX_OB_ORACLE;
}

@Override
protected String newTableSuffix() {
return DdlConstants.NEW_TABLE_NAME_SUFFIX_OB_ORACLE;
}

@Override
protected String renamedTableSuffix() {
return DdlConstants.RENAMED_TABLE_NAME_SUFFIX_OB_ORACLE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import org.antlr.v4.runtime.tree.TerminalNode;

import com.oceanbase.odc.common.util.StringUtils;
import com.oceanbase.odc.core.shared.PreConditions;
import com.oceanbase.tools.sqlparser.FastFailErrorListener;
import com.oceanbase.tools.sqlparser.oboracle.OBLexer;
Expand Down Expand Up @@ -133,10 +134,7 @@ public void enterConstraint_name(OBParser.Constraint_nameContext ctx) {
ParseTree childNode = relation_nameContext.getChild(0);
if (childNode instanceof TerminalNode) {
TerminalNode terminalNode = (TerminalNode) childNode;
// todo replace by OscFactoryWrapper
tokenStreamRewriter.replace(terminalNode.getSymbol(), DdlUtils.getNewNameWithSuffix(
terminalNode.getSymbol().getText(), DdlConstants.OSC_TABLE_NAME_PREFIX_OB_ORACLE,
DdlUtils.getUUIDWithoutUnderline()));
tokenStreamRewriter.replace(terminalNode.getSymbol(), "A" + StringUtils.uuidNoHyphen());
}
}
}
Expand All @@ -153,10 +151,8 @@ public void enterIndex_name(Index_nameContext ctx) {
ParseTree childNode = relation_nameContext.getChild(0);
if (childNode instanceof TerminalNode) {
TerminalNode terminalNode = (TerminalNode) childNode;
// todo replace by OscFactoryWrapper
tokenStreamRewriter.replace(terminalNode.getSymbol(), DdlUtils.getNewNameWithSuffix(
terminalNode.getSymbol().getText(), DdlConstants.OSC_TABLE_NAME_PREFIX,
DdlUtils.getUUIDWithoutUnderline()));
// replace constraints name
tokenStreamRewriter.replace(terminalNode.getSymbol(), "A" + StringUtils.uuidNoHyphen());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.oceanbase.odc.common.util.StringUtils;
import com.oceanbase.odc.core.shared.constant.DialectType;

Expand Down Expand Up @@ -91,7 +90,6 @@ public class OnlineSchemaChangeScheduleTaskParameters {
/**
* For ODC internal usage
*/
@JsonIgnore
private List<String> sqlsToBeExecuted = new ArrayList<>();

public String getOriginTableNameWithSchema() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,33 +77,39 @@ private OnlineSchemaChangeScheduleTaskParameters createNewParameter(String sql,
if (sqlType == OnlineSchemaChangeSqlType.ALTER) {
AlterTable statement = (AlterTable) parse(sql);
String tableName = statement.getTableName();
taskParameter.setNewTableCreateDdlForDisplay("");
populateTaskParameter(sqlType, taskParameter, tableName);
TableNameDescriptor tableNameDescriptor = tableNameDescriptorFactory.getTableNameDescriptor(tableName);
String originTableCreateDdl = DdlUtils.queryOriginTableCreateDdl(session, tableName);
taskParameter.setOriginTableCreateDdl(originTableCreateDdl);
taskParameter.setNewTableCreateDdl(DdlUtils.replaceTableName(originTableCreateDdl,
tableNameDescriptor.getNewTableName(), session.getDialectType(), OnlineSchemaChangeSqlType.CREATE));

populateTaskParameter(tableNameDescriptor, taskParameter, tableName);
} else {
CreateTable statement = (CreateTable) parse(sql);
String tableName = statement.getTableName();
String originTableCreateDdl = DdlUtils.queryOriginTableCreateDdl(session, tableName);
taskParameter.setOriginTableCreateDdl(originTableCreateDdl);
TableNameDescriptor tableNameDescriptor = tableNameDescriptorFactory.getTableNameDescriptor(tableName);
taskParameter.setNewTableCreateDdl(DdlUtils.replaceTableName(sql,
tableNameDescriptor.getNewTableName(), session.getDialectType(), OnlineSchemaChangeSqlType.CREATE));
taskParameter.setNewTableCreateDdlForDisplay(sql);
populateTaskParameter(sqlType, taskParameter, tableName);

populateTaskParameter(tableNameDescriptor, taskParameter, tableName);
}
return taskParameter;
}

private void populateTaskParameter(OnlineSchemaChangeSqlType sqlType,
private void populateTaskParameter(TableNameDescriptor tableNameDescriptor,
OnlineSchemaChangeScheduleTaskParameters taskParameter,
String tableName) throws SQLException {
String tableName) {
taskParameter.setOriginTableName(tableName);
TableNameDescriptor tableNameDescriptor = tableNameDescriptorFactory.getTableNameDescriptor(tableName);

taskParameter.setNewTableName(tableNameDescriptor.getNewTableName());
taskParameter.setRenamedTableName(tableNameDescriptor.getRenamedTableName());
taskParameter.setNewTableNameUnwrapped(tableNameDescriptor.getNewTableNameUnWrapped());
taskParameter.setOriginTableNameUnwrapped(tableNameDescriptor.getOriginTableNameUnwrapped());
taskParameter.setRenamedTableNameUnwrapped(tableNameDescriptor.getRenamedTableNameUnWrapped());

String originTableCreateDdl = DdlUtils.queryOriginTableCreateDdl(session, tableName);
taskParameter.setOriginTableCreateDdl(originTableCreateDdl);
taskParameter.setNewTableCreateDdl(DdlUtils.replaceTableName(originTableCreateDdl,
tableNameDescriptor.getNewTableName(), session.getDialectType(), sqlType));

}

private Statement parse(String sql) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.io.StringReader;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -50,6 +51,8 @@
import com.oceanbase.tools.dbbrowser.schema.DBSchemaAccessor;
import com.oceanbase.tools.sqlparser.OBMySQLParser;
import com.oceanbase.tools.sqlparser.OBOracleSQLParser;
import com.oceanbase.tools.sqlparser.SQLParser;
import com.oceanbase.tools.sqlparser.SyntaxErrorException;
import com.oceanbase.tools.sqlparser.statement.Statement;
import com.oceanbase.tools.sqlparser.statement.alter.table.AlterTable;
import com.oceanbase.tools.sqlparser.statement.createtable.CreateTable;
Expand All @@ -67,25 +70,24 @@ public class OnlineSchemaChangeValidator {
private OscConnectionConfigValidator oscConnectionConfigValidator;

public void validate(CreateFlowInstanceReq createReq) {

OnlineSchemaChangeParameters parameter = (OnlineSchemaChangeParameters) createReq.getParameters();
PreConditions.notEmpty(parameter.getSqlContent(), "Input sql cant not bee empty");

ConnectionConfig connectionConfig =
connectionService.getForConnectionSkipPermissionCheck(createReq.getConnectionId());
connectionConfig.setDefaultSchema(createReq.getDatabaseName());
ConnectionSession session = new DefaultConnectSessionFactory(connectionConfig).generateSession();
List<String> sqls = SqlUtils.split(connectionConfig.getDialectType(), parameter.getSqlContent(),
parameter.getDelimiter());;

PreConditions.notEmpty(sqls, "Parser sqls is empty");
oscConnectionConfigValidator.valid(connectionConfig);

try {
List<String> sqls =
SqlUtils.split(connectionConfig.getDialectType(), parameter.getSqlContent(),
parameter.getDelimiter());
for (String sql : sqls) {
Statement statement =
(connectionConfig.getDialectType().isMysql() ? new OBMySQLParser() : new OBOracleSQLParser())
.parse(new StringReader(sql));

validateType(sql, getSqlType(statement), parameter.getSqlType());
List<Statement> statements = parseStatements(parameter, connectionConfig, sqls);

ConnectionSession session = new DefaultConnectSessionFactory(connectionConfig).generateSession();
try {
for (Statement statement : statements) {
String database = createReq.getDatabaseName();
String tableName;
if (parameter.getSqlType() == OnlineSchemaChangeSqlType.CREATE) {
Expand All @@ -103,7 +105,6 @@ public void validate(CreateFlowInstanceReq createReq) {
}

validateTableNameLength(tableName, connectionConfig.getDialectType());

validateOriginTableExists(database, tableName, session);
validateOldTableNotExists(database, tableName, session);
validateTableConstraints(database, tableName, session);
Expand All @@ -122,6 +123,23 @@ public void validate(CreateFlowInstanceReq createReq) {

}

private List<Statement> parseStatements(OnlineSchemaChangeParameters parameter,
ConnectionConfig connectionConfig, List<String> sqls) {
List<Statement> statements = null;
try {
SQLParser sqlParser =
connectionConfig.getDialectType().isMysql() ? new OBMySQLParser() : new OBOracleSQLParser();
statements = sqls.stream().map(sql -> {
Statement statement = sqlParser.parse(new StringReader(sql));
validateType(sql, getSqlType(statement), parameter.getSqlType());
return statement;
}).collect(Collectors.toList());
} catch (SyntaxErrorException ex) {
throw new BadArgumentException(ErrorCodes.ObPreCheckDdlFailed, ex.getLocalizedMessage());
}
return statements;
}

private void validateSchema(String currentSchema, String expectedSchema, DialectType dialectType) {
currentSchema = dialectType.isMysql() ? StringUtils.unquoteMySqlIdentifier(currentSchema)
: StringUtils.unquoteOracleIdentifier(currentSchema);
Expand Down
Loading

0 comments on commit 496b8e1

Please sign in to comment.