3838from pyiceberg .exceptions import NoSuchTableError
3939from pyiceberg .partitioning import PartitionField , PartitionSpec
4040from pyiceberg .schema import Schema
41- from pyiceberg .table import TableProperties , _dataframe_to_data_files
41+ from pyiceberg .table import TableProperties , _dataframe_to_data_files , SnapshotRef
4242from pyiceberg .transforms import IdentityTransform
4343from pyiceberg .types import IntegerType , NestedField
4444from tests .conftest import TEST_DATA_WITH_NULL
@@ -873,39 +873,51 @@ def table_write_subset_of_schema(session_catalog: Catalog, arrow_table_with_null
873873
874874@pytest .mark .integration
875875@pytest .mark .parametrize ("catalog" , [pytest .lazy_fixture ("session_catalog_hive" ), pytest .lazy_fixture ("session_catalog" )])
876- def test_update_metadata_rollback_to_snapshot (table_v2 : Table ) -> None :
877- assert table_v2 .rollback_to_snapshot (snapshot_id = 3051729675574597004 ) == Snapshot (
878- snapshot_id = 3051729675574597004 ,
879- parent_snapshot_id = None ,
880- sequence_number = 0 ,
881- timestamp_ms = 1515100955770 ,
882- manifest_list = 's3://a/b/1.avro' ,
883- summary = Summary (Operation .APPEND ),
884- schema_id = None ,
885- )
876+ def test_rollback_to_snapshot (catalog : Catalog ) -> None :
877+ identifier = "default.test_table_snapshot_operations"
878+ tbl = catalog .load_table (identifier )
879+ assert len (tbl .history ()) > 3
880+ rollback_snapshot_id = tbl .history ()[- 3 ].snapshot_id
881+ current_snapshot_id = tbl .current_snapshot ().snapshot_id
882+ ms = tbl .manage_snapshots ().rollback_to_snapshot (snapshot_id = rollback_snapshot_id )
883+ ms .commit ()
884+ tbl .refresh ()
885+ assert tbl .current_snapshot ().snapshot_id is not current_snapshot_id
886+ assert tbl .metadata .refs ["main" ] == SnapshotRef (snapshot_id = rollback_snapshot_id )
886887
887888
888889@pytest .mark .integration
889890@pytest .mark .parametrize ("catalog" , [pytest .lazy_fixture ("session_catalog_hive" ), pytest .lazy_fixture ("session_catalog" )])
890- def test_update_metadata_rollback_to_timestamp (table_v2 : Table ) -> None :
891- assert table_v2 .rollback_to_timestamp (timestamp = 1555100955771 ) == Snapshot (
892- snapshot_id = 3055729675574597004 ,
893- parent_snapshot_id = 3051729675574597004 ,
894- sequence_number = 1 ,
895- timestamp_ms = 1555100955770 ,
896- manifest_list = 's3://a/b/2.avro' ,
897- summary = Summary (Operation .APPEND ),
898- schema_id = 1 ,
899- )
900-
901-
902- def test_update_metadata_set_current_snapshot (table_v2 : Table ) -> None :
903- assert table_v2 .set_current_snapshot (snapshot_id = 3051729675574597004 ) == Snapshot (
904- snapshot_id = 3051729675574597004 ,
905- parent_snapshot_id = None ,
906- sequence_number = 0 ,
907- timestamp_ms = 1515100955770 ,
908- manifest_list = 's3://a/b/1.avro' ,
909- summary = Summary (Operation .APPEND ),
910- schema_id = None ,
911- )
891+ def test_rollback_to_timestamp (catalog : Catalog ) -> None :
892+ identifier = "default.test_table_snapshot_operations"
893+ tbl = catalog .load_table (identifier )
894+ assert len (tbl .history ()) > 3
895+ timestamp = tbl .history ()[- 2 ].timestamp_ms
896+ current_snapshot_id = tbl .current_snapshot ().snapshot_id
897+ # not inclusive of rollback_timestamp
898+ ms = tbl .manage_snapshots ().rollback_to_timestamp (timestamp = timestamp )
899+ ms .commit ()
900+ tbl .refresh ()
901+ assert tbl .current_snapshot ().snapshot_id is not current_snapshot_id
902+ assert tbl .metadata .refs ["main" ] == SnapshotRef (snapshot_id = tbl .history ()[- 4 ].snapshot_id )
903+
904+
905+ def test_set_current_snapshot (catalog : Catalog ) -> None :
906+ identifier = "default.test_table_snapshot_operations"
907+ tbl = catalog .load_table (identifier )
908+ assert len (tbl .history ()) > 3
909+ # test with snapshot_id
910+ target_snapshot_id = tbl .history ()[- 4 ].snapshot_id
911+ current_snapshot_id = tbl .current_snapshot ().snapshot_id
912+ tbl .manage_snapshots ().set_current_snapshot (snapshot_id = target_snapshot_id ).commit ()
913+ tbl .refresh ()
914+ assert tbl .current_snapshot ().snapshot_id is not current_snapshot_id
915+ assert tbl .metadata .refs ["main" ] == SnapshotRef (snapshot_id = target_snapshot_id )
916+ # test with ref_name
917+ new_current_snapshot_id = tbl .current_snapshot ().snapshot_id
918+ expected_snapshot_id = tbl .history ()[- 3 ].snapshot_id
919+ tbl .manage_snapshots ().create_tag (snapshot_id = expected_snapshot_id , tag_name = "test-tag" ).commit ()
920+ tbl .manage_snapshots ().set_current_snapshot (ref_name = "test-tag" ).commit ()
921+ tbl .refresh ()
922+ assert tbl .current_snapshot ().snapshot_id is not new_current_snapshot_id
923+ assert tbl .metadata .refs ["main" ] == SnapshotRef (snapshot_id = expected_snapshot_id )
0 commit comments