예제 #1
0
        private static SchemaComparisonResult CompareTable(Table source, Table target, bool strict)
        {
            SchemaComparisonResult result = new SchemaComparisonResult();

            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }

            var sourceObjectsMissingInTarget = source.Objects.Where(x => !target.Objects.Any(y => y.Name == x.Name && y.Type == x.Type));
            if (sourceObjectsMissingInTarget.Count() > 0)
            {
                result.Inequalities.AddRange(sourceObjectsMissingInTarget.Select(x => new SchemaComparisonInequality
                {
                    ObjectName = x.Name,
                    ObjectType = x.Type,
                    InequalityType = SchemaComparisonInequalityTypes.MissingInTarget
                }));
            }

            if (strict)
            {
                var targetObjectsMissingInSource = source.Objects.Where(x => !target.Objects.Any(y => y.Name == x.Name && y.Type == x.Type));
                if (targetObjectsMissingInSource.Count() > 0)
                {
                    result.Inequalities.AddRange(targetObjectsMissingInSource.Select(x => new SchemaComparisonInequality
                    {
                        ObjectName = x.Name,
                        ObjectType = x.Type,
                        InequalityType = SchemaComparisonInequalityTypes.MissingInSource
                    }));
                }
            }

            return result;
        }
        /// <summary>
        /// Converts information from sql schema discovery queries and turns it into serializable DatabaseSchema objects.
        /// </summary>
        private static Database ProcessResults(SchemaDiscoveryResult results)
        {
            if (results == null)
            {
                return new Database();
            }

            Database databaseSchema = new Database(results.DatabaseName);

            //Tables
            foreach (var tableResult in results.Objects.Where(x => x.Type == SchemaObjectTypeStrings.UserTable))
            {
                var tableSchema = new Table(tableResult.Name);

                //columns
                tableSchema.Objects.AddRange(
                    results.Columns.Where(x => x.TableName == tableResult.Name).Select(x => new TableColumn(x.ColumnName))
                );

                var tableObjects = results.Objects.Where(x => x.ParentName == tableResult.Name).ToList();
                var tableIndexes = results.Indexes.Where(x => x.TableName == tableResult.Name).ToList();

                //primary keys
                tableSchema.Objects.AddRange(
                    tableObjects.Where(x => x.Type == SchemaObjectTypeStrings.PrimaryKeyConstraint).Select(x => new PrimaryKeyConstraint(x.Name))
                );

                //foreign keys
                tableSchema.Objects.AddRange(
                    tableObjects.Where(x => x.Type == SchemaObjectTypeStrings.ForeignKeyConstraint).Select(x => new ForeignKeyConstraint(x.Name))
                );

                //default contstraints
                tableSchema.Objects.AddRange(
                    tableObjects.Where(x => x.Type == SchemaObjectTypeStrings.DefaultConstraint).Select(x => new DefaultConstraint(x.Name))
                );

                //unique contstraints
                tableSchema.Objects.AddRange(
                    tableObjects.Where(x => x.Type == SchemaObjectTypeStrings.UniqueConstraint).Select(x => new UniqueConstraint(x.Name))
                );

                //check constraints
                tableSchema.Objects.AddRange(
                    tableObjects.Where(x => x.Type == SchemaObjectTypeStrings.CheckConstraint).Select(x => new CheckConstraint(x.Name))
                );

                //triggers
                tableSchema.Objects.AddRange(
                    tableObjects.Where(x => x.Type == SchemaObjectTypeStrings.SqlTrigger).Select(x => new Trigger(x.Name))
                );

                //indexes
                tableSchema.Objects.AddRange(
                    tableIndexes.Select(x => new TableIndex(x.IndexName))
                );

                databaseSchema.Tables.Add(tableSchema);
            }

            //Stored Procedures
            databaseSchema.StoredProcedures.AddRange(
                results.Objects.Where(x => x.Type == SchemaObjectTypeStrings.SqlStoredProcedure).Select(x => new StoredProcedure(x.Name))
            );

            //Functions
            databaseSchema.Functions.AddRange(
                results.Objects.Where(x => x.Type == SchemaObjectTypeStrings.SqlScalarFunction).Select(x => new ScalarValuedFunction(x.Name))
            );
            databaseSchema.Functions.AddRange(
                results.Objects.Where(x => x.Type == SchemaObjectTypeStrings.SqlTableValuedFunction).Select(x => new TableValuedFunction(x.Name))
            );

            return databaseSchema;
        }