Shorten crate directory names and update workspace paths
Verify databases and deploy Aster VM / verify-and-deploy (push) Successful in 14m53s

This commit is contained in:
2026-10-18 19:57:21 +08:00
parent 891defb34c
commit 50e8fcb5e7
24 changed files with 15 additions and 15 deletions
+16
View File
@@ -0,0 +1,16 @@
[package]
name = "aster-storage"
version.workspace = true
edition.workspace = true
rust-version.workspace = true
license.workspace = true
[dependencies]
sqlx.workspace = true
tokio.workspace = true
[dev-dependencies]
tempfile = "3"
[lints]
workspace = true
+249
View File
@@ -0,0 +1,249 @@
//! Host metadata storage; deliberately has no dependency on aster-core.
use std::fmt;
use std::path::PathBuf;
use std::str::FromStr;
use std::time::Duration;
use sqlx::postgres::{PgConnectOptions, PgPoolOptions};
use sqlx::sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePoolOptions};
use sqlx::{PgPool, SqlitePool};
const SCHEMA: &str = include_str!("schema.sql");
/// An explicit PostgreSQL configuration never falls back to SQLite.
#[derive(Clone)]
pub enum StorageConfig {
SqliteFile(PathBuf),
SqliteMemory,
Postgres(String),
}
impl fmt::Debug for StorageConfig {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::SqliteFile(path) => f.debug_tuple("SqliteFile").field(path).finish(),
Self::SqliteMemory => f.write_str("SqliteMemory"),
Self::Postgres(_) => f.write_str("Postgres([REDACTED])"),
}
}
}
impl StorageConfig {
/// DATABASE_URL accepts PostgreSQL only; SQLITE_PATH selects file or :memory:.
/// Empty/invalid explicit values are errors rather than default selection.
pub fn from_values(
database_url: Option<&str>,
sqlite_path: Option<&str>,
) -> Result<Self, StorageError> {
if let Some(url) = database_url {
if !(url.starts_with("postgres://") || url.starts_with("postgresql://")) {
return Err(StorageError::Config(
"DATABASE_URL must be a PostgreSQL URL",
));
}
return Ok(Self::Postgres(url.into()));
}
match sqlite_path.unwrap_or("data/aster.db") {
"" => Err(StorageError::Config("SQLITE_PATH cannot be empty")),
":memory:" => Ok(Self::SqliteMemory),
path => Ok(Self::SqliteFile(path.into())),
}
}
pub fn from_env() -> Result<Self, StorageError> {
let database_url = read_env("DATABASE_URL")?;
// Irrelevant SQLite settings must not prevent PostgreSQL configuration.
if database_url.is_some() {
return Self::from_values(database_url.as_deref(), None);
}
let sqlite_path = read_env("SQLITE_PATH")?;
Self::from_values(None, sqlite_path.as_deref())
}
}
fn read_env(name: &str) -> Result<Option<String>, StorageError> {
match std::env::var(name) {
Ok(value) => Ok(Some(value)),
Err(std::env::VarError::NotPresent) => Ok(None),
Err(std::env::VarError::NotUnicode(_)) => Err(StorageError::Config(
"storage environment must be valid Unicode",
)),
}
}
#[derive(Debug)]
pub enum StorageError {
Config(&'static str),
Directory(std::io::Error),
Database(sqlx::Error),
}
impl fmt::Display for StorageError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Config(message) => f.write_str(message),
Self::Directory(_) => f.write_str("cannot create SQLite parent directory"),
// CLI/HTTP logs must never print a connection URL or credentials.
Self::Database(_) => {
f.write_str("database operation failed; no fallback was attempted")
}
}
}
}
impl std::error::Error for StorageError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Config(_) => None,
Self::Directory(error) => Some(error),
Self::Database(error) => Some(error),
}
}
}
impl From<sqlx::Error> for StorageError {
fn from(error: sqlx::Error) -> Self {
Self::Database(error)
}
}
#[derive(Clone)]
enum Backend {
Sqlite(SqlitePool),
Postgres(PgPool),
}
/// Clones share a pool. A memory SQLite store uses exactly one connection, so
/// initialization and every operation see the same database for its lifetime.
#[derive(Clone)]
pub struct Storage {
backend: Backend,
}
impl Storage {
/// Connects eagerly and initializes the v1 metadata schema before returning.
pub async fn connect(config: &StorageConfig) -> Result<Self, StorageError> {
let backend = match config {
StorageConfig::Postgres(url) => {
let options = PgConnectOptions::from_str(url)
.map_err(|_| StorageError::Config("invalid PostgreSQL connection URL"))?;
Backend::Postgres(
PgPoolOptions::new()
.max_connections(5)
.acquire_timeout(Duration::from_secs(5))
.connect_with(options)
.await?,
)
}
StorageConfig::SqliteFile(path) => {
if path.as_os_str().is_empty() || path == std::path::Path::new(":memory:") {
return Err(StorageError::Config(
"use SqliteMemory for an in-memory database",
));
}
if let Some(parent) = path.parent().filter(|p| !p.as_os_str().is_empty()) {
tokio::fs::create_dir_all(parent)
.await
.map_err(StorageError::Directory)?;
}
let options = SqliteConnectOptions::new()
.filename(path)
.create_if_missing(true)
.journal_mode(SqliteJournalMode::Wal)
.busy_timeout(Duration::from_secs(5));
Backend::Sqlite(
SqlitePoolOptions::new()
.max_connections(5)
.acquire_timeout(Duration::from_secs(5))
.connect_with(options)
.await?,
)
}
StorageConfig::SqliteMemory => {
let options = SqliteConnectOptions::new().in_memory(true);
Backend::Sqlite(
SqlitePoolOptions::new()
.max_connections(1)
.min_connections(1)
.idle_timeout(None)
.max_lifetime(None)
.connect_with(options)
.await?,
)
}
};
let storage = Self { backend };
storage.initialize().await?;
Ok(storage)
}
async fn initialize(&self) -> Result<(), StorageError> {
match &self.backend {
Backend::Sqlite(pool) => {
sqlx::query(SCHEMA).execute(pool).await?;
}
Backend::Postgres(pool) => {
sqlx::query(SCHEMA).execute(pool).await?;
}
}
Ok(())
}
pub fn backend_name(&self) -> &'static str {
match &self.backend {
Backend::Sqlite(_) => "sqlite",
Backend::Postgres(_) => "postgresql",
}
}
pub async fn ping(&self) -> Result<(), StorageError> {
match &self.backend {
Backend::Sqlite(pool) => {
sqlx::query("SELECT 1").execute(pool).await?;
}
Backend::Postgres(pool) => {
sqlx::query("SELECT 1").execute(pool).await?;
}
}
Ok(())
}
pub async fn put(&self, key: &str, value: &str) -> Result<(), StorageError> {
match &self.backend {
Backend::Sqlite(pool) => {
sqlx::query("INSERT INTO aster_metadata (key, value) VALUES (?1, ?2) ON CONFLICT (key) DO UPDATE SET value = excluded.value")
.bind(key).bind(value).execute(pool).await?;
}
Backend::Postgres(pool) => {
sqlx::query("INSERT INTO aster_metadata (key, value) VALUES ($1, $2) ON CONFLICT (key) DO UPDATE SET value = excluded.value")
.bind(key).bind(value).execute(pool).await?;
}
}
Ok(())
}
pub async fn get(&self, key: &str) -> Result<Option<String>, StorageError> {
Ok(match &self.backend {
Backend::Sqlite(pool) => {
sqlx::query_scalar("SELECT value FROM aster_metadata WHERE key = ?1")
.bind(key)
.fetch_optional(pool)
.await?
}
Backend::Postgres(pool) => {
sqlx::query_scalar("SELECT value FROM aster_metadata WHERE key = $1")
.bind(key)
.fetch_optional(pool)
.await?
}
})
}
/// Explicitly closes the shared pool, including all Storage clones.
pub async fn close(&self) {
match &self.backend {
Backend::Sqlite(pool) => pool.close().await,
Backend::Postgres(pool) => pool.close().await,
}
}
}
+4
View File
@@ -0,0 +1,4 @@
CREATE TABLE IF NOT EXISTS aster_metadata (
key TEXT PRIMARY KEY NOT NULL,
value TEXT NOT NULL
)
+35
View File
@@ -0,0 +1,35 @@
use aster_storage::{Storage, StorageConfig};
/// Run explicitly against a disposable database; never silently skip a missing URL.
#[tokio::test]
#[ignore = "requires ASTER_TEST_POSTGRES_URL pointing to a test PostgreSQL database"]
async fn postgres_roundtrip_and_reconnect() {
let url = std::env::var("ASTER_TEST_POSTGRES_URL")
.expect("set ASTER_TEST_POSTGRES_URL to a disposable PostgreSQL database");
let config = StorageConfig::from_values(Some(&url), None).unwrap();
let store = Storage::connect(&config).await.unwrap();
assert_eq!(store.backend_name(), "postgresql");
store.ping().await.unwrap();
let key = format!(
"test.{}.{}",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
);
assert_eq!(store.get(&key).await.unwrap(), None);
store.put(&key, "你好 ' ; $1").await.unwrap();
assert_eq!(
store.get(&key).await.unwrap().as_deref(),
Some("你好 ' ; $1")
);
store.put(&key, "updated").await.unwrap();
store.close().await;
let reopened = Storage::connect(&config).await.unwrap();
assert_eq!(
reopened.get(&key).await.unwrap().as_deref(),
Some("updated")
);
reopened.close().await;
}
+97
View File
@@ -0,0 +1,97 @@
use aster_storage::{Storage, StorageConfig};
#[test]
fn configuration_defaults_precedence_and_errors() {
assert!(
matches!(StorageConfig::from_values(None, None).unwrap(), StorageConfig::SqliteFile(p) if p.to_str() == Some("data/aster.db"))
);
assert!(matches!(
StorageConfig::from_values(None, Some(":memory:")).unwrap(),
StorageConfig::SqliteMemory
));
assert!(matches!(
StorageConfig::from_values(Some("postgresql://localhost/aster"), Some("")).unwrap(),
StorageConfig::Postgres(_)
));
assert!(StorageConfig::from_values(Some(""), None).is_err());
assert!(StorageConfig::from_values(Some("mysql://localhost/aster"), None).is_err());
assert!(StorageConfig::from_values(None, Some("")).is_err());
let config = StorageConfig::Postgres("postgres://user:secret@localhost/db".into());
assert!(!format!("{config:?}").contains("secret"));
}
#[tokio::test]
async fn memory_store_is_shared_by_clones_but_isolated_between_stores() {
let store = Storage::connect(&StorageConfig::SqliteMemory)
.await
.unwrap();
let other = Storage::connect(&StorageConfig::SqliteMemory)
.await
.unwrap();
store.ping().await.unwrap();
assert_eq!(store.get("missing").await.unwrap(), None);
store.put("message", "你好 ' ? $1 ;").await.unwrap();
assert_eq!(
store.clone().get("message").await.unwrap().as_deref(),
Some("你好 ' ? $1 ;")
);
assert_eq!(other.get("message").await.unwrap(), None);
store.put("message", "updated").await.unwrap();
assert_eq!(
store.get("message").await.unwrap().as_deref(),
Some("updated")
);
store.close().await;
other.close().await;
}
#[tokio::test]
async fn sqlite_file_creates_parents_and_survives_reopen() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("nested/data/metadata.db");
let config = StorageConfig::SqliteFile(path.clone());
let store = Storage::connect(&config).await.unwrap();
store.put("project", "Aster VM").await.unwrap();
store.close().await;
assert!(path.is_file());
let reopened = Storage::connect(&config).await.unwrap();
assert_eq!(
reopened.get("project").await.unwrap().as_deref(),
Some("Aster VM")
);
reopened.put("project", "v2").await.unwrap();
assert_eq!(
reopened.get("project").await.unwrap().as_deref(),
Some("v2")
);
reopened.close().await;
}
#[tokio::test]
async fn invalid_sqlite_directory_reports_error() {
let dir = tempfile::tempdir().unwrap();
let file = dir.path().join("file");
std::fs::write(&file, "occupied").unwrap();
assert!(
Storage::connect(&StorageConfig::SqliteFile(file.join("aster.db")))
.await
.is_err()
);
}
#[tokio::test]
async fn postgres_connection_failure_is_an_error() {
// Hold a TCP port without serving PostgreSQL: deterministic local failure.
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let url = format!(
"postgres://aster:secret@{}/aster?sslmode=disable",
listener.local_addr().unwrap()
);
let result = Storage::connect(&StorageConfig::Postgres(url)).await;
let error = match result {
Ok(_) => panic!("unreachable PostgreSQL must fail"),
Err(error) => error,
};
assert!(!error.to_string().contains("secret"));
assert!(error.to_string().contains("no fallback"));
}