diff --git a/go/vt/vtorc/inst/shard_dao.go b/go/vt/vtorc/inst/shard_dao.go index f7fdc0dafc0..89a9652bf71 100644 --- a/go/vt/vtorc/inst/shard_dao.go +++ b/go/vt/vtorc/inst/shard_dao.go @@ -105,14 +105,14 @@ func SaveShard(shard *topo.ShardInfo) error { } // DeleteStaleKeyspaceShards deletes shard records that have not been updated since a provided time. -func DeleteStaleKeyspaceShards(keyspace string, staleTime time.Time) error { +func DeleteStaleKeyspaceShards(keyspaceName string, staleTime time.Time) error { _, err := db.ExecVTOrc(`DELETE FROM vitess_shard WHERE keyspace = ? AND - updated_timestamp < DATETIME(?, 'unixepoch') + updated_timestamp <= DATETIME(?, 'unixepoch') `, - keyspace, + keyspaceName, staleTime.Unix(), ) return err diff --git a/go/vt/vtorc/inst/shard_dao_test.go b/go/vt/vtorc/inst/shard_dao_test.go index bbc781ae40c..cc6734e30e9 100644 --- a/go/vt/vtorc/inst/shard_dao_test.go +++ b/go/vt/vtorc/inst/shard_dao_test.go @@ -138,3 +138,29 @@ func TestGetKeyspaceShardNames(t *testing.T) { require.NoError(t, err) require.Equal(t, []string{"-80", "80-"}, shardNames) } + +func TestDeleteStaleKeyspaceShards(t *testing.T) { + // Clear the database after the test. The easiest way to do that is to run all the initialization commands again. + defer func() { + db.ClearVTOrcDatabase() + }() + + shardInfo := topo.NewShardInfo("ks1", "-80", &topodatapb.Shard{}, nil) + err := SaveShard(shardInfo) + require.NoError(t, err) + shards, err := GetKeyspaceShardNames("ks1") + require.NoError(t, err) + require.Len(t, shards, 1) + + // test a staletime before save causes no delete + require.NoError(t, DeleteStaleKeyspaceShards("ks1", time.Now().Add(-time.Hour))) + shards, err = GetKeyspaceShardNames("ks1") + require.NoError(t, err) + require.Len(t, shards, 1) + + // test statetime of now deletes everything + require.NoError(t, DeleteStaleKeyspaceShards("ks1", time.Now())) + shards, err = GetKeyspaceShardNames("ks1") + require.NoError(t, err) + require.Len(t, shards, 0) +}