mirror of
https://github.com/google/nomulus.git
synced 2025-04-30 03:57:51 +02:00
Use a SQL date object for LocalDates (#842)
* Use a SQL date object for LocalDates * Clean up comment
This commit is contained in:
parent
7f87938942
commit
12ec54f56c
12 changed files with 3325 additions and 3278 deletions
|
@ -24,6 +24,7 @@ import java.time.ZonedDateTime;
|
|||
import java.util.TimeZone;
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.DateTimeZone;
|
||||
import org.joda.time.LocalDate;
|
||||
|
||||
/** Utilities methods and constants related to Joda {@link DateTime} objects. */
|
||||
public class DateTimeUtils {
|
||||
|
@ -108,4 +109,12 @@ public class DateTimeUtils {
|
|||
zonedDateTime.toInstant().toEpochMilli(),
|
||||
DateTimeZone.forTimeZone(TimeZone.getTimeZone(zonedDateTime.getZone())));
|
||||
}
|
||||
|
||||
public static java.sql.Date toSqlDate(LocalDate localDate) {
|
||||
return new java.sql.Date(localDate.toDateTimeAtStartOfDay().getMillis());
|
||||
}
|
||||
|
||||
public static LocalDate toLocalDate(java.sql.Date date) {
|
||||
return new LocalDate(date.getTime(), DateTimeZone.UTC);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -75,7 +75,7 @@ public class Spec11ThreatMatch extends ImmutableObject implements Buildable, Sql
|
|||
String registrarId;
|
||||
|
||||
/** Date on which the check was run, on which the domain was flagged as abusive. */
|
||||
@Column(nullable = false)
|
||||
@Column(nullable = false, columnDefinition = "date")
|
||||
LocalDate checkDate;
|
||||
|
||||
/** The domain's top-level domain. */
|
||||
|
|
|
@ -16,13 +16,15 @@ package google.registry.model.reporting;
|
|||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import google.registry.persistence.transaction.JpaTransactionManager;
|
||||
import google.registry.util.DateTimeUtils;
|
||||
import javax.persistence.TemporalType;
|
||||
import org.joda.time.LocalDate;
|
||||
|
||||
/**
|
||||
* Data access object for {@link google.registry.model.reporting.Spec11ThreatMatch}.
|
||||
*
|
||||
* <p>A JpaTransactionManager is passed into each static method because they are called from a BEAM
|
||||
* pipeline and we don't know where it's coming from.</p>
|
||||
* pipeline and we don't know where it's coming from.
|
||||
*/
|
||||
public class Spec11ThreatMatchDao {
|
||||
|
||||
|
@ -32,7 +34,7 @@ public class Spec11ThreatMatchDao {
|
|||
jpaTm
|
||||
.getEntityManager()
|
||||
.createQuery("DELETE FROM Spec11ThreatMatch WHERE check_date = :date")
|
||||
.setParameter("date", date.toString())
|
||||
.setParameter("date", DateTimeUtils.toSqlDate(date), TemporalType.DATE)
|
||||
.executeUpdate();
|
||||
}
|
||||
|
||||
|
|
|
@ -14,17 +14,23 @@
|
|||
|
||||
package google.registry.persistence.converter;
|
||||
|
||||
import google.registry.util.DateTimeUtils;
|
||||
import java.sql.Date;
|
||||
import javax.persistence.AttributeConverter;
|
||||
import javax.persistence.Converter;
|
||||
import org.joda.time.LocalDate;
|
||||
import org.joda.time.format.ISODateTimeFormat;
|
||||
|
||||
/** JPA converter for {@link LocalDate}. */
|
||||
/** JPA converter for {@link LocalDate}, to/from {@link Date}. */
|
||||
@Converter(autoApply = true)
|
||||
public class LocalDateConverter extends ToStringConverterBase<LocalDate> {
|
||||
public class LocalDateConverter implements AttributeConverter<LocalDate, Date> {
|
||||
|
||||
/** Converts the string (a date in ISO-8601 format) into a LocalDate. */
|
||||
@Override
|
||||
public LocalDate convertToEntityAttribute(String columnValue) {
|
||||
return (columnValue == null) ? null : LocalDate.parse(columnValue, ISODateTimeFormat.date());
|
||||
public Date convertToDatabaseColumn(LocalDate attribute) {
|
||||
return attribute == null ? null : DateTimeUtils.toSqlDate(attribute);
|
||||
}
|
||||
|
||||
@Override
|
||||
public LocalDate convertToEntityAttribute(Date dbData) {
|
||||
return dbData == null ? null : DateTimeUtils.toLocalDate(dbData);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -92,15 +92,13 @@ public class Spec11ThreatMatchDaoTest extends EntityTestCase {
|
|||
}
|
||||
|
||||
private Spec11ThreatMatch createThreatMatch(String domainName, LocalDate date) {
|
||||
Spec11ThreatMatch threatMatch =
|
||||
new Spec11ThreatMatch()
|
||||
.asBuilder()
|
||||
.setThreatTypes(ImmutableSet.of(ThreatType.MALWARE))
|
||||
.setCheckDate(date)
|
||||
.setDomainName(domainName)
|
||||
.setRegistrarId("Example Registrar")
|
||||
.setDomainRepoId("1-COM")
|
||||
.build();
|
||||
return threatMatch;
|
||||
return new Spec11ThreatMatch()
|
||||
.asBuilder()
|
||||
.setThreatTypes(ImmutableSet.of(ThreatType.MALWARE))
|
||||
.setCheckDate(date)
|
||||
.setDomainName(domainName)
|
||||
.setRegistrarId("Example Registrar")
|
||||
.setDomainRepoId("1-COM")
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
|
@ -62,3 +62,4 @@ V61__domain_hist_columns.sql
|
|||
V62__disable_key_auto_generation_for_history_tables.sql
|
||||
V63__add_schema_for_ds_data.sql
|
||||
V64__transfer_history_columns.sql
|
||||
V65__local_date_date_type.sql
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
-- 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.
|
||||
|
||||
ALTER TABLE "Spec11ThreatMatch" ALTER COLUMN "check_date" TYPE date USING check_date::date;
|
|
@ -597,7 +597,7 @@
|
|||
|
||||
create table "Spec11ThreatMatch" (
|
||||
id bigserial not null,
|
||||
check_date text not null,
|
||||
check_date date not null,
|
||||
domain_name text not null,
|
||||
domain_repo_id text not null,
|
||||
registrar_id text not null,
|
||||
|
|
|
@ -847,7 +847,7 @@ ALTER SEQUENCE public."ReservedList_revision_id_seq" OWNED BY public."ReservedLi
|
|||
|
||||
CREATE TABLE public."Spec11ThreatMatch" (
|
||||
id bigint NOT NULL,
|
||||
check_date text NOT NULL,
|
||||
check_date date NOT NULL,
|
||||
domain_name text NOT NULL,
|
||||
domain_repo_id text NOT NULL,
|
||||
registrar_id text NOT NULL,
|
||||
|
|
|
@ -24,12 +24,16 @@ import static google.registry.util.DateTimeUtils.latestOf;
|
|||
import static google.registry.util.DateTimeUtils.leapSafeAddYears;
|
||||
import static google.registry.util.DateTimeUtils.leapSafeSubtractYears;
|
||||
import static google.registry.util.DateTimeUtils.toJodaDateTime;
|
||||
import static google.registry.util.DateTimeUtils.toLocalDate;
|
||||
import static google.registry.util.DateTimeUtils.toSqlDate;
|
||||
import static google.registry.util.DateTimeUtils.toZonedDateTime;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import java.sql.Date;
|
||||
import java.time.ZonedDateTime;
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.LocalDate;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** Unit tests for {@link DateTimeUtils}. */
|
||||
|
@ -136,4 +140,16 @@ class DateTimeUtilsTest {
|
|||
DateTime dateTime = toJodaDateTime(zonedDateTime);
|
||||
assertThat(dateTime.toString()).isEqualTo("2016-02-29T11:22:33.168Z");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSuccess_toSqlDate() {
|
||||
LocalDate localDate = LocalDate.parse("2020-02-29");
|
||||
assertThat(toSqlDate(localDate)).isEqualTo(Date.valueOf("2020-02-29"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSuccess_toLocalDate() {
|
||||
Date date = Date.valueOf("2020-02-29");
|
||||
assertThat(toLocalDate(date)).isEqualTo(LocalDate.parse("2020-02-29"));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue