Exemplo n.º 1
0
        void GetEnums(NpgsqlConnection connection, DatabaseModel databaseModel)
        {
            _enums.Clear();
            using (var command = connection.CreateCommand())
            {
                command.CommandText = @"
SELECT nspname, typname, array_agg(enumlabel ORDER BY enumsortorder) AS labels
FROM pg_enum
JOIN pg_type ON pg_type.oid=enumtypid
JOIN pg_namespace ON pg_namespace.oid=pg_type.typnamespace
GROUP BY nspname, typname";
                using (var reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var schema = reader.GetValueOrDefault <string>("nspname");
                        var name   = reader.GetValueOrDefault <string>("typname");
                        var labels = reader.GetValueOrDefault <string[]>("labels");

                        if (schema == "public")
                        {
                            schema = null;
                        }
                        PostgresEnum.GetOrAddPostgresEnum(databaseModel, schema, name, labels);
                        _enums.Add(name);
                    }
                }
            }
        }
        public void DropPostgresEnum()
        {
            var op = new AlterDatabaseOperation();

            PostgresEnum.GetOrAddPostgresEnum(op.OldDatabase, "public", "my_enum", new[] { "value1", "value2" });
            Generate(op);

            Assert.Equal(@"DROP TYPE public.my_enum;" + EOL, Sql);
        }
        public void CreatePostgresEnum()
        {
            var op = new AlterDatabaseOperation();

            PostgresEnum.GetOrAddPostgresEnum(op, "public", "my_enum", new[] { "value1", "value2" });
            Generate(op);

            Assert.Equal(@"CREATE TYPE public.my_enum AS ENUM ('value1', 'value2');" + EOL, Sql);
        }
        public void CreatePostgresEnumWithSchema()
        {
            var op = new AlterDatabaseOperation();

            PostgresEnum.GetOrAddPostgresEnum(op, "some_schema", "my_enum", new[] { "value1", "value2" });
            Generate(op);

            Assert.Equal(
                @"CREATE SCHEMA IF NOT EXISTS some_schema;" + EOL +
                @"GO" + EOL + EOL +
                @"CREATE TYPE some_schema.my_enum AS ENUM ('value1', 'value2');" + EOL,
                Sql);
        }
Exemplo n.º 5
0
 public static PostgresEnum GetOrAddPostgresEnum(
     this IMutableModel model,
     string?schema,
     string name,
     string[] labels)
 => PostgresEnum.GetOrAddPostgresEnum(model, schema, name, labels);
Exemplo n.º 6
0
 public static PostgresEnum GetOrAddPostgresEnum(
     [NotNull] this IMutableModel model,
     [CanBeNull] string schema,
     [NotNull] string name,
     [NotNull] string[] labels)
 => PostgresEnum.GetOrAddPostgresEnum(model, schema, name, labels);