sql: Cannot add and then drop a column within the same transaction
You cannot add a column and then drop it within the scope of the same transaction in cockroachdb sql. When I try this I get an error on the drop statement that the column I am trying to drop is in the middle of being added.
Obviously this is ostensibly an odd thing to need to do but the use case is actually pretty common - we have a database migrations project which loops through sql scripts running those which haven't yet been applied to the database. If you run the migrator somewhere where the add statement hasn't been applied yet ( build server/fresh deployment ) then the two statements will come together in a single transaction and the migration will fail.
Really this ought to happen with any schema modifications, not just dropping columns.
This should be easy to fix. It will involve the drop code looking at the cached schema for the transaction and dropping the schema element if it has been added by the transaction.
@vivekmenezes I am thinking to have a look at this issue this evening, unless you are already working on it?
@jrote1 go for it! I'd first add a test to one of the logictests pkg/sql/logictest/testdata/logic_test/alter_table
and fix the problem by reading the table descriptor from the store, finding the mutation id of the column ADD and ensuring there is an outstanding schema change for that mutation id in schemaChangerCollection
Since you're doing this for columns, I'd also do this for indexes.
@vivekmenezes Yeah I want to do it for Tables, Columns and Indexes, both adding then removing and removing then adding. And also some add some tests for adding and renaming, different combinations like that. Will probably begin work tomorrow was getting dev environment configured today.
Adding and removing in the same transaction is much easier than removing and then adding, because when you're removing a schema element you have to deal with the fact that the schema element could be cached somewhere. I'd stick with adding and then removing in the same transaction as a first PR. Thanks so much for your willingness to take this on!
@vivekmenezes Am progressing, got cockroachdb building and tests running. Have created a failing test now to fix :)
Excellent!
On Wed, Nov 1, 2017 at 7:43 PM Jake Rote [email protected] wrote:
@vivekmenezes https://github.com/vivekmenezes Am progressing, got cockroachdb building and tests running. Have created a failing test now to fix :)
— You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub https://github.com/cockroachdb/cockroach/issues/19210#issuecomment-341275877, or mute the thread https://github.com/notifications/unsubscribe-auth/ALOpBMj7tkXmWCU-7dbL9Bo7W7xTHwbIks5syQIFgaJpZM4P2w7h .
same as #24626