public async Task TrackingTable_Exists()
        {
            var dbName = HelperDatabase.GetRandomName("tcp_lo_");
            await HelperDatabase.CreateDatabaseAsync(ProviderType.Sql, dbName, true);

            var cs          = HelperDatabase.GetConnectionString(ProviderType.Sql, dbName);
            var sqlProvider = new SqlSyncProvider(cs);

            var ctx = new AdventureWorksContext((dbName, ProviderType.Sql, sqlProvider), true, false);
            await ctx.Database.EnsureCreatedAsync();

            var options = new SyncOptions();
            var setup   = new SyncSetup(new string[] { "SalesLT.Product", "SalesLT.ProductCategory" });

            setup.TrackingTablesPrefix = "t_";
            setup.TrackingTablesSuffix = "_t";

            var remoteOrchestrator = new RemoteOrchestrator(sqlProvider, options);

            var scopeInfo = await remoteOrchestrator.GetServerScopeInfoAsync(setup);

            await remoteOrchestrator.CreateTrackingTableAsync(scopeInfo, "Product", "SalesLT");

            var exists = await remoteOrchestrator.ExistTrackingTableAsync(scopeInfo, "Product", "SalesLT");

            Assert.True(exists);

            exists = await remoteOrchestrator.ExistTrackingTableAsync(scopeInfo, "ProductCategory", "SalesLT");

            Assert.False(exists);

            HelperDatabase.DropDatabase(ProviderType.Sql, dbName);
        }
        public async Task TrackingTable_Create_One()
        {
            var dbName = HelperDatabase.GetRandomName("tcp_lo_");
            await HelperDatabase.CreateDatabaseAsync(ProviderType.Sql, dbName, true);

            var cs          = HelperDatabase.GetConnectionString(ProviderType.Sql, dbName);
            var sqlProvider = new SqlSyncProvider(cs);

            var ctx = new AdventureWorksContext((dbName, ProviderType.Sql, sqlProvider), true, false);
            await ctx.Database.EnsureCreatedAsync();

            var options = new SyncOptions();

            var setup = new SyncSetup(new string[] { "SalesLT.Product" })
            {
                TrackingTablesPrefix = "t_",
                TrackingTablesSuffix = "_t"
            };

            var remoteOrchestrator = new RemoteOrchestrator(sqlProvider, options);

            var scopeInfo = await remoteOrchestrator.GetServerScopeInfoAsync(setup);

            var onCreating = false;
            var onCreated  = false;

            remoteOrchestrator.OnTrackingTableCreating(ttca =>
            {
                var addingID              = $" ALTER TABLE {ttca.TrackingTableName.Schema().Quoted()} ADD internal_id int identity(1,1)";
                ttca.Command.CommandText += addingID;
                onCreating = true;
            });

            remoteOrchestrator.OnTrackingTableCreated(ttca =>
            {
                onCreated = true;
            });

            await remoteOrchestrator.CreateTrackingTableAsync(scopeInfo, "Product", "SalesLT");

            Assert.True(onCreating);
            Assert.True(onCreated);


            // Check we have a new column in tracking table
            using (var c = new SqlConnection(cs))
            {
                await c.OpenAsync().ConfigureAwait(false);

                var cols = await SqlManagementUtils.GetColumnsForTableAsync(c, null, "t_Product_t", "SalesLT").ConfigureAwait(false);

                Assert.Equal(7, cols.Rows.Count);
                Assert.NotNull(cols.Rows.FirstOrDefault(r => r["name"].ToString() == "internal_id"));
                c.Close();
            }

            HelperDatabase.DropDatabase(ProviderType.Sql, dbName);
        }
        public async Task TrackingTable_Drop_One_Cancel()
        {
            var dbName = HelperDatabase.GetRandomName("tcp_lo_");
            await HelperDatabase.CreateDatabaseAsync(ProviderType.Sql, dbName, true);

            var cs          = HelperDatabase.GetConnectionString(ProviderType.Sql, dbName);
            var sqlProvider = new SqlSyncProvider(cs);

            var ctx = new AdventureWorksContext((dbName, ProviderType.Sql, sqlProvider), true, false);
            await ctx.Database.EnsureCreatedAsync();

            var options = new SyncOptions();
            var setup   = new SyncSetup(new string[] { "SalesLT.Product" });

            setup.TrackingTablesPrefix = "t_";
            setup.TrackingTablesSuffix = "_t";

            var remoteOrchestrator = new RemoteOrchestrator(sqlProvider, options);

            var scopeInfo = await remoteOrchestrator.GetServerScopeInfoAsync(setup);

            var onDropping = false;
            var onDropped  = false;

            remoteOrchestrator.OnTrackingTableDropping(ttca =>
            {
                ttca.Cancel = true;
                onDropping  = true;
            });

            remoteOrchestrator.OnTrackingTableDropped(ttca =>
            {
                onDropped = true;
            });

            await remoteOrchestrator.CreateTrackingTableAsync(scopeInfo, "Product", "SalesLT");

            await remoteOrchestrator.DropTrackingTableAsync(scopeInfo, "Product", "SalesLT");

            Assert.True(onDropping);
            Assert.False(onDropped);

            // Check we have a new column in tracking table
            using (var c = new SqlConnection(cs))
            {
                await c.OpenAsync().ConfigureAwait(false);

                var table = await SqlManagementUtils.GetTableAsync(c, null, "t_Product_t", "SalesLT").ConfigureAwait(false);

                Assert.NotEmpty(table.Rows);

                c.Close();
            }

            HelperDatabase.DropDatabase(ProviderType.Sql, dbName);
        }
示例#4
0
        /// <summary>
        /// Create one tracking table
        /// </summary>
        private static async Task CreateOneTrackingTable()
        {
            var provider     = new SqlSyncProvider(serverConnectionString);
            var options      = new SyncOptions();
            var setup        = new SyncSetup("ProductCategory", "ProductModel", "Product");
            var orchestrator = new RemoteOrchestrator(provider, options);

            var serverScope = await orchestrator.GetServerScopeInfoAsync(setup);

            var spExists = await orchestrator.ExistTrackingTableAsync(serverScope, "Product");;

            if (!spExists)
            {
                await orchestrator.CreateTrackingTableAsync(serverScope, "Product");
            }
        }
示例#5
0
        /// <summary>
        /// Create one tracking table
        /// </summary>
        private static async Task CreateOneTrackingTable()
        {
            var provider     = new SqlSyncProvider(serverConnectionString);
            var options      = new SyncOptions();
            var setup        = new SyncSetup(new string[] { "ProductCategory", "ProductModel", "Product" });
            var orchestrator = new RemoteOrchestrator(provider, options, setup);

            // working on the product Table
            var productSetupTable = setup.Tables["Product"];

            var spExists = await orchestrator.ExistTrackingTableAsync(productSetupTable);

            if (!spExists)
            {
                await orchestrator.CreateTrackingTableAsync(productSetupTable);
            }
        }
示例#6
0
        /// <summary>
        /// Create one tracking table
        /// </summary>
        private static async Task AddingOneColumnInTrackingTable()
        {
            var provider     = new SqlSyncProvider(serverConnectionString);
            var options      = new SyncOptions();
            var setup        = new SyncSetup(new string[] { "ProductCategory", "ProductModel", "Product" });
            var orchestrator = new RemoteOrchestrator(provider, options, setup);

            // working on the product Table
            var productSetupTable = setup.Tables["Product"];

            orchestrator.OnTrackingTableCreating(ttca =>
            {
                var addingID              = $" ALTER TABLE {ttca.TrackingTableName.Schema().Quoted()} ADD internal_id varchar(10) null";
                ttca.Command.CommandText += addingID;
            });

            var trExists = await orchestrator.ExistTrackingTableAsync(productSetupTable);

            if (!trExists)
            {
                await orchestrator.CreateTrackingTableAsync(productSetupTable);
            }

            orchestrator.OnTriggerCreating(tca =>
            {
                string val;
                if (tca.TriggerType == DbTriggerType.Insert)
                {
                    val = "INS";
                }
                else if (tca.TriggerType == DbTriggerType.Delete)
                {
                    val = "DEL";
                }
                else
                {
                    val = "UPD";
                }

                var cmdText = $"UPDATE Product_tracking " +
                              $"SET Product_tracking.internal_id='{val}' " +
                              $"FROM Product_tracking JOIN Inserted ON Product_tracking.ProductID = Inserted.ProductID;";

                tca.Command.CommandText += Environment.NewLine + cmdText;
            });

            var trgExists = await orchestrator.ExistTriggerAsync(productSetupTable, DbTriggerType.Insert);

            if (!trgExists)
            {
                await orchestrator.CreateTriggerAsync(productSetupTable, DbTriggerType.Insert);
            }

            trgExists = await orchestrator.ExistTriggerAsync(productSetupTable, DbTriggerType.Update);

            if (!trgExists)
            {
                await orchestrator.CreateTriggerAsync(productSetupTable, DbTriggerType.Update);
            }

            trgExists = await orchestrator.ExistTriggerAsync(productSetupTable, DbTriggerType.Delete);

            if (!trgExists)
            {
                await orchestrator.CreateTriggerAsync(productSetupTable, DbTriggerType.Delete);
            }

            orchestrator.OnTriggerCreating(null);
        }