示例#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));
        }
示例#2
0
        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());
        }
示例#3
0
 public void WritePatch(IDocumentSchema schema, IDDLRunner runner)
 {
     if (functionShouldBeReloaded(schema))
     {
         runner.Apply(this, GenerateFunction());
     }
 }
示例#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};");
     }
 }
示例#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);
     }
 }
示例#6
0
 private void runDependentScripts(IDDLRunner runner)
 {
     DependentScripts.Each(script =>
     {
         var sql = SchemaBuilder.GetSqlScript(_mapping.DatabaseSchemaName, script);
         runner.Apply(this, sql);
     });
 }
示例#7
0
        private void rebuildTableAndUpsertFunction(IDocumentSchema schema, IDDLRunner runner)
        {
            var writer = new StringWriter();

            WriteSchemaObjects(schema, writer);

            var sql = writer.ToString();

            runner.Apply(this, sql);
        }
示例#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"));
        }
示例#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));
        }
示例#11
0
        public void WritePatch(IDocumentSchema schema, IDDLRunner runner)
        {
            var diff = CreateSchemaDiff(schema);

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

            if (diff.CanPatch())
            {
                diff.CreatePatch(runner);
            }
        }
示例#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);
        }
示例#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);
        }
示例#14
0
 public SchemaPatch(DdlRules rules, IDDLRunner liveRunner) : this(rules)
 {
     _liveRunner = liveRunner;
 }
示例#15
0
 public SchemaPatch(IDDLRunner liveRunner)
 {
     _liveRunner = liveRunner;
 }
示例#16
0
 public SchemaPatch(DdlRules rules, IDDLRunner liveRunner) : this(rules)
 {
     _liveRunner = liveRunner;
 }
示例#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());
                }
            }
        }
示例#19
0
 public void WritePatch(DocumentMapping mapping, IDDLRunner runner)
 {
     throw new NotSupportedException();
 }
示例#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);
        }
示例#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;
            }
        }
示例#22
0
 public void WritePatch(DocumentMapping mapping, IDDLRunner runner)
 {
     // Nothing
 }
示例#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);
            }
        }