2024-10-29 06:58:19 +00:00
|
|
|
# pgutil
|
|
|
|
|
2024-10-29 15:00:22 +00:00
|
|
|
## Transactions
|
|
|
|
|
|
|
|
Simplify postgres transactions using `WithTx` for serializable transactions,
|
|
|
|
or `WithTxDefault` for the default isolation level. Use the `SerialTxRunner`
|
|
|
|
type to get automatic retries of serialization errors.
|
|
|
|
|
|
|
|
## Migrations
|
|
|
|
|
2024-10-29 15:02:51 +00:00
|
|
|
Put your migrations into a directory, for example `migrations`, ordered by name
|
|
|
|
(YYYY-MM-DD prefix, for example). Embed the directory and pass it to the
|
|
|
|
`Migrate` function:
|
|
|
|
|
|
|
|
```Go
|
|
|
|
//go:embed migrations
|
|
|
|
var migrations embed.FS
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Migrate(db, migrations) // Check the error, of course.
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2024-10-29 15:00:22 +00:00
|
|
|
## Testing
|
|
|
|
|
|
|
|
In order to test this packge, we need to create a test user and database:
|
|
|
|
|
|
|
|
```
|
|
|
|
sudo su postgres
|
|
|
|
psql
|
|
|
|
|
|
|
|
CREATE DATABASE test;
|
|
|
|
CREATE USER test WITH ENCRYPTED PASSWORD 'test';
|
|
|
|
GRANT ALL PRIVILEGES ON DATABASE test TO test;
|
|
|
|
|
|
|
|
use test
|
|
|
|
|
|
|
|
GRANT ALL ON SCHEMA public TO test;
|
|
|
|
```
|
|
|
|
|
|
|
|
Check that you can connect via the command line:
|
|
|
|
|
|
|
|
```
|
|
|
|
psql -h 127.0.0.1 -U test --password test
|
|
|
|
```
|