示例#1
0
        static void RunOptions(Options opts)
        {
            var migrator = new DatabaseMigrator(opts.ConnectionString);

            if (opts.DropDatabase)
            {
                Log.Warning("!!! Dropping database !!!");
                migrator.DropDatabase();
            }

            Log.Information("*** Ensuring database exists ***");
            migrator.EnsureDatabaseExists();

            Log.Information("^^^ Running incremental migrations ^^^");
            var result = migrator.RunIncrementalMigrations();

            Log.Information("Database migration completed: {Result}", result);
            if (!result.Successful)
            {
                Log.Error("Database migration failed: {ErrorMessage}", result.Error);
                _returnCode = 1;
                return;
            }

            if (opts.InsertSeedData)
            {
                Log.Information("+++ Inserting seed data +++");
                result = migrator.SeedDatabase();

                Log.Information("Insert seed data completed: {Result}", result);
                if (!result.Successful)
                {
                    Log.Error("Insert seed data failed: {ErrorMessage}", result.Error);
                    _returnCode = 1;
                }
            }
        }
示例#2
0
        private static void RunDatabaseDropAndMigrateIfRequired(IHost host)
        {
            using var scope = host.Services.CreateScope();
            var services = scope.ServiceProvider;

            var hostingEnvironment = services.GetService <IWebHostEnvironment>();

            if (!hostingEnvironment.IsLocal())
            {
                return;
            }

            Log.Warning(" !!! Running database drop and migrate !!!");
            var migrator = new DatabaseMigrator(Configuration.GetConnectionString("ZenTimeDatabase"));

            migrator.DropDatabase();
            migrator.EnsureDatabaseExists();

            if (!migrator.RunIncrementalMigrations().Successful ||
                !migrator.SeedDatabase().Successful)
            {
                throw new Exception("Error running database migrations, check logs for details");
            }
        }