Skip to content

Commit 107fc47

Browse files
committed
add documentation and changelogs
1 parent eefde8d commit 107fc47

3 files changed

Lines changed: 111 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
# Revision history for cardano-db-sync
22

3+
## 13.7.0.0
4+
5+
### Summary
6+
- Complete migration from Persistent ORM to Hasql for direct PostgreSQL access.
7+
8+
### Performance Improvements
9+
- 3-4x faster epoch processing: ~30min → ~8min per epoch
10+
- Improved cache efficiency: Stake address hit rates increased from 57-79% to 89-99%
11+
- Reduced memory overhead: Eliminated ORM abstraction layer
12+
13+
### Compatibility
14+
- PostgreSQL schema remains unchanged
15+
- Existing database instances compatible without migration
16+
317
## 13.6.0.5
418
- Fix offchain data so it supports files up to 3MB [#1928]
519
- Upgrade to PostgreSQL 17

doc/Readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ This directory contains various documentation files for setting up, configuring,
2424

2525
10. [Migrations](https://github.com/IntersectMBO/cardano-db-sync/blob/master/doc/migrations.md) - Details on database migrations for different versions of Cardano DB Sync, including instructions on applying migrations, handling schema changes, and ensuring data integrity during upgrades.
2626

27+
11. [Developer Hasql Instructions](https://github.com/IntersectMBO/cardano-db-sync/blob/master/doc/hasql.md) - Guide for developers working with the new Hasql implementation, covering the DbAction monad, statement construction patterns, type-safe schema operations, and migration strategies from the previous Persistent ORM to ensure efficient and maintainable database interactions.
28+
2729
11. [Schema](https://github.com/IntersectMBO/cardano-db-sync/blob/master/doc/schema.md) - Overview of the database schema used by the Cardano DB Sync Node, providing a detailed description of the tables, relationships, and data types used in the database.
2830

2931
12. [Schema Management](https://github.com/IntersectMBO/cardano-db-sync/blob/master/doc/schema-management.md) - Instructions on managing the database schema and creating migrations, covering tools and techniques for making schema changes and ensuring they are applied correctly.

doc/hasql.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Developer Guide: Working with Hasql Implementation
2+
3+
## Core Concepts
4+
5+
### DbAction Monad
6+
All database operations now use `DbAction m` instead of `ReaderT SqlBackend m`:
7+
8+
```haskell
9+
-- Old (Persistent)
10+
insertBlock :: MonadIO m => Block -> ReaderT SqlBackend m BlockId
11+
12+
-- New (Hasql)
13+
insertBlock :: MonadIO m => Block -> DbAction m BlockId
14+
```
15+
16+
### Statement Construction
17+
Database operations built using Hasql's encoder/decoder pattern:
18+
19+
```haskell
20+
insertBlockStmt :: HsqlStmt.Statement Block BlockId
21+
insertBlockStmt =
22+
insert
23+
blockEncoder
24+
(WithResult $ HsqlD.singleRow $ idDecoder BlockId)
25+
```
26+
27+
## Module Structure
28+
29+
- `Cardano.Db.Schema.*` - Type-safe schema definitions
30+
- `Cardano.Db.Statement.*` - Database operations organized by domain
31+
- `Cardano.Db.Statement.Function.*` - Core statement building utilities
32+
33+
## Key Operations
34+
35+
### Inserts
36+
```haskell
37+
-- Simple insert
38+
insert :: HsqlE.Params a -> ResultType r r -> HsqlS.Statement a r
39+
40+
-- Bulk insert
41+
insertBulk :: [a] -> DbAction m [r]
42+
43+
-- Conditional insert
44+
insertIfUnique :: HsqlE.Params a -> ResultType r r -> HsqlS.Statement a r
45+
```
46+
47+
### Queries
48+
```haskell
49+
-- Count operations
50+
countAll :: HsqlStmt.Statement () Word64
51+
countWhere :: Text -> Text -> HsqlStmt.Statement () Word64
52+
53+
-- Existence checks
54+
existsById :: Key a -> DbAction m Bool
55+
existsWhereByColumn :: Text -> p -> DbAction m Bool
56+
```
57+
58+
### Execution Pattern
59+
```haskell
60+
runOperation :: MonadIO m => SomeRecord -> DbAction m SomeId
61+
runOperation record =
62+
runDbSession (mkDbCallStack "runOperation") $
63+
HsqlSes.statement record someStmt
64+
```
65+
66+
## Type Safety
67+
68+
### Column Validation
69+
All column references validated at compile time:
70+
```haskell
71+
validateColumn @Block "epoch_no" -- Compile-time check
72+
```
73+
74+
### Schema Correspondence
75+
Each table has corresponding encoder/decoder pairs ensuring type safety.
76+
77+
## Migration Notes
78+
79+
### Database Functions
80+
- Replace `rawSql` calls with typed statements
81+
- Use `HsqlStmt.Statement` construction pattern
82+
- Wrap operations in `runDbSession` with call stack
83+
84+
### Error Handling
85+
```haskell
86+
-- Handle Maybe results
87+
case result of
88+
Just value -> pure value
89+
Nothing -> throwError $ DbError callStack errorMsg Nothing
90+
```
91+
92+
### Testing
93+
- Test database roundtrips with property-based testing
94+
- Use `runDbLovelaceRoundtrip` style functions for validation
95+
- Test encoders/decoders separately from business logic

0 commit comments

Comments
 (0)