Ejemplo n.º 1
0
        public void CreatePatch(StoreOptions options, SchemaPatch patch)
        {
            TableDiff.CreatePatch(_mapping, patch);

            FunctionDiff.WritePatch(options, patch);

            IndexChanges.Each(x => patch.Updates.Apply(this, x));
            IndexRollbacks.Each(x => patch.Rollbacks.Apply(this, x));
        }
Ejemplo n.º 2
0
        public SchemaDiff(SchemaObjects existing, DocumentMapping mapping, DdlRules rules)
        {
            if (existing.HasNone())
            {
                AllMissing = true;
            }
            else
            {
                var expectedTable = mapping.SchemaObjects.As<DocumentSchemaObjects>().StorageTable();
                TableDiff = new TableDiff(expectedTable, existing.Table);

                // TODO -- drop obsolete indices?

                mapping.Indexes.Each(index =>
                {
                    if (existing.ActualIndices.ContainsKey(index.IndexName))
                    {
                        var actualIndex = existing.ActualIndices[index.IndexName];
                        if (!index.Matches(actualIndex))
                        {
                            IndexChanges.Add($"drop index {expectedTable.Table.Schema}.{index.IndexName};{Environment.NewLine}{index.ToDDL()};");
                            IndexRollbacks.Add($"drop index {expectedTable.Table.Schema}.{index.IndexName};{Environment.NewLine}{actualIndex.DDL};");
                        }
                    }
                    else
                    {
                        IndexChanges.Add(index.ToDDL());
                        IndexRollbacks.Add($"drop index concurrently if exists {expectedTable.Table.Schema}.{index.IndexName};");
                    }
                });

                existing.ActualIndices.Values.Where(x => mapping.Indexes.All(_ => _.IndexName != x.Name)).Each(
                    index =>
                    {
                        IndexRollbacks.Add(index.DDL);
                        IndexChanges.Add($"drop index concurrently if exists {mapping.Table.Schema}.{index.Name};");
                    });

                var expectedFunction = new UpsertFunction(mapping);

                FunctionDiff = new FunctionDiff(expectedFunction.ToBody(rules), existing.Function);

                var missingFKs = mapping.ForeignKeys.Where(x => !existing.ForeignKeys.Contains(x.KeyName));
                MissingForeignKeys.AddRange(missingFKs);
            }

            _mapping = mapping;


        }
Ejemplo n.º 3
0
        public SchemaDiff(SchemaObjects existing, DocumentMapping mapping, DdlRules rules)
        {
            if (existing.HasNone())
            {
                AllMissing = true;
            }
            else
            {
                var expectedTable = mapping.SchemaObjects.As <DocumentSchemaObjects>().StorageTable();
                TableDiff = new TableDiff(expectedTable, existing.Table);

                // TODO -- drop obsolete indices?

                mapping.Indexes.Each(index =>
                {
                    if (existing.ActualIndices.ContainsKey(index.IndexName))
                    {
                        var actualIndex = existing.ActualIndices[index.IndexName];
                        if (!index.Matches(actualIndex))
                        {
                            IndexChanges.Add($"drop index {expectedTable.Table.Schema}.{index.IndexName};{Environment.NewLine}{index.ToDDL()};");
                            IndexRollbacks.Add($"drop index {expectedTable.Table.Schema}.{index.IndexName};{Environment.NewLine}{actualIndex.DDL};");
                        }
                    }
                    else
                    {
                        IndexChanges.Add(index.ToDDL());
                        IndexRollbacks.Add($"drop index concurrently if exists {expectedTable.Table.Schema}.{index.IndexName};");
                    }
                });

                existing.ActualIndices.Values.Where(x => mapping.Indexes.All(_ => _.IndexName != x.Name)).Each(
                    index =>
                {
                    IndexRollbacks.Add(index.DDL);
                    IndexChanges.Add($"drop index concurrently if exists {mapping.Table.Schema}.{index.Name};");
                });

                var expectedFunction = new UpsertFunction(mapping);

                FunctionDiff = new FunctionDiff(expectedFunction.ToBody(rules), existing.Function);

                var missingFKs = mapping.ForeignKeys.Where(x => !existing.ForeignKeys.Contains(x.KeyName));
                MissingForeignKeys.AddRange(missingFKs);
            }

            _mapping = mapping;
        }
Ejemplo n.º 4
0
        public void write_patch_with_designated_owner()
        {
            var func = new FunctionBody(new FunctionName("public", "mt_upsert_target"), new string[0], theFunctionBody);


            var patch = new SchemaPatch();

            var options = new StoreOptions { OwnerName = "bill" };

            var diff = new FunctionDiff(func, null);

            diff.WritePatch(options, patch);

            patch.UpdateDDL.ShouldContain("ALTER FUNCTION public.mt_upsert_target(jsonb, character varying, uuid, uuid) OWNER TO \"bill\";");

        }
Ejemplo n.º 5
0
        public void write_patch_without_designated_owner()
        {
            var func = new FunctionBody(new FunctionName("public", "mt_upsert_target"), new string[0], theFunctionBody);


            var patch = new SchemaPatch();

            var options = new StoreOptions {OwnerName = null};

            var diff = new FunctionDiff(func, null);

            diff.WritePatch(options, patch);

            patch.UpdateDDL.ShouldNotContain("OWNER TO");


        }