<commit_before>﻿using HeroesStatTracker.Data.Databases;
using HeroesStatTracker.Migrations;
using System;
using System.IO;
using System.Linq;

namespace HeroesStatTracker.Data.Migrations
{
    internal class MigratorBase<T, U> 
        where T : StatTrackerDbContext, new()
        where U : ContextMigrator, new()
    {
        private string DbName;
        bool DatabaseFileCreated;
        int RequiredDatabaseVersion;

        public MigratorBase(string dbName, bool databaseFileCreated, int requiredDatabaseVersion)
        {
            DbName = dbName;
            DatabaseFileCreated = databaseFileCreated;
            RequiredDatabaseVersion = requiredDatabaseVersion;
        }

        public void Initialize(bool logger = false)
        {
            try
            {
                using (T db = new T())
                {
                    int currentVersion = 0;
                    if (DatabaseFileCreated)
                    {
                        currentVersion = RequiredDatabaseVersion;
                        db.SchemaInfo.Add(new SchemaInfo() { Version = currentVersion });
                        db.SaveChanges();
                    }

                    if (db.SchemaInfo.Count() > 0)
                    {
                        currentVersion = db.SchemaInfo.Max(x => x.Version);
                        if (logger)
                        {
                            MigrationLogger($"Current Version: {currentVersion}");
                            MigrationLogger($"Required Version: {RequiredDatabaseVersion}");
                        }
                    }

                    if (currentVersion >= RequiredDatabaseVersion)
                    {
                        if (logger)
                            MigrationLogger("No migration required");

                        return;
                    }

                    U contextMigrator = new U();

                    while (currentVersion < RequiredDatabaseVersion)
                    {
                        currentVersion++;

                        if (logger)
                            MigrationLogger($"Migrating to version {currentVersion}");

                        foreach (string migration in contextMigrator.Migrations[currentVersion])
                        {
                            db.Database.ExecuteSqlCommand(migration);
                        }

                        if (contextMigrator.MigrationAddons.ContainsKey(currentVersion))
                        {
                            foreach (IMigrationAddon migration in contextMigrator.MigrationAddons[currentVersion])
                            {
                                migration.Execute();
                            }
                        }

                        db.SchemaInfo.Add(new SchemaInfo() { Version = currentVersion });
                        db.SaveChanges();

                        if (logger)
                            MigrationLogger($"Migration version {currentVersion} completed");
                    }

                    if (logger)
                        MigrationLogger($"Migration completed");
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        protected void MigrationLogger(string message)
        {
            using (StreamWriter writer = new StreamWriter($"Logs/DatabasesMigrationLog.txt", true))
            {
                writer.WriteLine($"[{DbName}] {message}");
            }
        }
    }
}<commit_msg>Delete the old MigrationsLogs on startup<commit_after>﻿using HeroesStatTracker.Data.Databases;
using HeroesStatTracker.Migrations;
using System;
using System.IO;
using System.Linq;

namespace HeroesStatTracker.Data.Migrations
{
    internal class MigratorBase<T, U> 
        where T : StatTrackerDbContext, new()
        where U : ContextMigrator, new()
    {
        private string DbName;
        bool DatabaseFileCreated;
        int RequiredDatabaseVersion;

        private readonly string MigrationLogFile = "Logs/DatabasesMigrationLog.txt";

        public MigratorBase(string dbName, bool databaseFileCreated, int requiredDatabaseVersion)
        {
            DeleteMigrationLog();

            DbName = dbName;
            DatabaseFileCreated = databaseFileCreated;
            RequiredDatabaseVersion = requiredDatabaseVersion;
        }

        public void Initialize(bool logger = false)
        {
            try
            {
                using (T db = new T())
                {
                    int currentVersion = 0;
                    if (DatabaseFileCreated)
                    {
                        currentVersion = RequiredDatabaseVersion;
                        db.SchemaInfo.Add(new SchemaInfo() { Version = currentVersion });
                        db.SaveChanges();
                    }

                    if (db.SchemaInfo.Count() > 0)
                    {
                        currentVersion = db.SchemaInfo.Max(x => x.Version);
                        if (logger)
                        {
                            MigrationLogger($"Current Version: {currentVersion}");
                            MigrationLogger($"Required Version: {RequiredDatabaseVersion}");
                        }
                    }

                    if (currentVersion >= RequiredDatabaseVersion)
                    {
                        if (logger)
                            MigrationLogger("No migration required");

                        return;
                    }

                    U contextMigrator = new U();

                    while (currentVersion < RequiredDatabaseVersion)
                    {
                        currentVersion++;

                        if (logger)
                            MigrationLogger($"Migrating to version {currentVersion}");

                        foreach (string migration in contextMigrator.Migrations[currentVersion])
                        {
                            db.Database.ExecuteSqlCommand(migration);
                        }

                        if (contextMigrator.MigrationAddons.ContainsKey(currentVersion))
                        {
                            foreach (IMigrationAddon migration in contextMigrator.MigrationAddons[currentVersion])
                            {
                                migration.Execute();
                            }
                        }

                        db.SchemaInfo.Add(new SchemaInfo() { Version = currentVersion });
                        db.SaveChanges();

                        if (logger)
                            MigrationLogger($"Migration version {currentVersion} completed");
                    }

                    if (logger)
                        MigrationLogger($"Migration completed");
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        protected void MigrationLogger(string message)
        {
            using (StreamWriter writer = new StreamWriter(MigrationLogFile, true))
            {
                writer.WriteLine($"[{DateTime.Now.ToLocalTime()}] [{DbName}] {message}");
            }
        }

        private void DeleteMigrationLog()
        {
            if (File.Exists(MigrationLogFile))
                File.Delete(MigrationLogFile);
        }
    }
}
{'fraction_non_alphanumeric': 0.07249898744430944, 'fraction_numerical': 0.0005400297016335898, 'mean_word_length': 1.1367176233054515, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4548288', 'n_tokens_mistral': 1616, 'n_tokens_neox': 1496, 'n_words': 314}