### Example Migration: CreateUsers Source: https://github.com/canton7/simple.migrations/blob/master/README.md A sample migration class demonstrating the Up and Down methods for creating and dropping a 'Users' table. ```csharp [Migration(1, "Create Users table")] public class CreateUsers : SQLiteNetMigration { protected override void Up() { Execute(@"CREATE TABLE Users ( Id SERIAL NOT NULL PRIMARY KEY, Name TEXT NOT NULL );"); } protected override void Down() { Execute("DROP TABLE Users"); } } ``` -------------------------------- ### Install Simple.Migrations via Package Manager Console Source: https://github.com/canton7/simple.migrations/blob/master/README.md Use this command in the Package Manager Console to install the Simple.Migrations NuGet package. ```powershell PM> Install-Package Simple.Migrations ``` -------------------------------- ### Running Migrations with SQLite.NET Source: https://github.com/canton7/simple.migrations/blob/master/README.md Example of setting up and running migrations using SimpleMigrator with a SQLite.NET connection. ```csharp using (var connection = new SQLiteConnection("SQLiteNetdatabase.sqlite")) { var databaseProvider = new SQLiteNetDatabaseProvider(connection); var migrator = new SimpleMigrator( Assembly.GetEntryAssembly(), databaseProvider); var runner = new ConsoleRunner(migrator); runner.Run(args); } ``` -------------------------------- ### Basic Console Application for Migrations Source: https://github.com/canton7/simple.migrations/blob/master/README.md Set up a console application to manage database migrations. This example demonstrates initializing SimpleMigrator with an assembly and a database provider, and then using ConsoleRunner to handle migration commands. ```csharp class Program { static int Main(string[] args) { var migrationsAssembly = typeof(Program).Assembly; using (var db = new SQLiteConnection("DataSource=database.sqlite")) { var databaseProvider = new SqliteDatabaseProvider(db); var migrator = new SimpleMigrator(migrationsAssembly, databaseProvider); // Either interact directly with the migrator... migrator.Load(); migrator.MigrateTo(1); migrator.MigrateToLatest(); migrator.Baseline(); // Or turn it into a console application // Note: ConsoleRunner is only available in .NET Standard 1.3 and .NET 4.5 (not .NET Standard 1.2) var consoleRunner = new ConsoleRunner(migrator); return consoleRunner.Run(args); } } } ``` -------------------------------- ### ConsoleRunner for Command-Line Migrations Source: https://github.com/canton7/simple.migrations/blob/master/README.md Example of using ConsoleRunner to handle command-line arguments for database migrations. This runner takes an ISimpleMigrator instance and executes commands based on provided arguments. ```csharp static int Main(string[] args) { using (var connection = ...) { var databaseProvider = new ...; var migrator = new SimpleMigrator(typeof(Program).Assembly, databaseProvider); var runner = new ConsoleRunner(migrator); return runner.Run(args); } } ``` -------------------------------- ### SimpleMigrator Instantiation (Pre-v0.9.7) Source: https://github.com/canton7/simple.migrations/blob/master/CHANGELOG.md Demonstrates the older method of instantiating SimpleMigrator, which involved separate connection and version providers. ```csharp using SimpleMigrations; using SimpleMigrations.VersionProviders; var migrationsAssembly = Assembly.GetEntryAssembly(); using (var connection = new ConnectionProvider(/* connection string */)) { var versionProvider = new SQLiteVersionProvider(); var migrator = new SimpleMigrator(migrationsAssembly, connection, versionProvider); migrator.Load(); } ``` -------------------------------- ### MssqlDatabaseProvider Instantiation (Pre-v0.9.8) Source: https://github.com/canton7/simple.migrations/blob/master/CHANGELOG.md Shows the previous method for instantiating MssqlDatabaseProvider, which accepted a connection factory delegate. ```csharp var databaseProvider = new MssqlDatabaseProvider(() => new SqlConnection("Connection String")); var migrator = new SimpleMigrator(migrationsAssembly, databaseProvider); migrator.Load(); ``` -------------------------------- ### Initialize SimpleMigrator with MySQL Connection Source: https://github.com/canton7/simple.migrations/blob/master/README.md Demonstrates how to initialize a SimpleMigrator with a MySQL connection string. Ensure you have the necessary MySqlConnection and MysqlDatabaseProvider classes available. ```csharp using (var connection = new MySqlConnection("Connection String")) { var databaseProvider = new MysqlDatabaseProvider(connection); var migrator = new SimpleMigrator(migrationsAssembly, databaseProvider); migrator.Load(); } ``` -------------------------------- ### Migrate to Latest Version with SQLite Source: https://github.com/canton7/simple.migrations/blob/master/README.md Shows how to load and then migrate to the latest version using a SQLite connection. This is a common pattern for ensuring the database is up-to-date on application startup. ```csharp var migrationsAssembly = Assembly.GetEntryAssembly(); using (var connection = new SqliteConnection("Connection String")) { var databaseProvider = new SqliteDatabaseProvider(connection); var migrator = new SimpleMigrator(migrationsAssembly, databaseProvider); migrator.Load(); migrator.MigrateToLatest(); } ``` -------------------------------- ### MssqlDatabaseProvider Instantiation (v0.9.8) Source: https://github.com/canton7/simple.migrations/blob/master/CHANGELOG.md Illustrates the updated way to instantiate MssqlDatabaseProvider using an advisory lock, requiring a connection object. ```csharp using (var connection = new SqlConnection("Connection String")) { var databaseProvider = new MssqlDatabaseProvider(connection); var migrator = new SimpleMigrator(migrationsAssembly, databaseProvider); migrator.Load(); } ``` -------------------------------- ### Define a Database Migration Class Source: https://github.com/canton7/simple.migrations/blob/master/README.md Create migration classes by inheriting from 'Migration' and decorating with '[Migration(versionNumber, description)]'. Implement 'Up' for applying changes and 'Down' for reverting them. Use the 'Execute' method to run SQL commands. ```csharp using SimpleMigrations; namespace Migrations { [Migration(1, "Create Users table")] public class CreateUsers : Migration { protected override void Up() { Execute(@"CREATE TABLE Users ( Id SERIAL NOT NULL PRIMARY KEY, Name TEXT NOT NULL )"); } protected override void Down() { Execute("DROP TABLE Users"); } } } ``` -------------------------------- ### SimpleMigrator Instantiation with SQLite (v0.9.7) Source: https://github.com/canton7/simple.migrations/blob/master/CHANGELOG.md Shows the new method for instantiating SimpleMigrator with a SQLiteVersionProvider, where the provider now takes the connection directly. ```csharp using SimpleMigrations; using SimpleMigrations.DatabaseProvider; var migrationsAssembly = Assembly.GetEntryAssembly(); using (var connection = new ConnectionProvider(/* connection string */)) { var databaseProvider = new SQLiteVersionProvider(connection); var migrator = new SimpleMigrator(migrationsAssembly, databaseProvider); migrator.Load(); } ``` -------------------------------- ### Migrate to Specific Version Source: https://github.com/canton7/simple.migrations/blob/master/README.md Demonstrates migrating the database to a specific version number. This is useful for rolling back or applying a particular schema version. ```csharp var migrator = new SimpleMigrator(migrationsAssembly, databaseProvider); migrator.Load(); migrator.MigrateTo(3); ``` -------------------------------- ### PostgreSQL Database Provider Usage Source: https://github.com/canton7/simple.migrations/blob/master/README.md Use this snippet to initialize the PostgreSQL database provider. PostgreSQL supports concurrent migrators using advisory locks. ```csharp using (var connection = new NpgsqlConnection("Connection String")) { var databaseProvider = new PostgresqlDatabaseProvider(connection); var migrator = new SimpleMigrator(migrationsAssembly, databaseProvider); migrator.Load(); } ``` -------------------------------- ### Remove All Migrations Source: https://github.com/canton7/simple.migrations/blob/master/README.md Shows how to use MigrateTo(0) to remove all applied migrations from the database. Use this with caution as it will revert the schema to its initial state. ```csharp var migrator = new SimpleMigrator(migrationsAssembly, databaseProvider); migrator.Load(); migrator.MigrateTo(0); ``` -------------------------------- ### SQLite Database Provider Usage Source: https://github.com/canton7/simple.migrations/blob/master/README.md Use this snippet to initialize the SQLite database provider. SQLite does not support concurrent migrators. ```csharp using (var connection = new SqliteConnection("Connection String")) { var databaseProvider = new SqliteDatabaseProvider(connection); var migrator = new SimpleMigrator(migrationsAssembly, databaseProvider); migrator.Load(); } ``` -------------------------------- ### SQLiteNetDatabaseProvider Implementation Source: https://github.com/canton7/simple.migrations/blob/master/README.md Implements the IDatabaseProvider interface for SQLite.NET, managing the SchemaVersion table. ```csharp public class SQLiteNetDatabaseProvider : IDatabaseProvider { private readonly SQLiteConnection connection; public SQLiteNetDatabaseProvider(SQLiteConnection connection) { this.connection = connection; } public SQLiteConnection BeginOperation() { return this.connection; } public void EndOperation() { } public long EnsurePrerequisitesCreatedAndGetCurrentVersion() { this.connection.CreateTable(); return this.GetCurrentVersion(); } public long GetCurrentVersion() { // Return 0 if the table has no entries var latestOrNull = this.connection.Table().OrderByDescending(x => x.Id).FirstOrDefault(); return latestOrNull?.Version ?? 0; } public void UpdateVersion(long oldVersion, long newVersion, string newDescription) { this.connection.Insert(new SchemaVersion() { Version = newVersion, AppliedOn = DateTime.Now, Description = newDescription, }); } } ``` -------------------------------- ### Check Current Migration Version Source: https://github.com/canton7/simple.migrations/blob/master/README.md Illustrates how to check if the current database migration version matches the latest available migration version. This can be used to trigger migration processes if the database is not up-to-date. ```csharp var migrator = new SimpleMigrator(migrationsAssembly, databaseProvider); migrator.Load(); if (migrator.CurrentMigration.Version != migrator.LatestMigration.Version) { // Throw a 'you must migrate' exception } ``` -------------------------------- ### SQLiteNetMigration Base Class Source: https://github.com/canton7/simple.migrations/blob/master/README.md Abstract base class for migrations using SQLite.NET, providing transaction support and logging. ```csharp public abstract class SQLiteNetMigration : IMigration { protected SQLiteConnection Connection { get; set; } protected IMigrationLogger Logger { get; set; } public abstract void Down(); public abstract void Up(); public void Execute(string sql) { this.Logger.LogSql(sql); this.Connection.Execute(sql); } void IMigration.RunMigration(MigrationRunData data) { this.Connection = data.Connection; this.Logger = data.Logger; if (this.UseTransaction) { try { this.Connection.BeginTransaction(); if (data.Direction == MigrationDirection.Up) this.Up(); else this.Down(); this.Connection.Commit(); } catch { this.Connection.Rollback(); throw; } } else { if (data.Direction == MigrationDirection.Up) this.Up(); else this.Down(); } } } ``` -------------------------------- ### SchemaVersion Table Model Source: https://github.com/canton7/simple.migrations/blob/master/README.md Defines the structure for the SchemaVersion table used to track migration history. ```csharp public class SchemaVersion { [PrimaryKey, AutoIncrement] public int Id { get; set; } public long Version { get; set; } public DateTime AppliedOn { get; set; } public string Description { get; set; } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.