# Copyright 2023 PingCAP, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

=> DBGInvoke __enable_schema_sync_service('false')

## clean up
=> DBGInvoke __clean_up_region()
=> DBGInvoke __drop_tidb_table(default, test_dm)
=> DBGInvoke __refresh_schemas()

## create table and apply an empty snapshot
=> DBGInvoke __mock_tidb_table(default, test_dm, 'col_1 Int64')
=> DBGInvoke __refresh_mapped_table_schema(default, test_dm)
=> DBGInvoke query_mapped('select tidb_database,tidb_name,engine from system.tables where database=''\$d'' and name=''\$t''', default, test_dm)
┌─database─┬─name────┬─engine─────┐
│ default  │ test_dm │ DeltaMerge │
└──────────┴─────────┴────────────┘

=> DBGInvoke __region_snapshot(4, 0, 1000, default, test_dm)
## insert some rows
## raft_insert_row_full(db_name, tbl_name, region_id, handle_id, tso, del, val1, val2, ...)
=> DBGInvoke __raft_insert_row_full(default, test_dm, 4,   2, 1, 0, 10)
=> DBGInvoke __raft_insert_row_full(default, test_dm, 4,   2, 3, 0, 11)
=> DBGInvoke __raft_insert_row_full(default, test_dm, 4,   1, 2, 0, 12)
=> DBGInvoke __raft_insert_row_full(default, test_dm, 4,   3, 1, 0, 14)
=> DBGInvoke __raft_insert_row_full(default, test_dm, 4,   3, 4, 0, 15)
=> DBGInvoke __raft_insert_row_full(default, test_dm, 4,   4, 4, 1, 0) -- this row is mark deleted
=> DBGInvoke __try_flush_region(4)

=> DBGInvoke query_mapped('select * from \$d.\$t order by _tidb_rowid', default, test_dm)
┌─col_1─┬─_tidb_rowid─┐
│    12 │           1 │
│    11 │           2 │
│    15 │           3 │
└───────┴─────────────┘

## apply another snapshot, old data should be clear
## region_snapshot_data(database_name, table_name,
##                      region_id, start, end,
##                      handle_id1, tso1, del1, r1_c1, r1_c2, ...,
##                      handle_id2, tso2, del2, r2_c1, r2_c2, ... )
=> DBGInvoke __region_snapshot_data(
    default, test_dm,
    4, 0, 1000,
    2, 3, 0, 11,
    1, 3, 0, 13,
    4, 4, 1, 0
    )
=> DBGInvoke __try_flush_region(4)
## now we should not find rowid==3
=> DBGInvoke query_mapped('select * from \$d.\$t', default, test_dm)
┌─col_1─┬─_tidb_rowid─┐
│    13 │           1 │
│    11 │           2 │
└───────┴─────────────┘

=> DBGInvoke __enable_schema_sync_service('false')
=> DBGInvoke region_snapshot_pre_handle_block(default, test_dm, 5, 1000, 2000, 1002, 30, 0, 110, 1003, 31, 0, 130)
┌─region_snapshot_pre_handle_block(default, test, 5, 1000, 2000, 1002, 30, 0, 110, 1003, 31, 0, 130)──────────────────────────────────────────────────┐
│ pre-handle [region_id=5] snapshot with data [write 2 ], pre-decode block cache { schema_version: ?, data_list size: 2, block row: 2 col: 4 bytes: 50 } │
└─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

=> DBGInvoke __add_column_to_tidb_table(default, test_dm, 'col_2 Nullable(Int64) default 23333')
=> DBGInvoke __refresh_mapped_table_schema(default, test_dm)
=> DBGInvoke __region_snapshot_apply_block(5)
=> DBGInvoke query_mapped('select * from \$d.\$t order by col_1', default, test_dm)
┌─col_1─┬─col_2─┬─_tidb_rowid─┐
│    11 │ 23333 │           2 │
│    13 │ 23333 │           1 │
│   110 │ 23333 │        1002 │
│   130 │ 23333 │        1003 │
└───────┴───────┴─────────────┘

=> DBGInvoke region_snapshot_pre_handle_block(default, test_dm, 5, 1000, 2000, 1002, 34, 0, 110, 888)
┌─region_snapshot_pre_handle_block(default, test_dm, 5, 1000, 2000, 1002, 30, 0, 110, 888)────────────────────────────────────────────────────────────┐
│ pre-handle [region_id=5] snapshot with data [write 1 ], pre-decode block cache { schema_version: ?, data_list size: 1, block row: 1 col: 5 bytes: 34 } │
└─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

## clean up
=> DBGInvoke __drop_tidb_table(default, test_dm)
=> DBGInvoke __refresh_schemas()

