Beispiel #1
0
        public void Migrate(string[] args)
        {
            var options          = Cli.Parse <MigrateOptions>(args);
            var connectionString = $"SslMode=none;Server={options.Host};Port={options.Port};Database={options.Database};Uid={options.User};Pwd={options.Password};";

            using (var con = new MySqlConnection(connectionString))
            {
                con.Open();
                var databaseProvider = new MysqlDatabaseProvider(con)
                {
                    TableName = "__version"
                };
                AssemblyMigrationProvider assemblyProvider = null;
                switch (options.Schema.ToLower())
                {
                case "auth":
                    assemblyProvider = new AssemblyMigrationProvider(s_authType.Assembly, s_authType.Namespace);
                    break;

                case "game":
                    assemblyProvider = new AssemblyMigrationProvider(s_gameType.Assembly, s_gameType.Namespace);
                    break;

                default:
                    Error("Invalid schema");
                    break;
                }

                try
                {
                    var migrator = new SimpleMigrator(assemblyProvider, databaseProvider, new ConsoleLogger());
                    migrator.Load();
                    if (options.CurrentVersion > 0)
                    {
                        migrator.Baseline(options.CurrentVersion);
                    }

                    if (options.Version == 0)
                    {
                        migrator.MigrateToLatest();
                    }
                    else
                    {
                        migrator.MigrateTo(options.Version);
                    }
                }
                catch (MigrationNotFoundException ex)
                {
                    Error(ex.Message);
                }
            }
        }
Beispiel #2
0
        public void RunMigrations(DbService db)
        {
            var migrationsAssembly = typeof(Startup).Assembly;

            using (var pg = db.CreateConnection(longTimeout: true))
            {
                var databaseProvider = new PostgresqlDatabaseProvider(pg);
                var migrator         = new SimpleMigrator(migrationsAssembly, databaseProvider);
                migrator.Load();

                if (migrator.CurrentMigration == null || migrator.CurrentMigration.Version == 0)
                {
                    migrator.Baseline(2);
                }

                migrator.MigrateTo(5);

                if (migrator.LatestMigration.Version != migrator.CurrentMigration.Version)
                {
                    throw new Exception($"The newest available migration ({migrator.LatestMigration.Version}) != The current database migration ({migrator.CurrentMigration.Version}). You probably need to add a call to run the migration.");
                }
            }
        }