Ejemplo n.º 1
0
        /// <summary>
        /// Returns full definition of the column.
        /// </summary>
        /// <param name="addDefaults">Whether default value should be added in case NOT NULL constraint is specified but no default value is set.</param>
        /// <returns>Full definition of the column.</returns>
        public string GetFullDefinition(bool addDefaults)
        {
            var definitionSql = new StringBuilder(100);

            definitionSql.Append(PgDiffStringExtension.QuoteName(this.Name));
            definitionSql.Append(' ');
            definitionSql.Append(this.Type);

            if (this.DefaultValue != null && this.DefaultValue.Length > 0)
            {
                definitionSql.Append(" DEFAULT ");
                definitionSql.Append(this.DefaultValue);
            }
            else if (!this.NullValue && addDefaults)
            {
                var defaultColValue = PgColumnUtils.GetDefaultValue(this.Type);

                if (defaultColValue != null)
                {
                    definitionSql.Append(" DEFAULT ");
                    definitionSql.Append(defaultColValue);
                }
            }

            if (!this.NullValue)
            {
                definitionSql.Append(" NOT NULL");
            }

            return(definitionSql.ToString());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Generate the needed alter table xxx set storage when needed.
        /// </summary>
        private static void AddAlterStorage(StreamWriter writer, PgTable oldTable, PgTable newTable, SearchPathHelper searchPathHelper)
        {
            foreach (PgColumn newColumn in newTable.Columns)
            {
                PgColumn oldColumn = oldTable.GetColumn(newColumn.Name);

                var oldStorage = (oldColumn == null || oldColumn.Storage == null || oldColumn.Storage == string.Empty) ? null : oldColumn.Storage;

                var newStorage = (newColumn.Storage == null || newColumn.Storage == string.Empty) ? null : newColumn.Storage;

                if (newStorage == null && oldStorage != null)
                {
                    searchPathHelper.OutputSearchPath(writer);
                    writer.WriteLine();
                    writer.WriteLine($"WarningUnableToDetermineStorageType {newTable.Name + '.' + newColumn.Name}");

                    continue;
                }

                if (newStorage == null || newStorage.Equals(oldStorage, StringComparison.CurrentCultureIgnoreCase))
                {
                    continue;
                }

                searchPathHelper.OutputSearchPath(writer);
                writer.WriteLine();
                writer.Write("ALTER TABLE ONLY ");
                writer.Write(PgDiffStringExtension.QuoteName(newTable.Name));
                writer.Write(" ALTER COLUMN ");
                writer.Write(PgDiffStringExtension.QuoteName(newColumn.Name));
                writer.Write(" SET STORAGE ");
                writer.Write(newStorage);
                writer.Write(';');
            }
        }
Ejemplo n.º 3
0
            /// <summary>
            /// Creates argument declaration.
            /// </summary>
            /// <param name="includeDefaultValue">Whether to include default value.</param>
            public string GetDeclaration(bool includeDefaultValue)
            {
                var declarationSql = new StringBuilder(50);

                if (this.mode != null && !"IN".Equals(this.mode, StringComparison.CurrentCultureIgnoreCase))
                {
                    declarationSql.Append(this.mode);
                    declarationSql.Append(' ');
                }

                if (this.Name != null && this.Name.Length > 0)
                {
                    declarationSql.Append(PgDiffStringExtension.QuoteName(this.Name));
                    declarationSql.Append(' ');
                }

                declarationSql.Append(this.DataType);

                if (includeDefaultValue && this.DefaultExpression != null && this.DefaultExpression.Length > 0)
                {
                    declarationSql.Append(" = ");
                    declarationSql.Append(this.DefaultExpression);
                }

                return(declarationSql.ToString());
            }
Ejemplo n.º 4
0
        /// <summary>
        /// Checks whether OIDS are dropped from the new table. There is no way to add OIDS to existing table so we do not
        /// create SQL statement for addition of OIDS but we issue warning.
        /// </summary>
        private static void CheckWithOIDS(StreamWriter writer, PgTable oldTable, PgTable newTable, SearchPathHelper searchPathHelper)
        {
            if ((oldTable.With == null && newTable.With == null) ||
                (oldTable.With != null && oldTable.With.Equals(newTable.With)))
            {
                return;
            }

            searchPathHelper.OutputSearchPath(writer);
            writer.WriteLine();
            writer.WriteLine("ALTER TABLE " + PgDiffStringExtension.QuoteName(newTable.Name));

            if (newTable.With == null || "OIDS=false".Equals(newTable.With, StringComparison.CurrentCultureIgnoreCase))
            {
                writer.WriteLine("\tSET WITHOUT OIDS;");
            }
            else if ("OIDS".Equals(newTable.With, StringComparison.CurrentCultureIgnoreCase) || "OIDS=true".Equals(newTable.With, StringComparison.CurrentCultureIgnoreCase))
            {
                writer.WriteLine("\tSET WITH OIDS;");
            }
            else
            {
                writer.WriteLine("\tSET " + newTable.With + ";");
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Adds statements for modification of columns to the list of statements.
        /// </summary>
        private static void AddModifyTableColumns(IList <string> statements, PgTable oldTable, PgTable newTable, IList <PgColumn> dropDefaultsColumns, bool addDefaults)
        {
            foreach (PgColumn newColumn in newTable.Columns)
            {
                if (!oldTable.ContainsColumn(newColumn.Name))
                {
                    continue;
                }

                PgColumn oldColumn = oldTable.GetColumn(newColumn.Name);

                var newColumnName = PgDiffStringExtension.QuoteName(newColumn.Name);

                if (!oldColumn.Type.Equals(newColumn.Type))
                {
                    statements.Add($"\tALTER COLUMN {newColumnName} TYPE {newColumn.Type} /* TypeParameterChange of table {newTable.Name}. Old: {oldColumn.Type} New: {newColumn.Type} */");
                }

                var oldDefault = oldColumn.DefaultValue ?? string.Empty;
                var newDefault = newColumn.DefaultValue ?? string.Empty;

                if (!oldDefault.Equals(newDefault))
                {
                    if (newDefault.Length == 0)
                    {
                        statements.Add("\tALTER COLUMN " + newColumnName + " DROP DEFAULT");
                    }
                    else
                    {
                        statements.Add("\tALTER COLUMN " + newColumnName + " SET DEFAULT " + newDefault);
                    }
                }

                if (oldColumn.NullValue != newColumn.NullValue)
                {
                    if (newColumn.NullValue)
                    {
                        statements.Add("\tALTER COLUMN " + newColumnName + " DROP NOT NULL");
                    }
                    else
                    {
                        if (addDefaults)
                        {
                            var defaultValue = PgColumnUtils.GetDefaultValue(newColumn.Type);

                            if (defaultValue != null)
                            {
                                statements.Add("\tALTER COLUMN " + newColumnName + " SET DEFAULT " + defaultValue);
                                dropDefaultsColumns.Add(newColumn);
                            }
                        }

                        statements.Add("\tALTER COLUMN " + newColumnName + " SET NOT NULL");
                    }
                }
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Outputs statements for tables and columns for which comments have
        /// changed.
        /// </summary>
        private static void AlterComments(StreamWriter writer, PgTable oldTable, PgTable newTable, SearchPathHelper searchPathHelper)
        {
            if ((oldTable.Comment == null && newTable.Comment != null) ||
                (oldTable.Comment != null &&
                 newTable.Comment != null &&
                 !oldTable.Comment.Equals(newTable.Comment)))
            {
                searchPathHelper.OutputSearchPath(writer);
                writer.WriteLine();
                writer.Write("COMMENT ON TABLE ");
                writer.Write(PgDiffStringExtension.QuoteName(newTable.Name));
                writer.Write(" IS ");
                writer.Write(newTable.Comment);
                writer.WriteLine(';');
            }
            else if (oldTable.Comment != null && newTable.Comment == null)
            {
                searchPathHelper.OutputSearchPath(writer);
                writer.WriteLine();
                writer.Write("COMMENT ON TABLE ");
                writer.Write(PgDiffStringExtension.QuoteName(newTable.Name));
                writer.WriteLine(" IS NULL;");
            }

            foreach (PgColumn newColumn in newTable.Columns)
            {
                PgColumn oldColumn = oldTable.GetColumn(newColumn.Name);

                var oldComment = oldColumn?.Comment;

                var newComment = newColumn.Comment;

                if (newComment != null && (oldComment == null ? newComment != null : !oldComment.Equals(newComment)))
                {
                    searchPathHelper.OutputSearchPath(writer);
                    writer.WriteLine();
                    writer.Write("COMMENT ON COLUMN ");
                    writer.Write(PgDiffStringExtension.QuoteName(newTable.Name));
                    writer.Write('.');
                    writer.Write(PgDiffStringExtension.QuoteName(newColumn.Name));
                    writer.Write(" IS ");
                    writer.Write(newColumn.Comment);
                    writer.WriteLine(';');
                }
                else if (oldComment != null && newComment == null)
                {
                    searchPathHelper.OutputSearchPath(writer);
                    writer.WriteLine();
                    writer.Write("COMMENT ON COLUMN ");
                    writer.Write(PgDiffStringExtension.QuoteName(newTable.Name));
                    writer.Write('.');
                    writer.Write(PgDiffStringExtension.QuoteName(newColumn.Name));
                    writer.WriteLine(" IS NULL;");
                }
            }
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Adds statements for removal of columns to the list of statements.
 /// </summary>
 private static void AddDropTableColumns(IList <string> statements, PgTable oldTable, PgTable newTable)
 {
     foreach (PgColumn column in oldTable.Columns)
     {
         if (!newTable.ContainsColumn(column.Name))
         {
             statements.Add("\tDROP COLUMN " + PgDiffStringExtension.QuoteName(column.Name));
         }
     }
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Outputs statements for trigger comments that have changed.
        /// </summary>
        public static void AlterComments(StreamWriter writer, [NullGuard.AllowNull] PgSchema oldSchema, PgSchema newSchema, SearchPathHelper searchPathHelper)
        {
            if (oldSchema == null)
            {
                return;
            }

            foreach (PgTable oldTable in oldSchema.Tables)
            {
                PgTable newTable = newSchema.GetTable(oldTable.Name);

                if (newTable == null)
                {
                    continue;
                }

                foreach (PgTrigger oldTrigger in oldTable.Triggers)
                {
                    PgTrigger newTrigger = newTable.GetTrigger(oldTrigger.Name);

                    if (newTrigger == null)
                    {
                        continue;
                    }

                    if ((oldTrigger.Comment == null && newTrigger.Comment != null) ||
                        (oldTrigger.Comment != null &&
                         newTrigger.Comment != null &&
                         !oldTrigger.Comment.Equals(newTrigger.Comment)))
                    {
                        searchPathHelper.OutputSearchPath(writer);
                        writer.WriteLine();
                        writer.Write("COMMENT ON TRIGGER ");
                        writer.Write(PgDiffStringExtension.QuoteName(newTrigger.Name));
                        writer.Write(" ON ");
                        writer.Write(PgDiffStringExtension.QuoteName(newTrigger.TableName));
                        writer.Write(" IS ");
                        writer.Write(newTrigger.Comment);
                        writer.WriteLine(';');
                    }
                    else if (oldTrigger.Comment != null && newTrigger.Comment == null)
                    {
                        searchPathHelper.OutputSearchPath(writer);
                        writer.WriteLine();
                        writer.Write("COMMENT ON TRIGGER ");
                        writer.Write(PgDiffStringExtension.QuoteName(newTrigger.Name));
                        writer.Write(" ON ");
                        writer.Write(PgDiffStringExtension.QuoteName(newTrigger.TableName));
                        writer.WriteLine(" IS NULL;");
                    }
                }
            }
        }
        public string RenameToSql(string newName)
        {
            var renameSql = new StringBuilder("ALTER TABLE ");

            renameSql.Append(PgDiffStringExtension.QuoteName(this.TableName));
            renameSql.Append("\tRENAME CONSTRAINT ");
            renameSql.Append(PgDiffStringExtension.QuoteName(this.Name));
            renameSql.Append(" TO ");
            renameSql.Append(PgDiffStringExtension.QuoteName(newName));
            renameSql.Append(';');

            return(renameSql.ToString());
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Gets the SQL which can be used to alter the <see cref="PgType"/> and adds the given value to it.
        /// </summary>
        /// <param name="newValue">Value which is added to the <see cref="PgType"/>.</param>
        /// <returns>The SQL which can alter the <see cref="PgType"/>.</returns>
        public string AlterSQL(string newValue)
        {
            var alterString = new StringBuilder(100);

            alterString.AppendLine("-- Add value only if it does not exist because if the undo diff is called the value wont be removed (not possible in postgres 9.4 without dropping the whole enum)");
            alterString.Append("ALTER TYPE ");
            alterString.Append(PgDiffStringExtension.QuoteName(this.Name));
            alterString.Append(" ADD VALUE IF NOT EXISTS ");
            alterString.Append(newValue);
            alterString.Append(";");

            return(alterString.ToString());
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Checks tablespace modification.
        /// </summary>
        private static void CheckTablespace(StreamWriter writer, PgTable oldTable, PgTable newTable, SearchPathHelper searchPathHelper)
        {
            if ((oldTable.Tablespace == null && newTable.Tablespace == null) ||
                (oldTable.Tablespace != null &&
                 oldTable.Tablespace.Equals(newTable.Tablespace)))
            {
                return;
            }

            searchPathHelper.OutputSearchPath(writer);
            writer.WriteLine();
            writer.WriteLine("ALTER TABLE " + PgDiffStringExtension.QuoteName(newTable.Name));
            writer.WriteLine("\tTABLESPACE " + newTable.Tablespace + ';');
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Generate the needed alter table xxx set statistics when needed.
        /// </summary>
        private static void AddAlterStatistics(StreamWriter writer, PgTable oldTable, PgTable newTable, SearchPathHelper searchPathHelper)
        {
            var stats = new Dictionary <string, int?>();

            foreach (PgColumn newColumn in newTable.Columns)
            {
                PgColumn oldColumn = oldTable.GetColumn(newColumn.Name);

                if (oldColumn != null)
                {
                    int?oldStat = oldColumn.Statistics;

                    int?newStat      = newColumn.Statistics;
                    int?newStatValue = null;

                    if (newStat != null && (oldStat == null || !newStat.Equals(oldStat)))
                    {
                        newStatValue = newStat;
                    }
                    else if (oldStat != null && newStat == null)
                    {
                        newStatValue = Convert.ToInt32(-1);
                    }

                    if (newStatValue != null)
                    {
                        stats[newColumn.Name] = newStatValue;
                    }
                }
            }

            foreach (KeyValuePair <string, int?> entry in stats)
            {
                searchPathHelper.OutputSearchPath(writer);
                writer.WriteLine();
                writer.Write("ALTER TABLE ONLY ");
                writer.Write(PgDiffStringExtension.QuoteName(newTable.Name));
                writer.Write(" ALTER COLUMN ");
                writer.Write(PgDiffStringExtension.QuoteName(entry.Key));
                writer.Write(" SET STATISTICS ");
                writer.Write(entry.Value);
                writer.WriteLine(';');
            }
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Outputs statements for dropping of clusters.
        /// </summary>
        public static void CreateClusters(StreamWriter writer, [NullGuard.AllowNull] PgSchema oldSchema, PgSchema newSchema, SearchPathHelper searchPathHelper)
        {
            foreach (PgTable newTable in newSchema.Tables)
            {
                PgTable oldTable;

                if (oldSchema == null)
                {
                    oldTable = null;
                }
                else
                {
                    oldTable = oldSchema.GetTable(newTable.Name);
                }

                string oldCluster;

                if (oldTable == null)
                {
                    oldCluster = null;
                }
                else
                {
                    oldCluster = oldTable.ClusterIndexName;
                }

                var newCluster = newTable.ClusterIndexName;

                if ((oldCluster == null && newCluster != null) || (oldCluster != null && newCluster != null && newCluster.CompareTo(oldCluster) != 0))
                {
                    searchPathHelper.OutputSearchPath(writer);
                    writer.WriteLine();
                    writer.Write("ALTER TABLE ");
                    writer.Write(PgDiffStringExtension.QuoteName(newTable.Name));
                    writer.Write(" CLUSTER ON ");
                    writer.Write(PgDiffStringExtension.QuoteName(newCluster));
                    writer.WriteLine(';');
                }
            }
        }
        /// <summary>
        /// Outputs statements for index comments that have changed.
        /// </summary>
        public static void AlterComments(StreamWriter writer, [NullGuard.AllowNull] PgSchema oldSchema, PgSchema newSchema, SearchPathHelper searchPathHelper)
        {
            if (oldSchema == null)
            {
                return;
            }

            foreach (PgIndex oldIndex in oldSchema.Indexes)
            {
                PgIndex newIndex = newSchema.GetIndex(oldIndex.Name);

                if (newIndex == null)
                {
                    continue;
                }

                if ((oldIndex.Comment == null && newIndex.Comment != null) ||
                    (oldIndex.Comment != null &&
                     newIndex.Comment != null &&
                     !oldIndex.Comment.Equals(newIndex.Comment)))
                {
                    searchPathHelper.OutputSearchPath(writer);
                    writer.WriteLine();
                    writer.Write("COMMENT ON INDEX ");
                    writer.Write(PgDiffStringExtension.QuoteName(newIndex.Name));
                    writer.Write(" IS ");
                    writer.Write(newIndex.Comment);
                    writer.WriteLine(';');
                }
                else if (oldIndex.Comment != null && newIndex.Comment == null)
                {
                    searchPathHelper.OutputSearchPath(writer);
                    writer.WriteLine();
                    writer.Write("COMMENT ON INDEX ");
                    writer.Write(PgDiffStringExtension.QuoteName(newIndex.Name));
                    writer.WriteLine(" IS NULL;");
                }
            }
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Outputs statements for addition, removal and modifications of table
        /// columns.
        /// </summary>
        private static void UpdateTableColumns(StreamWriter writer, PgTable oldTable, PgTable newTable, SearchPathHelper searchPathHelper)
        {
            IList <string>   statements          = new List <string>();
            IList <PgColumn> dropDefaultsColumns = new List <PgColumn>();

            AddDropTableColumns(statements, oldTable, newTable);
            AddCreateTableColumns(statements, oldTable, newTable, dropDefaultsColumns, false);
            AddModifyTableColumns(statements, oldTable, newTable, dropDefaultsColumns, false);

            if (statements.Count > 0)
            {
                var quotedTableName = PgDiffStringExtension.QuoteName(newTable.Name);
                searchPathHelper.OutputSearchPath(writer);
                writer.WriteLine();
                writer.WriteLine("ALTER TABLE " + quotedTableName);

                for (var i = 0; i < statements.Count; i++)
                {
                    writer.Write(statements[i]);
                    writer.WriteLine((i + 1) < statements.Count ? "," : ";");
                }

                if (dropDefaultsColumns.Count > 0)
                {
                    writer.WriteLine();
                    writer.WriteLine("ALTER TABLE " + quotedTableName);

                    for (var i = 0; i < dropDefaultsColumns.Count; i++)
                    {
                        writer.Write("\tALTER COLUMN ");
                        writer.Write(PgDiffStringExtension.QuoteName(dropDefaultsColumns[i].Name));
                        writer.Write(" DROP DEFAULT");
                        writer.WriteLine((i + 1) < dropDefaultsColumns.Count ? "," : ";");
                    }
                }
            }
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Checks whether there is a discrepancy in INHERITS for original and new
        /// table.
        /// </summary>
        private static void CheckInherits(StreamWriter writer, PgTable oldTable, PgTable newTable, SearchPathHelper searchPathHelper)
        {
            foreach (var tableName in oldTable.Inherits)
            {
                if (!newTable.Inherits.Contains(tableName))
                {
                    searchPathHelper.OutputSearchPath(writer);
                    writer.WriteLine();
                    writer.WriteLine("ALTER TABLE " + PgDiffStringExtension.QuoteName(newTable.Name));
                    writer.WriteLine("\tNO INHERIT " + PgDiffStringExtension.QuoteName(tableName) + ';');
                }
            }

            foreach (var tableName in newTable.Inherits)
            {
                if (!oldTable.Inherits.Contains(tableName))
                {
                    searchPathHelper.OutputSearchPath(writer);
                    writer.WriteLine();
                    writer.WriteLine("ALTER TABLE " + PgDiffStringExtension.QuoteName(newTable.Name));
                    writer.WriteLine("\tINHERIT " + PgDiffStringExtension.QuoteName(tableName) + ';');
                }
            }
        }
        /// <summary>
        /// Outputs statement for modified sequences.
        /// </summary>
        public static void Alter(StreamWriter writer, [NullGuard.AllowNull] PgSchema oldSchema, PgSchema newSchema, SearchPathHelper searchPathHelper, bool ignoreStartWith)
        {
            if (oldSchema == null)
            {
                return;
            }

            var alterSequenceSql = new StringBuilder(100);

            foreach (PgSequence newSequence in newSchema.Sequences)
            {
                PgSequence oldSequence = oldSchema.GetSequence(newSequence.Name);

                if (oldSequence == null)
                {
                    continue;
                }

                alterSequenceSql.Length = 0;

                var oldIncrement = oldSequence.Increment;
                var newIncrement = newSequence.Increment;

                if (newIncrement != null && !newIncrement.Equals(oldIncrement))
                {
                    alterSequenceSql.Append("\n\tINCREMENT BY ");
                    alterSequenceSql.Append(newIncrement);
                }

                var oldMinValue = oldSequence.MinValue;
                var newMinValue = newSequence.MinValue;

                if (newMinValue == null && oldMinValue != null)
                {
                    alterSequenceSql.Append("\n\tNO MINVALUE");
                }
                else if (newMinValue != null && !newMinValue.Equals(oldMinValue))
                {
                    alterSequenceSql.Append("\n\tMINVALUE ");
                    alterSequenceSql.Append(newMinValue);
                }

                var oldMaxValue = oldSequence.MaxValue;
                var newMaxValue = newSequence.MaxValue;

                if (newMaxValue == null && oldMaxValue != null)
                {
                    alterSequenceSql.Append("\n\tNO MAXVALUE");
                }
                else if (newMaxValue != null && !newMaxValue.Equals(oldMaxValue))
                {
                    alterSequenceSql.Append("\n\tMAXVALUE ");
                    alterSequenceSql.Append(newMaxValue);
                }

                if (!ignoreStartWith)
                {
                    var oldStart = oldSequence.StartWith;
                    var newStart = newSequence.StartWith;

                    if (newStart != null && !newStart.Equals(oldStart))
                    {
                        alterSequenceSql.Append("\n\tRESTART WITH ");
                        alterSequenceSql.Append(newStart);
                    }
                }

                var oldCache = oldSequence.Cache;
                var newCache = newSequence.Cache;

                if (newCache != null && !newCache.Equals(oldCache))
                {
                    alterSequenceSql.Append("\n\tCACHE ");
                    alterSequenceSql.Append(newCache);
                }

                var oldCycle = oldSequence.Cycle;
                var newCycle = newSequence.Cycle;

                if (oldCycle && !newCycle)
                {
                    alterSequenceSql.Append("\n\tNO CYCLE");
                }
                else if (!oldCycle && newCycle)
                {
                    alterSequenceSql.Append("\n\tCYCLE");
                }

                var oldOwnedBy = oldSequence.Owner;
                var newOwnedBy = newSequence.Owner;

                if (newOwnedBy != null && !newOwnedBy.Equals(oldOwnedBy))
                {
                    alterSequenceSql.Append("\n\tOWNED BY ");
                    alterSequenceSql.Append(newOwnedBy);
                }

                if (alterSequenceSql.Length > 0)
                {
                    searchPathHelper.OutputSearchPath(writer);
                    writer.WriteLine();
                    writer.Write("ALTER SEQUENCE " + PgDiffStringExtension.QuoteName(newSequence.Name));
                    writer.Write(alterSequenceSql.ToString());
                    writer.WriteLine(';');
                }

                if ((oldSequence.Comment == null && newSequence.Comment != null) ||
                    (oldSequence.Comment != null &&
                     newSequence.Comment != null &&
                     !oldSequence.Comment.Equals(newSequence.Comment)))
                {
                    searchPathHelper.OutputSearchPath(writer);
                    writer.WriteLine();
                    writer.Write("COMMENT ON SEQUENCE ");
                    writer.Write(PgDiffStringExtension.QuoteName(newSequence.Name));
                    writer.Write(" IS ");
                    writer.Write(newSequence.Comment);
                    writer.WriteLine(';');
                }
                else if (oldSequence.Comment != null && newSequence.Comment == null)
                {
                    searchPathHelper.OutputSearchPath(writer);
                    writer.WriteLine();
                    writer.Write("COMMENT ON SEQUENCE ");
                    writer.Write(newSequence.Name);
                    writer.WriteLine(" IS NULL;");
                }
            }
        }
        /// <summary>
        /// Outputs statements for function comments that have changed.
        /// </summary>
        public static void AlterComments(StreamWriter writer, [NullGuard.AllowNull] PgSchema oldSchema, PgSchema newSchema, SearchPathHelper searchPathHelper)
        {
            if (oldSchema == null)
            {
                return;
            }

            foreach (PgFunction oldfunction in oldSchema.Functions)
            {
                PgFunction newFunction = newSchema.GetFunction(oldfunction.Signature);

                if (newFunction == null)
                {
                    continue;
                }

                if ((oldfunction.Comment == null && newFunction.Comment != null) ||
                    (oldfunction.Comment != null && newFunction.Comment != null && !oldfunction.Comment.Equals(newFunction.Comment)))
                {
                    searchPathHelper.OutputSearchPath(writer);
                    writer.WriteLine();

                    writer.Write("COMMENT ON FUNCTION ");
                    writer.Write(PgDiffStringExtension.QuoteName(newFunction.Name));
                    writer.Write('(');

                    var addComma = false;
                    foreach (PgFunction.Argument argument in newFunction.Arguments)
                    {
                        if (addComma)
                        {
                            writer.Write(", ");
                        }
                        else
                        {
                            addComma = true;
                        }

                        writer.Write(argument.GetDeclaration(false));
                    }

                    writer.Write(") IS ");
                    writer.Write(newFunction.Comment);
                    writer.WriteLine(';');
                }
                else if (oldfunction.Comment != null && newFunction.Comment == null)
                {
                    searchPathHelper.OutputSearchPath(writer);
                    writer.WriteLine();

                    writer.Write("COMMENT ON FUNCTION ");
                    writer.Write(PgDiffStringExtension.QuoteName(newFunction.Name));
                    writer.Write('(');

                    var addComma = false;
                    foreach (PgFunction.Argument argument in newFunction.Arguments)
                    {
                        if (addComma)
                        {
                            writer.Write(", ");
                        }
                        else
                        {
                            addComma = true;
                        }

                        writer.Write(argument.GetDeclaration(false));
                    }

                    writer.WriteLine(") IS NULL;");
                }
            }
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Outputs statements for altering view default values.
        /// </summary>
        public static void Alter(StreamWriter writer, [NullGuard.AllowNull] PgSchema oldSchema, PgSchema newSchema, SearchPathHelper searchPathHelper)
        {
            if (oldSchema == null)
            {
                return;
            }

            foreach (PgView oldView in oldSchema.Views)
            {
                PgView newView = newSchema.GetView(oldView.Name);

                if (newView == null)
                {
                    continue;
                }

                DiffDefaultValues(writer, oldView, newView, searchPathHelper);

                if ((oldView.Comment == null && newView.Comment != null) ||
                    (oldView.Comment != null &&
                     newView.Comment != null &&
                     !oldView.Comment.Equals(newView.Comment)))
                {
                    searchPathHelper.OutputSearchPath(writer);
                    writer.WriteLine();
                    writer.Write("COMMENT ON VIEW ");
                    writer.Write(PgDiffStringExtension.QuoteName(newView.Name));
                    writer.Write(" IS ");
                    writer.Write(newView.Comment);
                    writer.WriteLine(';');
                }
                else if (oldView.Comment != null && newView.Comment == null)
                {
                    searchPathHelper.OutputSearchPath(writer);
                    writer.WriteLine();
                    writer.Write("COMMENT ON VIEW ");
                    writer.Write(PgDiffStringExtension.QuoteName(newView.Name));
                    writer.WriteLine(" IS NULL;");
                }

                IList <string> columnNames = new List <string>(newView.ColumnComments.Count);

                foreach (PgView.ColumnComment columnComment in newView.ColumnComments)
                {
                    columnNames.Add(columnComment.ColumnName);
                }

                foreach (PgView.ColumnComment columnComment in oldView.ColumnComments)
                {
                    if (!columnNames.Contains(columnComment.ColumnName))
                    {
                        columnNames.Add(columnComment.ColumnName);
                    }
                }

                foreach (var columnName in columnNames)
                {
                    PgView.ColumnComment oldColumnComment = null;
                    PgView.ColumnComment newColumnComment = null;

                    foreach (PgView.ColumnComment columnComment in oldView.ColumnComments)
                    {
                        if (columnName.Equals(columnComment.ColumnName))
                        {
                            oldColumnComment = columnComment;
                            break;
                        }
                    }

                    foreach (PgView.ColumnComment columnComment in newView.ColumnComments)
                    {
                        if (columnName.Equals(columnComment.ColumnName))
                        {
                            newColumnComment = columnComment;
                            break;
                        }
                    }

                    if ((oldColumnComment == null && newColumnComment != null) ||
                        (oldColumnComment != null &&
                         newColumnComment != null &&
                         !oldColumnComment.Comment.Equals(newColumnComment.Comment)))
                    {
                        searchPathHelper.OutputSearchPath(writer);
                        writer.WriteLine();
                        writer.Write("COMMENT ON COLUMN ");
                        writer.Write(PgDiffStringExtension.QuoteName(newView.Name));
                        writer.Write('.');
                        writer.Write(PgDiffStringExtension.QuoteName(newColumnComment.ColumnName));
                        writer.Write(" IS ");
                        writer.Write(newColumnComment.Comment);
                        writer.WriteLine(';');
                    }
                    else if (oldColumnComment != null && newColumnComment == null)
                    {
                        searchPathHelper.OutputSearchPath(writer);
                        writer.WriteLine();
                        writer.Write("COMMENT ON COLUMN ");
                        writer.Write(PgDiffStringExtension.QuoteName(newView.Name));
                        writer.Write('.');
                        writer.Write(PgDiffStringExtension.QuoteName(oldColumnComment.ColumnName));
                        writer.WriteLine(" IS NULL;");
                    }
                }
            }
        }
        /// <summary>
        /// Outputs statements for constraint comments that have changed.
        /// </summary>
        public static void AlterComments(StreamWriter writer, [NullGuard.AllowNull] PgSchema oldSchema, PgSchema newSchema, SearchPathHelper searchPathHelper)
        {
            if (oldSchema == null)
            {
                return;
            }

            foreach (PgTable oldTable in oldSchema.Tables)
            {
                PgTable newTable = newSchema.GetTable(oldTable.Name);

                if (newTable == null)
                {
                    continue;
                }

                foreach (PgConstraint oldConstraint in oldTable.Constraints)
                {
                    PgConstraint newConstraint = newTable.GetConstraint(oldConstraint.Name);

                    if (newConstraint == null)
                    {
                        continue;
                    }

                    if ((oldConstraint.Comment == null && newConstraint.Comment != null) ||
                        (oldConstraint.Comment != null && newConstraint.Comment != null && !oldConstraint.Comment.Equals(newConstraint.Comment)))
                    {
                        searchPathHelper.OutputSearchPath(writer);
                        writer.WriteLine();
                        writer.Write("COMMENT ON ");

                        if (newConstraint.PrimaryKeyConstraint)
                        {
                            writer.Write("INDEX ");
                            writer.Write(PgDiffStringExtension.QuoteName(newConstraint.Name));
                        }
                        else
                        {
                            writer.Write("CONSTRAINT ");
                            writer.Write(PgDiffStringExtension.QuoteName(newConstraint.Name));
                            writer.Write(" ON ");
                            writer.Write(PgDiffStringExtension.QuoteName(newConstraint.TableName));
                        }

                        writer.Write(" IS ");
                        writer.Write(newConstraint.Comment);
                        writer.WriteLine(';');
                    }
                    else if (oldConstraint.Comment != null && newConstraint.Comment == null)
                    {
                        searchPathHelper.OutputSearchPath(writer);
                        writer.WriteLine();
                        writer.Write("COMMENT ON ");

                        if (newConstraint.PrimaryKeyConstraint)
                        {
                            writer.Write("INDEX ");
                            writer.Write(PgDiffStringExtension.QuoteName(newConstraint.Name));
                        }
                        else
                        {
                            writer.Write("CONSTRAINT ");
                            writer.Write(PgDiffStringExtension.QuoteName(newConstraint.Name));
                            writer.Write(" ON ");
                            writer.Write(PgDiffStringExtension.QuoteName(newConstraint.TableName));
                        }

                        writer.WriteLine(" IS NULL;");
                    }
                }
            }
        }
Ejemplo n.º 21
0
        /// <summary>
        /// Outputs statements for aggregate comments that have changed.
        /// </summary>
        public static void AlterComments(StreamWriter writer, [NullGuard.AllowNull] PgSchema oldSchema, PgSchema newSchema, SearchPathHelper searchPathHelper)
        {
            if (oldSchema == null)
            {
                return;
            }

            foreach (PgAggregate oldAggregate in oldSchema.Aggregates)
            {
                PgAggregate newAggregate = newSchema.GetAggregate(oldAggregate.Signature);

                if (newAggregate == null)
                {
                    continue;
                }

                if ((oldAggregate.Comment == null && newAggregate.Comment != null) ||
                    (oldAggregate.Comment != null && newAggregate.Comment != null && !oldAggregate.Comment.Equals(newAggregate.Comment)))
                {
                    searchPathHelper.OutputSearchPath(writer);
                    writer.WriteLine();

                    writer.Write("COMMENT ON AGGREGATE ");
                    writer.Write(PgDiffStringExtension.QuoteName(newAggregate.Name));
                    writer.Write('(');

                    var addComma = false;

                    foreach (PgAggregate.Argument argument in newAggregate.Arguments)
                    {
                        if (addComma)
                        {
                            writer.Write(", ");
                        }
                        else
                        {
                            addComma = true;
                        }

                        writer.Write(argument.DataType);
                    }

                    writer.Write(") IS ");
                    writer.Write(newAggregate.Comment);
                    writer.WriteLine(';');
                }
                else if (oldAggregate.Comment != null && newAggregate.Comment == null)
                {
                    searchPathHelper.OutputSearchPath(writer);
                    writer.WriteLine();

                    writer.Write("COMMENT ON AGGREGATE ");
                    writer.Write(PgDiffStringExtension.QuoteName(newAggregate.Name));
                    writer.Write('(');

                    var addComma = false;
                    foreach (PgAggregate.Argument argument in newAggregate.Arguments)
                    {
                        if (addComma)
                        {
                            writer.Write(", ");
                        }
                        else
                        {
                            addComma = true;
                        }

                        writer.Write(argument.DataType);
                    }

                    writer.WriteLine(") IS NULL;");
                }
            }
        }
Ejemplo n.º 22
0
        /// <summary>
        /// Diffs default values in views.
        /// </summary>
        private static void DiffDefaultValues(StreamWriter writer, PgView oldView, PgView newView, SearchPathHelper searchPathHelper)
        {
            IList <PgView.DefaultValue> oldValues = oldView.DefaultValues;

            IList <PgView.DefaultValue> newValues = newView.DefaultValues;

            // modify defaults that are in old view
            foreach (PgView.DefaultValue oldValue in oldValues)
            {
                var found = false;

                foreach (PgView.DefaultValue newValue in newValues)
                {
                    if (oldValue.ColumnName.Equals(newValue.ColumnName))
                    {
                        found = true;

                        if (!oldValue.Value.Equals(newValue.Value))
                        {
                            searchPathHelper.OutputSearchPath(writer);
                            writer.WriteLine();
                            writer.Write("ALTER TABLE ");
                            writer.Write(PgDiffStringExtension.QuoteName(newView.Name));
                            writer.Write(" ALTER COLUMN ");
                            writer.Write(PgDiffStringExtension.QuoteName(newValue.ColumnName));
                            writer.Write(" SET DEFAULT ");
                            writer.Write(newValue.Value);
                            writer.WriteLine(';');
                        }

                        break;
                    }
                }

                if (!found)
                {
                    searchPathHelper.OutputSearchPath(writer);
                    writer.WriteLine();
                    writer.Write("ALTER TABLE ");
                    writer.Write(PgDiffStringExtension.QuoteName(newView.Name));
                    writer.Write(" ALTER COLUMN ");
                    writer.Write(PgDiffStringExtension.QuoteName(oldValue.ColumnName));
                    writer.WriteLine(" DROP DEFAULT;");
                }
            }

            // add new defaults
            foreach (PgView.DefaultValue newValue in newValues)
            {
                var found = false;

                foreach (PgView.DefaultValue oldValue in oldValues)
                {
                    if (newValue.ColumnName.Equals(oldValue.ColumnName))
                    {
                        found = true;
                        break;
                    }
                }

                if (found)
                {
                    continue;
                }

                searchPathHelper.OutputSearchPath(writer);
                writer.WriteLine();
                writer.Write("ALTER TABLE ");
                writer.Write(PgDiffStringExtension.QuoteName(newView.Name));
                writer.Write(" ALTER COLUMN ");
                writer.Write(PgDiffStringExtension.QuoteName(newValue.ColumnName));
                writer.Write(" SET DEFAULT ");
                writer.Write(newValue.Value);
                writer.WriteLine(';');
            }
        }