Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            Console.OutputEncoding = Encoding.UTF8;

            Console.WriteLine("SchedulerBot Database Migration Tool (v1.0.x to v2.0.0)");

            string environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

            if (String.IsNullOrWhiteSpace(environment))
            {
                throw new ArgumentNullException("Environment not found in ASPNETCORE_ENVIRONMENT");
            }

            Console.WriteLine($"Environment: {environment}");

            Console.WriteLine("Reading configuration file...");
            Configure(environment);

            Console.WriteLine("Connecting to Postgres server...");
            var contextFactory = new SchedulerBotContextFactory();

            PostgresContext = contextFactory.CreateDbContext(null);

            Console.WriteLine("Connecting to Mongo server...");
            MongoClient = new MongoClient(Configuration.GetConnectionString("MongoDb"));
            Console.WriteLine("Getting database...");
            IMongoDatabase mongoDb = MongoClient.GetDatabase("schedulerbot");

            Console.WriteLine("Getting calendar collection...");
            var calendarCollection = mongoDb.GetCollection <Documents.Calendar>("calendars");
            var calendars          = calendarCollection.Find(new BsonDocument()).ToList();

            Console.WriteLine($"Found approximately {calendars.Count} calendar documents.");
            Console.WriteLine("Migrating...");
            Console.WriteLine($"0 out of {calendars.Count} calendars migrated.");
            int migrateCount = 0;

            foreach (var calendar in calendars)
            {
                MigrateCalendar(calendar, out Calendar newCalendar, out List <Permission> newPermissions);
                PostgresContext.Calendars.Add(newCalendar);
                PostgresContext.Permissions.AddRange(newPermissions);
                migrateCount++;
                Console.SetCursorPosition(0, Console.CursorTop - 1);
                Console.WriteLine($"{migrateCount} out of {calendars.Count} calendars migrated.");
            }
            Console.WriteLine("Saving to database...");
            PostgresContext.SaveChanges();
            Console.WriteLine("Migration completed.");

            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Verifies whether the database is migrated and applies migrations if not.
        /// </summary>
        /// <param name="host">The host.</param>
        /// <returns>The same <see cref="IWebHost"/> instance which has been passed to the method.</returns>
        public static IWebHost EnsureDatabaseMigrated(this IWebHost host)
        {
            using (IServiceScope scope = host.Services.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                IServiceProvider    scopeServiceProvider = scope.ServiceProvider;
                SchedulerBotContext context = scopeServiceProvider.GetRequiredService <SchedulerBotContext>();

                context.Database.Migrate();
            }

            return(host);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ScheduledMessageDetailsRepository"/> class.
 /// </summary>
 /// <param name="dbContext">The database context.</param>
 public ScheduledMessageDetailsRepository(SchedulerBotContext dbContext)
     : base(dbContext)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ScheduledMessageEventRepository"/> class.
 /// </summary>
 /// <param name="dbContext">The database context.</param>
 public ScheduledMessageEventRepository(SchedulerBotContext dbContext)
     : base(dbContext)
 {
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ManageConversationLinkRepository"/> class.
 /// </summary>
 /// <param name="dbContext">The database context.</param>
 public ManageConversationLinkRepository(SchedulerBotContext dbContext)
     : base(dbContext)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ServiceUrlRepository"/> class.
 /// </summary>
 /// <param name="dbContext">The database context.</param>
 public ServiceUrlRepository(SchedulerBotContext dbContext)
     : base(dbContext)
 {
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BaseRepository{T}"/> class.
 /// </summary>
 /// <param name="dbContext">The database context.</param>
 /// <exception cref="ArgumentNullException">dbContext</exception>
 protected BaseRepository(SchedulerBotContext dbContext)
 {
     DbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
     DbSet     = DbContext.Set <T>();
 }