コード例 #1
0
 public static void MigrateTable <T, O>(
     this DnsDbContext context,
     string tableName,
     DbSet <T> sourceDbSet, DbSet <O> destDbSet, Func <T, O> transformFunc,
     bool hasSequence = true, Func <int> maxId = null)
     where T : class where O : class
 {
     foreach (var item in sourceDbSet)
     {
         destDbSet.Add(transformFunc(item));
     }
     if (hasSequence && sourceDbSet.Any())
     {
         context.SetCurrentSquenceId(tableName, maxId());
     }
     context.SaveChanges();
     Console.WriteLine($"Table {tableName} migrated");
 }
コード例 #2
0
        private static void Migrate()
        {
            var sqlOpt = new DbContextOptionsBuilder <MSSQL.MsSqlContext>();

            sqlOpt.UseSqlServer(SQL_CONNECTION);
            sqlOpt.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);

            var pgOpt = new DbContextOptionsBuilder <DnsDbContext>();

            pgOpt.UseNpgsql(PG_CONNECTION);

            using (var sql = new MSSQL.MsSqlContext(sqlOpt.Options))
                using (var pg = new DnsDbContext(pgOpt.Options))
                    using (var transaction = pg.Database.BeginTransaction())
                    {
                        pg.TruncateTables(TABLES);
                        pg.DisableTriggers(TABLES);

                        pg.MigrateTable(AttackGroups, sql.AttackGroups, pg.AttackGroups,
                                        ag => new Dns.DAL.Models.AttackGroups
                        {
                            DateBegin = ag.DateBegin.ToUniversalTime(),
                            DateClose = ag.DateClose?.ToUniversalTime(),
                            Id        = ag.Id,
                            Status    = ag.Status
                        }, maxId: () => sql.AttackGroups.Max(x => x.Id));

                        pg.MigrateTable(AttackHistories, sql.AttackHistories, pg.AttackHistories,
                                        ah => new Dns.DAL.Models.AttackHistories
                        {
                            AttackId      = ah.AttackId,
                            CurrentStatus = ah.CurrentStatus,
                            Date          = ah.Date.ToUniversalTime(),
                            Id            = ah.Id,
                            PrevStatus    = ah.PrevStatus
                        }, maxId: () => sql.AttackHistories.Max(x => x.Id));

                        pg.MigrateTable(DnsAttacks, sql.DnsAttacks, pg.DnsAttacks,
                                        a => new Dns.DAL.Models.Attacks
                        {
                            AttackGroupId = a.AttackGroupId,
                            BlackDomain   = a.BlackDomain,
                            Id            = a.Id,
                            Ip            = a.Ip,
                            IpBlocked     = a.IpBlocked,
                            Status        = a.Status,
                            SubnetBlocked = a.SubnetBlocked,
                            WhiteDomain   = a.WhiteDomain
                        }, maxId: () => sql.DnsAttacks.Max(x => x.Id));

                        pg.MigrateTable(DomainExcludedNames, sql.DomainExcludedNames, pg.DomainExcludedNames,
                                        den => new Dns.DAL.Models.DomainExcludedNames
                        {
                            BlackDomain = den.BlackDomain,
                            Id          = den.Id,
                            WhiteDomain = den.WhiteDomain
                        }, maxId: () => sql.DomainExcludedNames.Max(x => x.Id));

                        pg.MigrateTable(DomainInfo, sql.DomainInfo, pg.DomainInfo,
                                        di => new Dns.DAL.Models.DomainInfo
                        {
                            Company    = di.Company,
                            DateCreate = di.DateCreate?.ToUniversalTime(),
                            DateUntil  = di.DateUntil?.ToUniversalTime(),
                            Id         = di.Id,
                            Registrant = di.Registrant
                        }, maxId: () => sql.DomainInfo.Max(x => x.Id));

                        pg.MigrateTable(DomainNs, sql.DomainNs, pg.DomainNs,
                                        dn => new Dns.DAL.Models.DomainNSs
                        {
                            DomainId = dn.DomainId,
                            Id       = dn.Id,
                            NsId     = dn.NsId
                        }, maxId: () => sql.DomainNs.Max(x => x.Id));

                        pg.MigrateTable(GroupHistories, sql.GroupHistories, pg.GroupHistories,
                                        gh => new Dns.DAL.Models.AttackGroupHistories
                        {
                            AttackGroupId = gh.AttackGroupId,
                            CurrentStatus = gh.CurrentStatus,
                            Date          = gh.Date.ToUniversalTime(),
                            Id            = gh.Id,
                            PrevStatus    = gh.PrevStatus
                        }, maxId: () => sql.GroupHistories.Max(x => x.Id));

                        pg.MigrateTable(IpInfo, sql.IpInfo, pg.IpInfo,
                                        ii => new Dns.DAL.Models.IpInfo
                        {
                            Company = ii.Company,
                            Country = ii.Country,
                            Id      = ii.Id,
                            Ip      = ii.Ip,
                            Subnet  = ii.Subnet
                        }, maxId: () => sql.IpInfo.Max(x => x.Id));

                        pg.MigrateTable(NameServers, sql.NameServers, pg.NameServers,
                                        ns => new Dns.DAL.Models.NameServers
                        {
                            Id         = ns.Id,
                            NameServer = ns.NameServer
                        }, maxId: () => sql.NameServers.Max(x => x.Id));

                        pg.MigrateTable(Notes, sql.Notes, pg.Notes,
                                        n => new Dns.DAL.Models.AttackNotes
                        {
                            AttackGroupId = n.AttackGroupId,
                            Date          = n.Date.ToUniversalTime(),
                            Id            = n.Id,
                            Text          = n.Text
                        }, maxId: () => sql.Notes.Max(x => x.Id));

                        pg.MigrateTable(StatisticHistories, sql.StatisticHistories, pg.StatisticHistories,
                                        sh => new Dns.DAL.Models.StatisticHistories
                        {
                            AttackComplete     = sh.AttackComplete,
                            AttackIntersection = sh.AttackIntersection,
                            AttackNew          = sh.AttackNew,
                            AttackTotal        = sh.AttackTotal,
                            Date          = sh.Date.ToUniversalTime(),
                            GroupAttack   = sh.GroupAttack,
                            GroupComplete = sh.GroupComplete,
                            GroupDynamic  = sh.GroupDynamic,
                            GroupNew      = sh.GroupNew,
                            GroupThreat   = sh.GroupThreat,
                            GroupTotal    = sh.GroupTotal,
                            Id            = sh.Id
                        }, maxId: () => sql.StatisticHistories.Max(x => x.Id));

                        pg.MigrateTable(SuspectDomains, sql.SuspectDomains, pg.SuspectDomains,
                                        sd => new Dns.DAL.Models.SuspectDomains
                        {
                            Domain = sd.Domain,
                            Id     = sd.Id,
                            Ip     = sd.Ip
                        }, false);

                        pg.MigrateTable(WhiteDomains, sql.WhiteDomains, pg.WhiteDomains,
                                        wd => new Dns.DAL.Models.WhiteDomains
                        {
                            DateAdded = wd.DateAdded.ToUniversalTime(),
                            Domain    = wd.Domain,
                            Id        = wd.Id
                        }, maxId: () => sql.WhiteDomains.Max(x => x.Id));

                        pg.EnableTriggers(TABLES);
                        pg.SaveChanges();
                        transaction.Commit();
                        Console.WriteLine("Migration Complete");
                    }
        }