We are going to design a deployment artifacts catalog that tracks:
Provenance: where the artifact came from (source code, build system, etc.)
Dependencies: other artifacts that this artifact depends on (could be libraries, configuration, etc.)
Rollback scripts: scripts that can be used to rollback to a previous version.
We assume that we are storing this information in a database and exposing an API.
Let's break down:
Database Schema:
We'll have a table for artifacts and a table for dependencies (many-to-many relationship between artifacts).
Also, we need to store rollout and rollback scripts? Or at least references to them? And provenance information.
Let's define:
Table: artifacts
id (primary key)
name (string, unique for each version? Actually, we might have multiple versions of the same artifact, so we need versioning)
version (string)
build_id (string, to identify the build that produced this artifact, from the CI system)
artifact_url (string, where to download the artifact)
rollback_script_url (string, optional, where to get the rollback script for this artifact)
rollout_script_url (string, optional, where to get the rollout script for this artifact)
metadata (JSON, for other info)
But note: the rollback script might be specific to a version? Or we might have a generic rollback script that works for multiple versions?
We'll assume that each artifact version might have its own rollback and rollout scripts. Alternatively, we could have a separate table for scripts.
However, for simplicity, let's store the URLs for rollback and rollout scripts in the artifact table.
But what if we want to track the exact script content? Then we might want to store the script in the database? That could be heavy and we might want to keep the database light.
We'll store URLs and let the scripts be stored in an artifact repository (like an S3 bucket) or in the same code repository.
Now for dependencies:
Table: dependencies
id (primary key)
artifact_id (foreign key to artifacts.id) -> the artifact that has the dependency
depends_on_artifact_id (foreign key to artifacts.id) -> the artifact it depends on
version_constraint (string, e.g., ">=1.0.0", or exact version) [optional]
But note: an artifact might depend on multiple other artifacts. This table will allow that.
However, we might also have non-artifact dependencies (like external libraries). How to handle?
We can have two types of dependencies:
a) Artifact dependencies (from our catalog)
b) External dependencies (like Maven, npm, etc.)
For external dependencies, we might just store a string representation (like "org.apache.commons:commons-lang3:3.12.0") and a type (e.g., "maven", "npm").
Let's adjust:
Table: dependencies
id
artifact_id
dependency_type (enum: 'internal', 'external') or just a string?
dependency_name (if internal, then we can use artifact_id? Actually, for internal we have depends_on_artifact_id, but for external we don't have an artifact record)
version_constraint
But for internal dependencies, we have a foreign key to another artifact. So:
Option 1: two separate tables? Not sure.
Alternatively, we can have:
Table: artifact_dependencies
- artifact_id
- depends_on_artifact_id
- version_constraint (for the depended artifact, if we have multiple versions of the same artifact, we might want to specify which one)
But then when we query for all dependencies of an artifact, we have to union two tables.
Alternatively, we can have one table with nullable foreign key:
Table: dependencies
- id
- artifact_id
- depends_on_artifact_id (nullable, if not null then it's an internal dependency)
- external_dependency_name (nullable, if depends_on_artifact_id is null then we use this and the ecosystem)
- ecosystem (nullable, for external dependencies)
- version_constraint
But then we have to enforce that either depends_on_artifact_id is set (and then external_dependency_name and ecosystem are null) or the other way.
Alternatively, we can store all dependencies as strings and have a separate table for internal artifact references? That might be messy.
Let me think: we want to know the full dependency tree, including transitive dependencies. For that, we need to resolve internal dependencies to specific artifact versions.
For external dependencies, we might not have an artifact record in our catalog, but we can still store the string representation.
So, I propose:
Table: artifacts (as above)
Table: dependencies
- id
- artifact_id (the artifact that has the dependency)
- depends_on_artifact_id (nullable, if set, points to the specific artifact version we depend on)
- external_dependency_string (nullable, if depends_on_artifact_id is null, then this is a string like "org.apache.commons:commons-lang3:3.12.0")
- version_constraint (nullable, for internal we might have a constraint that is already satisfied by the specific artifact version we point to? Actually, we point to a specific version, so the constraint might be redundant? But we might want to store the constraint as declared in the build file)
However, for internal dependencies, we are pointing to a specific artifact (with version). So the version_constraint for internal might be the constraint that was in the build file (like "1.0.0" or ">=1.0.0") and then we resolved it to a specific artifact. But if we have the specific artifact, we can get its version. So maybe we don't need version_constraint for internal?
But for external, we don't have a specific artifact (unless we also catalog external dependencies, which we don't). So for external, we need the version_constraint.
Alternatively, we can have:
For internal: we store the specific artifact we depend on (via depends_on_artifact_id) and the version_constraint is not needed because the artifact version is fixed.
For external: we store the external_dependency_string and version_constraint.
But then if we want to know the declared dependency (without resolution) for an internal dependency, we might lose that info? We might want to store the original constraint.
So let's store:
For internal:
- depends_on_artifact_id (the resolved artifact)
- declared_version_constraint (nullable, the constraint as written in the build file)
For external:
- external_dependency_string (the string from the build file, which might include version? or not)
- declared_version_constraint (the version constraint, if not included in the string)
But wait, in Maven, you declare groupId, artifactId, and version. In npm, you have package name and version in package.json. So the external_dependency_string might be just the name (without version) and then the version_constraint is separate.
Alternatively, we can store:
dependency_type: 'internal' or 'external'
for internal:
depends_on_artifact_id (not null)
declared_version_constraint (the constraint as in the build file, might be null if exact version was given and we resolved to that exact version? but we might want to store the constraint anyway)
for external:
external_dependency_name (e.g., "commons-lang3")
external_dependency_group (optional, for maven)
ecosystem (e.g., "maven")
declared_version_constraint
But then we have two sets of columns for two types. We can make all columns nullable and have a constraint that for internal, depends_on_artifact_id is not null and the external ones are null, and vice versa.
However, let's try to keep it simple and have two tables? Because the way we resolve and query might be different.
But if we want to get all dependencies of an artifact, we want to see both internal and external. Having one table might be easier for querying.
CHECK (
(dependency_type = 'internal' AND depends_on_artifact_id IS NOT NULL AND external_name IS NULL AND external_group IS NULL AND ecosystem IS NULL)
OR
(dependency_type = 'external' AND depends_on_artifact_id IS NULL AND external_name IS NOT NULL AND ecosystem IS NOT NULL)
)
But note: for external, we might have external_name and external_group and ecosystem, but declared_version_constraint can be null? Actually, we should have a version constraint for external? Not necessarily, because the version might be included in the external_dependency_string? Or we might store the version in declared_version_constraint.
Let me think about the data we have:
For an internal dependency, we have:
- depends_on_artifact_id: the specific artifact we depend on (so we know the exact version)
- declared_version_constraint: the constraint as written (optional, but good to have for auditing)
For an external dependency, we have:
- external_name: the name of the dependency (without version)
- external_group: the group (if applicable, for maven)
- ecosystem: the package manager (maven, npm, etc.)
- declared_version_constraint: the version constraint (could be a range, or a specific version)
We might also want to store the resolved version for external? But we don't control the external dependency resolution? Actually, we might, if we are building the artifact and we know what version was resolved. But then we would have to store the resolved version for external as well? And that might change over time?
However, for reproducibility, we want to know exactly what was used. So for external dependencies, we might also want to store the resolved version. But then we are essentially creating an artifact for the external dependency? Not exactly, because we don't have a build for it.
Alternatively, we can store the resolved version in the same table? We can add:
code
- resolved_version (nullable, for external we store the resolved version, for internal we don't need because we have the artifact version)
But for internal, we have the artifact version via the artifact record.
So:
Table: dependencies
id
artifact_id
dependency_type
depends_on_artifact_id (nullable)
external_name (nullable)
external_group (nullable)
ecosystem (nullable)
declared_version_constraint (nullable)
resolved_version (nullable) -- only for external? or also for internal? For internal, we can get from the artifact record.
We can set resolved_version to be not null for external, and for internal we leave it null and get from the artifact.
But then when we query for the version of a dependency, we have to check the type.
Alternatively, we can have a view that unifies:
code
For internal:
dependency_name = (select name from artifacts where id = depends_on_artifact_id)
version = (select version from artifacts where id = depends_on_artifact_id)
For external:
dependency_name = external_name
version = resolved_version
But then we lose the group for external? We can include it.
This is getting complicated.
Let's simplify: we only track internal dependencies as artifacts and external dependencies as strings with their resolved version.
But we might want to track the same external dependency across multiple artifacts? And if we do, we might want to know if two artifacts use the same external dependency version?
We can create a table for external dependencies and then have a foreign key? But then we are cataloging external dependencies as well.
Alternatively, we can have:
code
Table: external_dependencies
id
name (with group, e.g., "org.apache.commons:commons-lang3")
ecosystem
version (the resolved version)
Then in dependencies table for external, we have external_dependency_id (foreign key to external_dependencies.id)
But then we are normalizing external dependencies. However, the same external dependency (same name, same version) can be shared by multiple artifacts.
But note: the same external dependency might be used with different version constraints? But the resolved version is what we used.
So:
code
Table: artifacts (as above)
Table: external_dependencies
id
name (string, e.g., "commons-lang3" or "org.apache.commons:commons-lang3")
group (optional, separate from name? Actually, in Maven it's groupId:artifactId, so we can store as two fields or one. Let's store as two for flexibility.)
ecosystem (e.g., "maven")
version (the resolved version)
Then in dependencies:
artifact_id
dependency_type
depends_on_artifact_id (for internal)
external_dependency_id (for external)
declared_version_constraint (optional, the constraint as in the build file)
Now, when we add an external dependency, we first check if the external_dependency (by name, group, ecosystem, and version) exists, and if not, we create it. Then we link.
But note: the same external dependency (same name, group, ecosystem) with a different version is a different external_dependency record.
This way, we can track which external dependency versions are used.
However, we might have the same external dependency (same name, group, ecosystem) with the same version used in multiple artifacts? Then we share the same external_dependency record.
With constraints:
- If dependency_type = 'internal', then depends_on_artifact_id is not null and external_dependency_id is null.
- If dependency_type = 'external', then depends_on_artifact_id is null and external_dependency_id is not null.
And we have foreign keys:
- depends_on_artifact_id references artifacts(id)
- external_dependency_id references external_dependencies(id)
API:
We want to expose:
- Get an artifact by id, name, version, etc.
- Get the dependencies of an artifact (both internal and external, with resolved versions).
- Get the provenance of an artifact (the build_id, source_repository, source_commit, etc.)
- Get the rollback and rollout scripts (by URL, so we might want to proxy or just return the URL? We can return the URL and let the client download, or we can proxy and return the content? We'll just return the URL and maybe a checksum? But we don't store checksum for the scripts. We can store the script content in the database? Or we can store the script in an artifact repository and just return the URL.)
We might also want to:
- List all artifacts
- Search artifacts by name, version, build_id, etc.
- List the artifact's dependency tree (transitive dependencies) -> we can do this by recursively querying.
For the dependency tree, we can do:
code
WITH RECURSIVE dependency_tree AS (
SELECT d.id, d.artifact_id, d.dependency_type, d.depends_on_artifact_id, d.external_dependency_id, 1 as level
FROM dependencies d
WHERE d.artifact_id = ?
UNION ALL
SELECT d.id, d.artifact_id, d.dependency_type, d.depends_on_artifact_id, d.external_dependency_id, dt.level + 1
FROM dependencies d
INNER JOIN dependency_tree dt ON d.artifact_id = dt.depends_on_artifact_id
)
SELECT * FROM dependency_tree;
But note: for internal dependencies, we are joining on depends_on_artifact_id (which is an artifact). For external, we don't have further dependencies? External dependencies don't have dependencies in our catalog? So we stop at external.
But wait, external dependencies might have their own dependencies? But we don't track that. So we only traverse internal dependencies.
So the recursive part should only join when dependency_type = 'internal' and then join on the depends_on_artifact_id.
Alternatively, we can do:
code
WITH RECURSIVE internal_deps AS (
SELECT d.artifact_id, d.depends_on_artifact_id, 1 as level
FROM dependencies d
WHERE d.artifact_id = ? AND d.dependency_type = 'internal'
UNION ALL
SELECT d.artifact_id, d.depends_on_artifact_id, id.level + 1
FROM dependencies d
INNER JOIN internal_deps id ON d.artifact_id = id.depends_on_artifact_id AND d.dependency_type = 'internal'
)
SELECT * FROM internal_deps;
Then we can get the internal dependencies (transitive) and then we can also get the external dependencies of the root artifact and of each internal artifact? Actually, we want the full tree including external dependencies at each level.
We might want to return a tree structure. We can do:
code
For the given artifact, we get:
- its direct dependencies (both internal and external)
- for each internal dependency, recursively get its dependencies.
We can do:
code
WITH RECURSIVE full_tree AS (
SELECT
d.id,
d.artifact_id,
d.dependency_type,
d.depends_on_artifact_id,
d.external_dependency_id,
d.declared_version_constraint,
NULL as parent_id,
1 as level
FROM dependencies d
WHERE d.artifact_id = ?
UNION ALL
SELECT
d.id,
d.artifact_id,
d.dependency_type,
d.depends_on_artifact_id,
d.external_dependency_id,
d.declared_version_constraint,
ft.id as parent_id,
ft.level + 1 as level
FROM dependencies d
INNER JOIN full_tree ft ON d.artifact_id = ft.depends_on_artifact_id
)
SELECT * FROM full_tree;
But note: this will also include external dependencies? We are joining on d.artifact_id = ft.depends_on_artifact_id. For external dependencies, depends_on_artifact_id is null, so they won't have children. So we will get:
code
Root artifact (artifact_id = ?) and its direct dependencies (both internal and external).
Then for each internal dependency (which has a depends_on_artifact_id), we get its dependencies (both internal and external) and so on.
This will give us a flat list with parent-child relationships (via parent_id). We can then build the tree in the application.
However, note that the root artifact is not in the dependencies table? We are starting from the dependencies of the root artifact. So the root artifact is not in the result set. We might want to include the root artifact? Or we can get the root artifact separately.
Alternatively, we can start from the artifact and then get its dependencies. The root artifact is not a dependency of itself. So the root artifact is not in the tree.
We can return:
code
{
"artifact": { ... artifact info ... },
"dependencies": [ ... array of direct dependencies ... ],
"transitive_dependencies": { ... maybe a map of artifact_id to its dependencies ... }
}
But the recursive query gives us a flat list with parent_id. We can then build the tree.
Alternatively, we can use JSON functions in the database to build the tree? That might be complex.
Let's keep it simple: return the flat list with parent_id and let the client build the tree.
We also need to join with the artifacts table for internal dependencies and with external_dependencies for external ones.
So the final query might be:
code
WITH RECURSIVE full_tree AS (
SELECT
d.id,
d.artifact_id,
d.dependency_type,
d.depends_on_artifact_id,
d.external_dependency_id,
d.declared_version_constraint,
NULL as parent_id,
1 as level
FROM dependencies d
WHERE d.artifact_id = ?
UNION ALL
SELECT
d.id,
d.artifact_id,
d.dependency_type,
d.depends_on_artifact_id,
d.external_dependency_id,
d.declared_version_constraint,
ft.id as parent_id,
ft.level + 1 as level
FROM dependencies d
INNER JOIN full_tree ft ON d.artifact_id = ft.depends_on_artifact_id
)
SELECT
ft.*,
a.name as internal_artifact_name,
a.version as internal_artifact_version,
a.artifact_url as internal_artifact_url,
ed.name as external_name,
ed.group as external_group,
ed.ecosystem as external_ecosystem,
ed.version as external_version
FROM full_tree ft
LEFT JOIN artifacts a ON ft.depends_on_artifact_id = a.id
LEFT JOIN external_dependencies ed ON ft.external_dependency_id = ed.id
ORDER BY ft.level, ft.artifact_id;
This will give us all the dependencies (direct and transitive) with the details of the depended artifact (if internal) or external dependency.
But note: for the direct dependencies of the root artifact, the artifact_id in the dependencies table is the root artifact's id. And for the next level, the artifact_id is the internal artifact's id (the one that is the parent in the tree).
So we have:
code
For a direct dependency of the root:
artifact_id = root_artifact_id
depends_on_artifact_id = internal_artifact_id (if internal) or null
external_dependency_id = external_dependency_id (if external)
For a transitive dependency (child of an internal dependency):
artifact_id = internal_artifact_id (the parent)
depends_on_artifact_id = another internal_artifact_id or null
So we can build the tree by:
code
root: the root artifact (we get separately)
children: all rows where parent_id is null and artifact_id = root_artifact_id? Actually, the first level in the recursive CTE is the direct dependencies of the root, and they have parent_id null.
Then for each node (row) in the tree, its children are the rows where parent_id = node.id.
We can do:
code
Let's say we have:
id | artifact_id | depends_on_artifact_id | external_dependency_id | parent_id
1 | 100 | 200 | null | null
2 | 100 | null | 1 | null
3 | 200 | 300 | null | 1
4 | 200 | null | 2 | 1
5 | 300 | 400 | null | 3
Then:
Root artifact (id=100) has two direct dependencies:
- internal: artifact 200 (id=1)
- external: external dependency 1 (id=2)
Then artifact 200 (id=1 in dependencies table) has two dependencies:
- internal: artifact 300 (id=3)
- external: external dependency 2 (id=4)
Then artifact 300 (id=3 in dependencies table) has one dependency:
- internal: artifact 400 (id=5)
So the tree:
root (100)
|
|-- internal (200)
| |
| |-- internal (300)
| | |
| | |-- internal (400)
| |
| |-- external (2)
|
|-- external (1)
We can build this recursively in the application.
But note: the same internal artifact might appear multiple times in the tree (if it is a dependency of multiple parents). We are tracking the path.
We are also tracking external dependencies per parent.
This should be acceptable.
Rollback:
We have a rollback_script_url in the artifact. So for a given artifact, we can provide the rollback script.
But what if we want to rollback to a previous version of the same artifact? Then we would use the rollback script of the current version to rollback to the previous version? Or the rollback script of the previous version to rollback to it?
Typically, the rollback script is provided with the new version to rollback to the old version. So if we are deploying version 2.0 and we want to rollback to 1.0, we use the rollback script that comes with 2.0? Or the rollout script of 1.0?
It depends. Sometimes the rollback script is part of the new release and knows how to undo the changes made by the new release. Sometimes we just redeploy the old version (using the rollout script of the old version).
We can store both rollout and rollback scripts for each artifact. Then:
code
To deploy an artifact: use its rollout_script_url (if available) or some default.
To rollback from an artifact: use its rollback_script_url (if available) to go to the previous version.
But what if we want to rollback to an arbitrary version? Then we might use the rollout script of that version.
We can design:
code
When deploying version X:
- If there is a rollout script for X, use it.
- Otherwise, use a default rollout process.
When rolling back from version X to version Y:
Option 1: Use the rollback script of X (if available) to undo the changes of X.
Option 2: Use the rollout script of Y to deploy Y.
But note: if we are rolling back because X is broken, we might not trust the rollback script of X? But that's the one that knows how to undo X.
Alternatively, we can store:
For each artifact, we store:
- rollout_script: how to deploy this version.
- rollback_script: how to rollback from this version to the previous one.
Then, when we are at version X and want to go to version Y (which is older), we have two options:
a) If we have the rollback script for X, we run it and it should take us to the state before X was applied (which might be Y or something else?).
b) Alternatively, we run the rollout script for Y.
But if we want to rollback to a specific version Y, and we are at X, and we don't have the rollback script for X, then we must use the rollout script for Y.
So we might want to store the rollout script for every version and the rollback script for every version (except maybe the first version?).
We'll store both in the artifact table.
In the API, we can provide:
code
GET /artifacts/{id}/rollout -> redirects to the rollout_script_url or returns the script content? We can return a JSON with the URL and maybe a checksum.
Similarly for rollback.
But note: the rollout and rollback scripts might be specific to the environment? We might have multiple environments (staging, production). We might need to parameterize the scripts.
We can store the scripts as templates and then have a service that fills in the parameters (like environment variables, target hosts, etc.) and then executes.
Alternatively, we can store the scripts as is and let the user download and run with their own parameters.
For simplicity, we'll just store the URL and let the user handle the parameters.
However, we might want to validate the script (e.g., check that it has the necessary placeholders). But that's beyond the scope.
Implementation:
We'll design a REST API:
code
GET /artifacts -> list artifacts (with pagination, filtering by name, version, etc.)
GET /artifacts/{id} -> get artifact by id (including provenance, and maybe the rollout/rollback URLs)
GET /artifacts/{id}/dependencies -> get the dependency tree for the artifact (using the recursive query above)
We might also want:
GET /artifacts?name={name}&version={version} -> get by name and version (since name and version together should be unique? Not necessarily, because we might have the same artifact built multiple times? We have build_id to distinguish. But we might want to have the latest? We can have a field `is_latest`? Or we can just return all and let the client choose.)
We'll assume that the combination of (name, version, build_id) is unique? Because we might rebuild the same version with a different build.
So we can have:
code
GET /artifacts?name={name}&version={version} -> returns all builds for that version? Or we can have a query parameter `latest` to get the latest build for that version.
Alternatively, we can have:
code
GET /artifacts?name={name}&version={version}&build_id={build_id} -> to get a specific build.
And:
code
GET /artifacts?name={name}&version={version}&latest=true -> to get the latest build for that version (by built_at timestamp).
We'll implement:
code
GET /artifacts -> with optional query parameters: name, version, build_id, latest (boolean)
We'll also want to get the artifact by its build_id? Because the build_id is unique? We can have:
code
GET /artifacts/build/{build_id}
Similarly, we might want to get by source_commit?
We can have:
code
GET /artifacts?source_commit={commit}
But we'll keep it simple and just have the basic ones.
Rollback:
We can have:
code
POST /artifacts/{id}/rollback -> this would trigger the rollback script for the current version? But we need to know what to rollback to?
Actually, rolling back is a process that depends on the current state and the target state. We might want to rollback to a specific artifact.
We can have:
code
POST /artifacts/{target_artifact_id}/rollback?from={current_artifact_id}
But how do we know the current version? We might have a separate table for deployments? That is, we track what is currently deployed in each environment.
We are just a catalog of artifacts, not a deployment tracker. So we don't know what is currently deployed.
Alternatively, we can have:
code
POST /deployments -> to deploy an artifact to an environment (and record the deployment)
POST /deployments/{deployment_id}/rollback -> to rollback that deployment.
But then we need a deployment tracking system.
Since the requirement is for a catalog, we might not handle deployments. We just provide the artifacts and their rollback scripts.
So the client (deployment system) would:
code
1. Query the catalog for the artifact they want to rollback to (or from).
2. Download the rollback script (or rollout script) and run it with appropriate parameters.
Therefore, our API only needs to provide:
code
- The artifact details (including rollback_script_url and rollout_script_url)
- The dependency tree.
So we don't need a rollback endpoint. We just provide the script.
Storing the scripts:
We are storing URLs. But we might want to store the script content in the database? We can store it as text? But then we have to serve it.
We can have:
code
Table: scripts
id
artifact_id (foreign key to artifacts, and also store which type: rollout or rollback? But we have two fields in artifact. So we can store two records per artifact? Or we can have two tables? Or we can have one table with a type column.)
Alternatively, we can store the script content in the artifact table as two text columns? But that might make the row very large.
We'll stick to URLs for now. But if we want to version the scripts independently? Then we need a separate table.
Since the rollback and rollout scripts are part of the artifact (they are built from the same source), they are versioned with the artifact. So storing the URL in the artifact table is acceptable.
But note: the URL might be to a file in the same build? For example, in the Build artifact we might have:
artifacts:
id (PK)
name (string)
version (string)
build_id (string, unique? we can make it unique because each build has a unique ID)
source_repository (string)
source_commit (string)
built_at (timestamp)
artifact_url (string)
rollout_script_url (string, nullable)
rollback_script_url (string, nullable)
metadata (JSON, nullable)
external_dependencies:
id (PK)
name (string) -- the name of the dependency (e.g., "commons-lang3")
group (string, nullable) -- for maven, the groupId
ecosystem (string) -- e.g., "maven", "npm", "pypi"
version (string) -- the resolved version
dependencies:
id (PK)
artifact_id (FK to artifacts.id)
dependency_type (enum: 'internal', 'external')
depends_on_artifact_id (FK to artifacts.id, nullable)
external_dependency_id (FK to external_dependencies.id, nullable)
declared_version_constraint (string, nullable) -- the constraint as in the build file
Constraints:
code
ALTER TABLE dependencies ADD CONSTRAINT dep_internal_chk
CHECK (dependency_type = 'internal' AND depends_on_artifact_id IS NOT NULL AND external_dependency_id IS NULL);
ALTER TABLE dependencies ADD CONSTRAINT dep_external_chk
CHECK (dependency_type = 'external' AND depends_on_artifact_id IS NULL AND external_dependency_id IS NOT NULL);
Indexes:
code
On artifacts: name, version, build_id (unique), maybe source_commit.
On dependencies: artifact_id, depends_on_artifact_id, external_dependency_id.
8. API Design:
We'll use a RESTful API with JSON.
Endpoints:
code
GET /artifacts
Query parameters:
name: string (optional)
version: string (optional)
build_id: string (optional)
latest: boolean (optional) - if true and version is provided, return the latest build for that version (by built_at). If version is not provided, then return the latest build for each name? That might be heavy. We can disallow latest without version.
Returns:
[ {artifact object}, ... ]
GET /artifacts/{id}
Returns:
{artifact object}
The artifact object includes:
id, name, version, build_id, source_repository, source_commit, built_at, artifact_url, rollout_script_url, rollback_script_url, metadata
GET /artifacts/{id}/dependencies
Returns:
{
"artifact": { ... artifact object ... },
"dependencies": [
{
"id": ...,
"dependency_type": "internal" or "external",
"depends_on_artifact": { ... artifact object ... } (if internal),
"external_dependency": { ... external dependency object ... } (if external),
"declared_version_constraint": "...",
"level": 1, // distance from the root artifact
"parent_id": null or the id of the parent dependency in this tree (the dependency record id, not the artifact id)
},
...
]
}
But note: the recursive query returns a flat list with parent_id (which is the id of the dependency record). We can build the tree in the client.
Alternatively, we can return a tree structure. Let's return a flat list and let the client build the tree? Or we can build the tree in the database with JSON functions? That might be complex and not portable.
We'll return a flat list with:
[
{
"id": 1,
"artifact_id": 100,
"dependency_type": "internal",
"depends_on_artifact": { ... },
"external_dependency": null,
"declared_version_constraint": ">=1.0.0",
"level": 1,
"parent_id": null
},
...
]
Then the client can build the tree by grouping by parent_id.
But note: the same artifact might appear multiple times (as a dependency of multiple parents). That's okay.
We might also want:
GET /external-dependencies
GET /external-dependencies/{id}
But we can get external dependencies from the artifact's dependencies.
9. Example:
Suppose we have:
code
Artifact A (version 1.0, build_id 1) depends on:
- internal: Artifact B (version 1.0, build_id 2)
- external: commons-lang3 version 3.12.0 (ecosystem: maven)
Artifact B (version 1.0, build_id 2) depends on:
- internal: Artifact C (version 1.0, build_id 3)
- external: slf4j version 1.7.30 (ecosystem: maven)
Then for artifact A:
Direct dependencies:
dependency record 1: artifact_id=A, dependency_type=internal, depends_on_artifact_id=B, external_dependency_id=null, parent_id=null
dependency record 2: artifact_id=A, dependency_type=external, depends_on_artifact_id=null, external_dependency_id=commons-lang3-3.12.0, parent_id=null
Then for artifact B (which is the depends_on_artifact_id of record 1):
dependency record 3: artifact_id=B, dependency_type=internal, depends_on_artifact_id=C, external_dependency_id=null, parent_id=1
dependency record 4: artifact_id=B, dependency_type=external, depends_on_artifact_id=null, external_dependency_id=slf4j-1.7.30, parent_id=1
Then for artifact C (which is the depends_on_artifact_id of record 3):
... (if C has dependencies, they would have parent_id=3)
So the tree:
code
A
|-- B (record1)
| |-- C (record3)
| |-- slf4j (record4)
|
|-- commons-lang3 (record2)
We return the flat list with the parent_id set as above.
Additional considerations:
We might want to store the build logs? Not required by the prompt.
We might want to store the size of the artifact? We can add a field in artifacts.
We might want to store the checksum (SHA256) of the artifact for verification? Yes, for security and integrity.
Let's add:
code
artifact_checksum (string, nullable) in artifacts.
Similarly, for the scripts? We can store the checksum of the rollout and rollback scripts? We can add:
code
rollout_script_checksum
rollback_script_checksum
But then we are storing three checksums. Alternatively, we can store the checksum of the artifact and the scripts in the metadata? But then we lose the ability to query.
Table: external_dependencies
id (PK)
name (string)
group (string, nullable)
ecosystem (string)
version (string)
Unique constraint on (name, group, ecosystem, version) ?
Table: dependencies
id (PK)
artifact_id (FK to artifacts.id)
dependency_type (enum: 'internal', 'external')
depends_on_artifact_id (FK to artifacts.id, nullable)
external_dependency_id (FK to external_dependencies.id, nullable)
declared_version_constraint (string, nullable)
GET /artifacts/{id}/provenance -> same as GET /artifacts/{id} but maybe without the large fields? But we have all in one.
We can just use GET /artifacts/{id} for provenance.
Implementation:
We'll use a framework (like Spring Boot, Express, etc.) but we don't need to choose now.
We need to:
code
- Set up a database with the above schema.
- Create the API endpoints.
We'll assume we are using a relational database (PostgreSQL) because of the recursive CTE.
We'll write the recursive CTE for the dependencies.
Let's test the CTE:
code
WITH RECURSIVE full_tree AS (
SELECT
d.id,
d.artifact_id,
d.dependency_type,
d.depends_on_artifact_id,
d.external_dependency_id,
d.declared_version_constraint,
NULL as parent_id,
1 as level
FROM dependencies d
WHERE d.artifact_id = ? -- the root artifact id
UNION ALL
SELECT
d.id,
d.artifact_id,
d.dependency_type,
d.depends_on_artifact_id,
d.external_dependency_id,
d.declared_version_constraint,
ft.id as parent_id,
ft.level + 1 as level
FROM dependencies d
INNER JOIN full_tree ft ON d.artifact_id = ft.depends_on_artifact_id
)
SELECT
ft.*,
a.name as internal_artifact_name,
a.version as internal_artifact_version,
a.artifact_url as internal_artifact_url,
ed.name as external_name,
ed.group as external_group,
ed.ecosystem as external_ecosystem,
ed.version as external_version
FROM full_tree ft
LEFT JOIN artifacts a ON ft.depends_on_artifact_id = a.id
LEFT JOIN external_dependencies ed ON ft.external_dependency_id = ed.id
ORDER BY ft.level, ft.artifact_id;
This should work.
Security:
We should consider:
code
- Authentication and authorization for the API? Not specified, but we might want to restrict who can read the catalog.
- The rollout and rollback scripts might contain secrets? We should not expose the script content via the API? We are just providing the URL. The client must have access to the storage (like S3) to download the script.
- We might want to sign the scripts? Not in the scope.
15. Scalability:
The dependency tree might be very deep and wide. We might need to limit the depth? Or we can paginate the flat list? But the tree is recursive and we want the whole tree? We can set a max depth (like 10) to avoid cycles? But we should also detect cycles in the dependencies? Because if there is a cycle, the recursive query will run indefinitely.
We can use the dbf-spc (PostgreSQL) to set a max recursion depth? Or we can use WHERE level < 10 in the recursive part? But then we might cut off the tree.
Alternatively, we can use the CYCLE clause in the recursive CTE to detect cycles? But we are traversing by artifact_id, and if an artifact depends on itself (directly or indirectly) we have a cycle.
We can do:
code
WITH RECURSIVE full_tree AS (
SELECT
d.id,
d.artifact_id,
d.dependency_type,
d.depends_on_artifact_id,
d.external_dependency_id,
d.declared_version_constraint,
NULL as parent_id,
1 as level,
ARRAY[d.artifact_id] as path -- to track the path
FROM dependencies d
WHERE d.artifact_id = ?
UNION ALL
SELECT
d.id,
d.artifact_id,
d.dependency_type,
d.depends_on_artifact_id,
d.external_dependency_id,
d.declared_version_constraint,
ft.id as parent_id,
ft.level + 1 as level,
ft.path || d.artifact_id
FROM dependencies d
INNER JOIN full_tree ft ON d.artifact_id = ft.depends_on_artifact_id
WHERE d.artifact_id != ANY(ft.path) -- avoid cycles by checking if the artifact_id is already in the path
)
But note: we are joining on d.artifact_id = ft.depends_on_artifact_id, and we are checking that d.artifact_id is not in the path of the parent. This will prevent cycles that involve the same artifact appearing twice in the same branch.
However, if there is a cycle, we break at the point where we would revisit an artifact.
But note: the same artifact might appear multiple times in the tree (as a dependency of different parents) and that's okay. The cycle check is per branch.
We can also set a max level (like 20) to avoid infinite recursion in case we miss a cycle.
We'll do:
code
WHERE d.artifact_id != ANY(ft.path) AND ft.level < 20
Then we return the tree.
Example response for GET /artifacts/{id}/dependencies:
- Provenance: stored in the artifact table (source_repository, source_commit, build_id, built_at)
- Dependencies: stored in the dependencies and external_dependencies tables, with a recursive query to get the tree.
- Rollback scripts: stored as a URL in the artifact table.
We can extend as needed.
Let's write the SQL for the schema:
Note: We are using PostgreSQL.
We'll create the tables and constraints.
We'll also create indexes.
We'll assume the database is named deployment_catalog.
Let's write the DDL.
I'll design a comprehensive deployment artifacts catalog system with provenance tracking, dependency management, and rollback capabilities. This will be a production-ready solution with API, database schema, and operational components.
Base URL: /api/v1
Authentication: Bearer Token or mTLS
Artifact Management
http
GET /artifacts # List with filters
GET /artifacts/{id} # Get artifact details
GET /artifacts/name/{name}/version/{version} # Get by name/version
POST /artifacts # Register new artifact
PUT /artifacts/{id}/deprecate # Deprecate artifact
Dependency Queries
http
GET /artifacts/{id}/dependencies # Direct dependencies
GET /artifacts/{id}/dependencies/transitive # Full dependency tree
GET /artifacts/{id}/dependents # What depends on this
GET /dependencies/cycle-check # Detect dependency cycles
Provenance & SBOM
http
GET /artifacts/{id}/provenance # Full attestation
GET /artifacts/{id}/sbom # SPDX/SBOM document
GET /artifacts/{id}/attestation # Sigstore bundle
GET /artifacts/{id}/scan-results # Security scan results
Rollback Management
http
GET /artifacts/{id}/rollback-procedures # Available rollback strategies
POST /artifacts/{id}/rollback # Execute rollback
GET /rollback/{rollback_id}/status # Check rollback status
POST /rollback/{rollback_id}/approve # Approve rollback (if required)
Deployment Tracking
http
GET /deployments?environment=prod&status=failed # Failed deployments
GET /deployments/{deployment_id}/rollback-info # Rollback info for deployment
POST /deployments/{deployment_id}/trigger-rollback # Trigger rollback
-- Find artifacts with vulnerable dependencies
SELECT
a.name,
a.version,
jsonb_array_length(a.scan_results->'vulnerabilities') as vuln_count,
array_agg(d.external_name) as vulnerable_deps
FROM artifacts a
JOIN artifact_dependencies ad ON a.id = ad.artifact_id
JOIN external_dependencies d ON ad.external_dependency_id = d.id
WHERE a.scan_results->'vulnerabilities' IS NOT NULL
GROUP BY a.id
HAVING jsonb_array_length(a.scan_results->'vulnerabilities') > 0
ORDER BY vuln_count DESC;
Sharding: Shard by artifact name prefix or environment
Caching: Redis cache for frequently accessed dependency trees
Read Replicas: For reporting and dependency resolution queries
Event Streaming: Kafka/PubSub for async provenance processing
CDN: For artifact downloads (S3 + CloudFront)
Database Partitioning: Partition deployment_history by month
This system provides:
✅ Complete provenance with SBOM and attestations
✅ Dependency management with cycle detection and transitive tracking
✅ Automated rollback with approval workflows
✅ Security integration with vulnerability scanning
✅ Audit compliance with full history
✅ High availability with replication and failover
✅ Scalability for thousands of artifacts and dependencies
The catalog becomes the single source of truth for all deployment artifacts, enabling secure, auditable, and reliable deployments with instant rollback capabilities.
-- Core artifacts table
CREATE TABLE artifacts (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name VARCHAR(255) NOT NULL,
version VARCHAR(100) NOT NULL,
type VARCHAR(50) NOT NULL, -- 'container', 'jar', 'binary', 'script', 'config'
build_id VARCHAR(255) NOT NULL,
build_number INTEGER NOT NULL,
environment VARCHAR(50) NOT NULL, -- 'dev', 'staging', 'prod'
status VARCHAR(50) DEFAULT 'active', -- 'active', 'deprecated', 'failed'
-- Provenance metadata
source_repository_url VARCHAR(500),
source_commit_sha CHAR(40),
source_branch VARCHAR(100),
ci_pipeline_id VARCHAR(255),
ci_job_id VARCHAR(255),
built_at TIMESTAMP NOT NULL DEFAULT NOW(),
built_by VARCHAR(255),
-- Artifact location
storage_backend VARCHAR(50) NOT NULL, -- 's3', 'gcs', 'azure', 'nexus', 'docker'
storage_path VARCHAR(1000) NOT NULL,
checksum_algorithm VARCHAR(20) DEFAULT 'sha256',
checksum VARCHAR(255) NOT NULL,
size_bytes BIGINT,
-- Deployment metadata
deployed_to_environment VARCHAR(50),
deployed_at TIMESTAMP,
deployed_by VARCHAR(255),
deployment_id VARCHAR(255),
-- Lifecycle
created_at TIMESTAMP DEFAULT NOW(),
updated_at TIMESTAMP DEFAULT NOW(),
UNIQUE(name, version, environment),
INDEX idx_artifacts_name_version (name, version),
INDEX idx_artifacts_build (build_id, build_number),
INDEX idx_artifacts_commit (source_commit_sha)
);
-- Dependency relationships (DAG)
CREATE TABLE artifact_dependencies (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
artifact_id UUID NOT NULL REFERENCES artifacts(id) ON DELETE CASCADE,
depends_on_artifact_id UUID NOT NULL REFERENCES artifacts(id) ON DELETE CASCADE,
dependency_type VARCHAR(50) NOT NULL, -- 'runtime', 'build', 'test', 'optional'
version_constraint VARCHAR(100), -- '>=1.2.0', '^2.0.0', etc.
scope VARCHAR(50), -- 'compile', 'provided', 'runtime', 'test'
-- External dependencies not in catalog
external_name VARCHAR(255),
external_version VARCHAR(100),
external_source VARCHAR(100), -- 'maven', 'npm', 'pypi', 'apt'
created_at TIMESTAMP DEFAULT NOW(),
UNIQUE(artifact_id, depends_on_artifact_id, dependency_type),
INDEX idx_dependencies_artifact (artifact_id),
INDEX idx_dependencies_depends_on (depends_on_artifact_id)
);
-- Rollback procedures
CREATE TABLE rollback_procedures (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
artifact_id UUID NOT NULL REFERENCES artifacts(id) ON DELETE CASCADE,
target_artifact_id UUID REFERENCES artifacts(id),
-- Rollback strategy
strategy VARCHAR(50) NOT NULL, -- 'blue-green', 'canary', 'revert', 'restore-backup'
pre_checks_script TEXT,
rollback_script TEXT NOT NULL,
post_checks_script TEXT,
-- Rollback metadata
rollback_conditions JSONB, -- Conditions that trigger rollback
max_rollback_time_seconds INTEGER DEFAULT 300,
requires_approval BOOLEAN DEFAULT true,
approved_by VARCHAR(255),
approved_at TIMESTAMP,
created_at TIMESTAMP DEFAULT NOW(),
updated_at TIMESTAMP DEFAULT NOW(),
UNIQUE(artifact_id, strategy),
INDEX idx_rollback_artifact (artifact_id)
);
-- Provenance records (full SBOM/attestation)
CREATE TABLE provenance_records (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
artifact_id UUID NOT NULL REFERENCES artifacts(id) ON DELETE CASCADE,
-- Build attestation
build_attestation JSONB NOT NULL, -- Sigstore, in-toto, etc.
reproducibility_verified BOOLEAN DEFAULT false,
scan_results JSONB, -- Security scan results
-- Supply chain
supplier_name VARCHAR(255),
supplier_contact VARCHAR(255),
license_expression TEXT,
created_at TIMESTAMP DEFAULT NOW()
);
-- Deployment history with rollback tracking
CREATE TABLE deployment_history (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
artifact_id UUID NOT NULL REFERENCES artifacts(id),
environment VARCHAR(50) NOT NULL,
-- Deployment details
deployment_id VARCHAR(255) NOT NULL,
deployment_strategy VARCHAR(50) NOT NULL,
deployment_status VARCHAR(50) NOT NULL, -- 'success', 'failed', 'rolled_back'
-- Rollback tracking
rollback_id UUID REFERENCES rollback_procedures(id),
rollback_status VARCHAR(50),
rollback_triggered_at TIMESTAMP,
rollback_completed_at TIMESTAMP,
rollback_reason TEXT,
deployed_at TIMESTAMP NOT NULL DEFAULT NOW(),
completed_at TIMESTAMP,
INDEX idx_deployments_artifact_env (artifact_id, environment),
INDEX idx_deployments_status (deployment_status, environment)
);
-- Tags for categorization
CREATE TABLE artifact_tags (
artifact_id UUID REFERENCES artifacts(id) ON DELETE CASCADE,
tag VARCHAR(100) NOT NULL,
created_at TIMESTAMP DEFAULT NOW(),
PRIMARY KEY(artifact_id, tag)
);