pub struct MonarchDB { /* private fields */ }Expand description
MonarchDB manages schema migrations and new connections for a database.
Implementations§
Source§impl MonarchDB
impl MonarchDB
Sourcepub fn open_in_memory(&self) -> Result<Connection>
pub fn open_in_memory(&self) -> Result<Connection>
Creates a new in-memory SQLite database connection with migrations applied.
This is useful for testing or for applications that need a temporary database. All migrations will be automatically applied to the in-memory database.
§Returns
Returns a rusqlite::Result<Connection> with migrations applied on success.
Sourcepub fn from_configuration(configuration: MonarchConfiguration) -> Result<Self>
pub fn from_configuration(configuration: MonarchConfiguration) -> Result<Self>
Creates a new MonarchDB instance from a configuration that loads migrations from disk.
This reads all migration files from the specified directory and creates a MonarchDB instance that can be used to manage database connections and schema migrations.
§Arguments
configuration- A MonarchConfiguration containing the migration directory path, database name, and foreign key settings.
§Returns
Returns a io::Result<Self> containing the configured MonarchDB instance.
§Errors
This function will return an error if:
- The migration directory cannot be read
- Any migration file cannot be read
- File system operations fail
Sourcepub fn current_version(&self) -> u32
pub fn current_version(&self) -> u32
Returns the current schema version, which is the number of migrations available.
This represents the latest version that the database schema can be migrated to.
§Returns
Returns the number of migrations as a u32.
Sourcepub fn create_connection(
&self,
configuration: &ConnectionConfiguration,
) -> Result<Connection>
pub fn create_connection( &self, configuration: &ConnectionConfiguration, ) -> Result<Connection>
Creates a new SQLite database connection with migrations applied.
If a database path is specified in the configuration, opens that file. Otherwise, creates an in-memory database. All migrations will be automatically applied to ensure the schema is up to date.
§Arguments
configuration- A ConnectionConfiguration specifying the database path. Ifdatabaseis None, an in-memory database will be created.
§Returns
Returns a rusqlite::Result<Connection> with migrations applied on success.
Sourcepub fn migrate(&self, connection: Connection) -> Result<Connection>
pub fn migrate(&self, connection: Connection) -> Result<Connection>
Applies all necessary migrations to an existing database connection.
This method takes ownership of a connection and returns it after applying all migrations to bring the schema up to the current version. It will also configure foreign key constraints if enabled.
§Arguments
connection- An existing SQLite connection to migrate.
§Returns
Returns the connection with migrations applied on success.
Sourcepub fn migrations<'c>(
&'c self,
connection: &'c mut Connection,
) -> Migrations<'c>
pub fn migrations<'c>( &'c self, connection: &'c mut Connection, ) -> Migrations<'c>
Create a migration manager for the given connection.
This method initializes a new Migrations instance, which can be used to
apply migrations to the provided connection.