提交 c1c791ce authored 作者: noelgrandin's avatar noelgrandin

Issue 485: Database get corrupted when column is renamed for which check…

Issue 485: Database get corrupted when column is renamed for which check constraint was defined inside create table statement.
上级 112c69e5
......@@ -61,6 +61,7 @@ Change Log
</li><li>Issue 486: MySQL compatibiltity, support the "DEFAULT CHARSET" part of the CREATE TABLE statement.
</li><li>Issue 487: support the MySQL "SET foreign_key_checks = 0" command
</li><li>Issue 490: support MySQL "USING BTREE" index declaration
</li><li>Issue 485: Database get corrupted when column is renamed for which check constraint was defined inside create table statement.
</li></ul>
<h2>Version 1.3.172 (2013-05-25)</h2>
......
......@@ -11,6 +11,7 @@ import org.h2.engine.Database;
import org.h2.engine.DbObject;
import org.h2.engine.Right;
import org.h2.engine.Session;
import org.h2.expression.Expression;
import org.h2.table.Column;
import org.h2.table.Table;
......@@ -46,7 +47,11 @@ public class AlterTableRenameColumn extends DefineCommand {
Database db = session.getDatabase();
session.getUser().checkRight(table, Right.ALL);
table.checkSupportAlter();
// we need to update CHECK constraint since it might reference the name of the column
Expression newCheckExpr = column.getCheckConstraint(session, newName);
table.renameColumn(column, newName);
column.removeCheckConstraint();
column.addCheckConstraint(session, newCheckExpr);
table.setModified();
db.update(session, table);
for (DbObject child : table.getChildren()) {
......
......@@ -560,6 +560,11 @@ public class Column {
checkConstraintSQL = getCheckConstraintSQL(session, name);
}
public void removeCheckConstraint() {
checkConstraint = null;
checkConstraintSQL = null;
}
/**
* Get the check constraint expression for this column if set.
*
......
......@@ -39,6 +39,7 @@ public class TestAlter extends TestBase {
stat = conn.createStatement();
testAlterTableAlterColumnAsSelfColumn();
testAlterTableDropColumnWithReferences();
testAlterTableAlterColumnWithConstraint();
testAlterTableAlterColumn();
testAlterTableDropIdentityColumn();
testAlterTableAddColumnIfNotExists();
......@@ -107,6 +108,23 @@ public class TestAlter extends TestBase {
}
/**
* Tests a bug we used to have where altering the name of a column that had a check constraint
* that referenced itself would result in not being able to re-open the DB.
*/
private void testAlterTableAlterColumnWithConstraint() throws SQLException {
stat.execute("create table test(id int check(id in (1,2)) )");
stat.execute("alter table test alter id rename to id2");
// disconnect and reconnect
conn.close();
conn = getConnection("alter");
stat = conn.createStatement();
stat.execute("insert into test values(1)");
assertThrows(ErrorCode.CHECK_CONSTRAINT_VIOLATED_1, stat).
execute("insert into test values(3)");
stat.execute("drop table test");
}
private void testAlterTableDropIdentityColumn() throws SQLException {
stat.execute("create table test(id int auto_increment, name varchar)");
stat.execute("alter table test drop column id");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论