예제 #1
0
 public IDictionary<string, List<MigrationStep>> SelectMigrations()
 {
   var all = _migrationFinder.FindMigrations();
   var selected = new Dictionary<string, List<MigrationStep>>();
   selected[string.Empty] = new List<MigrationStep>();
   if (all.Count == 0)
   {
     return selected;
   }
   var versionStates = _versionStateFactory.CreateVersionState(all);
   foreach (var state in versionStates)
   {
     selected[state.Key] = new List<MigrationStep>();
   }
   foreach (var migration in all)
   {
     var version = versionStates[migration.ConfigurationKey];
     if (version.IsApplicable(migration))
     {
       var step = new MigrationStep(migration, version.IsReverting);
       selected[migration.ConfigurationKey].Add(step);
     }
   }
   foreach (var pair in versionStates)
   {
     if (pair.Value.IsReverting)
     {
       selected[pair.Key].Reverse();
     }
   }
   return selected;
 }
예제 #2
0
        private void AddMigrationStep(MigrationStepType stepType, TableSchema.Table table)
        {
            MigrationStep step = new MigrationStep(stepType);

            step.Table = table;
            steps.Add(step);
        }
예제 #3
0
        private void AddMigrationSql(string sql)
        {
            MigrationStep step = new MigrationStep(MigrationStepType.ExecuteSql);

            step.Sql = sql;
            steps.Add(step);
        }
예제 #4
0
        public IDictionary <string, List <MigrationStep> > SelectMigrations()
        {
            var all      = _migrationFinder.FindMigrations();
            var selected = new Dictionary <string, List <MigrationStep> >();

            selected[string.Empty] = new List <MigrationStep>();
            if (all.Count == 0)
            {
                return(selected);
            }
            var versionStates = _versionStateFactory.CreateVersionState(all);

            foreach (var state in versionStates)
            {
                selected[state.Key] = new List <MigrationStep>();
            }
            foreach (var migration in all)
            {
                var version = versionStates[migration.ConfigurationKey];
                if (version.IsApplicable(migration))
                {
                    var step = new MigrationStep(migration, version.IsReverting);
                    selected[migration.ConfigurationKey].Add(step);
                }
            }
            foreach (var pair in versionStates)
            {
                if (pair.Value.IsReverting)
                {
                    selected[pair.Key].Reverse();
                }
            }
            return(selected);
        }
예제 #5
0
        private void AddMigrationStep(MigrationStepType stepType, TableSchema.TableColumn col1, TableSchema.TableColumn col2)
        {
            MigrationStep step = new MigrationStep(stepType);

            step.Column  = col1;
            step.Column2 = col2;
            steps.Add(step);
        }
예제 #6
0
        internal async Task RunOrderedMigrationAsync(MigrationStep key, IDbProvider dbProvider)
        {
            if (!Migration.ContainsKey(key))
            {
                return;
            }
            var database = new Database(dbProvider.DatabaseName, dbProvider.Dialect);

            Migration[key].Invoke(database, dbProvider);
            await dbProvider.ExecuteNonQueryAsync(database.ToString());
        }
예제 #7
0
 private void OnStepBeforeFilled(Type entityType, MigrationStep step, DateTime startTime)
 {
     if (StepBeforeFilled != null)
     {
         StepBeforeFilled(this, new MigratorBeforeStepEventArgs
         {
             AllTypesCount = EntityTypesCount(),
             CurrentEntity = entityType,
             Step          = step,
             StartTime     = startTime
         });
     }
 }
예제 #8
0
        public static async Task RunOrderedMigration(this IDbMigration migration, MigrationStep key, IDbProvider dbProvider)
        {
            if (!migration.Migration.ContainsKey(key))
            {
                return;
            }
            var database = new Database(dbProvider.DatabaseName, dbProvider.Dialect);

            migration.Migration[key].Invoke(database, dbProvider);
            var dbCommand = database.ToString();

            if (!string.IsNullOrWhiteSpace(dbCommand))
            {
                await dbProvider.ExecuteNonQuery(dbCommand);
            }
        }
예제 #9
0
 public ICollection<MigrationStep> SelectMigrations()
 {
     ICollection<MigrationReference> all = _migrationFinder.FindMigrations();
       List<MigrationStep> selected = new List<MigrationStep>();
       if (all.Count == 0)
       {
     return selected;
       }
       VersionState version = _versionStateFactory.CreateVersionState(all);
       foreach (MigrationReference migration in all)
       {
     if (version.IsApplicable(migration))
     {
       MigrationStep step = new MigrationStep(migration, version.IsReverting);
       selected.Add(step);
     }
       }
       if (version.IsReverting)
       {
     selected.Reverse();
       }
       return selected;
 }
예제 #10
0
 public static bool IsMigrationRequired(MigrationStep step)
 {
     return(MigrationStartIndex <= (int)step);
 }
 /// <summary>
 /// Invoke OperationStepEvent
 /// </summary>
 /// <param name="step">
 /// progress operation's step <see cref="MigrationStep"/>
 /// </param>
 private void NotifyStep(MigrationStep step)
 {
     OperationStepEvent?.Invoke(step);
 }
예제 #12
0
        public static void Main(string[] args)
        {
            // USER INPUT /////////////////////////////////////////////////////////////////////////////////

            // Always first create a new database migration with DatabaseStep.ADD_MIGRATION,
            // and include the created files in the project and set resource file to EmbeddedResource.
            // After creating a migration run UPDATE_DATABASE to update the database.

            const MigrationStep step = MigrationStep.CREATE_SCRIPT;             /* Never forget: MIGRATION_NAME ! */

            // Specify the name of the database migration in case of ADD-MIGRATION.
            // Note: Make sure to create a new name for each new migration.
            //       After creating migration include the files in the folder by right clicking on
            //       Oogstplanner.Migrations and selecting "Add files from folder". Then add the .cs, .resx and
            //       .Designer.cs files with the name specified below.
            //       Last but not least set the .resx file's build action to EmbeddedResource by right
            //       clicking on it.
            // Make sure that the setup-database.sh script has run to create the database and user.


            // END USER INPUT /////////////////////////////////////////////////////////////////////////////


            // Get executing path from which the location of the Update_Scripts and new Migrations can be determined.
            var executingPath = AppDomain.CurrentDomain.BaseDirectory;

            // Add a new migration (PowerShell: Add-Migration)
            if (step == MigrationStep.ADD_MIGRATION)
            {
                // Initialize the wrapper classes around the Entity Framework PowerShell API.
                var config     = new Configuration();
                var scaffolder = new MigrationScaffolder(config);
                var migration  = scaffolder.Scaffold(MIGRATION_NAME);

                // Place migration code in main project "Migrations" folder and migration scripts in "App_Data"
                var migrationsPath = Regex.Replace(executingPath, "bin/.*", "");

                // Write migrations
                File.WriteAllText(migrationsPath + MIGRATION_NAME + ".cs", migration.UserCode);
                File.WriteAllText(migrationsPath + MIGRATION_NAME + ".Designer.cs", migration.DesignerCode);

                using (var writer = new ResXResourceWriter(migrationsPath + MIGRATION_NAME + ".resx"))
                {
                    foreach (var resource in migration.Resources)
                    {
                        writer.AddResource(resource.Key, resource.Value);
                    }
                }
                Console.WriteLine("EF code migration {0} written to Migrations folder...\n\n" +
                                  "Next step is to include the .cs, .resx and .Designer.cs file in the project" +
                                  "by right clicking on the project and selecting " +
                                  "\"Add files from folder.\"\n" +
                                  "Then right click on {1}.resx and set build action to \"EmbeddedResource\""
                                  , migration.MigrationId, MIGRATION_NAME);
            }

            else if (step == MigrationStep.CREATE_SCRIPT)
            {
                var config   = new Configuration();
                var migrator = new DbMigrator(config);
                var scriptor = new MigratorScriptingDecorator(migrator);

                // Determine name of the previous run migration if exists.
                string lastMigration = migrator.GetDatabaseMigrations().FirstOrDefault();

                // Get the script
                string script = scriptor.ScriptUpdate(sourceMigration: lastMigration, targetMigration: MIGRATION_NAME);

                // Create the PostgreSQL update script based on last migration on database and
                // current migration.
                string formattedScript = string.Format
                                             ("/* * * * * * * * * * * * * * * * * * * * * * *\n" +
                                             " *\n" +
                                             " * Migration:\t\t{0}\n *\n" +
                                             " * Date and time:\t{1}\n" +
                                             " *\n" +
                                             " * * * * * * * * * * * * * * * * * * * * * * */\n\n" +
                                             "{2}",
                                             MIGRATION_NAME,
                                             DateTime.Now,
                                             script);

                // Write string to file in Migrations folder of main project
                var migrationsPath = Regex.Replace(executingPath, "bin/.*", "");
                File.WriteAllText(migrationsPath + MIGRATION_NAME + ".sql", formattedScript);
                Console.WriteLine("Script {0}.sql successfully generated.",
                                  MIGRATION_NAME);
            }

            // If a new migration is created the database can be updated. (PowerShell: Update-Database)
            else if (step == MigrationStep.UPDATE_DATABASE)
            {
                var config   = new Configuration();
                var migrator = new DbMigrator(config);

                // Write to database
                migrator.Update();

                // Show which migrations are applied.
                var migrationNames = string.Join(", ", migrator.GetDatabaseMigrations().ToArray());
                Console.WriteLine("Applied migration {0} to database.", migrationNames);
            }
        }