示例#1
0
        public void ProgressiveRestore()
        {
            var mover = new AgDatabaseMove(new MoveOptions {
                Source        = Source,
                Destination   = Test,
                Overwrite     = false,
                Finalize      = false,
                FileRelocator = RestoreFileRelocator
            });

            int     seconds;
            decimal?lastLsn = null;

            // AgDatabaseMove the database and subsequent log files
            do
            {
                var timer = new Stopwatch();
                timer.Start();
                lastLsn = mover.Move(lastLsn);
                timer.Stop();
                seconds = timer.Elapsed.Seconds;
            } while(seconds > 5);

            // Do things here to disconnect users: Set single user mode, signal the service etc.
            Source.RestrictedUserMode();

            // Hopefully a quick backup and restore
            mover._options.Finalize = true;
            mover.Move(lastLsn);

            // The database is migrated
            Source.MultiUserMode();
            Test.Delete();
        }
示例#2
0
        private static void Main(string[] args)
        {
            var builder   = new ConfigurationBuilder().AddCommandLine(args);
            var arguments = builder.Build().Get <MoveArgs>();

            Console.WriteLine("Beginning AgDatabaseMove...");

            var mover = new AgDatabaseMove(new MoveOptions {
                Source        = new AgDatabase(arguments.From),
                Destination   = new AgDatabase(arguments.To),
                Overwrite     = arguments.Overwrite,
                Finalize      = arguments.Finalize,
                CopyLogins    = arguments.CopyLogins,
                DeleteSource  = arguments.DeleteSource,
                FileRelocator = filename =>
                                RestoreFileRelocator(arguments.From.DatabaseName, arguments.To.DatabaseName, filename)
            });

            try {
                mover.Move();
            }
            catch (Exception e) {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);

                Console.WriteLine(e.InnerException?.Message);
                Console.WriteLine(e.InnerException?.StackTrace);
                throw;
            }
        }
示例#3
0
        public void DefaultDatabase(string sourceDbName, string destinationDbName, string defaultDb)
        {
            var source = new Mock <IAgDatabase>();

            source.Setup(s => s.Name).Returns(sourceDbName);

            var destination = new Mock <IAgDatabase>();

            destination.Setup(d => d.Name).Returns(destinationDbName);
            var restore = new AgDatabaseMove(new MoveOptions {
                Source = source.Object, Destination = destination.Object
            });

            restore.UpdateDefaultDb(_loginProperties);

            Assert.Equal(defaultDb, _loginProperties.DefaultDatabase);
        }
示例#4
0
        public void RestoreAndCleanup()
        {
            Assert.False(Test.Exists());

            var mover = new AgDatabaseMove(new MoveOptions {
                Source        = Source,
                Destination   = Test,
                Finalize      = true,
                CopyLogins    = true,
                FileRelocator = RestoreFileRelocator
            });

            mover.Move();
            Assert.True(Test.Exists());

            Test.Delete();
            Assert.False(Test.Exists());
        }
示例#5
0
        public void DetectsInitializing()
        {
            // AgDatabaseMove databases across AG instances
            Assert.False(Test.Exists());

            var mover = new AgDatabaseMove(new MoveOptions {
                Source        = Source,
                Destination   = Test,
                Overwrite     = false,
                Finalize      = false,
                FileRelocator = RestoreFileRelocator
            });

            mover.Move();

            // AgDatabaseMove with recovery primary
            Test.FinalizePrimary();

            // Write sufficient data to the primary
            var connectionStringBuilder = new SqlConnectionStringBuilder(_testRestoreFixture._config.To.ConnectionString);

            connectionStringBuilder.InitialCatalog = Test.Name;
            using var connection = new SqlConnection(connectionStringBuilder.ToString());
            connection.Open();
            var createTableSql =
                "CREATE TABLE TestSync (Id INT IDENTITY(1,1) NOT NULL PRIMARY KEY, script VARCHAR(MAX) NULL)";

            using var createTable = new SqlCommand(createTableSql, connection);
            createTable.ExecuteNonQuery();

            var fillTableSql =
                "INSERT INTO TestSync (script) (SELECT TOP 100000 sm.[definition] FROM sys.all_sql_modules AS sm CROSS JOIN sys.all_sql_modules AS asm)";

            using var fillTable = new SqlCommand(fillTableSql, connection);
            fillTable.ExecuteNonQuery();

            Test.JoinAg();

            Assert.True(Test.IsInitializing());

            Test.Delete();
        }