Fix foreign key constraints still exist after table flashback rename

This commit is contained in:
18523270951@163.com
2023-07-11 18:53:59 +00:00
committed by ob-robot
parent e269245733
commit 742766c42e

View File

@ -4398,26 +4398,28 @@ int ObTableSqlService::delete_foreign_key(
} }
for (int64_t i = 0; OB_SUCC(ret) && i < foreign_key_infos.count(); i++) { for (int64_t i = 0; OB_SUCC(ret) && i < foreign_key_infos.count(); i++) {
const ObForeignKeyInfo &foreign_key_info = foreign_key_infos.at(i); const ObForeignKeyInfo &foreign_key_info = foreign_key_infos.at(i);
if (is_truncate_table // truncate table will drop parent table and create it again /*When the parent table or child table is deleted,
|| table_schema.is_offline_ddl_table() // offline ddl table will swap and drop parent table *the foreign key relationship should be deleted.
|| table_schema.get_table_id() == foreign_key_info.child_table_id_) { *If the parent table is deleted under foreign_key_checks=off,
uint64_t foreign_key_id = foreign_key_info.foreign_key_id_; *the foreign key relationship will be mocked by the child table
if (OB_FAIL(delete_from_all_foreign_key(sql_client, tenant_id, new_schema_version, foreign_key_info))) { */
LOG_WARN("failed to delete __all_foreign_key_history", K(table_schema), K(ret)); uint64_t foreign_key_id = foreign_key_info.foreign_key_id_;
} else if (OB_UNLIKELY(foreign_key_info.child_column_ids_.count() != foreign_key_info.parent_column_ids_.count())) { if (OB_FAIL(delete_from_all_foreign_key(sql_client, tenant_id, new_schema_version, foreign_key_info))) {
ret = OB_INVALID_ARGUMENT; LOG_WARN("failed to delete __all_foreign_key_history", K(table_schema), K(ret));
LOG_WARN("child column num and parent column num should be equal", K(ret), } else if (OB_UNLIKELY(foreign_key_info.child_column_ids_.count() != foreign_key_info.parent_column_ids_.count())) {
K(foreign_key_info.child_column_ids_.count()), ret = OB_INVALID_ARGUMENT;
K(foreign_key_info.parent_column_ids_.count())); LOG_WARN("child column num and parent column num should be equal", K(ret),
} else { K(foreign_key_info.child_column_ids_.count()),
for (int64_t j = 0; OB_SUCC(ret) && j < foreign_key_info.child_column_ids_.count(); j++) { K(foreign_key_info.parent_column_ids_.count()));
// delete from __all_foreign_key_column_history } else if (table_schema.get_table_id() == foreign_key_info.child_table_id_) {
uint64_t child_column_id = foreign_key_info.child_column_ids_.at(j); //The column table needs to be updated only when the child table is deleted
uint64_t parent_column_id = foreign_key_info.parent_column_ids_.at(j); for (int64_t j = 0; OB_SUCC(ret) && j < foreign_key_info.child_column_ids_.count(); j++) {
if (OB_FAIL(delete_from_all_foreign_key_column( // delete from __all_foreign_key_column_history
sql_client, tenant_id, foreign_key_id, child_column_id, parent_column_id, j + 1 /* fk_column_pos */, new_schema_version))) { uint64_t child_column_id = foreign_key_info.child_column_ids_.at(j);
LOG_WARN("failed to delete __all_foreign_key_column_history", K(ret)); uint64_t parent_column_id = foreign_key_info.parent_column_ids_.at(j);
} if (OB_FAIL(delete_from_all_foreign_key_column(
sql_client, tenant_id, foreign_key_id, child_column_id, parent_column_id, j + 1 /* fk_column_pos */, new_schema_version))) {
LOG_WARN("failed to delete __all_foreign_key_column_history", K(ret));
} }
} }
} }
@ -4533,51 +4535,29 @@ int ObTableSqlService::update_foreign_key_state(common::ObISQLClient &sql_client
// skip // skip
} else { } else {
dml.reset(); dml.reset();
if (OB_FAIL(dml.add_pk_column("tenant_id", ObSchemaUtils::get_extract_tenant_id( int64_t affected_rows = 0;
exec_tenant_id, tenant_id))) //UPDATE is not used to update __all_foreign_key because the parent table may have deleted the record in advance
|| OB_FAIL(dml.add_pk_column("foreign_key_id", ObSchemaUtils::get_extract_schema_id( if (OB_FAIL(gen_foreign_key_dml(exec_tenant_id, tenant_id, foreign_key_info, dml))) {
exec_tenant_id, foreign_key_info.foreign_key_id_))) LOG_WARN("failed to gen foreign key dml", K(ret));
|| OB_FAIL(dml.add_gmt_modified()) } else if (OB_FAIL(exec.exec_insert_update(OB_ALL_FOREIGN_KEY_TNAME, dml, affected_rows))) {
) { LOG_WARN("exec update failed", K(ret));
LOG_WARN("failed to add columns", K(ret)); } else if (affected_rows > 2) {
} else if (foreign_key_info.is_modify_enable_flag_ && OB_FAIL(dml.add_column("enable_flag", foreign_key_info.enable_flag_))) { ret = OB_ERR_UNEXPECTED;
LOG_WARN("failed to add enable_flag column", K(ret)); LOG_WARN("unexpected error", K(affected_rows), K(ret));
} else if (foreign_key_info.is_modify_validate_flag_ && OB_FAIL(dml.add_column("validate_flag", foreign_key_info.validate_flag_))) { } else if (OB_FAIL(dml.add_column("schema_version", table.get_schema_version()))) {
LOG_WARN("failed to add validate_flag column", K(ret)); LOG_WARN("add column failed", K(ret));
} else if (foreign_key_info.is_modify_rely_flag_ && OB_FAIL(dml.add_column("rely_flag", foreign_key_info.rely_flag_))) { } else if (OB_FAIL(dml.add_column("is_deleted", false))) {
LOG_WARN("failed to add rely_flag column", K(ret)); LOG_WARN("add column failed", K(ret));
} else if (foreign_key_info.is_modify_fk_name_flag_ && OB_FAIL(dml.add_column("foreign_key_name", foreign_key_info.foreign_key_name_))) { } else if (OB_FAIL(exec.exec_insert(OB_ALL_FOREIGN_KEY_HISTORY_TNAME, dml, affected_rows))) {
LOG_WARN("failed to add foreign_key_name column", K(ret)); LOG_WARN("execute insert failed", K(ret));
} else { } else if (!is_single_row(affected_rows)) {
int64_t affected_rows = 0; ret = OB_ERR_UNEXPECTED;
uint64_t table_id = foreign_key_info.table_id_; LOG_WARN("affected_rows unexpected to be one", K(affected_rows), K(ret));
if (OB_FAIL(exec_update(sql_client, tenant_id, table_id, }
OB_ALL_FOREIGN_KEY_TNAME, dml, affected_rows))) { if (OB_SUCC(ret)) {
LOG_WARN("exec update failed", K(ret)); if (OB_FAIL(update_data_table_schema_version(sql_client, tenant_id,
} else if (affected_rows > 1) { foreign_key_info.parent_table_id_, table.get_in_offline_ddl_white_list()))) {
ret = OB_ERR_UNEXPECTED; LOG_WARN("failed to update parent table schema version", K(ret));
LOG_WARN("unexpected error", K(affected_rows), K(ret));
}
if (OB_SUCC(ret)) {
dml.reset();
if (OB_FAIL(gen_foreign_key_dml(exec_tenant_id, tenant_id, foreign_key_info, dml))) {
LOG_WARN("failed to gen foreign key dml", K(ret));
} else if (OB_FAIL(dml.add_column("schema_version", table.get_schema_version()))) {
LOG_WARN("add column failed", K(ret));
} else if (OB_FAIL(dml.add_column("is_deleted", false))) {
LOG_WARN("add column failed", K(ret));
} else if (OB_FAIL(exec.exec_insert(OB_ALL_FOREIGN_KEY_HISTORY_TNAME, dml, affected_rows))) {
LOG_WARN("execute insert failed", K(ret));
} else if (!is_single_row(affected_rows)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("affected_rows unexpected to be one", K(affected_rows), K(ret));
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(update_data_table_schema_version(sql_client, tenant_id,
foreign_key_info.parent_table_id_, table.get_in_offline_ddl_white_list()))) {
LOG_WARN("failed to update parent table schema version", K(ret));
}
} }
} }
} }
@ -5142,41 +5122,24 @@ int ObTableSqlService::update_foreign_key_in_mock_fk_parent_table(
for (int64_t i = 0; OB_SUCC(ret) && i < foreign_key_infos.count(); ++i) { for (int64_t i = 0; OB_SUCC(ret) && i < foreign_key_infos.count(); ++i) {
const ObForeignKeyInfo &foreign_key_info = foreign_key_infos.at(i); const ObForeignKeyInfo &foreign_key_info = foreign_key_infos.at(i);
dml.reset(); dml.reset();
if (OB_FAIL(dml.add_pk_column("tenant_id", ObSchemaUtils::get_extract_tenant_id(exec_tenant_id, tenant_id))) int64_t affected_rows = 0;
|| OB_FAIL(dml.add_pk_column("foreign_key_id", ObSchemaUtils::get_extract_schema_id(exec_tenant_id, foreign_key_info.foreign_key_id_))) //UPDATE is not used to update __all_foreign_key because the parent table may have deleted the record in advance
|| OB_FAIL(dml.add_gmt_modified())) { if (OB_FAIL(gen_foreign_key_dml(exec_tenant_id, tenant_id, foreign_key_info, dml))) {
LOG_WARN("failed to add columns", K(ret)); LOG_WARN("failed to gen foreign key dml", K(ret));
} else if (OB_FAIL(dml.add_column("parent_table_id", ObSchemaUtils::get_extract_schema_id(exec_tenant_id, foreign_key_info.parent_table_id_)))) { } else if (OB_FAIL(exec.exec_insert_update(OB_ALL_FOREIGN_KEY_TNAME, dml, affected_rows))) {
LOG_WARN("failed to add parent_table_id column", K(ret)); LOG_WARN("exec update failed", K(ret));
} else if (OB_FAIL(dml.add_column("is_parent_table_mock", foreign_key_info.is_parent_table_mock_))) { } else if (affected_rows > 2) {
LOG_WARN("failed to add is_parent_table_mock column", K(ret)); ret = OB_ERR_UNEXPECTED;
} else if (OB_FAIL(dml.add_column("ref_cst_type", foreign_key_info.ref_cst_type_))) { LOG_WARN("unexpected error", K(ret), K(affected_rows));
LOG_WARN("failed to add ref_cst_type column", K(ret)); } else if (OB_FAIL(dml.add_column("schema_version", need_update_foreign_key_columns ? new_schema_version : new_mock_fk_parent_table_schema.get_schema_version()))) {
} else if (OB_FAIL(dml.add_column("ref_cst_id", foreign_key_info.ref_cst_id_))) { LOG_WARN("add column failed", K(ret));
LOG_WARN("failed to add ref_cst_id column", K(ret)); } else if (OB_FAIL(dml.add_column("is_deleted", false))) {
} else { LOG_WARN("add column failed", K(ret));
int64_t affected_rows = 0; } else if (OB_FAIL(exec.exec_insert(OB_ALL_FOREIGN_KEY_HISTORY_TNAME, dml, affected_rows))) {
if (OB_FAIL(exec.exec_update(OB_ALL_FOREIGN_KEY_TNAME, dml, affected_rows))) { LOG_WARN("execute insert failed", K(ret));
LOG_WARN("exec update failed", K(ret)); } else if (!is_single_row(affected_rows)) {
} else if (affected_rows > 1) { ret = OB_ERR_UNEXPECTED;
ret = OB_ERR_UNEXPECTED; LOG_WARN("affected_rows unexpected to be one", K(affected_rows), K(ret));
LOG_WARN("unexpected error", K(ret), K(affected_rows));
}
if (OB_SUCC(ret)) {
dml.reset();
if (OB_FAIL(gen_foreign_key_dml(exec_tenant_id, tenant_id, foreign_key_info, dml))) {
LOG_WARN("failed to gen foreign key dml", K(ret));
} else if (OB_FAIL(dml.add_column("schema_version", need_update_foreign_key_columns ? new_schema_version : new_mock_fk_parent_table_schema.get_schema_version()))) {
LOG_WARN("add column failed", K(ret));
} else if (OB_FAIL(dml.add_column("is_deleted", false))) {
LOG_WARN("add column failed", K(ret));
} else if (OB_FAIL(exec.exec_insert(OB_ALL_FOREIGN_KEY_HISTORY_TNAME, dml, affected_rows))) {
LOG_WARN("execute insert failed", K(ret));
} else if (!is_single_row(affected_rows)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("affected_rows unexpected to be one", K(affected_rows), K(ret));
}
}
} }
} }
return ret; return ret;