crates.io
bash
cargo install pgfine
bash
git clone https://gitlab.com/mrsk/pgfine
cargo install --path ./pgfine
env-local-db-0.sh
(as an example) file like this:bash
export PGFINE_CONNECTION_STRING="..."
export PGFINE_ADMIN_CONNECTION_STRING="..."
export PGFINE_DIR="./pgfine"
- Run pgfine init
- Modify newly created ./pgfine/create/*.sql
and ./pgfine/drop/*.sql
scripts if needed.
PGFINE_CONNECTION_STRING
credentials for altering target dbPGFINE_ADMIN_CONNECTION_STRING
credentials for creating a new database refereced above (usually postgres db with user postgres).PGFINE_DIR
path pointing to pgfine project, a good choice would be ./pgfine
Connection strings: https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING
./pgfine/create/*
scripts if needed.bash
source env-local-db-0.sh
pgfine migrate
./pgfine/**/*.sql
../pgfine/migrations/*
scripts../pgfine/views/public.view0.sql
)<schema>.<name>.sql
.Run
bash
source env-local-db-0.sh
pgfine migrate
Test your fresh db maybe.
Table constraints should be stored along with tables. You will have a problem if constraints form circular dependencies.
pgfine migrate
.Database objects are: - tables - views - indexes - constraints - functions - ...
Each database object has coresponding create/alter script in pgfine project directory (see bellow for details). Filenames should consist of schema name and object name and .sql
extension (example: ./pgfine/tables/public.some_table_0.sql
).
Database object scripts are executed when pgfine
attempts to create or alter database object; except for tables - pgfine
won't attempt to alter or drop tables, these changes have to be implemented using migration scripts.
Sometimes object needs to be dropped and created instead of updating it in place (one such case is when argument is removed from function definition). Drop script is generated using object id.
Example ./pgfine/tables/public.table0.sql
:
```sql
create table table0 (
id bigserial primary key
);
-- create indexes -- create constraints -- create rules -- create triggers
```
Example ./pgfine/views/public.view0.sql
:
```sql
-- it is recommended to include "or replace", otherwise it will be dropped and created again each time changes are made.
create or replace view view0 as
select t0.id
from table0 t0
join table1 t1 on t1.id = t0.id
-- create indexes maybe ```
pgfine init [./pgfine]
./pgfine/create.sql
example script.pgfine create
PGFINE_CONNECTION_STRING
credentials.pgfine_objects
and pgfine_migrations
tabls are created in default schema with the last migration_id and object hashes.pgfine migrate
PGFINE_ADMIN_CONNECTION_STRING
to create a new database and role referenced in PGFINE_CONNECTION_STRING
using /pgfine/create/*.sql
scripts (if they do not exist).PGFINE_CONNECTION_STRING
credentials to connect to a working database../pgfine/migrations/
and updates version in pgfine
table. (This is skipped if the database is newly created)../pgfine/
and builds the dependency tree.pgfine
table (or drop the object if it was deleted).pgfine_objects
table with newest information.pgfine_migrations
executed migration scripts.pgfine drop --no-joke
PGFINE_ADMIN_CONNECTION_STRING
credentials to connect to database./pgfine/drop/*.sql
scripts to drop database and role../pgfine/create/
./pgfine/drop/
./pgfine/tables/
./pgfine/views/
./pgfine/functions/
./pgfine/roles/
./pgfine/migrations/
pgfine_objects
tableContains a list of managed pgfine objects and their hashes.
pgfine_migrations
tableContains a list of executed migrations. Selecting the max value should reveal the current state of database.
tables
, views
and functions
correspond to database object. This information is used to track dependencies between objects (using simple text match)./pgfine/migrations/
has format <comparable_version_string>.*
object_md5
will be updated. (online if possible)Scripts are modified during pgfine migrate
execution
CREATE OR REPLACE
. If it fails and --offline
flag is provided we do DROP
and CREATE
including all the dependencies.does not support functions with same name different args.
./pgfine/constraints
)./example/
PGFINE_DIR
database::migrate
, database::drop
, project::init