@@ -42,9 +42,13 @@ private ViewVersion newViewVersion(int id, String sql) {
42
42
}
43
43
44
44
private ViewVersion newViewVersion (int id , int schemaId , String sql ) {
45
+ return newViewVersion (id , schemaId , System .currentTimeMillis (), sql );
46
+ }
47
+
48
+ private ViewVersion newViewVersion (int id , int schemaId , long timestampMillis , String sql ) {
45
49
return ImmutableViewVersion .builder ()
46
50
.versionId (id )
47
- .timestampMillis (System . currentTimeMillis () )
51
+ .timestampMillis (timestampMillis )
48
52
.defaultCatalog ("prod" )
49
53
.defaultNamespace (Namespace .of ("default" ))
50
54
.putSummary ("user" , "some-user" )
@@ -396,6 +400,70 @@ public void viewVersionHistoryIsCorrectlyRetained() {
396
400
.hasMessage ("Cannot set current version to unknown version: 1" );
397
401
}
398
402
403
+ @ Test
404
+ public void versionHistoryEntryMaintainCorrectTimeline () {
405
+ ViewVersion viewVersionOne = newViewVersion (1 , 0 , 1000 , "select * from ns.tbl" );
406
+ ViewVersion viewVersionTwo = newViewVersion (2 , 0 , 2000 , "select count(*) from ns.tbl" );
407
+ ViewVersion viewVersionThree =
408
+ newViewVersion (3 , 0 , 3000 , "select count(*) as count from ns.tbl" );
409
+
410
+ ViewMetadata viewMetadata =
411
+ ViewMetadata .builder ()
412
+ .setLocation ("location" )
413
+ .addSchema (new Schema (Types .NestedField .required (1 , "x" , Types .LongType .get ())))
414
+ .addVersion (viewVersionOne )
415
+ .addVersion (viewVersionTwo )
416
+ .setCurrentVersionId (1 )
417
+ .build ();
418
+
419
+ // setting an existing view version as the new current should update the timestamp in the
420
+ // history
421
+ ViewMetadata updated = ViewMetadata .buildFrom (viewMetadata ).setCurrentVersionId (2 ).build ();
422
+
423
+ List <ViewHistoryEntry > history = updated .history ();
424
+ assertThat (history )
425
+ .hasSize (2 )
426
+ .element (0 )
427
+ .isEqualTo (ImmutableViewHistoryEntry .builder ().versionId (1 ).timestampMillis (1000 ).build ());
428
+ assertThat (history )
429
+ .element (1 )
430
+ .satisfies (
431
+ v -> {
432
+ assertThat (v .versionId ()).isEqualTo (2 );
433
+ assertThat (v .timestampMillis ())
434
+ .isGreaterThan (3000 )
435
+ .isLessThanOrEqualTo (System .currentTimeMillis ());
436
+ });
437
+
438
+ // adding a new view version and setting it as current should use the view version's timestamp
439
+ // in the history (which has been set to a fixed value for testing)
440
+ updated =
441
+ ViewMetadata .buildFrom (updated ).addVersion (viewVersionThree ).setCurrentVersionId (3 ).build ();
442
+ List <ViewHistoryEntry > historyTwo = updated .history ();
443
+ assertThat (historyTwo )
444
+ .hasSize (3 )
445
+ .containsAll (history )
446
+ .element (2 )
447
+ .isEqualTo (ImmutableViewHistoryEntry .builder ().versionId (3 ).timestampMillis (3000 ).build ());
448
+
449
+ // setting an older view version as the new current (aka doing a rollback) should update the
450
+ // timestamp in the history
451
+ ViewMetadata reactiveOldViewVersion =
452
+ ViewMetadata .buildFrom (updated ).setCurrentVersionId (1 ).build ();
453
+ List <ViewHistoryEntry > historyThree = reactiveOldViewVersion .history ();
454
+ assertThat (historyThree )
455
+ .hasSize (4 )
456
+ .containsAll (historyTwo )
457
+ .element (3 )
458
+ .satisfies (
459
+ v -> {
460
+ assertThat (v .versionId ()).isEqualTo (1 );
461
+ assertThat (v .timestampMillis ())
462
+ .isGreaterThan (3000 )
463
+ .isLessThanOrEqualTo (System .currentTimeMillis ());
464
+ });
465
+ }
466
+
399
467
@ Test
400
468
public void versionsAddedInCurrentBuildAreRetained () {
401
469
ViewVersion v1 = newViewVersion (1 , "select 1 as count" );
0 commit comments