Пример #1
0
 protected virtual void RestoreDatabase(DatabaseName databaseName, string backupFile, string databaseFile, string databaseLogFile)
 {
     DisconnectedTools.RestoreDatabase(databaseName,
                                       Absolutize(backupFile),
                                       Absolutize(databaseFile),
                                       Absolutize(databaseLogFile));
 }
Пример #2
0
        protected virtual void BackupDatabase(DisconnectedMachineEntity machine, Lite <DisconnectedExportEntity> export, Connector newDatabase)
        {
            string backupFileName = Path.Combine(DisconnectedLogic.BackupFolder, BackupFileName(machine, export));

            FileTools.CreateParentDirectory(backupFileName);
            DisconnectedTools.BackupDatabase(new DatabaseName(null, newDatabase.DatabaseName()), backupFileName);
        }
Пример #3
0
        private void RestoreDatabase(DisconnectedMachineEntity machine, Lite <DisconnectedImportEntity> import)
        {
            string backupFileName = Path.Combine(DisconnectedLogic.BackupFolder, BackupFileName(machine, import));

            string fileName    = DatabaseFileName(machine);
            string logFileName = DatabaseLogFileName(machine);

            DisconnectedTools.RestoreDatabase(DatabaseName(machine), backupFileName, fileName, logFileName);
        }
Пример #4
0
        protected virtual void DisableForeignKeys(Table table)
        {
            DisconnectedTools.DisableForeignKeys(table);

            foreach (var rt in table.TablesMList())
            {
                DisconnectedTools.DisableForeignKeys(rt);
            }
        }
Пример #5
0
        private static SqlPreCommand Schema_Generating()
        {
            if (DisconnectedLogic.OfflineMode)
            {
                return(null);
            }

            return(GetTablesToSeed()
                   .Select(a => DisconnectedTools.SetNextIdSync(a, ServerSeed))
                   .Combine(Spacing.Simple));
        }
Пример #6
0
        protected virtual string CreateDatabase(DisconnectedMachineEntity machine)
        {
            DatabaseName databaseName = DatabaseName(machine);

            DisconnectedTools.DropIfExists(databaseName);

            string fileName    = DatabaseFileName(machine);
            string logFileName = DatabaseLogFileName(machine);

            DisconnectedTools.CreateDatabase(databaseName, fileName, logFileName);

            return(((SqlServerConnector)Connector.Current).ConnectionString.Replace(Connector.Current.DatabaseName(), databaseName.Name));
        }
Пример #7
0
        private static SqlPreCommand Schema_Synchronizing(Replacements arg)
        {
            if (DisconnectedLogic.OfflineMode)
            {
                return(null);
            }

            if (!arg.Interactive && arg.SchemaOnly) // Is ImportManager
            {
                return(null);
            }

            return(GetTablesToSeed()
                   .Where(a => DisconnectedTools.GetNextId(a) < ServerSeed)
                   .Select(a => DisconnectedTools.SetNextIdSync(a, ServerSeed))
                   .Combine(Spacing.Simple));
        }
Пример #8
0
 private void DeleteTable(Table table, DatabaseName newDatabaseName)
 {
     DisconnectedTools.DeleteTable(table.Name.OnDatabase(newDatabaseName));
 }
Пример #9
0
 protected virtual void DropIfExists(DatabaseName databaseName)
 {
     DisconnectedTools.DropIfExists(databaseName);
 }
Пример #10
0
 protected virtual string DatabaseLogFileName(DisconnectedMachineEntity machine)
 {
     return(Path.Combine(DisconnectedLogic.DatabaseFolder, Connector.Current.DatabaseName() + "_Export_" + DisconnectedTools.CleanMachineName(machine.MachineName) + "_Log.ldf"));
 }
Пример #11
0
        protected virtual DatabaseName DatabaseName(DisconnectedMachineEntity machine)
        {
            var isPostgres = Schema.Current.Settings.IsPostgres;

            return(new DatabaseName(null, Connector.Current.DatabaseName() + "_Export_" + DisconnectedTools.CleanMachineName(machine.MachineName), isPostgres));
        }
Пример #12
0
 public void DropDatabase(DatabaseName databaseName)
 {
     DisconnectedTools.DropDatabase(databaseName);
 }
Пример #13
0
        protected virtual void DropDatabase(Connector newDatabase)
        {
            var isPostgres = Schema.Current.Settings.IsPostgres;

            DisconnectedTools.DropDatabase(new DatabaseName(null, newDatabase.DatabaseName(), isPostgres));
        }
Пример #14
0
 private void DropDatabaseIfExists(DisconnectedMachineEntity machine)
 {
     DisconnectedTools.DropIfExists(DatabaseName(machine));
 }
Пример #15
0
 public virtual void BackupDatabase(DatabaseName databaseName, string backupFile)
 {
     DisconnectedTools.BackupDatabase(databaseName, Absolutize(backupFile));
 }
Пример #16
0
 protected virtual void DropDatabase(Connector newDatabase)
 {
     DisconnectedTools.DropDatabase(new DatabaseName(null, newDatabase.DatabaseName()));
 }
Пример #17
0
 protected virtual void DeleteTable(Table table, DatabaseName newDatabaseName)
 {
     DisconnectedTools.DeleteTable(table.Name.OnDatabase(newDatabaseName));
 }
Пример #18
0
 private void DropDatabase(SqlConnector newDatabase)
 {
     DisconnectedTools.DropDatabase(new DatabaseName(null, newDatabase.DatabaseName()));
 }
Пример #19
0
 protected virtual string BackupFileName(DisconnectedMachineEntity machine, Lite <DisconnectedImportEntity> import)
 {
     return("{0}.{1}.Import.{2}.bak".FormatWith(Connector.Current.DatabaseName(), DisconnectedTools.CleanMachineName(machine.MachineName), import.Id));
 }
Пример #20
0
 protected virtual DatabaseName DatabaseName(DisconnectedMachineEntity machine)
 {
     return(new DatabaseName(null, Connector.Current.DatabaseName() + "_Import_" + DisconnectedTools.CleanMachineName(machine.MachineName)));
 }
Пример #21
0
        public virtual Lite <DisconnectedExportEntity> BeginExportDatabase(DisconnectedMachineEntity machine)
        {
            Lite <DisconnectedExportEntity> export = new DisconnectedExportEntity
            {
                Machine = machine.ToLite(),
                Copies  = downloadTables.Select(t => new DisconnectedExportTableEmbedded
                {
                    Type = t.Type.ToTypeEntity().ToLite()
                }).ToMList()
            }.Save().ToLite();

            var cancelationSource = new CancellationTokenSource();

            var user = UserHolder.Current;

            var token = cancelationSource.Token;

            var task = Task.Factory.StartNew(() =>
            {
                using (UserHolder.UserSession(user))
                {
                    OnStartExporting(machine);
                    DisconnectedMachineEntity.Current = machine.ToLite();

                    try
                    {
                        using (token.MeasureTime(l => export.InDB().UnsafeUpdate().Set(s => s.Lock, s => l).Execute()))
                        {
                            foreach (var tuple in downloadTables)
                            {
                                token.ThrowIfCancellationRequested();

                                if (tuple.Strategy.Upload == Upload.Subset)
                                {
                                    miUnsafeLock.MakeGenericMethod(tuple.Type).Invoke(this, new object[] { machine.ToLite(), tuple.Strategy, export });
                                }
                            }
                        }

                        string connectionString;
                        using (token.MeasureTime(l => export.InDB().UnsafeUpdate().Set(s => s.CreateDatabase, s => l).Execute()))
                            connectionString = CreateDatabase(machine);

                        var newDatabase = new SqlServerConnector(connectionString, Schema.Current, ((SqlServerConnector)Connector.Current).Version);


                        using (token.MeasureTime(l => export.InDB().UnsafeUpdate().Set(s => s.CreateSchema, s => l).Execute()))
                            using (Connector.Override(newDatabase))
                                using (ObjectName.OverrideOptions(new ObjectNameOptions {
                                    AvoidDatabaseName = true
                                }))
                                {
                                    Administrator.TotalGeneration();
                                }

                        using (token.MeasureTime(l => export.InDB().UnsafeUpdate().Set(s => s.DisableForeignKeys, s => l).Execute()))
                            using (Connector.Override(newDatabase))
                                using (ObjectName.OverrideOptions(new ObjectNameOptions {
                                    AvoidDatabaseName = true
                                }))
                                {
                                    foreach (var tuple in downloadTables.Where(t => !t.Type.IsEnumEntity()))
                                    {
                                        token.ThrowIfCancellationRequested();

                                        DisableForeignKeys(tuple.Table);
                                    }
                                }

                        var isPostgres = Schema.Current.Settings.IsPostgres;
                        DatabaseName newDatabaseName = new DatabaseName(null, newDatabase.DatabaseName(), isPostgres);

                        foreach (var tuple in downloadTables)
                        {
                            token.ThrowIfCancellationRequested();
                            int ms = 0;
                            using (token.MeasureTime(l => ms = l))
                            {
                                tuple.Strategy.Exporter !.Export(tuple.Table, tuple.Strategy, newDatabaseName, machine);
                            }

                            export.MListElementsLite(_ => _.Copies).Where(c => c.Element.Type.Is(tuple.Type.ToTypeEntity())).UnsafeUpdateMList()
                            .Set(mle => mle.Element.CopyTable, mle => ms)
                            .Execute();
                        }

                        using (token.MeasureTime(l => export.InDB().UnsafeUpdate().Set(s => s.EnableForeignKeys, s => l).Execute()))
                            foreach (var tuple in downloadTables.Where(t => !t.Type.IsEnumEntity()))
                            {
                                token.ThrowIfCancellationRequested();

                                EnableForeignKeys(tuple.Table);
                            }

                        using (token.MeasureTime(l => export.InDB().UnsafeUpdate().Set(s => s.ReseedIds, s => l).Execute()))
                        {
                            var tablesToUpload = Schema.Current.Tables.Values.Where(t => DisconnectedLogic.GetStrategy(t.Type).Upload != Upload.None)
                                                 .SelectMany(t => t.TablesMList().Cast <ITable>().And(t)).Where(t => t.PrimaryKey.Identity).ToList();

                            var maxIdDictionary = tablesToUpload.ToDictionary(t => t,
                                                                              t => DisconnectedTools.MaxIdInRange(t, machine.SeedMin, machine.SeedMax));

                            using (Connector.Override(newDatabase))
                                using (ObjectName.OverrideOptions(new ObjectNameOptions {
                                    AvoidDatabaseName = true
                                }))
                                {
                                    foreach (var table in tablesToUpload)
                                    {
                                        token.ThrowIfCancellationRequested();

                                        long?max = maxIdDictionary.GetOrThrow(table);

                                        DisconnectedTools.SetNextId(table, (max + 1) ?? machine.SeedMin);
                                    }
                                }
                        }

                        CopyExport(export, newDatabase);

                        machine.InDB().UnsafeUpdate().Set(s => s.State, s => DisconnectedMachineState.Disconnected).Execute();
                        using (SqlServerConnector.Override(newDatabase))
                            using (ObjectName.OverrideOptions(new ObjectNameOptions {
                                AvoidDatabaseName = true
                            }))
                                machine.InDB().UnsafeUpdate().Set(s => s.State, s => DisconnectedMachineState.Disconnected).Execute();

                        using (token.MeasureTime(l => export.InDB().UnsafeUpdate().Set(s => s.BackupDatabase, s => l).Execute()))
                            BackupDatabase(machine, export, newDatabase);

                        using (token.MeasureTime(l => export.InDB().UnsafeUpdate().Set(s => s.DropDatabase, s => l).Execute()))
                            DropDatabase(newDatabase);

                        token.ThrowIfCancellationRequested();

                        export.InDB().UnsafeUpdate()
                        .Set(s => s.State, s => DisconnectedExportState.Completed)
                        .Set(s => s.Total, s => s.CalculateTotal())
                        .Execute();
                    }
                    catch (Exception e)
                    {
                        var ex = e.LogException();

                        export.InDB().UnsafeUpdate()
                        .Set(s => s.Exception, s => ex.ToLite())
                        .Set(s => s.State, s => DisconnectedExportState.Error)
                        .Execute();

                        OnExportingError(machine, export, e);
                    }
                    finally
                    {
                        runningExports.Remove(export);
                        DisconnectedMachineEntity.Current = null;

                        OnEndExporting();
                    }
                }
            });


            runningExports.Add(export, new RunningExports(task, cancelationSource));

            return(export);
        }
Пример #22
0
        private void DropDatabase(SqlServerConnector newDatabase)
        {
            var isPostgres = Schema.Current.Settings.IsPostgres;

            DisconnectedTools.DropDatabase(new DatabaseName(null, newDatabase.DatabaseName(), isPostgres));
        }