lutra-runner-postgres 0.5.0

PostgreSQL runner for Lutra
Documentation
import std::filter
import std::sort
import std::map
import std::find
import std::option::or_default
import std::to_int32
import std::to_int64
import pg_catalog as pg

func pull_interface() -> (
  pg::from_pg_namespace()
  | filter(n -> (
    n.nspname != "pg_catalog"
    && n.nspname != "pg_toast"
    && n.nspname != "information_schema"
  ))
  | map(func (n: pg::PgNamespaceRow) -> {
    schema_name = n.nspname,
    tables = (
      pg::from_pg_class()
      | filter(func (t: pg::PgClass) -> (
        t.relnamespace == n.oid
        && t.relkind == "r"
      ))
      | map(func (t: pg::PgClass) -> {
        table_name = t.relname,
        columns = (
          pg::from_pg_attribute()
          | filter(a -> a.attrelid == t.oid)
          | filter(a -> a.attnum > 0 && a.atttypid > 0)
          | sort(a -> (a.attnum | to_int64))
          | map(a -> {
            name = a.attname,
            typ_id = (a.atttypid | to_int32),
            is_nullable = !a.attnotnull,
          })
        ),
        indexes = (
          pg::from_pg_index()
          | filter(i -> i.indrelid == t.oid)
          | map(i -> (
            pg::from_pg_class()
            | find(ci -> ci.oid == i.indexrelid)
            | or_default()
            | ci -> {i = i, ci = ci}
          ))
          | map(this -> {
            index_name = this.ci.relname,
            is_unique = this.i.indisunique,
            columns = (
              this.i.indkey
              | map(ik -> (
                pg::from_pg_attribute()
                | find(a ->
                  a.attrelid == this.i.indrelid && a.attnum == ik
                )
                | or_default()
              ))
              | map(a -> a.attname)
            ),
          })
        ),
      })
    )
  })
)

module pg_catalog {
  type PgTypeRow: {oid: int32, typname: text, typnamespace: int32, typowner: int32, typlen: int16, typbyval: bool, typtype: text, typcategory: text, typispreferred: bool, typisdefined: bool, typdelim: text, typrelid: int32, typsubscript: text, typelem: int32, typarray: int32, typinput: text, typoutput: text, typreceive: text, typsend: text, typmodin: text, typmodout: text, typanalyze: text, typalign: text, typstorage: text, typnotnull: bool, typbasetype: int32, typtypmod: int32, typndims: int32, typcollation: int32, typdefaultbin: text, typdefault: text}
  func from_pg_type(): [PgTypeRow] -> std::sql::from("pg_catalog/pg_type")

  type PgAttributeRow: {attrelid: int32, attname: text, atttypid: int32, attlen: int16, attnum: int16, atttypmod: int32, attndims: int16, attbyval: bool, attalign: text, attstorage: text, attcompression: text, attnotnull: bool, atthasdef: bool, atthasmissing: bool, attidentity: text, attgenerated: text, attisdropped: bool, attislocal: bool, attinhcount: int16, attstattarget: int16, attcollation: int32, attoptions: text, attfdwoptions: text, attmissingval: text}
  func from_pg_attribute(): [PgAttributeRow] -> std::sql::from("pg_catalog/pg_attribute")

  type PgClass: {oid: int32, relname: text, relnamespace: int32, reltype: int32, reloftype: int32, relowner: int32, relam: int32, relfilenode: int32, reltablespace: int32, relpages: int32, reltuples: float32, relallvisible: int32, reltoastrelid: int32, relhasindex: bool, relisshared: bool, relpersistence: text, relkind: text, relnatts: int16, relchecks: int16, relhasrules: bool, relhastriggers: bool, relhassubclass: bool, relrowsecurity: bool, relforcerowsecurity: bool, relispopulated: bool, relreplident: text, relispartition: bool, relrewrite: int32, relfrozenxid: text, relminmxid: text, reloptions: text, relpartbound: text}
  func from_pg_class(): [PgClass] -> std::sql::from("pg_catalog/pg_class")

  type PgConstraintRow: {oid: int32, conname: text, connamespace: int32, contype: text, condeferrable: bool, condeferred: bool, convalidated: bool, conrelid: int32, contypid: int32, conindid: int32, conparentid: int32, confrelid: int32, confupdtype: text, confdeltype: text, confmatchtype: text, conislocal: bool, coninhcount: int16, connoinherit: bool, conkey: [int16], confkey: [int16], conpfeqop: [int32], conppeqop: [int32], conffeqop: [int32], confdelsetcols: [int16], conexclop: [int32], conbin: text}
  func from_pg_constraint(): [PgConstraintRow] -> std::sql::from("pg_catalog/pg_constraint")

  type PgIndexRow: {indexrelid: int32, indrelid: int32, indisunique: bool, indkey: [int16]}
  func from_pg_index(): [PgIndexRow] -> std::sql::raw(
    "SELECT indexrelid, indrelid, indisunique, to_jsonb(string_to_array(indkey::text, ' ')::int2[]) as indkey FROM pg_catalog.pg_index"
  )

  type PgNamespaceRow: {oid: int32, nspname: text, nspowner: int32}
  func from_pg_namespace(): [PgNamespaceRow] -> std::sql::from("pg_catalog/pg_namespace")
}