I'm looking for a way to write a extension method for fluent migrator check before create table, something like this
Schema.TableIfNotExist("table").InSchema("dbo").WithColumn("Id").AsInt32().NotNullable()
is it possible to write a extension method ?
Yes you can
public static class Ex
{
public static IFluentSyntax CreateTableIfNotExists(this MigrationBase self, string tableName, Func<ICreateTableWithColumnOrSchemaOrDescriptionSyntax, IFluentSyntax> constructTableFunction, string schemaName = "dbo")
{
if (!self.Schema.Schema(schemaName).Table(tableName).Exists())
{
return constructTableFunction(self.Create.Table(tableName));
}
else
{
return null;
}
}
}
You are going to have two caveats (that I know of):
To use toe above extension and your example, you would do
public override void Up()
{
this.CreateTableIfNotExists("table", table => table.WithColumn("Id").AsInt32().NotNullable());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With