Cassandra Fluent Migrator is a library that offers a set of fluent code and extensions to facilitate the creation and management of the migrations using code instead of CQL commands.
- .NET Standard 2.1
- Cassandra CSharp driver - v3.17: A modern, feature-rich and highly tunable C# client library for Apache Cassandra using Cassandra’s binary protocol and Cassandra Query Language v3.
PM> Install-Package Cassandra.Fluent.Migrator
- Add support for more complex types.
- Add support for the
Materialized views
.
- Wiki page: Full documentation for the library.
- Cassandra Migrator: The core class of the library.
- Cassandra Fluent Migrator: The migration helper class.
- Migrator interface: The base properties and method that the migrations should implement.
- Example: An example on how to use the library.
- Supported Types: List of supported types.
- Migration class
In your project create a migration class that implements the IMigrator
Interface as follow:
public class InitialMigration : IMigrator
{
private readonly ICassandraFluentMigrator cfm;
private readonly ILogger<InitialMigration> logger;
public InitialMigration(ILogger<InitialMigration> logger, ICassandraFluentMigrator cfm)
{
this.cfm = cfm;
this.logger = logger;
}
public string Name => this.GetType().Name;
public Version Version => new Version(1, 0, 0);
public string Description => "First migration to initialize the Schema";
public async Task ApplyMigrationAsync()
{
this.logger.LogDebug($"Creating the Address User-Defined type...");
await this.cfm.CreateUserDefinedTypeAsync<Address>();
// Should not be here in real world application.
// Used only for example purposes.
this.cfm
.GetCassandraSession()
.UserDefinedTypes.Define(
UdtMap.For<Address>()
.Map(a => a.Number, "Number".ToLower())
.Map(a => a.Street, "Street".ToLower())
.Map(a => a.City, "City".ToLower())
.Map(a => a.Country, "Country".ToLower())
.Map(a => a.Province, "Province".ToLower())
.Map(a => a.PostalCode, "PostalCode".ToLower()));
this.logger.LogDebug($"Creating the User table...");
await this.cfm.GetTable<Users>().CreateIfNotExistsAsync();
}
}
- Startup class
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
// Custom method that you can create to initialize the Cassandra {ISession}.
services.AddCassandraSession(this.Configuration);
// Register the migrations
services.AddTransient<IMigrator, InitialMigration>();
// Required by the library to register the needed classes.
services.AddCassandraFluentMigratorServices();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
...
// Start the migration process.
app.UseCassandraMigration();
...
}