schema_tx_rename_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package storage
  2. import "testing"
  3. // A RENAME that returns to the committed name must not leave a pending move:
  4. // publishing a put and a delete for the same key in one batch is order-dependent
  5. // and can lose the row or the schema record. Assert the property directly on the
  6. // transaction's catalog and row ops.
  7. func TestSchemaTxRenameAwayAndBackHasNoDuplicateOps(t *testing.T) {
  8. _, _, schemas, tables := newTestSession(t)
  9. createTestTable(t, schemas, "a", []Column{
  10. {Name: "id", Type: "INTEGER", PrimaryKey: true},
  11. {Name: "v", Type: "TEXT"},
  12. })
  13. if err := tables.Insert("a", Row{"id": int64(1), "v": "one"}); err != nil {
  14. t.Fatal(err)
  15. }
  16. s := NewSession(schemas, tables)
  17. if err := s.Begin(); err != nil {
  18. t.Fatal(err)
  19. }
  20. if err := s.RenameTable("a", "b"); err != nil {
  21. t.Fatalf("rename a->b: %v", err)
  22. }
  23. if err := s.RenameTable("b", "a"); err != nil {
  24. t.Fatalf("rename b->a: %v", err)
  25. }
  26. s.mu.Lock()
  27. defer s.mu.Unlock()
  28. // Renaming back to the committed name cancels the pending move, so the
  29. // overlay has nothing left to publish for it.
  30. if len(s.schemaTx.renames) != 0 {
  31. t.Fatalf("pending renames after renaming away and back = %v, want none", s.schemaTx.renames)
  32. }
  33. if source := s.schemaTx.dataSources["a"]; source != "a" {
  34. t.Fatalf("data source for a = %q, want a", source)
  35. }
  36. catalogOps, _, err := s.schemaTx.commitCatalogOps()
  37. if err != nil {
  38. t.Fatalf("catalog ops: %v", err)
  39. }
  40. dataOps, _, err := s.schemaTx.commitDataOps(&ddlBatchBudget{})
  41. if err != nil {
  42. t.Fatalf("data ops: %v", err)
  43. }
  44. seen := make(map[string]bool, len(catalogOps)+len(dataOps))
  45. for _, op := range append(catalogOps, dataOps...) {
  46. key := string(op.Key)
  47. if seen[key] {
  48. t.Fatalf("rename away and back emitted more than one operation for key %q", key)
  49. }
  50. seen[key] = true
  51. }
  52. }