@@ -13,11 +13,9 @@ use chrono::{DateTime, Utc};
1313use diesel:: { deserialize:: FromSql , serialize:: ToSql , sql_types:: Text } ;
1414use omicron_common:: {
1515 api:: external,
16- update:: {
17- ArtifactHash as ExternalArtifactHash , ArtifactId as ExternalArtifactId ,
18- ArtifactKind ,
19- } ,
16+ update:: { ArtifactHash as ExternalArtifactHash , ArtifactId , ArtifactKind } ,
2017} ;
18+ use omicron_uuid_kinds:: TufArtifactKind ;
2119use omicron_uuid_kinds:: TufRepoKind ;
2220use omicron_uuid_kinds:: TypedUuid ;
2321use serde:: { Deserialize , Serialize } ;
@@ -148,8 +146,10 @@ impl TufRepo {
148146#[ derive( Queryable , Insertable , Clone , Debug , Selectable , AsChangeset ) ]
149147#[ diesel( table_name = tuf_artifact) ]
150148pub struct TufArtifact {
151- #[ diesel( embed) ]
152- pub id : ArtifactId ,
149+ pub id : DbTypedUuid < TufArtifactKind > ,
150+ pub name : String ,
151+ pub version : SemverVersion ,
152+ pub kind : String ,
153153 pub time_created : DateTime < Utc > ,
154154 pub sha256 : ArtifactHash ,
155155 artifact_size : i64 ,
@@ -158,12 +158,15 @@ pub struct TufArtifact {
158158impl TufArtifact {
159159 /// Creates a new `TufArtifact` ready for insertion.
160160 pub fn new (
161- id : ArtifactId ,
161+ artifact_id : ArtifactId ,
162162 sha256 : ArtifactHash ,
163163 artifact_size : u64 ,
164164 ) -> Self {
165165 Self {
166- id,
166+ id : TypedUuid :: new_v4 ( ) . into ( ) ,
167+ name : artifact_id. name ,
168+ version : artifact_id. version . into ( ) ,
169+ kind : artifact_id. kind . as_str ( ) . to_owned ( ) ,
167170 time_created : Utc :: now ( ) ,
168171 sha256,
169172 artifact_size : artifact_size as i64 ,
@@ -177,21 +180,31 @@ impl TufArtifact {
177180 /// as part of the process, which `From` doesn't necessarily communicate
178181 /// and can be surprising.
179182 pub fn from_external ( artifact : external:: TufArtifactMeta ) -> Self {
180- Self :: new ( artifact. id . into ( ) , artifact. hash . into ( ) , artifact. size )
183+ Self :: new ( artifact. id , artifact. hash . into ( ) , artifact. size )
181184 }
182185
183186 /// Converts self into [`external::TufArtifactMeta`].
184187 pub fn into_external ( self ) -> external:: TufArtifactMeta {
185188 external:: TufArtifactMeta {
186- id : self . id . into ( ) ,
189+ id : ArtifactId {
190+ name : self . name ,
191+ version : self . version . into ( ) ,
192+ kind : ArtifactKind :: new ( self . kind ) ,
193+ } ,
187194 hash : self . sha256 . into ( ) ,
188195 size : self . artifact_size as u64 ,
189196 }
190197 }
191198
192199 /// Returns the artifact's ID.
193- pub fn id ( & self ) -> ( String , SemverVersion , String ) {
194- ( self . id . name . clone ( ) , self . id . version . clone ( ) , self . id . kind . clone ( ) )
200+ pub fn id ( & self ) -> TypedUuid < TufArtifactKind > {
201+ self . id . into ( )
202+ }
203+
204+ /// Returns the artifact's name, version, and kind, which is unique across
205+ /// all artifacts.
206+ pub fn nvk ( & self ) -> ( & str , & SemverVersion , & str ) {
207+ ( & self . name , & self . version , & self . kind )
195208 }
196209
197210 /// Returns the artifact length in bytes.
@@ -200,70 +213,12 @@ impl TufArtifact {
200213 }
201214}
202215
203- /// The ID (primary key) of a [`TufArtifact`].
204- ///
205- /// This is the internal variant of a [`ExternalArtifactId`].
206- #[ derive(
207- Queryable ,
208- Insertable ,
209- Clone ,
210- Debug ,
211- Selectable ,
212- PartialEq ,
213- Eq ,
214- Hash ,
215- Deserialize ,
216- Serialize ,
217- ) ]
218- #[ diesel( table_name = tuf_artifact) ]
219- pub struct ArtifactId {
220- pub name : String ,
221- pub version : SemverVersion ,
222- pub kind : String ,
223- }
224-
225- impl From < ExternalArtifactId > for ArtifactId {
226- fn from ( id : ExternalArtifactId ) -> Self {
227- Self {
228- name : id. name ,
229- version : id. version . into ( ) ,
230- kind : id. kind . as_str ( ) . to_owned ( ) ,
231- }
232- }
233- }
234-
235- impl From < ArtifactId > for ExternalArtifactId {
236- fn from ( id : ArtifactId ) -> Self {
237- Self {
238- name : id. name ,
239- version : id. version . into ( ) ,
240- kind : ArtifactKind :: new ( id. kind ) ,
241- }
242- }
243- }
244-
245- impl fmt:: Display for ArtifactId {
246- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
247- // This is the same as ExternalArtifactId's Display impl.
248- write ! ( f, "{} v{} ({})" , self . name, self . version, self . kind)
249- }
250- }
251-
252- /// Required by the authz_resource macro.
253- impl From < ArtifactId > for ( String , SemverVersion , String ) {
254- fn from ( id : ArtifactId ) -> Self {
255- ( id. name , id. version , id. kind )
256- }
257- }
258-
259216/// A many-to-many relationship between [`TufRepo`] and [`TufArtifact`].
260217#[ derive( Queryable , Insertable , Clone , Debug , Selectable ) ]
261218#[ diesel( table_name = tuf_repo_artifact) ]
262219pub struct TufRepoArtifact {
263220 pub tuf_repo_id : Uuid ,
264- pub tuf_artifact_name : String ,
265- pub tuf_artifact_version : SemverVersion ,
266- pub tuf_artifact_kind : String ,
221+ pub tuf_artifact_id : Uuid ,
267222}
268223
269224/// A wrapper around omicron-common's [`ArtifactHash`](ExternalArtifactHash),
0 commit comments