예제 #1
0
        public async Task <IEnumerable <MergeStatement> > GenerateMergeSprocs(string connectionString, IEnumerable <string> schemaNames)
        {
            Schema databaseSchema = null;
            var    schemaName     = "Analytics";

            /*
             * build up database objects
             */
            try
            {
                var databaseResult = await _dataAccess.GetDatabase(connectionString);

                var database = await _databaseStructure.CalculateStructure(connectionString.DatabaseName(), databaseResult);



                //            var databaseInformation = await _dataAccess.GetDatabaseInformation(connectionString);
                //
                //            var columnInfo = databaseInformation.Columns;
                //
                //            var fkInfo = databaseInformation.ForeignKeys;
                //
                //            var extendedProps = databaseInformation.ExtendedProperties;
                //
                //            databaseSchema = Schema
                //                .From(columnInfo, extendedProps, fkInfo)
                //                .Where(s => s.Name.Equals(schemaName))
                //                .FirstOrDefault();
                //
                //            databaseSchema
                //                .CalculateDependencies();
                //
                //            logger.LogInformation("Read schema information for [{schemaName}] with [{tableCount}] tables",
                //                schemaName,
                //                databaseSchema.Tables.Count());
            }
            catch (Exception ex)
            {
                logger.LogError(
                    "Unable to calculate schema: [{schemaName}] due to: {ex}",
                    schemaName,
                    ex.Message);
            }

            //
            //calculate dependency tree and build data structure
            //
            var dependencyTree = DependencyTree.Calculate(databaseSchema);

            var dataStructure = new DataStructure(databaseSchema, dependencyTree);

            logger.LogInformation("Calculated dependency tree with [{branchCount}] branches",
                                  dependencyTree.Branches.Count());

            //
            //build up merge variables
            //
            var(mergeVariables, errornousTables) = MergeVariables.From(dataStructure);

            if (errornousTables.Any())
            {
                logger.LogError("Tables that won't generate merge sprocs: [{tables}], due to exceptions: [{exceptions}]",
                                dataStructure.Schema.Tables.Select(t => t.Name).Except(mergeVariables.Select(m => m.Table.Name)),
                                errornousTables);
            }

            logger.LogInformation("Calculated [{mergeVariableCount}] merge variables",
                                  mergeVariables.Count());

            //
            //Create merge statements
            //
            var mergeStatements = new List <MergeStatement>();

            foreach (var mergeVariable in mergeVariables)
            {
                var script = await _scriptFactory.ScriptFrom(mergeVariable);

                mergeStatements.Add(new MergeStatement(script, mergeVariable));
            }

            logger.LogInformation("Built [{mergeStatementCount}] merge statements",
                                  mergeStatements.Count());

            return(mergeStatements);
        }
        public async Task <IReadOnlyCollection <MergeScript> > Generate(string connectionString, IReadOnlyCollection <string> schemas)
        {
            // Get the database to generate scripts for
            DatabaseModel database;

            try
            {
                var databaseResult = await _dataAccess.GetDatabase(connectionString, schemas);

                var databaseName = connectionString.DatabaseName();

                database = await _databaseStructure.CalculateStructure(databaseName, databaseResult);

                _logger.LogInformation(
                    "Constructed [{databaseName}] database object with [{tableCount}] tables",
                    databaseName,
                    database.Schemas.SelectMany(s => s.Tables).Count());
            }
            catch (Exception ex)
            {
                _logger.LogError(
                    ex,
                    "Unable to get database: [{databaseName}]",
                    connectionString.DatabaseName());

                return(null);
            }

            // Build up the script variables sets
            IReadOnlyCollection <IMergeScriptVariableSet> mergeScriptVariableSets;

            try
            {
                mergeScriptVariableSets = await _scriptVariableFactory.MergeVariablesFrom(database);
            }
            catch (Exception ex)
            {
                _logger.LogError(
                    ex,
                    "Unable to generate script variable sets");

                return(null);
            }

            // Generate merge scripts from the variable sets
            var mergeScripts = new List <MergeScript>();

            try
            {
                foreach (var mergeScriptVariableSet in mergeScriptVariableSets)
                {
                    var script = await _scriptFactory.ScriptFrom(mergeScriptVariableSet);

                    var mergeScript = new MergeScript(mergeScriptVariableSet.ScriptFileName, script);

                    mergeScripts.Add(mergeScript);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(
                    ex,
                    "Unable to generate scripts");

                return(null);
            }

            // Return scripts
            return(mergeScripts);
        }