-
Notifications
You must be signed in to change notification settings - Fork 0
Example
Youssef Benhessou edited this page Jun 7, 2020
·
1 revision
- 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.Contry, "Contry".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
In the Startup class, register your Cassandra Session, Migrations, and the Library classes in the ConfigureServices(...) method. Then in the Configure(...) call the UseCassandraMigration() method that will start the migration process.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
// Custom method that you can create in order to initialize the `Cassandra session`.
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();
...
}