-
Notifications
You must be signed in to change notification settings - Fork 319
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create dataset schema versions when writing dataset
Signed-off-by: David Goss <[email protected]>
- Loading branch information
1 parent
78a191b
commit 2d26f95
Showing
26 changed files
with
300 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package marquez.db; | ||
|
||
import java.time.Instant; | ||
import java.util.List; | ||
import java.util.UUID; | ||
import java.util.stream.Collectors; | ||
import marquez.common.Utils; | ||
import marquez.common.models.Version; | ||
import marquez.db.mappers.DatasetSchemaVersionRowMapper; | ||
import marquez.db.models.DatasetFieldRow; | ||
import marquez.db.models.DatasetRow; | ||
import marquez.db.models.DatasetSchemaVersionRow; | ||
import org.apache.commons.lang3.tuple.Pair; | ||
import org.jdbi.v3.sqlobject.config.RegisterRowMapper; | ||
import org.jdbi.v3.sqlobject.statement.SqlBatch; | ||
import org.jdbi.v3.sqlobject.statement.SqlQuery; | ||
|
||
@RegisterRowMapper(DatasetSchemaVersionRowMapper.class) | ||
public interface DatasetSchemaVersionDao extends BaseDao { | ||
default Version upsertSchemaVersion( | ||
DatasetRow datasetRow, List<DatasetFieldRow> datasetFields, Instant now) { | ||
final Version computedVersion = | ||
Utils.newDatasetSchemaVersionFor( | ||
datasetRow.getNamespaceName(), | ||
datasetRow.getName(), | ||
datasetFields.stream() | ||
.map(field -> Pair.of(field.getName(), field.getType())) | ||
.collect(Collectors.toSet())); | ||
final DatasetSchemaVersionRow newRow = | ||
upsertSchemaVersion(computedVersion.getValue(), datasetRow.getUuid(), now); | ||
if (newRow != null) { | ||
// if not null it means a new insert, so we have to do the fields as well | ||
upsertFieldMappings( | ||
newRow.getUuid(), | ||
datasetFields.stream().map(DatasetFieldRow::getUuid).collect(Collectors.toList())); | ||
} | ||
// if null then it means the version already exists, and so the fields must already exist | ||
return computedVersion; | ||
} | ||
|
||
@SqlQuery( | ||
"INSERT INTO dataset_schema_versions " | ||
+ "(uuid, dataset_uuid, created_at) " | ||
+ "VALUES (:uuid, :datasetUuid, :now) " | ||
+ "ON CONFLICT DO NOTHING " | ||
+ "RETURNING *") | ||
DatasetSchemaVersionRow upsertSchemaVersion(UUID uuid, UUID datasetUuid, Instant now); | ||
|
||
@SqlBatch( | ||
"INSERT INTO dataset_schema_versions_field_mapping " | ||
+ "(dataset_schema_version_uuid, dataset_field_uuid) " | ||
+ "VALUES (:schemaVersionUuid, :fieldUuid) " | ||
+ "ON CONFLICT DO NOTHING") | ||
void upsertFieldMappings(UUID schemaVersionUuid, Iterable<UUID> fieldUuid); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
api/src/main/java/marquez/db/mappers/DatasetSchemaVersionRowMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package marquez.db.mappers; | ||
|
||
import static marquez.db.Columns.timestampOrThrow; | ||
import static marquez.db.Columns.uuidOrThrow; | ||
|
||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import lombok.NonNull; | ||
import marquez.db.Columns; | ||
import marquez.db.models.DatasetSchemaVersionRow; | ||
import org.jdbi.v3.core.mapper.RowMapper; | ||
import org.jdbi.v3.core.statement.StatementContext; | ||
|
||
public final class DatasetSchemaVersionRowMapper implements RowMapper<DatasetSchemaVersionRow> { | ||
@Override | ||
public DatasetSchemaVersionRow map(@NonNull ResultSet results, @NonNull StatementContext context) | ||
throws SQLException { | ||
return new DatasetSchemaVersionRow( | ||
uuidOrThrow(results, Columns.ROW_UUID), | ||
uuidOrThrow(results, Columns.DATASET_UUID), | ||
timestampOrThrow(results, Columns.CREATED_AT)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
api/src/main/java/marquez/db/models/DatasetSchemaVersionRow.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package marquez.db.models; | ||
|
||
import java.time.Instant; | ||
import java.util.UUID; | ||
import lombok.AllArgsConstructor; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.NonNull; | ||
import lombok.ToString; | ||
|
||
@AllArgsConstructor | ||
@EqualsAndHashCode | ||
@Getter | ||
@ToString | ||
public class DatasetSchemaVersionRow { | ||
@NonNull private final UUID uuid; | ||
@NonNull private final UUID datasetUuid; | ||
@NonNull private final Instant createdAt; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.