mirror of
https://github.com/google/nomulus.git
synced 2025-07-10 21:23:22 +02:00
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:
parent
b005e3aeb0
commit
a392100852
2 changed files with 42 additions and 0 deletions
|
@ -19,6 +19,7 @@ import static google.registry.model.transaction.TransactionManagerFactory.jpaTm;
|
|||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import google.registry.schema.domain.RegistryLock;
|
||||
import java.util.Optional;
|
||||
import javax.persistence.EntityManager;
|
||||
|
||||
/** 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) {
|
||||
return jpaTm()
|
||||
.transact(
|
||||
|
@ -62,6 +64,26 @@ public final class RegistryLockDao {
|
|||
.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) {
|
||||
checkNotNull(registryLock, "Null registry lock cannot be saved");
|
||||
return jpaTm().transact(() -> jpaTm().getEntityManager().merge(registryLock));
|
||||
|
|
|
@ -120,6 +120,26 @@ public final class RegistryLockDaoTest {
|
|||
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() {
|
||||
return new RegistryLock.Builder()
|
||||
.setRepoId("repoId")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue