예제 #1
0
        private static SelectedConfigSections FillDefaults(ArgsParser parser, SyncConfigurationSection syncConfig)
        {
            SelectedConfigSections sections = new SelectedConfigSections();

            if (string.IsNullOrEmpty(parser.ScopeName) && syncConfig.SyncScopes.Count == 1)
            {
                sections.SelectedSyncScope = syncConfig.SyncScopes.Cast <SyncScopeConfigElement>().First();
                parser.ScopeName           = sections.SelectedSyncScope.Name;
            }
            else
            {
                sections.SelectedSyncScope = syncConfig.SyncScopes.Cast <SyncScopeConfigElement>().Single((e) => e.Name.Equals(parser.ScopeName, StringComparison.InvariantCultureIgnoreCase));
            }

            if (string.IsNullOrEmpty(parser.TargetDatabaseName) && syncConfig.Databases.Count == 1)
            {
                sections.SelectedTargetDatabase = syncConfig.Databases.Cast <TargetDatabaseConfigElement>().First();
                parser.TargetDatabaseName       = sections.SelectedTargetDatabase.Name;
            }
            else
            {
                sections.SelectedTargetDatabase = syncConfig.Databases.Cast <TargetDatabaseConfigElement>().Single((e) => e.Name.Equals(parser.TargetDatabaseName, StringComparison.InvariantCultureIgnoreCase));
            }

            return(sections);
        }
예제 #2
0
        private static void ValidateConfigFile(ArgsParser parser, SyncConfigurationSection syncConfig)
        {
            if (syncConfig == null)
            {
                throw new InvalidOperationException("Unable to parse config file.");
            }

            if (syncConfig.SyncScopes.Count == 0)
            {
                throw new InvalidOperationException("Config file should contain atleast one <SyncScope> definition.");
            }

            if (syncConfig.Databases.Count == 0)
            {
                throw new InvalidOperationException(
                          "Config file should contain atleast one <TargetDatabase> definition.");
            }

            if (syncConfig.SyncScopes.Count > 1 && string.IsNullOrEmpty(parser.ScopeName))
            {
                throw new InvalidOperationException(
                          "More than one <SyncScope> definitions found. Specify /scopename parameter.");
            }

            if (syncConfig.Databases.Count > 1 && string.IsNullOrEmpty(parser.TargetDatabaseName))
            {
                throw new InvalidOperationException(
                          "More than one <TargetDatabase> definitions found. Specify /database parameter.");
            }

            if (!string.IsNullOrEmpty(parser.ScopeName) &&
                syncConfig.SyncScopes.Cast <SyncScopeConfigElement>()
                .FirstOrDefault(e => e.Name.Equals(parser.ScopeName, StringComparison.InvariantCultureIgnoreCase)) ==
                null)
            {
                throw new ArgumentException("Scopename not found in SyncConfiguration/SyncScope definition.",
                                            parser.ScopeName);
            }

            if (!string.IsNullOrEmpty(parser.TargetDatabaseName) &&
                syncConfig.Databases.Cast <TargetDatabaseConfigElement>()
                .FirstOrDefault(
                    e => e.Name.Equals(parser.TargetDatabaseName, StringComparison.InvariantCultureIgnoreCase)) ==
                null)
            {
                throw new ArgumentException("TargetDatabase not found in SyncConfiguration/Databases definition.",
                                            parser.ScopeName);
            }
        }
        public void RunTheSyncService()
        {
            var config = new SyncConfigurationSection();

            config.Jobs.Add("TestJob");
            var job = config.Jobs[0];

            job.Type            = "TestJob";
            job.IntervalSeconds = 5;

            var configSvcMock = new Mock <IConfigurationService>();

            configSvcMock.Setup(svc => svc.GetConfig())
            .Returns(config);

            var startCount  = 0;
            var syncJobMock = new Mock <ISyncJob>();

            syncJobMock.Setup(syncJob => syncJob.Start())
            .Callback(() => startCount++);

            var container = ContainerFactory.GetContainer();

            container.Configure(c =>
            {
                c.For <IConfigurationService>().Use(() => configSvcMock.Object);
                c.For <ISyncJob>().Use(syncJobMock.Object).Named("TestJob");
            });

            var syncService = container.GetInstance <ISyncService>();

            if (!syncService.Start())
            {
                Assert.Fail("Could not start the sync service.");
            }

            Thread.Sleep(3000);

            syncService.Stop();

            syncJobMock.Verify(syncJob => syncJob.Start(), Times.AtLeastOnce);
            syncJobMock.Verify(syncJob => syncJob.Stop(), Times.AtLeastOnce);
        }
예제 #4
0
        private static void ProcessConfigFile(ArgsParser parser)
        {
            DbSyncScopeDescription scopeDescription;
            Dictionary <string, Dictionary <string, string> > tablesToColumnMappingsInfo = new Dictionary <string, Dictionary <string, string> >();

            if (string.IsNullOrEmpty(parser.ConfigFile))
            {
                LogArgsError("Required argument /scopeconfig is not specified.");
                return;
            }
            if (!System.IO.File.Exists(parser.ConfigFile))
            {
                LogArgsError("Unable to find scopeconfig file '" + parser.ConfigFile + "'");
                return;
            }

            System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap()
            {
                ExeConfigFilename = parser.ConfigFile
            }, ConfigurationUserLevel.None);

            Log("Reading specified config file...");
            SyncConfigurationSection syncConfig = config.GetSection(Constants.SyncConfigurationSectionName) as SyncConfigurationSection;

            // ValidateConfigFile the config and the passed in input parameters
            ValidateConfigFile(parser, syncConfig);

            // Fill in the defaults value for the parser.
            SelectedConfigSections selectedConfig = FillDefaults(parser, syncConfig);


            Log("Generating DbSyncScopeDescription for scope {0}...", selectedConfig.SelectedSyncScope.Name);
            scopeDescription           = GetDbSyncScopeDescription(selectedConfig);
            tablesToColumnMappingsInfo = BuildColumnMappingInfo(selectedConfig);
            switch (parser.OperationMode)
            {
            case OperationMode.Provision:
                try
                {
                    SqlSyncScopeProvisioning prov = new SqlSyncScopeProvisioning(new SqlConnection(selectedConfig.SelectedTargetDatabase.GetConnectionString()),
                                                                                 scopeDescription, selectedConfig.SelectedSyncScope.IsTemplateScope ? SqlSyncScopeProvisioningType.Template : SqlSyncScopeProvisioningType.Scope);

                    // Note: Deprovisioning does not work because of a bug in the provider when you set the ObjectSchema property to “dbo”.
                    // The workaround is to not set the property (it internally assumes dbo in this case) so that things work on deprovisioning.
                    if (!String.IsNullOrEmpty(selectedConfig.SelectedSyncScope.SchemaName))
                    {
                        prov.ObjectSchema = selectedConfig.SelectedSyncScope.SchemaName;
                    }

                    foreach (SyncTableConfigElement tableElement in selectedConfig.SelectedSyncScope.SyncTables)
                    {
                        // Check and set the SchemaName for individual table if specified
                        if (!string.IsNullOrEmpty(tableElement.SchemaName))
                        {
                            prov.Tables[tableElement.GlobalName].ObjectSchema = tableElement.SchemaName;
                        }

                        prov.Tables[tableElement.GlobalName].FilterClause = tableElement.FilterClause;
                        foreach (FilterColumnConfigElement filterCol in tableElement.FilterColumns)
                        {
                            prov.Tables[tableElement.GlobalName].FilterColumns.Add(scopeDescription.Tables[tableElement.GlobalName].Columns[filterCol.Name]);
                        }
                        foreach (FilterParameterConfigElement filterParam in tableElement.FilterParameters)
                        {
                            CheckFilterParamTypeAndSize(filterParam);
                            prov.Tables[tableElement.GlobalName].FilterParameters.Add(new SqlParameter(filterParam.Name, (SqlDbType)Enum.Parse(typeof(SqlDbType), filterParam.SqlType, true)));
                            prov.Tables[tableElement.GlobalName].FilterParameters[filterParam.Name].Size = filterParam.DataSize;
                        }
                    }

                    // enable bulk procedures.
                    prov.SetUseBulkProceduresDefault(selectedConfig.SelectedSyncScope.EnableBulkApplyProcedures);

                    // Create a new set of enumeration stored procs per scope.
                    // Without this multiple scopes share the same stored procedure which is not desirable.
                    prov.SetCreateProceduresForAdditionalScopeDefault(DbSyncCreationOption.Create);

                    if (selectedConfig.SelectedSyncScope.IsTemplateScope)
                    {
                        if (!prov.TemplateExists(selectedConfig.SelectedSyncScope.Name))
                        {
                            Log("Provisioning Database {0} for template scope {1}...", selectedConfig.SelectedTargetDatabase.Name, selectedConfig.SelectedSyncScope.Name);
                            prov.Apply();
                        }
                        else
                        {
                            throw new InvalidOperationException(string.Format("Database {0} already contains a template scope {1}. Please deprovision the scope and retry.", selectedConfig.SelectedTargetDatabase.Name,
                                                                              selectedConfig.SelectedSyncScope.Name));
                        }
                    }
                    else
                    {
                        if (!prov.ScopeExists(selectedConfig.SelectedSyncScope.Name))
                        {
                            Log("Provisioning Database {0} for scope {1}...", selectedConfig.SelectedTargetDatabase.Name, selectedConfig.SelectedSyncScope.Name);
                            prov.Apply();
                        }
                        else
                        {
                            throw new InvalidOperationException(string.Format("Database {0} already contains a scope {1}. Please deprovision the scope and retry.", selectedConfig.SelectedTargetDatabase.Name,
                                                                              selectedConfig.SelectedSyncScope.Name));
                        }
                    }
                }
                catch (ConfigurationErrorsException)
                {
                    throw;
                }
                catch (InvalidOperationException)
                {
                    throw;
                }
                catch (Exception e)
                {
                    throw new InvalidOperationException("Unexpected error when executing the Provisioning command. See inner exception for details.", e);
                }
                break;

            case OperationMode.Deprovision:
                try
                {
                    SqlSyncScopeDeprovisioning deprov = new SqlSyncScopeDeprovisioning(new SqlConnection(selectedConfig.SelectedTargetDatabase.GetConnectionString()));

                    // Set the ObjectSchema property.
                    if (!String.IsNullOrEmpty(selectedConfig.SelectedSyncScope.SchemaName))
                    {
                        deprov.ObjectSchema = selectedConfig.SelectedSyncScope.SchemaName;
                    }

                    Log("Deprovisioning Database {0} for scope {1}...", selectedConfig.SelectedTargetDatabase.Name, selectedConfig.SelectedSyncScope.Name);

                    if (selectedConfig.SelectedSyncScope.IsTemplateScope)
                    {
                        deprov.DeprovisionTemplate(selectedConfig.SelectedSyncScope.Name);
                    }
                    else
                    {
                        deprov.DeprovisionScope(selectedConfig.SelectedSyncScope.Name);
                    }
                }
                catch (Exception e)
                {
                    throw new InvalidOperationException("Unexpected error when executing the Deprovisioning command. See inner exception for details.", e);
                }

                break;

            case OperationMode.Deprovisionstore:
                try
                {
                    SqlSyncScopeDeprovisioning deprov = new SqlSyncScopeDeprovisioning(new SqlConnection(selectedConfig.SelectedTargetDatabase.GetConnectionString()));

                    Log("Deprovisioning Store Database {0} ...", selectedConfig.SelectedTargetDatabase.Name);

                    deprov.DeprovisionStore();
                }
                catch (Exception e)
                {
                    throw new InvalidOperationException("Unexpected error when executing the Deprovisioning command. See inner exception for details.", e);
                }

                break;

            case OperationMode.Codegen:
                Log("Generating files...");
                EntityGenerator generator = EntityGeneratorFactory.Create(parser.CodeGenMode, selectedConfig.SelectedSyncScope.SchemaName);
                generator.GenerateEntities(parser.GeneratedFilePrefix,
                                           string.IsNullOrEmpty(parser.Namespace)
                        ? string.IsNullOrEmpty(parser.GeneratedFilePrefix) ? scopeDescription.ScopeName : parser.GeneratedFilePrefix
                        : parser.Namespace,
                                           scopeDescription, tablesToColumnMappingsInfo, parser.WorkingDirectory, parser.Language, null /*serviceUri*/);
                break;

            default:
                break;
            }
        }
예제 #5
0
        private static void ProcessConfigFile(ArgsParser parser)
        {
            DbSyncScopeDescription scopeDescription;
            Dictionary <string, Dictionary <string, string> > tablesToColumnMappingsInfo = new Dictionary <string, Dictionary <string, string> >();

            if (string.IsNullOrEmpty(parser.ConfigFile))
            {
                LogArgsError("Required argument /scopeconfig is not specified.");
                return;
            }
            if (!System.IO.File.Exists(parser.ConfigFile))
            {
                LogArgsError("Unable to find scopeconfig file '" + parser.ConfigFile + "'");
                return;
            }

            System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap()
            {
                ExeConfigFilename = parser.ConfigFile
            }, ConfigurationUserLevel.None);

            Log("Reading specified config file...");
            SyncConfigurationSection syncConfig = config.GetSection(Constants.SyncConfigurationSectionName) as SyncConfigurationSection;

            // ValidateConfigFile the config and the passed in input parameters
            ValidateConfigFile(parser, syncConfig);

            // Fill in the defaults value for the parser.
            SelectedConfigSections selectedConfig = FillDefaults(parser, syncConfig);


            Log("Generating DbSyncScopeDescription for scope {0}...", selectedConfig.SelectedSyncScope.Name);
            scopeDescription           = GetDbSyncScopeDescription(selectedConfig);
            tablesToColumnMappingsInfo = BuildColumnMappingInfo(selectedConfig);
            switch (parser.OperationMode)
            {
            case OperationMode.Provision:
                Provision(selectedConfig, scopeDescription, false, parser.WorkingDirectory);
                break;

            case OperationMode.ProvisionScript:
                Provision(selectedConfig, scopeDescription, true, parser.WorkingDirectory);
                break;

            case OperationMode.Deprovision:
                Deprovision(selectedConfig, false, parser.WorkingDirectory);
                break;

            case OperationMode.DeprovisionScript:
                Deprovision(selectedConfig, true, parser.WorkingDirectory);
                break;

            case OperationMode.Deprovisionstore:
                DeprovisionStore(selectedConfig, false, parser.WorkingDirectory);
                break;

            case OperationMode.DeprovisionstoreScript:
                DeprovisionStore(selectedConfig, true, parser.WorkingDirectory);
                break;

            case OperationMode.Codegen:
                Log("Generating files...");
                EntityGenerator generator = EntityGeneratorFactory.Create(parser.CodeGenMode, selectedConfig.SelectedSyncScope.SchemaName);
                generator.GenerateEntities(parser.GeneratedFilePrefix,
                                           string.IsNullOrEmpty(parser.Namespace)
                        ? string.IsNullOrEmpty(parser.GeneratedFilePrefix) ? scopeDescription.ScopeName : parser.GeneratedFilePrefix
                        : parser.Namespace,
                                           scopeDescription, tablesToColumnMappingsInfo, parser.WorkingDirectory, parser.Language, null /*serviceUri*/);
                break;

            default:
                break;
            }
        }
예제 #6
0
        public void Provision(String[] args)
        {
            ArgsParser parser = ArgsParser.ParseArgs(args);

            System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap()
            {
                ExeConfigFilename = parser.ConfigFile
            }, ConfigurationUserLevel.None);
            SyncConfigurationSection syncConfig = config.GetSection(Constants.SyncConfigurationSectionName) as SyncConfigurationSection;

            SelectedConfigSections selectedConfig   = FillDefaults(parser, syncConfig);
            DbSyncScopeDescription scopeDescription = GetDbSyncScopeDescription(selectedConfig);

            try
            {
                SqlSyncScopeProvisioning prov = new SqlSyncScopeProvisioning(new SqlConnection(selectedConfig.SelectedTargetDatabase.GetConnectionString()),
                                                                             scopeDescription, selectedConfig.SelectedSyncScope.IsTemplateScope ? SqlSyncScopeProvisioningType.Template : SqlSyncScopeProvisioningType.Scope);
                prov.CommandTimeout = 60;

                // Note: Deprovisioning does not work because of a bug in the provider when you set the ObjectSchema property to “dbo”.
                // The workaround is to not set the property (it internally assumes dbo in this case) so that things work on deprovisioning.
                if (!String.IsNullOrEmpty(selectedConfig.SelectedSyncScope.SchemaName))
                {
                    prov.ObjectSchema = selectedConfig.SelectedSyncScope.SchemaName;
                }

                foreach (SyncTableConfigElement tableElement in selectedConfig.SelectedSyncScope.SyncTables)
                {
                    // Check and set the SchemaName for individual table if specified
                    if (!string.IsNullOrEmpty(tableElement.SchemaName))
                    {
                        prov.Tables[tableElement.GlobalName].ObjectSchema = tableElement.SchemaName;
                    }

                    prov.Tables[tableElement.GlobalName].FilterClause = tableElement.FilterClause;
                    foreach (FilterColumnConfigElement filterCol in tableElement.FilterColumns)
                    {
                        prov.Tables[tableElement.GlobalName].FilterColumns.Add(scopeDescription.Tables[tableElement.GlobalName].Columns[filterCol.Name]);
                    }
                    foreach (FilterParameterConfigElement filterParam in tableElement.FilterParameters)
                    {
                        CheckFilterParamTypeAndSize(filterParam);
                        prov.Tables[tableElement.GlobalName].FilterParameters.Add(new SqlParameter(filterParam.Name, (SqlDbType)Enum.Parse(typeof(SqlDbType), filterParam.SqlType, true)));
                        prov.Tables[tableElement.GlobalName].FilterParameters[filterParam.Name].Size = filterParam.DataSize;
                    }
                }

                // enable bulk procedures.
                prov.SetUseBulkProceduresDefault(selectedConfig.SelectedSyncScope.EnableBulkApplyProcedures);

                // Create a new set of enumeration stored procs per scope.
                // Without this multiple scopes share the same stored procedure which is not desirable.
                prov.SetCreateProceduresForAdditionalScopeDefault(DbSyncCreationOption.Create);

                if (selectedConfig.SelectedSyncScope.IsTemplateScope)
                {
                    if (!prov.TemplateExists(selectedConfig.SelectedSyncScope.Name))
                    {
                        //Log("Provisioning Database {0} for template scope {1}...", selectedConfig.SelectedTargetDatabase.Name, selectedConfig.SelectedSyncScope.Name);
                        prov.Apply();
                    }
                    else
                    {
                        throw new InvalidOperationException(string.Format("Database {0} already contains a template scope {1}. Please deprovision the scope and retry.", selectedConfig.SelectedTargetDatabase.Name,
                                                                          selectedConfig.SelectedSyncScope.Name));
                    }
                }
                else
                {
                    if (!prov.ScopeExists(selectedConfig.SelectedSyncScope.Name))
                    {
                        //Log("Provisioning Database {0} for scope {1}...", selectedConfig.SelectedTargetDatabase.Name, selectedConfig.SelectedSyncScope.Name);
                        prov.Apply();
                    }
                    else
                    {
                        throw new InvalidOperationException(string.Format("Database {0} already contains a scope {1}. Please deprovision the scope and retry.", selectedConfig.SelectedTargetDatabase.Name,
                                                                          selectedConfig.SelectedSyncScope.Name));
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }