Connect to a persistence store based on the database URL.
(url: &str)
| 174 | |
| 175 | /// Connect to a persistence store based on the database URL. |
| 176 | pub async fn connect(url: &str) -> CoreResult<Self> { |
| 177 | if url.starts_with("postgres://") || url.starts_with("postgresql://") { |
| 178 | let store = PostgresStore::connect(url) |
| 179 | .await |
| 180 | .map_err(|e| CoreError::execution(e.to_string()))?; |
| 181 | store |
| 182 | .migrate() |
| 183 | .await |
| 184 | .map_err(|e| CoreError::execution(e.to_string()))?; |
| 185 | Ok(Self::Postgres(store)) |
| 186 | } else if url.starts_with("sqlite:") { |
| 187 | let store = SqliteStore::connect(url) |
| 188 | .await |
| 189 | .map_err(|e| CoreError::execution(e.to_string()))?; |
| 190 | store |
| 191 | .migrate() |
| 192 | .await |
| 193 | .map_err(|e| CoreError::execution(e.to_string()))?; |
| 194 | Ok(Self::Sqlite(store)) |
| 195 | } else { |
| 196 | Err(CoreError::config(format!( |
| 197 | "unsupported database URL scheme: {url}" |
| 198 | ))) |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /// Verify connectivity to the underlying database. |
| 203 | pub async fn ping(&self) -> PersistenceResult<()> { |