在Magento 2商店中,我遇到了生产上唯一的约束违例,无法调试,只能在那儿复制。
我导出了完整的实时数据库,将其本地导入,并且无法再在那里再现它。
我目前的工作原理是生产数据库不一致,导出+导入解决了这个问题。
如何验证这一论点?有没有可以检查当前MySQL数据库是否存在任何现有约束违规的命令?
编辑:我的问题似乎在某种程度上处于Magento级别...请参阅https://magento.stackexchange.com/questions/277627/possible-bug-in-and-handling-of-getorigdata-when-updating-tier-prices
答案 0 :(得分:1)
假设您已验证以下查询,则对于怀疑的键将返回false:
SELECT
COUNT(0) <> 0 AS KeyViolated
FROM (
SELECT
NULL
FROM my_schema.my_table
GROUP BY unique_, key, columns
HAVING (COUNT(0) > 1) ) A
;
我知道有人可以使用this answer列出FK违规行为。为了验证关键约束,可以使用here中的查询和常规分组并具有计数查询:
DROP PROCEDURE IF EXISTS sp_validate_keys;
DROP PROCEDURE IF EXISTS statement;
DELIMITER $$
CREATE PROCEDURE statement(IN dynamic_statement TEXT)
BEGIN
SET @dynamic_statement := dynamic_statement;
PREPARE prepared_statement FROM @dynamic_statement;
EXECUTE prepared_statement;
DEALLOCATE PREPARE prepared_statement;
END$$
DELIMITER ;
DELIMITER $$
CREATE PROCEDURE sp_validate_keys()
BEGIN
DECLARE var_cur_idx INTEGER UNSIGNED DEFAULT 1;
DECLARE var_length INTEGER UNSIGNED DEFAULT 0;
DECLARE var_schema_name VARCHAR(64);
DECLARE var_table_name VARCHAR(64);
DECLARE var_column_names VARCHAR(255);
DECLARE var_cur_statement TEXT;
CREATE OR REPLACE VIEW v_db_table_key AS
select stat.table_schema as database_name,
stat.table_name,
-- stat.index_name,
group_concat(stat.column_name
order by stat.seq_in_index separator ', ') as columns
-- , tco.constraint_type
from information_schema.statistics stat
join information_schema.table_constraints tco
on stat.table_schema = tco.table_schema
and stat.table_name = tco.table_name
and stat.index_name = tco.constraint_name
where stat.non_unique = 0
and stat.table_schema not in ('information_schema', 'sys',
'performance_schema', 'mysql')
group by stat.table_schema,
stat.table_name,
stat.index_name,
tco.constraint_type
order by stat.table_schema,
stat.table_name
;
SET var_length := (SELECT COUNT(0) FROM v_db_table_key);
DROP TEMPORARY TABLE IF EXISTS tmp_db_table_key_idx;
CREATE TEMPORARY TABLE tmp_db_table_key_idx AS
SELECT
v.*,
(@cnt := @cnt + 1) AS idx
FROM v_db_table_key v
CROSS JOIN (SELECT @cnt := 0) _
;
DROP TEMPORARY TABLE IF EXISTS tmp_key_validation;
CREATE TEMPORARY TABLE tmp_key_validation AS (SELECT * FROM tmp_db_table_key_idx LIMIT 0);
WHILE (var_cur_idx <= var_length) DO
SET var_schema_name := (
SELECT database_name FROM tmp_db_table_key_idx WHERE idx = var_cur_idx LIMIT 1);
SET var_table_name := (
SELECT table_name FROM tmp_db_table_key_idx WHERE idx = var_cur_idx LIMIT 1);
SET var_column_names := (
SELECT columns FROM tmp_db_table_key_idx WHERE idx = var_cur_idx LIMIT 1);
SET var_cur_statement := CONCAT('
INSERT INTO tmp_key_validation
SELECT
\'', var_schema_name, '\' AS SchemaName,
\'', var_table_name, '\' AS TableName,
\'', var_column_names, '\' AS KeyColumns,
COUNT(0) <> 0 AS KeyViolated
FROM (
SELECT
NULL
FROM ', var_schema_name, '.', var_table_name, '
GROUP BY ', var_column_names, '
HAVING (COUNT(0) > 1)
) A
;
')
;
CALL statement(var_cur_statement);
SET var_cur_idx := var_cur_idx + 1;
END WHILE;
-- SELECT var_cur_statement;
DROP VIEW IF EXISTS v_db_table_key;
SELECT * FROM tmp_key_validation WHERE idx = TRUE;
END $$
DELIMITER ;
CALL sp_validate_keys();
DROP PROCEDURE IF EXISTS sp_validate_keys;
DROP PROCEDURE IF EXISTS statement;