diff --git a/acceleration/internal/api/datamove_test.go b/acceleration/internal/api/datamove_test.go index 7fe376d302..5898ab87a7 100644 --- a/acceleration/internal/api/datamove_test.go +++ b/acceleration/internal/api/datamove_test.go @@ -242,6 +242,28 @@ func (s *DataMoveSuite) TestAnExportIsNotWrittenOverAnAppThatHoldsDataWhenFullIs s.Equal("edited here", s.instructionsOf(agent.Id)) } +// Defect: an app whose only rows here are opt-outs had an import refused without full. +func (s *DataMoveSuite) TestAnAppHoldingOnlyAnOptOutImportsWithoutFull() { + ctx := context.Background() + s.Require().NoError(s.store.OptOuts.Insert(ctx, &store.OptOut{CustomerID: s.customerID(), Recipient: "+15550001112", Channel: "sms", Source: "keyword"})) + exported := s.export() + for i, line := range exported { + if line.Table == "opt_outs" { + exported[i].Row = s.withID(line.Row, store.NewID()) + } + } + s.useApp(s.data.createApp()) // has made no request, so no activity row + s.Require().NoError(s.store.OptOuts.Insert(ctx, &store.OptOut{CustomerID: s.customerID(), Recipient: "+15550001113", Channel: "sms", Source: "keyword"})) + + s.Require().Equal(http.StatusOK, s.serverClient.do(http.MethodPost, "/v1/data/import", ndjson(exported), nil)) + + for _, recipient := range []string{"+15550001112", "+15550001113"} { + optedOut, err := s.store.OptOuts.OptedOut(ctx, s.customerID(), recipient, "sms") + s.Require().NoError(err) + s.True(optedOut, recipient) + } +} + // A lookup that fails is not an answer that the app holds nothing: the export is not written. func (s *DataMoveSuite) TestAnExportIsNotWrittenWhenWhatTheAppHoldsCannotBeRead() { agent := s.data.createAgentConfig() diff --git a/acceleration/internal/store/datamove.go b/acceleration/internal/store/datamove.go index e9b1a2e881..5a2cf75a9d 100644 --- a/acceleration/internal/store/datamove.go +++ b/acceleration/internal/store/datamove.go @@ -46,6 +46,9 @@ type dataTable struct { // filter is an extra predicate on an export, for a table that holds more than one // kind of row. filter string + // order is the order an export reads the table's rows in, for a table whose rows must + // arrive in one. + order string // shard is a table kept in the shards rather than the shared database. shard bool // across is the column a row is known by in every deployment, for a table whose primary @@ -132,6 +135,10 @@ var dataTables = []dataTable{ {name: "users", customer: "customer_id", vouched: []string{"minted"}}, {name: "scheduled_tasks", customer: "customer_id"}, {name: "scheduled_task_runs", customer: "customer_id"}, + // Who asked not to be reached: STOP and START at the source carry on here through the changes. + // Revoked rows go first, so one revoked at the source lifts its copy here before the live + // row that replaced it arrives, rather than after the import skipped that row beside it. + {name: "opt_outs", customer: "customer_id", order: "revoked_at IS NULL"}, {name: "recordings", customer: "customer_id", shard: true}, {name: "agent_sessions", customer: "customer_id", shard: true}, {name: "agent_session_tools", parent: "agent_sessions", parentColumn: "session_id", shard: true}, @@ -659,6 +666,9 @@ func (t dataTable) selectRows(customerID string, secrets []string) (string, []an if t.filter != "" { where += " AND " + t.filter } + if t.order != "" { + where += " ORDER BY " + t.order + } return fmt.Sprintf("SELECT %s FROM %s AS t WHERE %s", subject.String(), t.name, where), []any{customerID} } @@ -805,6 +815,15 @@ func (s *Store) importRow(ctx context.Context, db bun.IDB, customerID, table str if spec.copied != "" { where = " WHERE NOT " + spec.copiedBefore(shape, columns) } + if table == "opt_outs" { + // An opt-out live here under another id is this deployment's, and stays: the row arriving + // would break the one-live-row index, and dropping the live one would let the person be + // reached again. A revoked row is history beside it, and revokes only the row of its own id, + // so a START replayed from the source lifts only what the source sent. Two writes of one + // live opt-out here at the same moment can still fail the statement on that index. + where = " WHERE NOT (r.revoked_at IS NULL AND EXISTS (SELECT 1 FROM opt_outs AS h WHERE h.customer_id = r.customer_id" + + " AND h.recipient = r.recipient AND h.channel = r.channel AND h.revoked_at IS NULL AND h.id <> r.id))" + } args := []any{string(row), customerID} if table == "requests" { // The organization a request counts against is the app's here, whatever the row says. @@ -851,6 +870,11 @@ func (s *Store) importRow(ctx context.Context, db bun.IDB, customerID, table str if table == "phone_numbers" { conflict += " AND NOT " + boughtHere("phone_numbers") } + // An opt-out here stays on the recipient and channel it was recorded for: a row arriving + // under its id for another one neither moves nor lifts it. + if table == "opt_outs" { + conflict += " AND opt_outs.recipient = EXCLUDED.recipient AND opt_outs.channel = EXCLUDED.channel" + } // Every writer of a log draws its id under its customer's lock (AgentLogs.Insert), so ids // commit in order and a reader resuming after one misses none. The row is read through the @@ -895,6 +919,7 @@ var importedModels = map[string]func() validated{ "simulation_cases": func() validated { return &SimulationCase{} }, "scheduled_tasks": func() validated { return &ScheduledTask{} }, "scheduled_task_runs": func() validated { return &ScheduledTaskRun{} }, + "opt_outs": func() validated { return &OptOut{} }, "recordings": func() validated { return &Recording{} }, "agent_sessions": func() validated { return &AgentSession{} }, "agent_session_tools": func() validated { return &agentSessionTools{} }, @@ -1951,10 +1976,12 @@ func dataTableByName(name string) (dataTable, bool) { // shared database, empty when none does. A table kept in the shards is left out: it holds the // customer's own traffic here, and a shard's pod reads only its own shard, so counting it would // make the answer depend on which pod runs. A table that belongs to a customer through a parent -// is left out too: its rows are written only under a parent of the customer's. +// is left out too: its rows are written only under a parent of the customer's. So are opt-outs: +// an import keeps one recorded here live (importRow), so an app holding only those holds nothing +// an export would write over. func (s *Store) HoldingTable(ctx context.Context, customerID string) (string, error) { for _, table := range dataTables { - if table.customer == "" || table.shard { + if table.customer == "" || table.shard || table.name == "opt_outs" { continue } where := table.customer + " = ?" diff --git a/acceleration/internal/store/datamove_test.go b/acceleration/internal/store/datamove_test.go index 26b1e6b99f..5c8b2e51ab 100644 --- a/acceleration/internal/store/datamove_test.go +++ b/acceleration/internal/store/datamove_test.go @@ -425,6 +425,166 @@ func (s *DataMoveSuite) TestADeleteIsReplayedToo() { s.Empty(s.exportFrom(s.destination, "acme").of("agent_configs")) } +// Defect: opt_outs left out of what a move carries, so whoever texted STOP is reached again. +func (s *DataMoveSuite) TestAMovedRecipientIsStillOptedOut() { + s.Require().NoError(s.source.OptOuts.Insert(s.ctx, &OptOut{CustomerID: "acme", Recipient: "+12025550101", Channel: "sms", Source: "keyword"})) + + s.move("acme", "moved") + + optedOut, err := s.destination.OptOuts.OptedOut(s.ctx, "moved", "+12025550101", "sms") + s.Require().NoError(err) + s.True(optedOut) +} + +// Defect: a STOP or START at the source after the copy recorded no change, or replayed as +// something else. +func (s *DataMoveSuite) TestAStopAndAStartAfterTheCopyArriveThroughTheChanges() { + s.capture("acme") + cursor := s.move("acme", "moved") + + s.Require().NoError(s.source.OptOuts.Insert(s.ctx, &OptOut{CustomerID: "acme", Recipient: "+12025550102", Channel: "sms", Source: "keyword"})) + changes, cursor, err := s.source.Changes(s.ctx, "acme", cursor, 100) + s.Require().NoError(err) + s.Require().NoError(s.destination.ApplyChanges(s.ctx, "moved", changes)) + optedOut, err := s.destination.OptOuts.OptedOut(s.ctx, "moved", "+12025550102", "sms") + s.Require().NoError(err) + s.True(optedOut, "the STOP") + + s.Require().NoError(s.source.OptOuts.RevokeAll(s.ctx, "acme", "+12025550102", "sms")) + changes, _, err = s.source.Changes(s.ctx, "acme", cursor, 100) + s.Require().NoError(err) + s.Require().NoError(s.destination.ApplyChanges(s.ctx, "moved", changes)) + optedOut, err = s.destination.OptOuts.OptedOut(s.ctx, "moved", "+12025550102", "sms") + s.Require().NoError(err) + s.False(optedOut, "the START") +} + +// Defect: the source's opt-out for a recipient already opted out here breaks the one-live-row +// index and fails the import, or replaces the one recorded here. +func (s *DataMoveSuite) TestAnImportOverALiveOptOutLeavesIt() { + here := OptOut{CustomerID: "moved", Recipient: "+12025550103", Channel: "sms", Source: "keyword"} + s.Require().NoError(s.destination.OptOuts.Insert(s.ctx, &here)) + s.Require().NoError(s.source.OptOuts.Insert(s.ctx, &OptOut{CustomerID: "acme", Recipient: "+12025550103", Channel: "sms", Source: "api"})) + + s.move("acme", "moved") + + live, err := s.destination.OptOuts.Select(s.ctx, "moved", 10, nil) + s.Require().NoError(err) + s.Require().Len(live, 1) + s.Equal(here.ID, live[0].ID) + s.Equal("keyword", live[0].Source) +} + +// Defect: a second copy read the source's new live opt-out before the revoked one it replaced, +// so the import skipped it beside the old copy here, and the revoked row then lifted that copy. +func (s *DataMoveSuite) TestASecondCopyAfterAStartAndAStopKeepsTheRecipientOptedOut() { + first := OptOut{CustomerID: "acme", Recipient: "+12025550105", Channel: "sms", Source: "keyword"} + s.Require().NoError(s.source.OptOuts.Insert(s.ctx, &first)) + s.move("acme", "moved") + + s.Require().NoError(s.source.OptOuts.RevokeAll(s.ctx, "acme", "+12025550105", "sms")) + s.Require().NoError(s.source.OptOuts.Insert(s.ctx, &OptOut{CustomerID: "acme", Recipient: "+12025550105", Channel: "sms", Source: "keyword"})) + // Store the rows newest first: an export may meet them in any order the table holds them. + _, err := s.source.GlobalDB().ExecContext(s.ctx, "CLUSTER opt_outs USING opt_outs_customer_idx") + s.Require().NoError(err) + s.move("acme", "moved") + + optedOut, err := s.destination.OptOuts.OptedOut(s.ctx, "moved", "+12025550105", "sms") + s.Require().NoError(err) + s.True(optedOut) +} + +// Defect: a START at the source, replayed here, lifts the opt-out recorded here for the same +// recipient. +func (s *DataMoveSuite) TestAnImportedRevokedOptOutDoesNotLiftOneRecordedHere() { + here := OptOut{CustomerID: "moved", Recipient: "+12025550104", Channel: "sms", Source: "keyword"} + s.Require().NoError(s.destination.OptOuts.Insert(s.ctx, &here)) + s.Require().NoError(s.source.OptOuts.Insert(s.ctx, &OptOut{CustomerID: "acme", Recipient: "+12025550104", Channel: "sms", Source: "keyword"})) + s.capture("acme") + cursor := s.move("acme", "moved") + + s.Require().NoError(s.source.OptOuts.RevokeAll(s.ctx, "acme", "+12025550104", "sms")) + changes, _, err := s.source.Changes(s.ctx, "acme", cursor, 100) + s.Require().NoError(err) + s.Require().NotEmpty(changes) + s.Require().NoError(s.destination.ApplyChanges(s.ctx, "moved", changes)) + + live, err := s.destination.OptOuts.Select(s.ctx, "moved", 10, nil) + s.Require().NoError(err) + s.Require().Len(live, 1) + s.Equal(here.ID, live[0].ID) + s.Len(s.exportFrom(s.destination, "moved").of("opt_outs"), 2, "the revoked copy is kept beside it as history") +} + +// Defect: a live opt-out here on another channel kept the source's one out. +func (s *DataMoveSuite) TestAnOptOutHereOnAnotherChannelDoesNotKeepOneOut() { + s.Require().NoError(s.destination.OptOuts.Insert(s.ctx, &OptOut{CustomerID: "moved", Recipient: "+12025550107", Channel: "voice", Source: "keyword"})) + s.Require().NoError(s.source.OptOuts.Insert(s.ctx, &OptOut{CustomerID: "acme", Recipient: "+12025550107", Channel: "sms", Source: "keyword"})) + + s.move("acme", "moved") + + optedOut, err := s.destination.OptOuts.OptedOut(s.ctx, "moved", "+12025550107", "sms") + s.Require().NoError(err) + s.True(optedOut) +} + +// Defect: a live opt-out here for another recipient kept the source's one out. +func (s *DataMoveSuite) TestAnOptOutHereForAnotherRecipientDoesNotKeepOneOut() { + s.Require().NoError(s.destination.OptOuts.Insert(s.ctx, &OptOut{CustomerID: "moved", Recipient: "+12025550108", Channel: "sms", Source: "keyword"})) + s.Require().NoError(s.source.OptOuts.Insert(s.ctx, &OptOut{CustomerID: "acme", Recipient: "+12025550109", Channel: "sms", Source: "keyword"})) + + s.move("acme", "moved") + + optedOut, err := s.destination.OptOuts.OptedOut(s.ctx, "moved", "+12025550109", "sms") + s.Require().NoError(err) + s.True(optedOut) +} + +// Defect: another app's live opt-out here for the same recipient kept the source's one out. +func (s *DataMoveSuite) TestAnotherAppsOptOutHereDoesNotKeepOneOut() { + s.Require().NoError(s.destination.OptOuts.Insert(s.ctx, &OptOut{CustomerID: "other", Recipient: "+12025550110", Channel: "sms", Source: "keyword"})) + s.Require().NoError(s.source.OptOuts.Insert(s.ctx, &OptOut{CustomerID: "acme", Recipient: "+12025550110", Channel: "sms", Source: "keyword"})) + + s.move("acme", "moved") + + optedOut, err := s.destination.OptOuts.OptedOut(s.ctx, "moved", "+12025550110", "sms") + s.Require().NoError(err) + s.True(optedOut) +} + +// Defect: an imported row under the id of an opt-out recorded here, for another recipient or +// channel, moved that opt-out off what it was recorded for, or lifted it. +func (s *DataMoveSuite) TestAnImportedRowUnderTheIdOfAnOptOutHereLeavesItsRecipientOptedOut() { + here := OptOut{CustomerID: "moved", Recipient: "+12025550111", Channel: "sms", Source: "keyword"} + s.Require().NoError(s.destination.OptOuts.Insert(s.ctx, &here)) + s.Require().NoError(s.source.OptOuts.Insert(s.ctx, &OptOut{CustomerID: "acme", Recipient: "+12025550112", Channel: "sms", Source: "keyword"})) + + s.Require().NoError(s.importEdited("opt_outs", "moved", func(row map[string]any) { row["id"] = here.ID })) + optedOut, err := s.destination.OptOuts.OptedOut(s.ctx, "moved", "+12025550111", "sms") + s.Require().NoError(err) + s.True(optedOut, "a live row for another recipient") + + s.Require().NoError(s.importEdited("opt_outs", "moved", func(row map[string]any) { + row["id"] = here.ID + row["recipient"] = here.Recipient + row["channel"] = "voice" + row["revoked_at"] = time.Now().UTC().Format(time.RFC3339Nano) + })) + optedOut, err = s.destination.OptOuts.OptedOut(s.ctx, "moved", "+12025550111", "sms") + s.Require().NoError(err) + s.True(optedOut, "a revoked row for another channel") +} + +// Defect: an imported opt-out that names no recipient was stored, one the API refuses. +func (s *DataMoveSuite) TestAnImportedOptOutWithoutARecipientIsRefused() { + s.Require().NoError(s.source.OptOuts.Insert(s.ctx, &OptOut{CustomerID: "acme", Recipient: "+12025550106", Channel: "sms", Source: "keyword"})) + + err := s.importEdited("opt_outs", "moved", func(row map[string]any) { row["recipient"] = "" }) + + s.ErrorContains(err, "recipient") + s.Empty(s.exportFrom(s.destination, "moved").of("opt_outs")) +} + // The columns a move copies are read from the catalog, so the bindings come with the // config without being named anywhere. func (s *DataMoveSuite) TestAConfigsBindingsMoveWithIt() {