CoscineMigrations.cs 3.41 KB
Newer Older
1
using Coscine.Configuration;
2
using Coscine.Database.Helpers;
3
4
using FluentMigrator.Runner;
using Microsoft.Extensions.DependencyInjection;
5
using System.Reflection;
6
7
8
9
10
11
12

namespace Coscine.Migrations
{
    public class CoscineMigrations
    {
        private readonly IConfiguration _configuration;

13
14
15
        public ConnectionSettings ConnectionSettings { get; set; }

        public Assembly TargetAssembly { get; set; } = typeof(CoscineMigrations).Assembly;
16
17
18
19
20
21
22
23
24
25
26

        public CoscineMigrations()
        {
            _configuration = new ConsulConfiguration();
        }

        public CoscineMigrations(IConfiguration configuration)
        {
            _configuration = configuration;
        }

27
        public ServiceProvider CreateServices()
28
        {
29
30
31
32
33
34
35
36
37
38
39
            return CreateServices(TargetAssembly, ConnectionSettings.GetConnectionString());
        }

        public ServiceProvider CreateServices(string dbConnectionString)
        {
            return CreateServices(TargetAssembly, dbConnectionString);
        }

        public ServiceProvider CreateServices(Assembly targetAssembly, string dbConnectionString)
        {
            return new ServiceCollection()
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
            // Registration of all FluentMigrator-specific services
            .AddFluentMigratorCore()
            // Configure the runner
            .ConfigureRunner(
                builder => builder
                    // Use SQLServer
                    .AddSqlServer()
                    // The SQLServer connection string  TODO: make configurable (via consul)
                    .WithGlobalConnectionString(dbConnectionString)
                    // Specify the assembly with the migrations
                    .ScanIn(targetAssembly).For.Migrations()
                    // Specify the assembly with the embedded resouces
                    .ScanIn(targetAssembly).For.EmbeddedResources())
                    
            // Enable logging to console in the FluentMigrator way
            .AddLogging(lb => lb.AddFluentMigratorConsole())
56
            .BuildServiceProvider(false);
57
58
59
60
61
62
        }

        public void MigrateUp()
        {
            // Put the database update into a scope to ensure
            // that all resources will be disposed.
63
            using (var service = CreateServices())
64
            {
65
66
67
68
                using (var scope = service.CreateScope())
                {
                    // Instantiate the runner
                    var runner = scope.ServiceProvider.GetRequiredService<IMigrationRunner>();
69

70
71
72
                    // Execute the migrations
                    runner.MigrateUp();
                }
73
74
75
76
77
            }
        }

        public void RollBack(int steps)
        {
78
            using (var service = CreateServices())
79
            {
80
81
82
83
84
                using (var scope = CreateServices().CreateScope())
                {
                    var runner = scope.ServiceProvider.GetRequiredService<IMigrationRunner>();
                    runner.Rollback(steps);
                }
85
86
87
88
89
90
            }
        }

        // Currently not in use
        public void MigrateDown(FluentMigrator.IMigration targetMigration)
        {
91
            using (var service = CreateServices())
92
            {
93
94
95
96
97
                using (var scope = CreateServices().CreateScope())
                {
                    var runner = scope.ServiceProvider.GetRequiredService<IMigrationRunner>();
                    runner.Down(targetMigration);
                }
98
99
100
101
102
            }
        }

    }
}