Skip to content

Commit

Permalink
sql: move validation of primary key existence to TableCollection
Browse files Browse the repository at this point in the history
To support `ADD/DROP PRIMARY KEY` operations within a single
transaction, we had added a step in the `connExecutor` to verify that
all tables modified in the transaction have a primary key before we
commit the transaction. This was done by examining `SchemaChanger`s
queued in the transaction in `extraTxnState`. To ease the process of
removing `SchemaChanger`s from `extraTxnState` as part of moving them to
jobs, this PR moves the validation implementation to a method on
`TableCollection` that examines all modified tables.

Release note: None
  • Loading branch information
lucy-zhang committed Mar 5, 2020
1 parent c9b189b commit 1007293
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
8 changes: 3 additions & 5 deletions pkg/sql/conn_executor_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,11 +614,9 @@ func (ex *connExecutor) commitSQLTransactionInternal(
) (ev fsm.Event, payload fsm.EventPayload, ok bool) {
ex.clearSavepoints()

for _, sc := range ex.extraTxnState.schemaChangers.schemaChangers {
if err := sc.validateTablePrimaryKeys(ctx, ex.state.mu.txn); err != nil {
ev, payload = ex.makeErrEvent(err, stmt)
return ev, payload, false
}
if err := ex.extraTxnState.tables.validatePrimaryKeys(); err != nil {
ev, payload = ex.makeErrEvent(err, stmt)
return ev, payload, false
}

if err := ex.checkTableTwoVersionInvariant(ctx); err != nil {
Expand Down
15 changes: 0 additions & 15 deletions pkg/sql/schema_changer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1953,21 +1953,6 @@ func (sc *SchemaChanger) reverseMutation(
return mutation, columns
}

// validateTablePrimaryKeys ensures that the table this schema changer
// references has a primary key. This is to avoid usage of the DROP PRIMARY KEY
// command without an ADD PRIMARY KEY command before the end of the transaction.
func (sc *SchemaChanger) validateTablePrimaryKeys(ctx context.Context, txn *client.Txn) error {
table, err := sqlbase.GetMutableTableDescFromID(ctx, txn, sc.tableID)
if err != nil {
return err
}
if !table.HasPrimaryKey() {
return errors.Errorf(
"primary key of table %s dropped without subsequent addition of new primary key", table.Name)
}
return nil
}

// TestingSchemaChangerCollection is an exported (for testing) version of
// schemaChangerCollection.
// TODO(andrei): get rid of this type once we can have tests internal to the sql
Expand Down
17 changes: 17 additions & 0 deletions pkg/sql/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,23 @@ type tableCollectionModifier interface {
copyModifiedSchema(to *TableCollection)
}

// validatePrimaryKeys verifies that all tables modified in the transaction have
// an enabled primary key after potentially undergoing DROP PRIMARY KEY, which
// is required to be followed by ADD PRIMARY KEY.
func (tc *TableCollection) validatePrimaryKeys() error {
modifiedTables := tc.getTablesWithNewVersion()
for i := range modifiedTables {
table := tc.getUncommittedTableByID(modifiedTables[i].id).MutableTableDescriptor
if !table.HasPrimaryKey() {
return errors.Errorf(
"primary key of table %s dropped without subsequent addition of new primary key",
table.Name,
)
}
}
return nil
}

// createOrUpdateSchemaChangeJob finalizes the current mutations in the table
// descriptor. If a schema change job in the system.jobs table has not been
// created for mutations in the current transaction, one is created. The
Expand Down

0 comments on commit 1007293

Please sign in to comment.