Skip to content

Commit

Permalink
delete: add delete namespace endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Maciej Obuchowski <obuchowski.maciej@gmail.com>
  • Loading branch information
mobuchowski committed Nov 17, 2022
1 parent 440abb3 commit 74abc92
Show file tree
Hide file tree
Showing 30 changed files with 367 additions and 342 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/headerchecker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
- name: Check for headers
run: |
ok=1
readarray -t files <<<"$(jq -r '.[]' <<<'${{ steps.files.outputs.all }}')"
readarray -t files <<<"$(jq -r '.[]' <<<'${{ steps.files.outputs.added_modified }}')"
for file in ${files[@]}; do
if [[ ($file == *".java") ]]; then
if ! grep -q Copyright "$file"; then
Expand Down
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

## [Unreleased](https://github.com/MarquezProject/marquez/compare/0.27.0...HEAD)

### Added
* Add possibility to soft-delete namespaces [`#2244`](https://github.com/MarquezProject/marquez/pull/2244) [@mobuchowski](https://github.com/mobuchowski)
*Adds the ability to "hide" inactive namespaces. The namespaces are being undeleted when relevant OL event is received.*

### Fixed
* Fix bug where job isn't properly deleted [`#2244`](https://github.com/MarquezProject/marquez/pull/2244) [@mobuchowski](https://github.com/mobuchowski)
*It wasn't possible to delete jobs created from events that had `ParentRunFacet`. Now it's possible.*


## [0.27.0](https://github.com/MarquezProject/marquez/compare/0.26.0...0.27.0) - 2022-10-24

### Added
Expand Down
18 changes: 18 additions & 0 deletions api/src/main/java/marquez/api/NamespaceResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
Expand Down Expand Up @@ -77,6 +78,23 @@ public Response list(
return Response.ok(new Namespaces(namespaces)).build();
}

@Timed
@ResponseMetered
@ExceptionMetered
@DELETE
@Path("/namespaces/{namespace}")
@Produces(APPLICATION_JSON)
public Response delete(@PathParam("namespace") NamespaceName name) {
final Namespace namespace =
namespaceService
.findBy(name.getValue())
.orElseThrow(() -> new NamespaceNotFoundException(name));
datasetService.deleteByNamespaceName(namespace.getName().getValue());
jobService.deleteByNamespaceName(namespace.getName().getValue());
namespaceService.delete(namespace.getName().getValue());
return Response.ok(namespace).build();
}

@Value
static class Namespaces {
@NonNull
Expand Down
9 changes: 9 additions & 0 deletions api/src/main/java/marquez/db/Columns.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ private Columns() {}
public static final String DATASET_NAME = "dataset_name";
public static final String FACETS = "facets";
public static final String TAGS = "tags";
public static final String IS_HIDDEN = "is_hidden";

/* NAMESPACE ROW COLUMNS */
public static final String CURRENT_OWNER_NAME = "current_owner_name";
Expand Down Expand Up @@ -197,6 +198,14 @@ public static boolean booleanOrDefault(
return results.getBoolean(column);
}

public static boolean booleanOrThrow(final ResultSet results, final String column)
throws SQLException {
if (results.getObject(column) == null) {
throw new IllegalArgumentException();
}
return results.getBoolean(column);
}

public static int intOrThrow(final ResultSet results, final String column) throws SQLException {
if (results.getObject(column) == null) {
throw new IllegalArgumentException();
Expand Down
23 changes: 16 additions & 7 deletions api/src/main/java/marquez/db/DatasetDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -292,15 +292,24 @@ DatasetRow upsert(
String name,
String physicalName);

@SqlUpdate(
"""
UPDATE datasets d
SET is_hidden = true
FROM namespaces n
WHERE n.uuid=d.namespace_uuid
AND n.name=:namespaceName
""")
void deleteByNamespaceName(String namespaceName);

@SqlQuery(
"""
UPDATE datasets
SET is_hidden = true
FROM dataset_symlinks, namespaces
WHERE dataset_symlinks.dataset_uuid = datasets.uuid
AND namespaces.uuid = dataset_symlinks.namespace_uuid
AND namespaces.name=:namespaceName AND dataset_symlinks.name=:name
RETURNING *
UPDATE datasets d
SET is_hidden = true
FROM namespaces n
WHERE n.uuid = d.namespace_uuid
AND n.name=:namespaceName AND d.name=:name
RETURNING *
""")
Optional<DatasetRow> delete(String namespaceName, String name);

Expand Down
10 changes: 10 additions & 0 deletions api/src/main/java/marquez/db/JobDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ SELECT run_uuid, JSON_AGG(e.facets) AS facets
""")
void delete(String namespaceName, String name);

@SqlUpdate(
"""
UPDATE jobs
SET is_hidden = true
FROM namespaces n
WHERE jobs.namespace_uuid = n.uuid
AND n.name = :namespaceName
""")
void deleteByNamespaceName(String namespaceName);

default Optional<Job> findWithRun(String namespaceName, String jobName) {
Optional<Job> job = findJobByName(namespaceName, jobName);
job.ifPresent(
Expand Down
81 changes: 49 additions & 32 deletions api/src/main/java/marquez/db/NamespaceDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,20 @@ default Namespace upsertNamespaceMeta(
@SqlQuery("SELECT * FROM namespaces ORDER BY name LIMIT :limit OFFSET :offset")
List<Namespace> findAll(int limit, int offset);

@SqlQuery("UPDATE namespaces SET is_hidden=false WHERE name = :name RETURNING *")
NamespaceRow undelete(String name);

@SqlUpdate("UPDATE namespaces SET is_hidden=true WHERE name = :name")
void delete(String name);

default NamespaceRow upsertNamespaceRow(
UUID uuid, Instant now, String name, String currentOwnerName) {
doUpsertNamespaceRow(uuid, now, name, currentOwnerName);
return findNamespaceByName(name).orElseThrow();
NamespaceRow namespaceRow = findNamespaceByName(name).orElseThrow();
if (namespaceRow.getIsHidden()) {
namespaceRow = undelete(namespaceRow.getName());
}
return namespaceRow;
}

/**
Expand All @@ -99,40 +109,47 @@ default NamespaceRow upsertNamespaceRow(
* @param currentOwnerName
*/
@SqlUpdate(
"INSERT INTO namespaces ( "
+ "uuid, "
+ "created_at, "
+ "updated_at, "
+ "name, "
+ "current_owner_name "
+ ") VALUES ("
+ ":uuid, "
+ ":now, "
+ ":now, "
+ ":name, "
+ ":currentOwnerName) "
+ "ON CONFLICT(name) DO NOTHING")
"""
INSERT INTO namespaces (
uuid,
created_at,
updated_at,
name,
current_owner_name
) VALUES (
:uuid,
:now,
:now,
:name,
:currentOwnerName)
ON CONFLICT(name) DO NOTHING
""")
void doUpsertNamespaceRow(UUID uuid, Instant now, String name, String currentOwnerName);

@SqlQuery(
"INSERT INTO namespaces ( "
+ "uuid, "
+ "created_at, "
+ "updated_at, "
+ "name, "
+ "current_owner_name, "
+ "description "
+ ") VALUES ("
+ ":uuid, "
+ ":now, "
+ ":now, "
+ ":name, "
+ ":currentOwnerName, "
+ ":description "
+ ") ON CONFLICT(name) DO "
+ "UPDATE SET "
+ "updated_at = EXCLUDED.updated_at "
+ "RETURNING *")
"""
INSERT INTO namespaces (
uuid,
created_at,
updated_at,
name,
current_owner_name,
description,
is_hidden
) VALUES (
:uuid,
:now,
:now,
:name,
:currentOwnerName,
:description,
false
) ON CONFLICT(name) DO
UPDATE SET
updated_at = EXCLUDED.updated_at,
is_hidden = false
RETURNING *
""")
NamespaceRow upsertNamespaceRow(
UUID uuid, Instant now, String name, String currentOwnerName, String description);

Expand Down
4 changes: 3 additions & 1 deletion api/src/main/java/marquez/db/mappers/NamespaceMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package marquez.db.mappers;

import static marquez.db.Columns.booleanOrThrow;
import static marquez.db.Columns.stringOrNull;
import static marquez.db.Columns.stringOrThrow;
import static marquez.db.Columns.timestampOrThrow;
Expand All @@ -28,6 +29,7 @@ public Namespace map(@NonNull ResultSet results, @NonNull StatementContext conte
timestampOrThrow(results, Columns.CREATED_AT),
timestampOrThrow(results, Columns.UPDATED_AT),
OwnerName.of(stringOrThrow(results, Columns.CURRENT_OWNER_NAME)),
stringOrNull(results, Columns.DESCRIPTION));
stringOrNull(results, Columns.DESCRIPTION),
booleanOrThrow(results, Columns.IS_HIDDEN));
}
}
4 changes: 3 additions & 1 deletion api/src/main/java/marquez/db/mappers/NamespaceRowMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package marquez.db.mappers;

import static marquez.db.Columns.booleanOrThrow;
import static marquez.db.Columns.stringOrNull;
import static marquez.db.Columns.stringOrThrow;
import static marquez.db.Columns.timestampOrThrow;
Expand All @@ -28,6 +29,7 @@ public NamespaceRow map(@NonNull ResultSet results, @NonNull StatementContext co
timestampOrThrow(results, Columns.UPDATED_AT),
stringOrThrow(results, Columns.NAME),
stringOrNull(results, Columns.DESCRIPTION),
stringOrThrow(results, Columns.CURRENT_OWNER_NAME));
stringOrThrow(results, Columns.CURRENT_OWNER_NAME),
booleanOrThrow(results, Columns.IS_HIDDEN));
}
}
1 change: 1 addition & 0 deletions api/src/main/java/marquez/db/models/NamespaceRow.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class NamespaceRow {
@NonNull String name;
@Nullable String description;
@NonNull String currentOwnerName;
@NonNull Boolean isHidden;

public Optional<String> getDescription() {
return Optional.ofNullable(description);
Expand Down
1 change: 1 addition & 0 deletions api/src/main/java/marquez/service/models/Namespace.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class Namespace {
@NonNull Instant updatedAt;
@NonNull OwnerName ownerName;
@Nullable String description;
@NonNull Boolean isHidden;

public Optional<String> getDescription() {
return Optional.ofNullable(description);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE namespaces ADD COLUMN is_hidden BOOLEAN DEFAULT FALSE;
Loading

0 comments on commit 74abc92

Please sign in to comment.