Пример #1
0
        private void generateOrUpdateFeature(Type featureType, IFeatureSchema feature)
        {
            lock (_updateLock)
            {
                using (var conn = _factory.Create())
                {
                    conn.Open();

                    var patch = new SchemaPatch(_options.DdlRules);
                    patch.Apply(conn, _options.AutoCreateSchemaObjects, feature.Objects);
                    patch.AssertPatchingIsValid(_options.AutoCreateSchemaObjects);

                    var ddl = patch.UpdateDDL;
                    if (patch.Difference != SchemaPatchDifference.None && ddl.IsNotEmpty())
                    {
                        var cmd = conn.CreateCommand(ddl);
                        try
                        {
                            cmd.ExecuteNonQuery();
                            _options.Logger().SchemaChange(ddl);
                            RegisterCheck(featureType, feature);
                        }
                        catch (Exception e)
                        {
                            throw MartenCommandExceptionFactory.Create(cmd, e);
                        }
                    }
                    else if (patch.Difference == SchemaPatchDifference.None)
                    {
                        RegisterCheck(featureType, feature);
                    }
                }
            }
        }
Пример #2
0
        private void handleCommandException(NpgsqlCommand cmd, Exception e)
        {
            this.SafeDispose();
            Logger.LogFailure(cmd, e);

            if ((e as PostgresException)?.SqlState == PostgresErrorCodes.SerializationFailure)
            {
                throw new ConcurrentUpdateException(e);
            }

            if (e is NpgsqlException)
            {
                throw MartenCommandExceptionFactory.Create(cmd, e);
            }
        }
Пример #3
0
        public static void RemoveAllObjects(this IFeatureSchema schema, DdlRules rules, NpgsqlConnection conn)
        {
            var writer = new StringWriter();

            schema.WriteDropStatements(rules, writer);

            var sql = writer.ToString();
            var cmd = conn.CreateCommand(sql);

            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                throw MartenCommandExceptionFactory.Create(cmd, e);
            }
        }
Пример #4
0
        public void Apply(NpgsqlConnection conn, AutoCreate autoCreate, ISchemaObject[] schemaObjects)
        {
            if (!schemaObjects.Any())
            {
                return;
            }

            // Let folks just fail if anything is wrong.
            // Per https://github.com/JasperFx/marten/issues/711
            if (autoCreate == AutoCreate.None)
            {
                return;
            }

            var cmd     = conn.CreateCommand();
            var builder = new CommandBuilder(cmd);

            foreach (var schemaObject in schemaObjects)
            {
                schemaObject.ConfigureQueryCommand(builder);
            }

            cmd.CommandText = builder.ToString();

            try
            {
                using (var reader = cmd.ExecuteReader())
                {
                    apply(schemaObjects[0], autoCreate, reader);
                    for (var i = 1; i < schemaObjects.Length; i++)
                    {
                        reader.NextResult();
                        apply(schemaObjects[i], autoCreate, reader);
                    }
                }
            }
            catch (Exception e)
            {
                throw MartenCommandExceptionFactory.Create(cmd, e);
            }

            AssertPatchingIsValid(autoCreate);
        }
Пример #5
0
        private void handleCommandException(NpgsqlCommand cmd, Exception e)
        {
            this.SafeDispose();
            Logger.LogFailure(cmd, e);

            if ((e as PostgresException)?.SqlState == PostgresErrorCodes.SerializationFailure)
            {
                throw new ConcurrentUpdateException(e);
            }

            // TODO -- this is fine for now, but we might wanna do more to centralize the
            // exception transformations
            if (EventStreamUnexpectedMaxEventIdExceptionTransform.Instance.TryTransform(e,
                                                                                        out var eventStreamUnexpectedMaxEventIdException))
            {
                throw eventStreamUnexpectedMaxEventIdException;
            }

            if (e is NpgsqlException)
            {
                throw MartenCommandExceptionFactory.Create(cmd, e);
            }
        }