Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
21c3926
fix Sch
odr Feb 20, 2026
6544c99
Add FieldInfo
odr Feb 20, 2026
f8a4c05
Step1. Add CFieldInfo. Review.
odr Feb 21, 2026
886bb49
using CFieldInfo in PgTagged & schemaRec'
odr Feb 21, 2026
eb0f362
drop CFldDef
odr Feb 21, 2026
111e5a6
drop CRelDef
odr Feb 21, 2026
8ae18e2
cleanup
odr Feb 21, 2026
e13474e
small
odr Feb 21, 2026
8e5015a
change .gitignore
odr Feb 22, 2026
e3982ee
small refactoring
odr Feb 23, 2026
11d4a1e
small cleanup
odr Feb 23, 2026
12efb35
Step 1. Add HListTag
odr Feb 23, 2026
e469e30
HlistTag with instances
odr Feb 23, 2026
b41dd4d
JSON decoding, tests
odr Feb 23, 2026
faf6ce5
streaming internal
odr Feb 23, 2026
9647b81
IsoHList
odr Feb 23, 2026
0915a34
plain aggr
odr Feb 23, 2026
68f5c84
refactoring HListTag
odr Feb 24, 2026
ca882fd
HListInfo
odr Feb 24, 2026
78a0b72
change Select
odr Feb 24, 2026
a71a396
not work
odr Feb 24, 2026
7c3b628
Revert "not work"
odr Feb 24, 2026
bce8cee
Revert "change Select"
odr Feb 24, 2026
836327f
generic iso class
odr Feb 24, 2026
7d8f600
Reapply "change Select"
odr Feb 24, 2026
babe383
select is ready
odr Feb 24, 2026
6cf1a5e
many fixes and improvements
odr Feb 25, 2026
7f18c9e
select is ready
odr Feb 25, 2026
01274f8
small changes
odr Feb 25, 2026
095ae77
all is working
odr Feb 25, 2026
5e3db50
cleanup
odr Feb 25, 2026
bb9765e
small changes
odr Feb 26, 2026
ca787fb
only Generic works!
odr Feb 26, 2026
20faa10
small
odr Feb 26, 2026
f86bc4b
no PgTagged in HList
odr Feb 26, 2026
2adbdd5
bye PgTagged
odr Feb 26, 2026
734a319
bye SchList
odr Feb 26, 2026
6780bef
improve errors
odr Feb 26, 2026
f9a33f7
refactor catalog
odr Feb 27, 2026
7764a8a
improve CanConvert and PgArr
odr Feb 27, 2026
827c287
add tests
odr Feb 27, 2026
5edc8b0
using of Tagged PgArr and green all tests so far
odr Feb 27, 2026
02cd9d4
add test_dml, fix gen bug
odr Mar 1, 2026
b826735
add tests (wip)
odr Mar 2, 2026
3553b45
Test Hierarchy. Fix duplicate
odr Mar 3, 2026
a32766d
small
odr Mar 3, 2026
1487419
more tests
odr Mar 7, 2026
8272fc4
HListTag -> HList, drop EmptyField
odr Mar 7, 2026
41cd4ac
move modules and some haddocking
odr Mar 8, 2026
6597d3f
moving..
odr Mar 8, 2026
6db40a2
haddocking
odr Mar 8, 2026
6e51664
make aggr
odr Mar 9, 2026
840f85c
add PgSchema.DML
odr Mar 9, 2026
8899609
AI haddock correction
odr Mar 9, 2026
3002dab
Haddock almost finished
odr Mar 10, 2026
52ff519
fix pg-schema.cabal
odr Mar 10, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
.stack-work/
*~
dist-newstyle
hie.yaml
*.bak
.hie/
*.log
dist-*
hie.yaml
trash
11 changes: 10 additions & 1 deletion cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,18 @@ optimization: False

with-compiler: ghc-9.12.2

-- allow-newer: all

packages:
pg-schema
pg-schema-tutorial

constraints:
pg-schema +arbitrary -debug +flat +hashable
pg-schema +arbitrary +flat +hashable

package *
ghc-options: -fwrite-ide-info -fprint-potential-instances

package pg-schema
haddock-options: "--quickjump"
haddock-hyperlink-source: True
46 changes: 22 additions & 24 deletions pg-schema-tutorial/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ Many GHC-extensions should be enabled. I use the following

### TL;DR

```haskell
ghci> import PgSchema
Schema types (e.g. `Tutorial`/`Sch`) are generated as a plain Haskell module; run `cabal run pgs-tutorial-generator` (or use `updateSchemaFile`).

ghci> {mkSchema "dbname=schema_test user=postgres" "Tutorial" (GenNames ["sch"] [])}
```haskell
ghci> import PgSchema.DML
ghci> import Sch -- generated module with data Sch, CSchema, CDBFieldInfo, etc.

ghci> { data Company = Company { name :: Text, address_id :: Maybe Int32 } deriving (Eq, Show, Generic); schemaRec id ''Company }

Expand Down Expand Up @@ -156,40 +157,39 @@ We will use ghci for tutorial. So in directory `pg-schema` run
> stack ghci
```
All necessary extensions are included in cabal file so they are activated by
default. All modules from `pg-schema` are imported. So run
default. Load the generated schema module (e.g. `Sch`) and use it:
```haskell
ghci> {mkSchema "dbname=schema_test user=postgres" "Tutorial" (GenNames ["sch"] [])}
ghci> :i Tutorial
ghci> import Sch
ghci> :i Sch

data Tutorial -- Defined at <interactive>:7:2
instance [safe] CSchema Tutorial -- Defined at <interactive>:7:2
type instance TTypes Tutorial
data Sch -- (or Tutorial if you named it so)
instance [safe] CSchema Sch -- ...
type instance TTypes Sch
= '[ 'NameNS "pg_catalog" "date", 'NameNS "pg_catalog" "int4",
'NameNS "pg_catalog" "numeric", 'NameNS "pg_catalog" "text",
'NameNS "pg_catalog" "timestamptz", 'NameNS "sch" "order_state"]
-- Defined at <interactive>:7:2
type instance TTabs Tutorial
type instance TTabs Sch
= '[ 'NameNS "sch" "addresses", 'NameNS "sch" "articles",
'NameNS "sch" "cities", 'NameNS "sch" "companies",
'NameNS "sch" "countries", 'NameNS "sch" "customers",
'NameNS "sch" "order_positions", 'NameNS "sch" "orders"]
-- Defined at <interactive>:7:2

ghci> :info! Tutorial
ghci> :info! Sch
```
You will see many class instances for Tutorial. The "root" instance is instance
for class `CSchema`.
You will see many class instances for the schema type. The "root" instance is for class `CSchema`.

Note that our "Schema" can include many PG-schemas (namespaces). So we did `mkSchema` for the list of PG-schemas (`["sch"]`). We can also set up a list of tables (as namespace->>tablename) to generate.
Note that our "Schema" can include many PG-schemas (namespaces). The generator is configured for a list of PG-schemas (e.g. `["sch"]`) and optionally a list of tables (namespace->>tablename).

There is predefined type `PgCatalog` in module `Database.PostgreSQL.Schema.Catalog`.
There is predefined type `PgCatalog` in module `PgSchema.Schema.Catalog`.
It is a type like `Tutorial` but defined "manually". It describes PostgreSQL system schema.

Type `Sch` and instances for it are predefined here in Tutorial application. It is the same as our `Tutorial` type.
Type `Sch` and instances for it are generated and used in this tutorial application.

With this instance we can get now:
```haskell
ghci> tabInfoMap @Tutorial
ghci> tabInfoMap @Sch

-- return Map of all tables with flds and "from" and "to" references
-- tabInfoMap :: forall sch. CSchema sch => M.Map NameNS TabInfo
Expand Down Expand Up @@ -276,7 +276,7 @@ type instance TTabs PgCatalog

We can now generate dot-description of our schema:
```haskell
ghci> import PgSchema.Gen
ghci> import PgSchema.Utils.GenDot
ghci> mapM_ T.putStrLn $ T.lines $ genDot @Tutorial False []

digraph G {
Expand Down Expand Up @@ -425,7 +425,7 @@ TRecordInfo Ord1 :: [FieldInfo' Symbol]
= '[ 'FieldInfo "day" "day", 'FieldInfo "num" "num",
'FieldInfo "seller_id" "seller_id"]

ghci> -- CFieldInfo
ghci> -- CDBFieldInfo

ghci> :kind! TFieldType Ord1 "seller_id"
TFieldType Ord1 "seller_id" :: *
Expand Down Expand Up @@ -566,9 +566,7 @@ Isn't it's worth to say "Wow"?

Notice that all these data are getting in one select from db.

In fact this way to get data from database was used also on populating `PgCatalog`
on processing TH-generation of schema. Function `mkSchema` make only three selects
to get all information about tables, relations and types in database.
The same approach is used when building schema info (e.g. for `PgCatalog` or when generating the schema module): a few queries load tables, relations and types from the database.

### Conditions

Expand All @@ -587,6 +585,6 @@ ghci> :{

### Generation of schema

Although we use TH here for generation of schema, I recommend to use generation of plain Haskell text. You can see it in `pg-schema-tutorial/Setup.hs` (note about `build-type: Custom` and `custom-setup` in `pg-schema-tutorial.cabal`).
Schema (data type + CSchema, CDBFieldInfo, CTabDef, etc.) is generated as a plain Haskell module; see `pg-schema-tutorial/Setup.hs` (`build-type: Custom`, `custom-setup` in `pg-schema-tutorial.cabal`). Run `cabal run pgs-tutorial-generator` to regenerate.

For instances of `CQueryRecord` TH is a single generation method because we need more flexibility here.
For instances of `CQueryRecord` TH remains the generation method (`schemaRec` / `deriveQueryRecord`) because we need more flexibility there.
Loading