Beispiel #1
0
 protected virtual void RestoreDatabase(DatabaseName databaseName, string backupFile, string databaseFile, string databaseLogFile)
 {
     DisconnectedTools.RestoreDatabase(databaseName,
                                       Absolutize(backupFile),
                                       Absolutize(databaseFile),
                                       Absolutize(databaseLogFile));
 }
Beispiel #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);
        var isPostgres = Schema.Current.Settings.IsPostgres;

        DisconnectedTools.BackupDatabase(new DatabaseName(null, newDatabase.DatabaseName(), isPostgres), backupFileName);
    }
Beispiel #3
0
    protected virtual void DisableForeignKeys(Table table)
    {
        DisconnectedTools.DisableForeignKeys(table);

        foreach (var rt in table.TablesMList())
        {
            DisconnectedTools.DisableForeignKeys(rt);
        }
    }
    private static SqlPreCommand?Schema_Generating()
    {
        if (DisconnectedLogic.OfflineMode)
        {
            return(null);
        }

        return(GetTablesToSeed()
               .Select(a => DisconnectedTools.SetNextIdSync(a, ServerSeed))
               .Combine(Spacing.Simple));
    }
Beispiel #5
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));
    }
    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));
    }
Beispiel #7
0
 public void DropDatabase(DatabaseName databaseName)
 {
     DisconnectedTools.DropDatabase(databaseName);
 }
Beispiel #8
0
 public virtual void BackupDatabase(DatabaseName databaseName, string backupFile)
 {
     DisconnectedTools.BackupDatabase(databaseName, Absolutize(backupFile));
 }
Beispiel #9
0
 protected virtual void DropIfExists(DatabaseName databaseName)
 {
     DisconnectedTools.DropIfExists(databaseName);
 }
Beispiel #10
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);
    }
Beispiel #11
0
 protected virtual void DeleteTable(Table table, DatabaseName newDatabaseName)
 {
     DisconnectedTools.DeleteTable(table.Name.OnDatabase(newDatabaseName));
 }
Beispiel #12
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));
    }
Beispiel #13
0
 protected virtual string DatabaseLogFileName(DisconnectedMachineEntity machine)
 {
     return(Path.Combine(DisconnectedLogic.DatabaseFolder, Connector.Current.DatabaseName() + "_Export_" + DisconnectedTools.CleanMachineName(machine.MachineName) + "_Log.ldf"));
 }
Beispiel #14
0
    protected virtual void DropDatabase(Connector newDatabase)
    {
        var isPostgres = Schema.Current.Settings.IsPostgres;

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