Add by-repo-id method to RegistryLockDao (#344)

* Add by-repo-id method to RegistryLockDao

When we create new registry lock objects on user request, we will want
to make sure that there are no pending lock/unlock actions on this
domain. In order to do that, we will need to know what lock actions
there have been for this domain.

* Inline the query into a single statement

* whoops

* comments
This commit is contained in:
gbrodman 2019-11-12 11:47:59 -05:00 committed by GitHub
parent b005e3aeb0
commit a392100852
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 0 deletions

View file

@ -19,6 +19,7 @@ import static google.registry.model.transaction.TransactionManagerFactory.jpaTm;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import google.registry.schema.domain.RegistryLock; import google.registry.schema.domain.RegistryLock;
import java.util.Optional;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
/** Data access object for {@link google.registry.schema.domain.RegistryLock}. */ /** Data access object for {@link google.registry.schema.domain.RegistryLock}. */
@ -47,6 +48,7 @@ public final class RegistryLockDao {
}); });
} }
/** Returns all lock objects that this registrar has created. */
public static ImmutableList<RegistryLock> getByRegistrarId(String registrarId) { public static ImmutableList<RegistryLock> getByRegistrarId(String registrarId) {
return jpaTm() return jpaTm()
.transact( .transact(
@ -62,6 +64,26 @@ public final class RegistryLockDao {
.getResultList())); .getResultList()));
} }
/**
* Returns the most recent lock object for a given repo ID (i.e. a domain) or empty if this domain
* hasn't been locked before.
*/
public static Optional<RegistryLock> getMostRecentByRepoId(String repoId) {
return jpaTm()
.transact(
() ->
jpaTm()
.getEntityManager()
.createQuery(
"SELECT lock FROM RegistryLock lock WHERE lock.repoId = :repoId"
+ " ORDER BY lock.revisionId DESC",
RegistryLock.class)
.setParameter("repoId", repoId)
.setMaxResults(1)
.getResultStream()
.findFirst());
}
public static RegistryLock save(RegistryLock registryLock) { public static RegistryLock save(RegistryLock registryLock) {
checkNotNull(registryLock, "Null registry lock cannot be saved"); checkNotNull(registryLock, "Null registry lock cannot be saved");
return jpaTm().transact(() -> jpaTm().getEntityManager().merge(registryLock)); return jpaTm().transact(() -> jpaTm().getEntityManager().merge(registryLock));

View file

@ -120,6 +120,26 @@ public final class RegistryLockDaoTest {
assertThat(RegistryLockDao.getByRegistrarId("nonexistent")).isEmpty(); assertThat(RegistryLockDao.getByRegistrarId("nonexistent")).isEmpty();
} }
@Test
public void testLoad_byRepoId() {
RegistryLock completedLock =
createLock().asBuilder().setCompletionTimestamp(jpaTmRule.getTxnClock().nowUtc()).build();
RegistryLockDao.save(completedLock);
jpaTmRule.getTxnClock().advanceOneMilli();
RegistryLock inProgressLock = createLock();
RegistryLockDao.save(inProgressLock);
Optional<RegistryLock> mostRecent = RegistryLockDao.getMostRecentByRepoId("repoId");
assertThat(mostRecent.isPresent()).isTrue();
assertThat(mostRecent.get().isVerified()).isFalse();
}
@Test
public void testLoad_byRepoId_empty() {
assertThat(RegistryLockDao.getMostRecentByRepoId("nonexistent").isPresent()).isFalse();
}
private RegistryLock createLock() { private RegistryLock createLock() {
return new RegistryLock.Builder() return new RegistryLock.Builder()
.setRepoId("repoId") .setRepoId("repoId")