/// <summary> /// Generates FluentMigration C# files based on the procedures found in <see cref="schemaDumper"/> /// </summary> /// <param name="context">Defines how, what and where the migrations will be generated</param> /// <param name="schemaDumper">The platform specific schema dumper instance to get procedure information from</param> public void GenerateMigrations(SchemaMigrationContext context, ISchemaDumper schemaDumper) { _announcer.Say("Reading procedures"); var defs = schemaDumper.ReadProcedures(); SetupMigrationsDirectory(context); var migrations = 0; foreach (var procedure in defs) { if ( context.ExcludeProcedures.Contains(procedure.Name)) { _announcer.Say("Excluding procedure " + procedure.Name); continue; } if (context.IncludeProcedures.Count != 0 && !context.IncludeProcedures.Contains(procedure.Name)) continue; migrations++; var migrationsFolder = Path.Combine(context.WorkingDirectory, context.MigrationsDirectory); var csFilename = Path.Combine(migrationsFolder, context.MigrationProcedureClassNamer(context.MigrationIndex + migrations, procedure) + ".cs"); _announcer.Say("Creating migration " + Path.GetFileName(csFilename)); using (var writer = new StreamWriter(csFilename)) { WriteToStream(context, procedure, context.MigrationIndex + migrations, writer); } } context.MigrationIndex += migrations; }
/// <summary> /// Generates FluentMigration C# files based on the views found in <see cref="schemaDumper"/> /// </summary> /// <param name="context">Defines how, what and where the migrations will be generated</param> /// <param name="schemaDumper">The platform specific schema dumper instance to get view information from</param> public void GenerateMigrations(SchemaMigrationContext context, ISchemaDumper schemaDumper) { _announcer.Say("Reading views"); var defs = schemaDumper.ReadViews(); SetupMigrationsDirectory(context); //TODO: Think about adding custom sort order for view definitions as there may be // dependancies between views. // if ( context.CustomViewSorter != null ) // defs = context.CustomViewSorter(defs); var migrations = 0; foreach (var view in defs) { if ( context.ExcludeViews.Contains(view.Name)) { _announcer.Say("Excluding view " + view.Name); continue; } if (context.IncludeViews.Count != 0 && !context.IncludeViews.Contains(view.Name)) continue; migrations++; var migrationsFolder = Path.Combine(context.WorkingDirectory, context.MigrationsDirectory); var csFilename = Path.Combine(migrationsFolder, context.MigrationViewClassNamer(context.MigrationIndex + migrations, view) + ".cs"); _announcer.Say("Creating migration " + Path.GetFileName(csFilename)); using (var writer = new StreamWriter(csFilename)) { WriteToStream(context, view, context.MigrationIndex + migrations, writer); } } context.MigrationIndex += migrations; }
/// <summary> /// Generates C# Migrations that Create tables Migrations /// </summary> /// <param name="context">The context that define how parts of the C# will be formatted</param> /// <param name="schemaDumper">The schema dump to use as the source of the schema migration</param> public void GenerateMigrations(SchemaMigrationContext context, ISchemaDumper schemaDumper) { _announcer.Say("Reading database schema"); var defs = schemaDumper.ReadDbSchema(); if (context.PreMigrationTableUpdate != null) context.PreMigrationTableUpdate(defs); SetupMigrationsDirectory(context); var migrations = 0; var foreignkeyTables = new List<TableDefinition>(); foreach (var table in defs) { // Check if we want to exclude this table if ( context.ExcludeTables.Contains(table.Name)) { _announcer.Say("Exluding table " + table.Name); continue; } if (context.MigrationRequired(MigrationType.Tables) || (context.MigrationRequired(MigrationType.Indexes) && table.Indexes.Count > 0)) { migrations++; var migrationsFolder = Path.Combine(context.WorkingDirectory, context.MigrationsDirectory); var csFilename = Path.Combine(migrationsFolder, context.MigrationClassNamer(context.MigrationIndex + migrations, table) + ".cs"); _announcer.Say("Creating migration " + Path.GetFileName(csFilename)); using (var writer = new StreamWriter(csFilename)) { WriteToStream(context, table, context.MigrationIndex + migrations, writer); } } if (context.MigrationRequired(MigrationType.Data)) { var data = schemaDumper.ReadTableData(table.SchemaName, table.Name); if (data != null && data.Tables.Count > 0 && data.Tables[0].Rows.Count > 0) { var dataDirectory = Path.Combine(context.WorkingDirectory, context.DataDirectory); if (!Directory.Exists(dataDirectory)) Directory.CreateDirectory(dataDirectory); data.Tables[0].WriteXmlSchema(Path.Combine(dataDirectory, table.Name + ".xsd")); using (var writer = new XmlTextWriter(Path.Combine(dataDirectory, table.Name + ".xml"), context.MigrationEncoding)) { data.Tables[0].WriteXml(writer); } } } if (context.MigrationRequired(MigrationType.ForeignKeys) && table.ForeignKeys.Count > 0) { // Add to list of tables to apply foreign key // ... done as two part process as may me interdepdancies between tables foreignkeyTables.Add(table); } } context.MigrationIndex += migrations; if (context.MigrationRequired(MigrationType.ForeignKeys)) GenerateForeignKeyMigrations(context, foreignkeyTables); }