Exemple #1
0
        public void CreatePatch(DocumentMapping mapping, IDDLRunner runner)
        {
            var systemFields = new string[] { DocumentMapping.LastModifiedColumn, DocumentMapping.DotNetTypeColumn, DocumentMapping.VersionColumn };

            var missingNonSystemFields = Missing.Where(x => !systemFields.Contains(x.Name)).ToArray();
            var fields = missingNonSystemFields.Select(x => mapping.FieldForColumn(x.Name)).ToArray();

            if (fields.Length != missingNonSystemFields.Length)
            {
                throw new InvalidOperationException("The expected columns did not match with the DocumentMapping");
            }


            var missingSystemColumns = Missing.Where(x => systemFields.Contains(x.Name)).ToArray();

            if (missingSystemColumns.Any())
            {
                missingSystemColumns.Each(col =>
                {
                    var patch =
                        $"alter table {_tableName.QualifiedName} add column {col.ToDeclaration(col.Name.Length + 1)};";

                    runner.Apply(this, patch);
                });
            }


            fields.Each(x => x.WritePatch(mapping, runner));
        }
        public void GenerateSchemaObjectsIfNecessary(AutoCreate autoCreateSchemaObjectsMode, IDocumentSchema schema,
                                                     IDDLRunner runner)
        {
            if (_checked)
            {
                return;
            }


            var shouldReload = functionShouldBeReloaded(schema);

            if (!shouldReload)
            {
                _checked = true;
                return;
            }


            if (autoCreateSchemaObjectsMode == AutoCreate.None)
            {
                string message =
                    $"The transform function {Function.QualifiedName} and cannot be created dynamically unless the {nameof(StoreOptions)}.{nameof(StoreOptions.AutoCreateSchemaObjects)} is higher than \"None\". See http://jasperfx.github.io/marten/documentation/documents/ for more information";
                throw new InvalidOperationException(message);
            }

            runner.Apply(this, GenerateFunction());
        }
 public void WritePatch(IDocumentSchema schema, IDDLRunner runner)
 {
     if (functionShouldBeReloaded(schema))
     {
         runner.Apply(this, GenerateFunction());
     }
 }
Exemple #4
0
 public static void OwnershipToTable(this IDDLRunner runner, StoreOptions options, TableName table)
 {
     if (options.OwnerName.IsNotEmpty())
     {
         runner.Apply(table, $"ALTER TABLE {table.QualifiedName} OWNER TO {options.OwnerName};");
     }
 }
Exemple #5
0
 public void WritePatch(IDocumentSchema schema, IDDLRunner runner)
 {
     if (!schema.DbObjects.TableExists(Table))
     {
         var sqlScript = SchemaBuilder.GetSqlScript(Table.Schema, "mt_hilo");
         runner.Apply(this, sqlScript);
     }
 }
Exemple #6
0
 private void runDependentScripts(IDDLRunner runner)
 {
     DependentScripts.Each(script =>
     {
         var sql = SchemaBuilder.GetSqlScript(_mapping.DatabaseSchemaName, script);
         runner.Apply(this, sql);
     });
 }
Exemple #7
0
        private void rebuildTableAndUpsertFunction(IDocumentSchema schema, IDDLRunner runner)
        {
            var writer = new StringWriter();

            WriteSchemaObjects(schema, writer);

            var sql = writer.ToString();

            runner.Apply(this, sql);
        }
Exemple #8
0
        // TODO -- think this one might have to change w/ FK's
        public void WritePatch(DocumentMapping mapping, IDDLRunner runner)
        {
            runner.Apply(mapping, $"ALTER TABLE {mapping.Table.QualifiedName} ADD COLUMN {ColumnName} {PgType};");

            var jsonField = new JsonLocatorField(_enumStorage, Members);

            // HOKEY, but I'm letting it pass for now.
            var sqlLocator = jsonField.SqlLocator.Replace("d.", "");

            runner.Apply(mapping, $"update {mapping.Table.QualifiedName} set {ColumnName} = {sqlLocator}");
        }
        public void WritePatch(IDocumentSchema schema, IDDLRunner runner)
        {
            var tableExists = schema.DbObjects.TableExists(_parent.Table);

            if (tableExists)
            {
                return;
            }

            runner.Apply(this, SchemaBuilder.GetSqlScript(_parent.DatabaseSchemaName, "mt_stream"));
        }
Exemple #10
0
        public void CreatePatch(IDDLRunner runner)
        {
            TableDiff.CreatePatch(_mapping, runner);

            if (HasFunctionChanged())
            {
                _existing.FunctionDropStatements.Each(x => runner.Apply(this, x));

                runner.Apply(this, expectedUpsertFunction());
            }

            IndexChanges.Each(x => runner.Apply(this, x));
        }
Exemple #11
0
        public void WritePatch(IDocumentSchema schema, IDDLRunner runner)
        {
            var diff = CreateSchemaDiff(schema);

            if (!diff.HasDifferences())
            {
                return;
            }

            if (diff.CanPatch())
            {
                diff.CreatePatch(runner);
            }
        }
Exemple #12
0
        private void buildOrModifySchemaObjects(SchemaDiff diff, AutoCreate autoCreateSchemaObjectsMode,
                                                IDocumentSchema schema, IDDLRunner runner)
        {
            if (autoCreateSchemaObjectsMode == AutoCreate.None)
            {
                var className = nameof(StoreOptions);
                var propName  = nameof(StoreOptions.AutoCreateSchemaObjects);

                string message =
                    $"No document storage exists for type {_mapping.DocumentType.FullName} and cannot be created dynamically unless the {className}.{propName} is greater than \"None\". See http://jasperfx.github.io/marten/documentation/documents/ for more information";
                throw new InvalidOperationException(message);
            }

            if (diff.AllMissing)
            {
                rebuildTableAndUpsertFunction(schema, runner);

                runDependentScripts(runner);

                return;
            }

            if (autoCreateSchemaObjectsMode == AutoCreate.CreateOnly)
            {
                throw new InvalidOperationException(
                          $"The table for document type {_mapping.DocumentType.FullName} is different than the current schema table, but AutoCreateSchemaObjects = '{nameof(AutoCreate.CreateOnly)}'");
            }

            if (diff.CanPatch())
            {
                diff.CreatePatch(runner);
            }
            else if (autoCreateSchemaObjectsMode == AutoCreate.All)
            {
                // TODO -- better evaluation here against the auto create mode
                rebuildTableAndUpsertFunction(schema, runner);
            }
            else
            {
                throw new InvalidOperationException(
                          $"The table for document type {_mapping.DocumentType.FullName} is different than the current schema table, but AutoCreateSchemaObjects = '{autoCreateSchemaObjectsMode}'");
            }

            runDependentScripts(runner);
        }
Exemple #13
0
        public static void RemoveColumn(this IDDLRunner runner, object subject, DbObjectName table, string columnName)
        {
            var sql = $"alter table if exists {table.QualifiedName} drop column if exists {columnName};";

            runner.Apply(subject, sql);
        }
Exemple #14
0
 public SchemaPatch(DdlRules rules, IDDLRunner liveRunner) : this(rules)
 {
     _liveRunner = liveRunner;
 }
Exemple #15
0
 public SchemaPatch(IDDLRunner liveRunner)
 {
     _liveRunner = liveRunner;
 }
Exemple #16
0
 public SchemaPatch(DdlRules rules, IDDLRunner liveRunner) : this(rules)
 {
     _liveRunner = liveRunner;
 }
Exemple #17
0
 public SchemaPatch(IDDLRunner liveRunner)
 {
     _liveRunner = liveRunner;
 }
        public void GenerateSchemaObjectsIfNecessary(AutoCreate autoCreateSchemaObjectsMode, IDocumentSchema schema, IDDLRunner runner)
        {
            if (_checkedSchema)
            {
                return;
            }

            _checkedSchema = true;

            var tableExists = schema.DbObjects.TableExists(_parent.Table);

            if (tableExists)
            {
                return;
            }

            if (autoCreateSchemaObjectsMode == AutoCreate.None)
            {
                throw new InvalidOperationException(
                          "The EventStore schema objects do not exist and the AutoCreateSchemaObjects is configured as " +
                          autoCreateSchemaObjectsMode);
            }

            lock (_locker)
            {
                if (!schema.DbObjects.TableExists(_parent.Table))
                {
                    var writer = new StringWriter();

                    writeBasicTables(schema, writer);

                    runner.Apply(this, writer.ToString());
                }
            }
        }
 public void WritePatch(DocumentMapping mapping, IDDLRunner runner)
 {
     throw new NotSupportedException();
 }
Exemple #20
0
        public static void Drop(this IDDLRunner runner, object subject, DbObjectName table)
        {
            var sql = $"drop table if exists {table.QualifiedName} cascade;";

            runner.Apply(subject, sql);
        }
Exemple #21
0
        public void GenerateSchemaObjectsIfNecessary(AutoCreate autoCreateSchemaObjectsMode, IDocumentSchema schema, IDDLRunner runner)
        {
            if (_hasCheckedSchema)
            {
                return;
            }

            DependentTypes.Each(schema.EnsureStorageExists);


            var diff = CreateSchemaDiff(schema);

            if (!diff.HasDifferences())
            {
                _hasCheckedSchema = true;
                return;
            }

            lock (_lock)
            {
                if (_hasCheckedSchema)
                {
                    return;
                }

                buildOrModifySchemaObjects(diff, autoCreateSchemaObjectsMode, schema, runner);

                _hasCheckedSchema = true;
            }
        }
Exemple #22
0
 public void WritePatch(DocumentMapping mapping, IDDLRunner runner)
 {
     // Nothing
 }
Exemple #23
0
        public void GenerateSchemaObjectsIfNecessary(AutoCreate autoCreateSchemaObjectsMode, IDocumentSchema schema, IDDLRunner runner)
        {
            if (_checked)
            {
                return;
            }

            _checked = true;

            if (!schema.DbObjects.TableExists(Table))
            {
                if (_options.AutoCreateSchemaObjects == AutoCreate.None)
                {
                    throw new InvalidOperationException($"Hilo table is missing, but {nameof(StoreOptions.AutoCreateSchemaObjects)} is {_options.AutoCreateSchemaObjects}");
                }

                WritePatch(schema, runner);
            }
        }