Return-Path: Delivered-To: apmail-db-derby-commits-archive@www.apache.org Received: (qmail 14324 invoked from network); 19 Jul 2009 01:28:40 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 19 Jul 2009 01:28:40 -0000 Received: (qmail 59871 invoked by uid 500); 19 Jul 2009 01:29:46 -0000 Delivered-To: apmail-db-derby-commits-archive@db.apache.org Received: (qmail 59803 invoked by uid 500); 19 Jul 2009 01:29:46 -0000 Mailing-List: contact derby-commits-help@db.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: Reply-To: "Derby Development" List-Id: Delivered-To: mailing list derby-commits@db.apache.org Received: (qmail 59794 invoked by uid 99); 19 Jul 2009 01:29:46 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 19 Jul 2009 01:29:46 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 19 Jul 2009 01:29:43 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 0BF0E238889B; Sun, 19 Jul 2009 01:29:23 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r795459 - in /db/derby/code/trunk/java: engine/org/apache/derby/impl/sql/compile/AlterTableNode.java testing/org/apache/derbyTesting/functionTests/tests/lang/AlterTableTest.java Date: Sun, 19 Jul 2009 01:29:22 -0000 To: derby-commits@db.apache.org From: bpendleton@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090719012923.0BF0E238889B@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: bpendleton Date: Sun Jul 19 01:29:22 2009 New Revision: 795459 URL: http://svn.apache.org/viewvc?rev=795459&view=rev Log: DERBY-4244: ALTER TABLE ASSERT in ADD COLUMN with autocommit off This patch was contributed by Eranda Sooriyabandara (070468D at gmail dot com) An ALTER TABLE ... ADD COLUMN statement which specifies to add a column to a table may also include any column constraints which apply to that column. For example, to add a column to a table and at the same time indicate that the column should be the primary key of the table, this statement is used: alter table t0 add column c2 int not null default 0 primary key; If the table already contains a primary key, this statement fails, as the table cannot have more than one primary key. However, the check for the duplicate primary key was occurring at execution time, *after* the column had already been added to the table. Then, during that same transaction, subsequent ALTER TABLE statements affecting that table failed with internal ASSERT messages because the data dictionary data structures were inconsistent. This patch adds logic to the compilation processing of the ALTER TABLE statement to check for a duplicate PRIMARY KEY constraint at compile time, before the ALTER TABLE statement has begun execution. This way, when such a duplicate constraint is rejected, the rejection occurs prior to execution of the table, allowing for more straightforward cleanup. The patch also re-enables a portion of the AlterTableTest, which had been disabled due to this problem. Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/AlterTableNode.java db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/AlterTableTest.java Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/AlterTableNode.java URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/AlterTableNode.java?rev=795459&r1=795458&r2=795459&view=diff ============================================================================== --- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/AlterTableNode.java (original) +++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/AlterTableNode.java Sun Jul 19 01:29:22 2009 @@ -32,6 +32,7 @@ import org.apache.derby.iapi.sql.compile.C_NodeTypes; import org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor; +import org.apache.derby.iapi.sql.dictionary.ConstraintDescriptorList; import org.apache.derby.iapi.sql.dictionary.DataDictionary; import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor; import org.apache.derby.iapi.sql.dictionary.TableDescriptor; @@ -41,6 +42,7 @@ import org.apache.derby.impl.sql.execute.ColumnInfo; import org.apache.derby.impl.sql.execute.ConstraintConstantAction; +import org.apache.derby.impl.sql.execute.CreateConstraintConstantAction; /** * A AlterTableNode represents a DDL statement that alters a table. @@ -511,6 +513,31 @@ tableElementList.genConstraintActions(false, conActions, getRelativeName(), schemaDescriptor, getDataDictionary()); + + for (int conIndex = 0; conIndex < conActions.length; conIndex++) + { + ConstraintConstantAction cca = conActions[conIndex]; + + if (cca instanceof CreateConstraintConstantAction) + { + int constraintType = cca.getConstraintType(); + if (constraintType == DataDictionary.PRIMARYKEY_CONSTRAINT) + { + DataDictionary dd = getDataDictionary(); + // Check to see if a constraint of the same type + // already exists + ConstraintDescriptorList cdl = + dd.getConstraintDescriptors(baseTable); + + if (cdl.getPrimaryKey() != null) + { + throw StandardException.newException( + SQLState.LANG_ADD_PRIMARY_KEY_FAILED1, + baseTable.getQualifiedName()); + } + } + } + } } } Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/AlterTableTest.java URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/AlterTableTest.java?rev=795459&r1=795458&r2=795459&view=diff ============================================================================== --- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/AlterTableTest.java (original) +++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/AlterTableTest.java Sun Jul 19 01:29:22 2009 @@ -113,6 +113,7 @@ public void testAddColumn() throws Exception { Statement st = createStatement(); createTestObjects(st); + conn.commit(); // add column negative tests alter a non-existing table assertStatementError("42Y55", st, @@ -202,18 +203,6 @@ JDBC.assertFullResultSet(rs, new String[][]{{"1"}, {"2"}}); } - // DERBY-4244 (START) - // Without these lines, this test fails a few lines later when - // it makes yet another attempt to add column c2 to table t0. This - // bug is logged as DERBY-4244. When that problem is fixed, this - // section could be removed. - conn.commit(); - st.executeUpdate("drop table t0"); - st.executeUpdate( - "create table t0(c1 int not null constraint p1 primary key)"); - conn.commit(); - // DERBY-4244 (END) - // add non-nullable column to 0 row table and verify st.executeUpdate("alter table t0 add column c2 int not null default 0"); st.executeUpdate("insert into t0 values (1, default)");