Add more Datastore/Sql Entity annotations (#826)

* Add more Datastore/Sql Entity annotations

* Move comments up a line
This commit is contained in:
gbrodman 2020-10-05 13:07:53 -04:00 committed by GitHub
parent 95f6ccc657
commit 0f09a4a0ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 47 additions and 22 deletions

View file

@ -252,7 +252,8 @@ public final class OteAccountBuilder {
private void saveAllEntities() { private void saveAllEntities() {
tm().assertInTransaction(); tm().assertInTransaction();
ImmutableList<Registry> registries = ImmutableList.of(sunriseTld, gaTld, eapTld); // use ImmutableObject instead of Registry so that the Key generation doesn't break
ImmutableList<ImmutableObject> registries = ImmutableList.of(sunriseTld, gaTld, eapTld);
ImmutableList<RegistrarContact> contacts = contactsBuilder.build(); ImmutableList<RegistrarContact> contacts = contactsBuilder.build();
if (!replaceExisting) { if (!replaceExisting) {

View file

@ -14,6 +14,7 @@
package google.registry.model.contact; package google.registry.model.contact;
import com.google.common.collect.ImmutableList;
import com.googlecode.objectify.Key; import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.EntitySubclass; import com.googlecode.objectify.annotation.EntitySubclass;
import google.registry.model.EppResource; import google.registry.model.EppResource;
@ -21,6 +22,8 @@ import google.registry.model.ImmutableObject;
import google.registry.model.contact.ContactHistory.ContactHistoryId; import google.registry.model.contact.ContactHistory.ContactHistoryId;
import google.registry.model.reporting.HistoryEntry; import google.registry.model.reporting.HistoryEntry;
import google.registry.persistence.VKey; import google.registry.persistence.VKey;
import google.registry.schema.replay.DatastoreEntity;
import google.registry.schema.replay.SqlEntity;
import java.io.Serializable; import java.io.Serializable;
import java.util.Optional; import java.util.Optional;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -52,7 +55,7 @@ import javax.persistence.PostLoad;
@EntitySubclass @EntitySubclass
@Access(AccessType.FIELD) @Access(AccessType.FIELD)
@IdClass(ContactHistoryId.class) @IdClass(ContactHistoryId.class)
public class ContactHistory extends HistoryEntry { public class ContactHistory extends HistoryEntry implements SqlEntity {
// Store ContactBase instead of ContactResource so we don't pick up its @Id // Store ContactBase instead of ContactResource so we don't pick up its @Id
@Nullable ContactBase contactBase; @Nullable ContactBase contactBase;
@ -101,6 +104,12 @@ public class ContactHistory extends HistoryEntry {
parent = Key.create(ContactResource.class, contactRepoId); parent = Key.create(ContactResource.class, contactRepoId);
} }
// In Datastore, save as a HistoryEntry object regardless of this object's type
@Override
public ImmutableList<DatastoreEntity> toDatastoreEntities() {
return ImmutableList.of(asHistoryEntry());
}
/** Class to represent the composite primary key of {@link ContactHistory} entity. */ /** Class to represent the composite primary key of {@link ContactHistory} entity. */
static class ContactHistoryId extends ImmutableObject implements Serializable { static class ContactHistoryId extends ImmutableObject implements Serializable {

View file

@ -16,6 +16,7 @@ package google.registry.model.domain;
import static google.registry.util.CollectionUtils.nullToEmptyImmutableCopy; import static google.registry.util.CollectionUtils.nullToEmptyImmutableCopy;
import com.google.common.collect.ImmutableList;
import com.googlecode.objectify.Key; import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.EntitySubclass; import com.googlecode.objectify.annotation.EntitySubclass;
import com.googlecode.objectify.annotation.Ignore; import com.googlecode.objectify.annotation.Ignore;
@ -26,6 +27,8 @@ import google.registry.model.host.HostResource;
import google.registry.model.reporting.DomainTransactionRecord; import google.registry.model.reporting.DomainTransactionRecord;
import google.registry.model.reporting.HistoryEntry; import google.registry.model.reporting.HistoryEntry;
import google.registry.persistence.VKey; import google.registry.persistence.VKey;
import google.registry.schema.replay.DatastoreEntity;
import google.registry.schema.replay.SqlEntity;
import java.io.Serializable; import java.io.Serializable;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
@ -65,7 +68,7 @@ import javax.persistence.Table;
@EntitySubclass @EntitySubclass
@Access(AccessType.FIELD) @Access(AccessType.FIELD)
@IdClass(DomainHistoryId.class) @IdClass(DomainHistoryId.class)
public class DomainHistory extends HistoryEntry { public class DomainHistory extends HistoryEntry implements SqlEntity {
// Store DomainContent instead of DomainBase so we don't pick up its @Id // Store DomainContent instead of DomainBase so we don't pick up its @Id
@Nullable DomainContent domainContent; @Nullable DomainContent domainContent;
@ -172,6 +175,12 @@ public class DomainHistory extends HistoryEntry {
parent = Key.create(DomainBase.class, domainRepoId); parent = Key.create(DomainBase.class, domainRepoId);
} }
// In Datastore, save as a HistoryEntry object regardless of this object's type
@Override
public ImmutableList<DatastoreEntity> toDatastoreEntities() {
return ImmutableList.of(asHistoryEntry());
}
/** Class to represent the composite primary key of {@link DomainHistory} entity. */ /** Class to represent the composite primary key of {@link DomainHistory} entity. */
static class DomainHistoryId extends ImmutableObject implements Serializable { static class DomainHistoryId extends ImmutableObject implements Serializable {

View file

@ -22,6 +22,7 @@ import google.registry.model.billing.BillingEvent;
import google.registry.model.billing.BillingEvent.Recurring; import google.registry.model.billing.BillingEvent.Recurring;
import google.registry.model.domain.rgp.GracePeriodStatus; import google.registry.model.domain.rgp.GracePeriodStatus;
import google.registry.persistence.VKey; import google.registry.persistence.VKey;
import google.registry.schema.replay.DatastoreAndSqlEntity;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Index; import javax.persistence.Index;
@ -37,7 +38,7 @@ import org.joda.time.DateTime;
@Embed @Embed
@Entity @Entity
@Table(indexes = @Index(columnList = "domainRepoId")) @Table(indexes = @Index(columnList = "domainRepoId"))
public class GracePeriod extends GracePeriodBase { public class GracePeriod extends GracePeriodBase implements DatastoreAndSqlEntity {
private static GracePeriod createInternal( private static GracePeriod createInternal(
GracePeriodStatus type, GracePeriodStatus type,

View file

@ -16,6 +16,7 @@ package google.registry.model.domain.secdns;
import com.googlecode.objectify.annotation.Embed; import com.googlecode.objectify.annotation.Embed;
import google.registry.model.ImmutableObject; import google.registry.model.ImmutableObject;
import google.registry.schema.replay.DatastoreAndSqlEntity;
import javax.xml.bind.DatatypeConverter; import javax.xml.bind.DatatypeConverter;
import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType; import javax.xml.bind.annotation.XmlType;
@ -31,7 +32,7 @@ import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
@Embed @Embed
@XmlType(name = "dsData") @XmlType(name = "dsData")
@javax.persistence.Entity @javax.persistence.Entity
public class DelegationSignerData extends ImmutableObject { public class DelegationSignerData extends ImmutableObject implements DatastoreAndSqlEntity {
private DelegationSignerData() {} private DelegationSignerData() {}

View file

@ -15,6 +15,7 @@
package google.registry.model.host; package google.registry.model.host;
import com.google.common.collect.ImmutableList;
import com.googlecode.objectify.Key; import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.EntitySubclass; import com.googlecode.objectify.annotation.EntitySubclass;
import google.registry.model.EppResource; import google.registry.model.EppResource;
@ -22,6 +23,8 @@ import google.registry.model.ImmutableObject;
import google.registry.model.host.HostHistory.HostHistoryId; import google.registry.model.host.HostHistory.HostHistoryId;
import google.registry.model.reporting.HistoryEntry; import google.registry.model.reporting.HistoryEntry;
import google.registry.persistence.VKey; import google.registry.persistence.VKey;
import google.registry.schema.replay.DatastoreEntity;
import google.registry.schema.replay.SqlEntity;
import java.io.Serializable; import java.io.Serializable;
import java.util.Optional; import java.util.Optional;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -54,7 +57,7 @@ import javax.persistence.PostLoad;
@EntitySubclass @EntitySubclass
@Access(AccessType.FIELD) @Access(AccessType.FIELD)
@IdClass(HostHistoryId.class) @IdClass(HostHistoryId.class)
public class HostHistory extends HistoryEntry { public class HostHistory extends HistoryEntry implements SqlEntity {
// Store HostBase instead of HostResource so we don't pick up its @Id // Store HostBase instead of HostResource so we don't pick up its @Id
@Nullable HostBase hostBase; @Nullable HostBase hostBase;
@ -101,6 +104,12 @@ public class HostHistory extends HistoryEntry {
parent = Key.create(HostResource.class, hostRepoId); parent = Key.create(HostResource.class, hostRepoId);
} }
// In Datastore, save as a HistoryEntry object regardless of this object's type
@Override
public ImmutableList<DatastoreEntity> toDatastoreEntities() {
return ImmutableList.of(asHistoryEntry());
}
/** Class to represent the composite primary key of {@link HostHistory} entity. */ /** Class to represent the composite primary key of {@link HostHistory} entity. */
static class HostHistoryId extends ImmutableObject implements Serializable { static class HostHistoryId extends ImmutableObject implements Serializable {

View file

@ -62,6 +62,7 @@ import google.registry.model.domain.fee.BaseFee.FeeType;
import google.registry.model.domain.fee.Fee; import google.registry.model.domain.fee.Fee;
import google.registry.model.registry.label.PremiumList; import google.registry.model.registry.label.PremiumList;
import google.registry.model.registry.label.ReservedList; import google.registry.model.registry.label.ReservedList;
import google.registry.schema.replay.DatastoreAndSqlEntity;
import google.registry.util.Idn; import google.registry.util.Idn;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
@ -86,7 +87,7 @@ import org.joda.time.Duration;
@ReportedOn @ReportedOn
@Entity @Entity
@javax.persistence.Entity(name = "Tld") @javax.persistence.Entity(name = "Tld")
public class Registry extends ImmutableObject implements Buildable { public class Registry extends ImmutableObject implements Buildable, DatastoreAndSqlEntity {
@Parent @Transient Key<EntityGroupRoot> parent = getCrossTldKey(); @Parent @Transient Key<EntityGroupRoot> parent = getCrossTldKey();

View file

@ -22,6 +22,7 @@ import com.googlecode.objectify.annotation.Embed;
import com.googlecode.objectify.annotation.Ignore; import com.googlecode.objectify.annotation.Ignore;
import google.registry.model.Buildable; import google.registry.model.Buildable;
import google.registry.model.ImmutableObject; import google.registry.model.ImmutableObject;
import google.registry.schema.replay.DatastoreAndSqlEntity;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.EnumType; import javax.persistence.EnumType;
@ -43,7 +44,8 @@ import org.joda.time.DateTime;
*/ */
@Embed @Embed
@Entity @Entity
public class DomainTransactionRecord extends ImmutableObject implements Buildable { public class DomainTransactionRecord extends ImmutableObject
implements Buildable, DatastoreAndSqlEntity {
@Id @Id
@Ignore @Ignore

View file

@ -62,7 +62,7 @@ import org.joda.time.DateTime;
@MappedSuperclass @MappedSuperclass
@WithStringVKey // TODO(b/162229294): This should be resolved during the course of that bug @WithStringVKey // TODO(b/162229294): This should be resolved during the course of that bug
@Access(AccessType.FIELD) @Access(AccessType.FIELD)
public class HistoryEntry extends ImmutableObject implements Buildable, DatastoreEntity, SqlEntity { public class HistoryEntry extends ImmutableObject implements Buildable, DatastoreEntity {
/** Represents the type of history entry. */ /** Represents the type of history entry. */
public enum Type { public enum Type {
@ -322,13 +322,7 @@ public class HistoryEntry extends ImmutableObject implements Buildable, Datastor
// In SQL, save the child type // In SQL, save the child type
@Override @Override
public ImmutableList<SqlEntity> toSqlEntities() { public ImmutableList<SqlEntity> toSqlEntities() {
return ImmutableList.of(toChildHistoryEntity()); return ImmutableList.of((SqlEntity) toChildHistoryEntity());
}
// In Datastore, save as a HistoryEntry object regardless of this object's type
@Override
public ImmutableList<DatastoreEntity> toDatastoreEntities() {
return ImmutableList.of(asHistoryEntry());
} }
/** A builder for {@link HistoryEntry} since it is immutable */ /** A builder for {@link HistoryEntry} since it is immutable */

View file

@ -46,6 +46,7 @@ import google.registry.model.annotations.NotBackedUp;
import google.registry.model.annotations.NotBackedUp.Reason; import google.registry.model.annotations.NotBackedUp.Reason;
import google.registry.model.annotations.VirtualEntity; import google.registry.model.annotations.VirtualEntity;
import google.registry.model.common.CrossTldSingleton; import google.registry.model.common.CrossTldSingleton;
import google.registry.schema.replay.DatastoreAndSqlEntity;
import google.registry.schema.replay.DatastoreEntity; import google.registry.schema.replay.DatastoreEntity;
import google.registry.schema.replay.SqlEntity; import google.registry.schema.replay.SqlEntity;
import google.registry.util.CollectionUtils; import google.registry.util.CollectionUtils;
@ -97,7 +98,7 @@ import org.joda.time.DateTime;
@NotBackedUp(reason = Reason.EXTERNALLY_SOURCED) @NotBackedUp(reason = Reason.EXTERNALLY_SOURCED)
@javax.persistence.Entity(name = "ClaimsList") @javax.persistence.Entity(name = "ClaimsList")
@Table @Table
public class ClaimsListShard extends ImmutableObject implements DatastoreEntity { public class ClaimsListShard extends ImmutableObject implements DatastoreAndSqlEntity {
private static final FluentLogger logger = FluentLogger.forEnclosingClass(); private static final FluentLogger logger = FluentLogger.forEnclosingClass();
@ -354,11 +355,6 @@ public class ClaimsListShard extends ImmutableObject implements DatastoreEntity
} }
} }
@Override
public ImmutableList<SqlEntity> toSqlEntities() {
return ImmutableList.of(); // ClaimsLists are dually written
}
/** Virtual parent entity for claims list shards of a specific revision. */ /** Virtual parent entity for claims list shards of a specific revision. */
@Entity @Entity
@VirtualEntity @VirtualEntity

View file

@ -18,6 +18,7 @@ import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.googlecode.objectify.annotation.Embed;
import io.github.classgraph.ClassGraph; import io.github.classgraph.ClassGraph;
import io.github.classgraph.ClassInfo; import io.github.classgraph.ClassInfo;
import io.github.classgraph.ClassInfoList; import io.github.classgraph.ClassInfoList;
@ -72,6 +73,7 @@ public class EntityTest {
.filter(ClassInfo::isStandardClass) .filter(ClassInfo::isStandardClass)
.map(ClassInfo::loadClass) .map(ClassInfo::loadClass)
.filter(clazz -> !clazz.isAnnotationPresent(EntityForTesting.class)) .filter(clazz -> !clazz.isAnnotationPresent(EntityForTesting.class))
.filter(clazz -> !clazz.isAnnotationPresent(Embed.class))
.map(Class::getName) .map(Class::getName)
.collect(toImmutableSet()); .collect(toImmutableSet());
} }