提交 ae0975d4 authored 作者: Thomas Mueller's avatar Thomas Mueller

Issue 125: Renaming primary keys was not persistent.

上级 e46a8da0
......@@ -3490,6 +3490,11 @@ public class Parser {
hash = true;
}
primaryKey = true;
if (!isToken("ON")) {
ifNotExists = readIfNoExists();
indexName = readIdentifierWithSchema(null);
oldSchema = getSchema();
}
} else {
if (readIf("UNIQUE")) {
unique = true;
......
......@@ -63,10 +63,12 @@ public class CreateIndex extends SchemaCommand {
persistent = false;
}
int id = getObjectId(true, false);
if (primaryKey) {
indexName = table.getSchema().getUniqueIndexName(session, table, Constants.PREFIX_PRIMARY_KEY);
} else if (indexName == null) {
indexName = table.getSchema().getUniqueIndexName(session, table, Constants.PREFIX_INDEX);
if (indexName == null) {
if (primaryKey) {
indexName = table.getSchema().getUniqueIndexName(session, table, Constants.PREFIX_PRIMARY_KEY);
} else {
indexName = table.getSchema().getUniqueIndexName(session, table, Constants.PREFIX_INDEX);
}
}
if (getSchema().findIndex(session, indexName) != null) {
if (ifNotExists) {
......
......@@ -323,9 +323,7 @@ public abstract class BaseIndex extends SchemaObjectBase implements Index {
public String getCreateSQLForCopy(Table table, String quotedName) {
StringBuilder buff = new StringBuilder("CREATE ");
buff.append(indexType.getSQL());
if (!indexType.isPrimaryKey()) {
buff.append(' ').append(quotedName);
}
buff.append(' ').append(quotedName);
buff.append(" ON ").append(table.getSQL());
if (comment != null) {
buff.append(" COMMENT ").append(StringUtils.quoteStringSQL(comment));
......
......@@ -36,6 +36,7 @@ public class TestIndex extends TestBase {
public void test() throws SQLException {
deleteDb("index");
testRenamePrimaryKey();
testRandomized();
testDescIndex();
testHashIndex();
......@@ -83,6 +84,28 @@ public class TestIndex extends TestBase {
deleteDb("index");
}
private void testRenamePrimaryKey() throws SQLException {
if (config.memory) {
return;
}
reconnect();
stat.execute("create table test(id int not null)");
stat.execute("alter table test add constraint x primary key(id)");
ResultSet rs;
rs = conn.getMetaData().getIndexInfo(null, null, "TEST", true, false);
rs.next();
String old = rs.getString("INDEX_NAME");
stat.execute("alter index " + old + " rename to y");
rs = conn.getMetaData().getIndexInfo(null, null, "TEST", true, false);
rs.next();
assertEquals("Y", rs.getString("INDEX_NAME"));
reconnect();
rs = conn.getMetaData().getIndexInfo(null, null, "TEST", true, false);
rs.next();
assertEquals("Y", rs.getString("INDEX_NAME"));
stat.execute("drop table test");
}
private void testRandomized() throws SQLException {
boolean reopen = !config.memory;
Random random = new Random(1);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论