This commit is contained in:
2026-04-23 17:18:15 +02:00
parent 3b8e029cde
commit 6d00c0ddf4
14 changed files with 545 additions and 65 deletions

View File

@@ -3,7 +3,9 @@
//! Database schema initialization.
/// Ensures that the database schema exists.
pub(crate) async fn ensure_schema(database: &crate::KbDatabase) -> Result<(), crate::KbError> {
pub(crate) async fn ensure_schema(
database: &crate::KbDatabase,
) -> Result<(), crate::KbError> {
match database.connection() {
crate::KbDatabaseConnection::Sqlite(pool) => {
let metadata_table_result = sqlx::query(
@@ -99,6 +101,58 @@ ON kb_db_runtime_events (created_at)
error
)));
}
let observed_tokens_result = sqlx::query(
r#"
CREATE TABLE IF NOT EXISTS kb_observed_tokens (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
mint TEXT NOT NULL UNIQUE,
symbol TEXT NULL,
name TEXT NULL,
decimals INTEGER NULL,
token_program TEXT NOT NULL,
status INTEGER NOT NULL,
first_seen_at TEXT NOT NULL,
last_seen_at TEXT NOT NULL,
updated_at TEXT NOT NULL
)
"#,
)
.execute(pool)
.await;
if let Err(error) = observed_tokens_result {
return Err(crate::KbError::Db(format!(
"cannot create table kb_observed_tokens on sqlite: {}",
error
)));
}
let observed_tokens_mint_index_result = sqlx::query(
r#"
CREATE UNIQUE INDEX IF NOT EXISTS kb_idx_observed_tokens_mint
ON kb_observed_tokens (mint)
"#,
)
.execute(pool)
.await;
if let Err(error) = observed_tokens_mint_index_result {
return Err(crate::KbError::Db(format!(
"cannot create index kb_idx_observed_tokens_mint on sqlite: {}",
error
)));
}
let observed_tokens_status_index_result = sqlx::query(
r#"
CREATE INDEX IF NOT EXISTS kb_idx_observed_tokens_status
ON kb_observed_tokens (status)
"#,
)
.execute(pool)
.await;
if let Err(error) = observed_tokens_status_index_result {
return Err(crate::KbError::Db(format!(
"cannot create index kb_idx_observed_tokens_status on sqlite: {}",
error
)));
}
let schema_version = crate::KbDbMetadataDto::new(
"schema_version".to_string(),
env!("CARGO_PKG_VERSION").to_string(),
@@ -108,6 +162,6 @@ ON kb_db_runtime_events (created_at)
return Err(error);
}
Ok(())
}
},
}
}