Skip to content

Commit

Permalink
[DPC-3599] Opt-out Import Database Migrations (#1941)
Browse files Browse the repository at this point in the history
## 🎫 Ticket

https://jira.cms.gov/browse/DPC-3599

## 🛠 Changes

This PR does the following:
1. A database migration for `dpc_consent` db. It creates a table called
`opt_out_file`, consisting of metadata for each opt out file we import,
as well as a foreign key `opt_out_file_id` on the `consent` table. For
every beneficiary who chooses to opt out, we'll be storing a record in
the `consent` table. The Many-to-One relationship here allows us to tie
each record to an opt out file and timestamp, for
visibility/transparency.
2. Creates an `OptOutFileEntity` for the ORM, so that our Java code can
interact with it.
3. Updates the `ConsentEntity` to reflect the Many-to-One relationship.

## ✅ Acceptance Validation

- Successfully ran the migration on my local environment without any
issues.
- Successfully compiled `dpc-common` and `dpc-consent`.

## 🔒 Security Implications

- [ ] This PR adds a new software dependency or dependencies.
- [ ] This PR modifies or invalidates one or more of our security
controls.
- [ ] This PR stores or transmits data that was not stored or
transmitted before.
- [ ] This PR requires additional review of its security implications
for other reasons.

If any security implications apply, add Jason Ashbaugh (GitHub username:
StewGoin) as a reviewer and do not merge this PR without his approval.

---------

Co-authored-by: Ashley Weaver <[email protected]>
  • Loading branch information
chris-ronning-ny and ashley-weaver authored Sep 11, 2023
1 parent 0b41f41 commit 18cc862
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ public ConsentEntity() {
@Column(name = "source_code")
private String sourceCode;

@ManyToOne()
@JoinColumn(name = "opt_out_file_id")
private OptOutFileEntity optOutFile;

public UUID getId() {
return id;
}
Expand Down Expand Up @@ -182,6 +186,9 @@ public void setUpdatedAt(OffsetDateTime updatedAt) {

public void setSourceCode(String sourceCode) { this.sourceCode = sourceCode; }

public OptOutFileEntity getOptOutFile() { return optOutFile; }

public void setOptOutFile(OptOutFileEntity optOutFile) { this.optOutFile = optOutFile; }

public static ConsentEntity defaultConsentEntity(Optional<UUID> id, Optional<String> hicn, Optional<String> mbi) {
ConsentEntity ce = new ConsentEntity();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package gov.cms.dpc.common.consent.entities;

import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.util.Optional;
import java.util.UUID;

@Entity(name = "opt_out_file")
public class OptOutFileEntity implements Serializable {
public static final String IMPORT_STATUS_IN_PROGRESS = "In-Progress";
public static final String IMPORT_STATUS_COMPLETED = "Completed";
public static final String IMPORT_STATUS_FAILED = "Failed";

public OptOutFileEntity() {}

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false, columnDefinition = "uuid")
private UUID id;

@Column(name = "name", nullable = false)
private String name;

@Column(name = "timestamp", nullable = false)
private LocalDate timestamp;

@Column(name = "import_status")
private String importStatus;

@Column(name = "created_at", columnDefinition = "TIMESTAMP WITH TIME ZONE")
@CreationTimestamp
private OffsetDateTime createdAt;

@Column(name = "updated_at", columnDefinition = "TIMESTAMP WITH TIME ZONE")
@UpdateTimestamp
private OffsetDateTime updatedAt;

public UUID getId() {
return id;
}

public void setId(UUID id) {
this.id = id;
}

public String getName() { return name; }

public void setName(String name) { this.name = name; }

public LocalDate getTimestamp() { return timestamp; }

public void setTimestamp(LocalDate timestamp) { this.timestamp = timestamp; }

public String getImportStatus() { return importStatus; }

public void setImportStatus(String importStatus) { this.importStatus = importStatus; }

public OffsetDateTime getCreatedAt() { return createdAt; }

public void setCreatedAt(OffsetDateTime createdAt) { this.createdAt = createdAt; }

public OffsetDateTime getUpdatedAt() { return updatedAt; }

public void setUpdatedAt(OffsetDateTime updatedAt) { this.updatedAt = updatedAt; }

public static OptOutFileEntity defaultOptOutEntity(Optional<UUID> id, Optional<String> name) {
OptOutFileEntity optOut = new OptOutFileEntity();

optOut.setId(UUID.randomUUID());
id.ifPresent(optOut::setId);

optOut.setName("TestOptOutFile");
name.ifPresent(optOut::setName);

optOut.setImportStatus(IMPORT_STATUS_COMPLETED);

optOut.setTimestamp(LocalDate.now(ZoneId.of("UTC")));
optOut.setCreatedAt(OffsetDateTime.now(ZoneId.of("UTC")));
optOut.setUpdatedAt(OffsetDateTime.now(ZoneId.of("UTC")));

return optOut;
}
}
26 changes: 26 additions & 0 deletions dpc-consent/src/main/resources/consent.migrations.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,30 @@
<column name="source_code" type="VARCHAR"/>
</addColumn>
</changeSet>
<changeSet id="add-opt-out-file" author="rswv">
<createTable tableName="OPT_OUT_FILE">
<column name="id" type="UUID">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="name" type="VARCHAR">
<constraints nullable="false"/>
</column>
<column name="timestamp" type="DATE">
<constraints nullable="false"/>
</column>
<column name="import_status" type="VARCHAR" />
<column name="created_at" type="TIMESTAMP WITH TIME ZONE" />
<column name="updated_at" type="TIMESTAMP WITH TIME ZONE" />
</createTable>
<addColumn tableName="CONSENT">
<column name="opt_out_file_id" type="UUID" />
</addColumn>
<addForeignKeyConstraint
baseTableName="CONSENT"
baseColumnNames="opt_out_file_id"
constraintName="fk_consent_opt_out_file"
referencedTableName="OPT_OUT_FILE"
referencedColumnNames="id"
/>
</changeSet>
</databaseChangeLog>

0 comments on commit 18cc862

Please sign in to comment.