Add lastUpdateTime column to epp resources (#683)

* Add lastUpdateTime column to epp resources

Property was inadvertently left out.

Renamed getter and setter to match the property name.

Added a test helper to compare EppResources while ignoring
lastUpdateTime, which changes every time an instance is persisted.
This commit is contained in:
Weimin Yu 2020-07-14 14:53:05 -04:00 committed by GitHub
parent 9649f49bce
commit 65c9cd3f4d
15 changed files with 218 additions and 54 deletions

View file

@ -50,12 +50,12 @@ public class ResaveAllEppResourcesActionTest
@Test
public void test_mapreduceSuccessfullyResavesEntity() throws Exception {
ContactResource contact = persistActiveContact("test123");
DateTime creationTime = contact.getUpdateAutoTimestamp().getTimestamp();
assertThat(ofy().load().entity(contact).now().getUpdateAutoTimestamp().getTimestamp())
DateTime creationTime = contact.getUpdateTimestamp().getTimestamp();
assertThat(ofy().load().entity(contact).now().getUpdateTimestamp().getTimestamp())
.isEqualTo(creationTime);
ofy().clearSessionCache();
runMapreduce();
assertThat(ofy().load().entity(contact).now().getUpdateAutoTimestamp().getTimestamp())
assertThat(ofy().load().entity(contact).now().getUpdateTimestamp().getTimestamp())
.isGreaterThan(creationTime);
}

View file

@ -0,0 +1,35 @@
// Copyright 2020 The Nomulus Authors. All Rights Reserved.
//
// 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.
package google.registry.model;
import static com.google.common.truth.Truth.assertThat;
import java.util.Objects;
/** Test helpers for {@link EppResource}. */
public final class EppResourceTestUtils {
private EppResourceTestUtils() {}
public static <E extends EppResource> void assertEqualsIgnoreLastUpdateTime(
E actual, E expected) {
if (Objects.equals(actual, expected)) {
return;
}
actual = (E) actual.asBuilder().build();
actual.updateTimestamp = expected.getUpdateTimestamp();
assertThat(actual).isEqualTo(expected);
}
}

View file

@ -165,8 +165,10 @@ public class EppResourceUtilsTest {
assertThat(host.getRevisions()).hasSize(2);
// Even though there is no revision, make a best effort guess to use the oldest revision.
assertThat(
loadAtPointInTime(host, clock.nowUtc().minus(Duration.standardDays(32)))
.now().getUpdateAutoTimestamp().getTimestamp())
.isEqualTo(host.getRevisions().firstKey());
loadAtPointInTime(host, clock.nowUtc().minus(Duration.standardDays(32)))
.now()
.getUpdateTimestamp()
.getTimestamp())
.isEqualTo(host.getRevisions().firstKey());
}
}

View file

@ -16,6 +16,7 @@ package google.registry.model.contact;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth8.assertThat;
import static google.registry.model.EppResourceTestUtils.assertEqualsIgnoreLastUpdateTime;
import static google.registry.model.EppResourceUtils.loadByForeignKey;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.ContactResourceSubject.assertAboutContacts;
@ -154,7 +155,7 @@ public class ContactResourceTest extends EntityTestCase {
.setServerApproveEntities(null)
.build())
.build();
assertThat(persisted).isEqualTo(fixed);
assertEqualsIgnoreLastUpdateTime(persisted, fixed);
}
@Test

View file

@ -14,7 +14,7 @@
package google.registry.model.domain;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.model.EppResourceTestUtils.assertEqualsIgnoreLastUpdateTime;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.SqlHelper.assertThrowForeignKeyViolation;
import static google.registry.testing.SqlHelper.saveRegistrar;
@ -154,7 +154,7 @@ public class DomainBaseSqlTest {
DomainBase org = domain.asBuilder().setCreationTime(result.getCreationTime()).build();
// Note that the equality comparison forces a lazy load of all fields.
assertThat(result).isEqualTo(org);
assertEqualsIgnoreLastUpdateTime(result, org);
});
}

View file

@ -247,8 +247,14 @@ public class OfyCommitLogTest {
void testSavingRootAndChild_updatesTimestampOnBackupGroupRoot() {
tm().transact(() -> ofy().save().entity(Root.create(1, getCrossTldKey())));
ofy().clearSessionCache();
assertThat(ofy().load().key(Key.create(getCrossTldKey(), Root.class, 1)).now()
.getUpdateAutoTimestamp().getTimestamp()).isEqualTo(clock.nowUtc());
assertThat(
ofy()
.load()
.key(Key.create(getCrossTldKey(), Root.class, 1))
.now()
.getUpdateTimestamp()
.getTimestamp())
.isEqualTo(clock.nowUtc());
clock.advanceOneMilli();
tm()
.transact(
@ -257,43 +263,79 @@ public class OfyCommitLogTest {
ofy().save().entity(new Child());
});
ofy().clearSessionCache();
assertThat(ofy().load().key(Key.create(getCrossTldKey(), Root.class, 1)).now()
.getUpdateAutoTimestamp().getTimestamp()).isEqualTo(clock.nowUtc());
assertThat(
ofy()
.load()
.key(Key.create(getCrossTldKey(), Root.class, 1))
.now()
.getUpdateTimestamp()
.getTimestamp())
.isEqualTo(clock.nowUtc());
}
@Test
void testSavingOnlyChild_updatesTimestampOnBackupGroupRoot() {
tm().transact(() -> ofy().save().entity(Root.create(1, getCrossTldKey())));
ofy().clearSessionCache();
assertThat(ofy().load().key(Key.create(getCrossTldKey(), Root.class, 1)).now()
.getUpdateAutoTimestamp().getTimestamp()).isEqualTo(clock.nowUtc());
assertThat(
ofy()
.load()
.key(Key.create(getCrossTldKey(), Root.class, 1))
.now()
.getUpdateTimestamp()
.getTimestamp())
.isEqualTo(clock.nowUtc());
clock.advanceOneMilli();
tm().transact(() -> ofy().save().entity(new Child()));
ofy().clearSessionCache();
assertThat(ofy().load().key(Key.create(getCrossTldKey(), Root.class, 1)).now()
.getUpdateAutoTimestamp().getTimestamp()).isEqualTo(clock.nowUtc());
assertThat(
ofy()
.load()
.key(Key.create(getCrossTldKey(), Root.class, 1))
.now()
.getUpdateTimestamp()
.getTimestamp())
.isEqualTo(clock.nowUtc());
}
@Test
void testDeletingChild_updatesTimestampOnBackupGroupRoot() {
tm().transact(() -> ofy().save().entity(Root.create(1, getCrossTldKey())));
ofy().clearSessionCache();
assertThat(ofy().load().key(Key.create(getCrossTldKey(), Root.class, 1)).now()
.getUpdateAutoTimestamp().getTimestamp()).isEqualTo(clock.nowUtc());
assertThat(
ofy()
.load()
.key(Key.create(getCrossTldKey(), Root.class, 1))
.now()
.getUpdateTimestamp()
.getTimestamp())
.isEqualTo(clock.nowUtc());
clock.advanceOneMilli();
// The fact that the child was never persisted is irrelevant.
tm().transact(() -> ofy().delete().entity(new Child()));
ofy().clearSessionCache();
assertThat(ofy().load().key(Key.create(getCrossTldKey(), Root.class, 1)).now()
.getUpdateAutoTimestamp().getTimestamp()).isEqualTo(clock.nowUtc());
assertThat(
ofy()
.load()
.key(Key.create(getCrossTldKey(), Root.class, 1))
.now()
.getUpdateTimestamp()
.getTimestamp())
.isEqualTo(clock.nowUtc());
}
@Test
void testReadingRoot_doesntUpdateTimestamp() {
tm().transact(() -> ofy().save().entity(Root.create(1, getCrossTldKey())));
ofy().clearSessionCache();
assertThat(ofy().load().key(Key.create(getCrossTldKey(), Root.class, 1)).now()
.getUpdateAutoTimestamp().getTimestamp()).isEqualTo(clock.nowUtc());
assertThat(
ofy()
.load()
.key(Key.create(getCrossTldKey(), Root.class, 1))
.now()
.getUpdateTimestamp()
.getTimestamp())
.isEqualTo(clock.nowUtc());
clock.advanceOneMilli();
tm()
.transact(
@ -304,16 +346,28 @@ public class OfyCommitLogTest {
ofy().load().entity(Root.create(1, getCrossTldKey()));
});
ofy().clearSessionCache();
assertThat(ofy().load().key(Key.create(getCrossTldKey(), Root.class, 1)).now()
.getUpdateAutoTimestamp().getTimestamp()).isEqualTo(clock.nowUtc().minusMillis(1));
assertThat(
ofy()
.load()
.key(Key.create(getCrossTldKey(), Root.class, 1))
.now()
.getUpdateTimestamp()
.getTimestamp())
.isEqualTo(clock.nowUtc().minusMillis(1));
}
@Test
void testReadingChild_doesntUpdateTimestampOnBackupGroupRoot() {
tm().transact(() -> ofy().save().entity(Root.create(1, getCrossTldKey())));
ofy().clearSessionCache();
assertThat(ofy().load().key(Key.create(getCrossTldKey(), Root.class, 1)).now()
.getUpdateAutoTimestamp().getTimestamp()).isEqualTo(clock.nowUtc());
assertThat(
ofy()
.load()
.key(Key.create(getCrossTldKey(), Root.class, 1))
.now()
.getUpdateTimestamp()
.getTimestamp())
.isEqualTo(clock.nowUtc());
clock.advanceOneMilli();
tm()
.transact(
@ -324,8 +378,14 @@ public class OfyCommitLogTest {
ofy().load().entity(new Child()); // All Child objects are under Root(1).
});
ofy().clearSessionCache();
assertThat(ofy().load().key(Key.create(getCrossTldKey(), Root.class, 1)).now()
.getUpdateAutoTimestamp().getTimestamp()).isEqualTo(clock.nowUtc().minusMillis(1));
assertThat(
ofy()
.load()
.key(Key.create(getCrossTldKey(), Root.class, 1))
.now()
.getUpdateTimestamp()
.getTimestamp())
.isEqualTo(clock.nowUtc().minusMillis(1));
}
@Test
@ -340,8 +400,14 @@ public class OfyCommitLogTest {
});
ofy().clearSessionCache();
for (int i = 1; i <= 3; i++) {
assertThat(ofy().load().key(Key.create(getCrossTldKey(), Root.class, i)).now()
.getUpdateAutoTimestamp().getTimestamp()).isEqualTo(clock.nowUtc());
assertThat(
ofy()
.load()
.key(Key.create(getCrossTldKey(), Root.class, i))
.now()
.getUpdateTimestamp()
.getTimestamp())
.isEqualTo(clock.nowUtc());
}
clock.advanceOneMilli();
// Mutate one root, and a child of a second, ignoring the third.
@ -353,14 +419,32 @@ public class OfyCommitLogTest {
});
ofy().clearSessionCache();
// Child was touched.
assertThat(ofy().load().key(Key.create(getCrossTldKey(), Root.class, 1)).now()
.getUpdateAutoTimestamp().getTimestamp()).isEqualTo(clock.nowUtc());
assertThat(
ofy()
.load()
.key(Key.create(getCrossTldKey(), Root.class, 1))
.now()
.getUpdateTimestamp()
.getTimestamp())
.isEqualTo(clock.nowUtc());
// Directly touched.
assertThat(ofy().load().key(Key.create(getCrossTldKey(), Root.class, 2)).now()
.getUpdateAutoTimestamp().getTimestamp()).isEqualTo(clock.nowUtc());
assertThat(
ofy()
.load()
.key(Key.create(getCrossTldKey(), Root.class, 2))
.now()
.getUpdateTimestamp()
.getTimestamp())
.isEqualTo(clock.nowUtc());
// Wasn't touched.
assertThat(ofy().load().key(Key.create(getCrossTldKey(), Root.class, 3)).now()
.getUpdateAutoTimestamp().getTimestamp()).isEqualTo(clock.nowUtc().minusMillis(1));
assertThat(
ofy()
.load()
.key(Key.create(getCrossTldKey(), Root.class, 3))
.now()
.getUpdateTimestamp()
.getTimestamp())
.isEqualTo(clock.nowUtc().minusMillis(1));
}
@Entity

View file

@ -79,8 +79,8 @@ public class OfyTest {
}
private void doBackupGroupRootTimestampInversionTest(Runnable runnable) {
DateTime groupTimestamp = ofy().load().key(someObject.getParent()).now()
.getUpdateAutoTimestamp().getTimestamp();
DateTime groupTimestamp =
ofy().load().key(someObject.getParent()).now().getUpdateTimestamp().getTimestamp();
// Set the clock in Ofy to the same time as the backup group root's save time.
Ofy ofy = new Ofy(new FakeClock(groupTimestamp));
TimestampInversionException thrown =