A simple CLI application for managing the PostgreSQL database during developing a dart project.
About dbtools
dbtools allows to create, drop and migrate the PostgreSQL database you use in your project based on migrations located in the subdirectory of your project.
It supports:
- different environments like
dev
,prod
, etc.; - custom path for the migrations directory;
- intermediate scripts to perform auxiliary commands;
- quick fix the specified migration in the database;
- optional password input.
How to use
- Add the
dbtools
as a dev dependency and activate it.
dart pub global activate dbtools
- Put the next settings in
dbtools
section in thepubspec.yaml
or put them indbtools.yaml
:
migrationsPath: migrations
dev:
user: postgres
dbName: your_database
prod:
host: your.server.com
post: 5432
user: admin
dbName: your_database
tableName: migrations
homeDbName: admin
migrationsPath
is the path to the migrations directory. Can be relative or absolute. The default value ismigrations
.host
is the address of your PostgreSQL installation.localhost
by default.port
is the port your PostgreSQL is listening to.5432
by default.user
is the PostgreSQL role with privileges for manipulating databases.postgres
by default.dbName
is the target database of your project. It is required.tableName
is the name of the migrations table in the target database.homeDbName
is default database of theuser
. By default, has the same name asuser
.
dev
is the default environment, but you can choose any set of environments. The only requirement is at least one environment must be presented in the config.
- Write migrations.
In the
migrationsPath
directory you write migration folders containing two scripts:
commit.sql
with sql-commands to migrate the database up.rollback.sql
with sql-commands to migrate the database down. These are required and may not be empty. Additionally you can place intermediate scripts:before_prepare.sql
is executed before creating the migrations table in the database. It may be helpful to create a new schema if you prefere to place the migrations table in another schema thanpublic
. Also you can set the search_path to this scheme if you prefere to name tables without explicit schema in your migration scripts.after_prepare.sql
is executed after creating the migrations table in the database.after_commits.sql
is executed right after commiting migrations if any.cleanup.sql
is executed when all migrations have been rolled back and there is no any migration to commit.
Here is an example structure:
your_project
bin
lib
migrations
ver1 Initial
commit.sql
rollback.sql
ver2 Feature 1
commit.sql
rollback.sql
before_prepare.sql
after_prepare.sql
after_commits.sql
cleanup.sql
dbtools.yaml
pubspec.yaml
You can choose any format of your migration folder. It is recommended to stick the same format. Here are acceptable variants:
ver1 Initial
v1-initial
#1: Initial
1
250101
Requirements:
- The migration identity must contain the unique number of version (
ver2 Feature 1
is the migration number 2). - All migrations are ordered by number, that means the migration
ver2 Feature 1
will be applied strictly after the migrationver1 Initial
, and vice versa,ver1 Initial
will be rolled back strictly afterver2 Feature 1
rollback.
- Create the database
dbtools create
- Migrate the database.
dbtools migrate
Now all migrations placed in your migrations directory are applied to the database. Don't forget to run this command after switching the current git branch to make the database actual.
Also, you can migrate to the specified version:
dbtools migrate v5
You can find other details of the dbtools usage by typing dart run dbtools help
.
Libraries
- ansi
- commands/argument
- commands/arguments_mixin
- commands/clean_command
- commands/config_mixin
- commands/create_command
- commands/drop_command
- commands/executors/database
- commands/executors/exceptions
- commands/executors/migration
- commands/executors/migration_mixin
- commands/executors/types
- commands/extensions
- commands/fix_command
- commands/migrate_command
- commands/models/config
- commands/models/migration
- commands/stdout_mixin
- constants
- runner