import albedo_sqlite import albedo_sqlite/marmot_script import albedo_sqlite/template import gleam/dynamic/decode import gleam/option.{type Option} import sqlight /// Shared return type for queries in this directory: /// src/albedo_ext_artifacts/ledger/sql/get.sql /// src/albedo_ext_artifacts/ledger/sql/page.sql pub type ArtifactRow { ArtifactRow( id: String, key: String, title: String, version: Int, versions: Int, entry: String, session: String, workspace: String, created_at: Int, updated_at: Int, bytes: Int, ) } fn artifact_row_decoder() -> decode.Decoder(ArtifactRow) { use id <- decode.field(0, decode.string) use key <- decode.field(1, decode.string) use title <- decode.field(2, decode.string) use version <- decode.field(3, decode.int) use versions <- decode.field(4, decode.int) use entry <- decode.field(5, decode.string) use session <- decode.field(6, decode.string) use workspace <- decode.field(7, decode.string) use created_at <- decode.field(8, decode.int) use updated_at <- decode.field(9, decode.int) use bytes <- decode.field(10, decode.int) decode.success(ArtifactRow( id:, key:, title:, version:, versions:, entry:, session:, workspace:, created_at:, updated_at:, bytes:, )) } /// Generated from src/albedo_ext_artifacts/ledger/sql/advance_version.sql pub fn advance_version( db db: sqlight.Connection, version version: Int, title title: Option(String), now now: Int, id id: String, ) -> Result(Nil, String) { albedo_sqlite.run( "UPDATE artifacts SET version=@version,title=COALESCE(@title,title),updated_at=@now WHERE id=@id", on: db, with: [ sqlight.int(version), sqlight.nullable(sqlight.text, title), sqlight.int(now), sqlight.text(id), ], ) } /// Generated from src/albedo_ext_artifacts/ledger/sql/data.sql pub fn data( db db: sqlight.Connection, hash hash: String, ) -> Result(List(BitArray), String) { albedo_sqlite.rows( "SELECT data FROM artifact_files WHERE hash=@hash", on: db, with: [sqlight.text(hash)], expecting: decode.field(0, decode.bit_array, decode.success), ) } /// Generated from src/albedo_ext_artifacts/ledger/sql/delete_artifact.sql pub fn delete_artifact( db db: sqlight.Connection, id id: String, ) -> Result(Nil, String) { albedo_sqlite.run("DELETE FROM artifacts WHERE id=@id", on: db, with: [ sqlight.text(id), ]) } /// Generated from src/albedo_ext_artifacts/ledger/sql/delete_expired_uploads.sql pub fn delete_expired_uploads( db db: sqlight.Connection, cutoff cutoff: Int, ) -> Result(Nil, String) { albedo_sqlite.run( "DELETE FROM artifact_uploads WHERE at<=@cutoff", on: db, with: [sqlight.int(cutoff)], ) } /// Generated from src/albedo_ext_artifacts/ledger/sql/delete_failures.sql pub fn delete_failures( db db: sqlight.Connection, id id: String, ) -> Result(Nil, String) { albedo_sqlite.run( "DELETE FROM artifact_errors WHERE artifact=@id", on: db, with: [sqlight.text(id)], ) } /// Generated from src/albedo_ext_artifacts/ledger/sql/delete_state.sql pub fn delete_state( db db: sqlight.Connection, id id: String, ) -> Result(Nil, String) { albedo_sqlite.run( "DELETE FROM artifact_state WHERE artifact=@id", on: db, with: [sqlight.text(id)], ) } /// Generated from src/albedo_ext_artifacts/ledger/sql/delete_unreferenced_files.sql pub fn delete_unreferenced_files( db db: sqlight.Connection, ) -> Result(Nil, String) { albedo_sqlite.run( "DELETE FROM artifact_files WHERE hash NOT IN (SELECT json_extract(m.value,'$.hash') FROM artifact_versions v, json_each(v.manifest) m) AND hash NOT IN (SELECT hash FROM artifact_uploads)", on: db, with: [], ) } /// Generated from src/albedo_ext_artifacts/ledger/sql/delete_versions.sql pub fn delete_versions( db db: sqlight.Connection, id id: String, ) -> Result(Nil, String) { albedo_sqlite.run( "DELETE FROM artifact_versions WHERE artifact=@id", on: db, with: [sqlight.text(id)], ) } /// Generated from src/albedo_ext_artifacts/ledger/sql/failures.sql pub type FailuresRow { FailuresRow( version: Int, at: Int, message: String, source: String, line: Int, column: Int, stack: String, ) } /// Generated from src/albedo_ext_artifacts/ledger/sql/failures.sql pub fn failures( db db: sqlight.Connection, id id: String, ) -> Result(List(FailuresRow), String) { albedo_sqlite.rows( "SELECT version,at,message,source,line,column,stack FROM artifact_errors WHERE artifact=@id ORDER BY id", on: db, with: [sqlight.text(id)], expecting: { use version <- decode.field(0, decode.int) use at <- decode.field(1, decode.int) use message <- decode.field(2, decode.string) use source <- decode.field(3, decode.string) use line <- decode.field(4, decode.int) use column <- decode.field(5, decode.int) use stack <- decode.field(6, decode.string) decode.success(FailuresRow( version:, at:, message:, source:, line:, column:, stack:, )) }, ) } /// Generated from src/albedo_ext_artifacts/ledger/sql/get.sql pub fn get( db db: sqlight.Connection, id id: String, ) -> Result(List(ArtifactRow), String) { albedo_sqlite.rows( "SELECT a.id AS id,a.key,a.title,a.version,CAST((SELECT count(*) FROM artifact_versions c WHERE c.artifact=a.id) AS INTEGER) AS versions,v.entry,a.session,a.workspace,a.created_at,a.updated_at,v.bytes FROM artifacts a JOIN artifact_versions v ON v.artifact=a.id AND v.version=a.version WHERE a.id=@id", on: db, with: [sqlight.text(id)], expecting: artifact_row_decoder(), ) } /// Generated from src/albedo_ext_artifacts/ledger/sql/initialise_schema.sql pub fn initialise_schema(db db: sqlight.Connection) -> Result(Nil, String) { albedo_sqlite.exec( marmot_script.source( "src/albedo_ext_artifacts/ledger/sql/initialise_schema.sql", "CREATE TABLE IF NOT EXISTS artifacts (\n id TEXT PRIMARY KEY,\n key TEXT NOT NULL,\n session TEXT NOT NULL,\n workspace TEXT NOT NULL,\n title TEXT NOT NULL,\n version INTEGER NOT NULL,\n created_at INTEGER NOT NULL,\n updated_at INTEGER NOT NULL\n);\nCREATE INDEX IF NOT EXISTS artifacts_by_change ON artifacts(updated_at DESC, id DESC);\nCREATE INDEX IF NOT EXISTS artifacts_by_session ON artifacts(session, updated_at DESC);\nCREATE TABLE IF NOT EXISTS artifact_versions (\n artifact TEXT NOT NULL,\n version INTEGER NOT NULL,\n entry TEXT NOT NULL,\n manifest TEXT NOT NULL,\n bytes INTEGER NOT NULL,\n cell TEXT,\n created_at INTEGER NOT NULL,\n PRIMARY KEY (artifact, version)\n);\nCREATE TABLE IF NOT EXISTS artifact_files (\n hash TEXT PRIMARY KEY,\n size INTEGER NOT NULL,\n data BLOB NOT NULL\n);\nCREATE TABLE IF NOT EXISTS artifact_uploads (\n hash TEXT PRIMARY KEY,\n at INTEGER NOT NULL\n);\nCREATE TABLE IF NOT EXISTS artifact_state (\n artifact TEXT PRIMARY KEY,\n state TEXT NOT NULL,\n updated_at INTEGER NOT NULL\n);\nCREATE TABLE IF NOT EXISTS artifact_errors (\n id INTEGER PRIMARY KEY AUTOINCREMENT,\n artifact TEXT NOT NULL,\n version INTEGER NOT NULL,\n at INTEGER NOT NULL,\n message TEXT NOT NULL,\n source TEXT NOT NULL,\n line INTEGER NOT NULL,\n column INTEGER NOT NULL,\n stack TEXT NOT NULL\n);\nCREATE INDEX IF NOT EXISTS artifact_errors_by_artifact ON artifact_errors(artifact, id);", ), on: db, ) } /// Generated from src/albedo_ext_artifacts/ledger/sql/insert_artifact.sql pub fn insert_artifact( db db: sqlight.Connection, id id: Option(String), key key: String, session session: String, workspace workspace: String, title title: String, now now: Int, ) -> Result(Nil, String) { albedo_sqlite.run( "INSERT INTO artifacts(id,key,session,workspace,title,version,created_at,updated_at) VALUES(@id,@key,@session,@workspace,@title,1,@now,@now)", on: db, with: [ sqlight.nullable(sqlight.text, id), sqlight.text(key), sqlight.text(session), sqlight.text(workspace), sqlight.text(title), sqlight.int(now), ], ) } /// Generated from src/albedo_ext_artifacts/ledger/sql/insert_failure_report.sql pub fn insert_failure_report( db db: sqlight.Connection, id id: String, version version: Int, at at: Int, message message: String, source source: String, line line: Int, column column: Int, stack stack: String, ) -> Result(Nil, String) { albedo_sqlite.run( "INSERT INTO artifact_errors(artifact,version,at,message,source,line,column,stack) VALUES(@id,@version,@at,@message,@source,@line,@column,@stack)", on: db, with: [ sqlight.text(id), sqlight.int(version), sqlight.int(at), sqlight.text(message), sqlight.text(source), sqlight.int(line), sqlight.int(column), sqlight.text(stack), ], ) } /// Generated from src/albedo_ext_artifacts/ledger/sql/insert_version.sql pub fn insert_version( db db: sqlight.Connection, id id: String, version version: Int, entry entry: String, manifest manifest: String, bytes bytes: Int, cell cell: Option(String), now now: Int, ) -> Result(Nil, String) { albedo_sqlite.run( "INSERT INTO artifact_versions(artifact,version,entry,manifest,bytes,cell,created_at) VALUES(@id,@version,@entry,@manifest,@bytes,@cell,@now)", on: db, with: [ sqlight.text(id), sqlight.int(version), sqlight.text(entry), sqlight.text(manifest), sqlight.int(bytes), sqlight.nullable(sqlight.text, cell), sqlight.int(now), ], ) } /// Generated from src/albedo_ext_artifacts/ledger/sql/latest.sql pub fn latest( db db: sqlight.Connection, id id: String, ) -> Result(List(Int), String) { albedo_sqlite.rows( "SELECT version FROM artifacts WHERE id=@id", on: db, with: [sqlight.text(id)], expecting: decode.field(0, decode.int, decode.success), ) } /// Generated from src/albedo_ext_artifacts/ledger/sql/mark_upload.sql pub fn mark_upload( db db: sqlight.Connection, hash hash: String, now now: Int, ) -> Result(Nil, String) { albedo_sqlite.run( "INSERT INTO artifact_uploads(hash,at) VALUES(@hash,@now) ON CONFLICT(hash) DO UPDATE SET at=excluded.at", on: db, with: [sqlight.text(hash), sqlight.int(now)], ) } /// Generated from src/albedo_ext_artifacts/ledger/sql/missing.sql pub fn missing( db db: sqlight.Connection, hash hash: String, ) -> Result(List(String), String) { albedo_sqlite.rows( "SELECT hash AS hash FROM artifact_files WHERE hash=@hash", on: db, with: [sqlight.text(hash)], expecting: decode.field(0, decode.string, decode.success), ) } /// Generated from src/albedo_ext_artifacts/ledger/sql/page.sql pub fn page( db db: sqlight.Connection, predicates predicates: List(template.Predicate), limit limit: Int, ) -> Result(List(ArtifactRow), String) { albedo_sqlite.rows( template.predicates( "SELECT a.id AS id,a.key,a.title,a.version,CAST((SELECT count(*) FROM artifact_versions c WHERE c.artifact=a.id) AS INTEGER) AS versions,v.entry,a.session,a.workspace,a.created_at,a.updated_at AS updated_at,v.bytes FROM artifacts a JOIN artifact_versions v ON v.artifact=a.id AND v.version=a.version WHERE 1", " ORDER BY a.updated_at DESC,a.id DESC LIMIT @limit", predicates, ), on: db, with: template.bindings(predicates, then: [sqlight.int(limit)]), expecting: artifact_row_decoder(), ) } /// Generated from src/albedo_ext_artifacts/ledger/sql/page_cursor_predicate.sql pub const page_cursor_predicate = "AND (a.updated_at,a.id)<(?,?)" /// Generated from src/albedo_ext_artifacts/ledger/sql/page_session_predicate.sql pub const page_session_predicate = "AND a.session=?" /// Generated from src/albedo_ext_artifacts/ledger/sql/prune_failure_reports.sql pub fn prune_failure_reports( db db: sqlight.Connection, id id: String, kept_failures kept_failures: Int, ) -> Result(Nil, String) { albedo_sqlite.run( "DELETE FROM artifact_errors WHERE artifact=@id AND id NOT IN (SELECT id FROM artifact_errors WHERE artifact=@id ORDER BY id DESC LIMIT @kept_failures)", on: db, with: [sqlight.text(id), sqlight.int(kept_failures)], ) } /// Generated from src/albedo_ext_artifacts/ledger/sql/put.sql pub fn put( db db: sqlight.Connection, expected expected: Option(String), size size: Int, data data: BitArray, ) -> Result(Nil, String) { albedo_sqlite.run( "INSERT OR IGNORE INTO artifact_files(hash,size,data) VALUES(@expected,@size,@data)", on: db, with: [ sqlight.nullable(sqlight.text, expected), sqlight.int(size), sqlight.blob(data), ], ) } /// Generated from src/albedo_ext_artifacts/ledger/sql/read_current_version_entry.sql pub type ReadCurrentVersionEntryRow { ReadCurrentVersionEntryRow(version: Int, entry: String) } /// Generated from src/albedo_ext_artifacts/ledger/sql/read_current_version_entry.sql pub fn read_current_version_entry( db db: sqlight.Connection, id id: String, ) -> Result(List(ReadCurrentVersionEntryRow), String) { albedo_sqlite.rows( "SELECT a.version,v.entry FROM artifacts a JOIN artifact_versions v ON v.artifact=a.id AND v.version=a.version WHERE a.id=@id", on: db, with: [sqlight.text(id)], expecting: { use version <- decode.field(0, decode.int) use entry <- decode.field(1, decode.string) decode.success(ReadCurrentVersionEntryRow(version:, entry:)) }, ) } /// Generated from src/albedo_ext_artifacts/ledger/sql/rename_artifact.sql pub fn rename_artifact( db db: sqlight.Connection, title title: String, now now: Int, id id: String, ) -> Result(Nil, String) { albedo_sqlite.run( "UPDATE artifacts SET title=@title,updated_at=@now WHERE id=@id", on: db, with: [sqlight.text(title), sqlight.int(now), sqlight.text(id)], ) } /// Generated from src/albedo_ext_artifacts/ledger/sql/set_state.sql pub fn set_state( db db: sqlight.Connection, id id: Option(String), text text: String, updated_at updated_at: Int, ) -> Result(Nil, String) { albedo_sqlite.run( "INSERT INTO artifact_state(artifact,state,updated_at) VALUES(@id,@text,@updated_at) ON CONFLICT(artifact) DO UPDATE SET state=excluded.state,updated_at=excluded.updated_at", on: db, with: [ sqlight.nullable(sqlight.text, id), sqlight.text(text), sqlight.int(updated_at), ], ) } /// Generated from src/albedo_ext_artifacts/ledger/sql/state.sql pub fn state( db db: sqlight.Connection, id id: String, ) -> Result(List(String), String) { albedo_sqlite.rows( "SELECT state FROM artifact_state WHERE artifact=@id", on: db, with: [sqlight.text(id)], expecting: decode.field(0, decode.string, decode.success), ) } /// Generated from src/albedo_ext_artifacts/ledger/sql/stored.sql pub fn stored( db db: sqlight.Connection, hash hash: String, ) -> Result(List(Int), String) { albedo_sqlite.rows( "SELECT size FROM artifact_files WHERE hash=@hash", on: db, with: [sqlight.text(hash)], expecting: decode.field(0, decode.int, decode.success), ) }