This library allows you to write migration scenarios using SQL and run them with a simple CLI.
- Simple: only plain SQL
- Lightweight: only 5.6 kB in node_modules
- Modern: ESM support out of the box
foo@bar:~$ yarn trona
Running evolve script
--- 1.sql ---
CREATE TABLE Customers (
id INTEGER NOT NULL,
name VARCHAR(32) NOT NULL,
primary key (id)
);
Evolution is successful!
foo@bar:~$
First you need to install trona
via package manager:
yarn add trona
Then you need to setup simple configuration file named .trona-config.js
and containing script that exports async function runQuery
. The function should be rejected in case of failure of said query and in case of SELECT query successfully executed returns array of selected rows in form of an object {[field]: value}
.
import PG from 'pg';
const client = new PG.Client({
// ...
});
await client.connect();
console.log(`Connected to database`);
export function runQuery(query) {
return client.query(query).then((result) => result.rows);
}
import mysql from 'mysql';
import { promisify } from 'util';
const connection = mysql.createConnection({
// ...
});
const connect = promisify(connection.connect).bind(connection);
const runQuery = promisify(connection.query).bind(connection);
await connect();
console.log(`Connected to database`);
export { runQuery };
Create a folder evolutions
for your evolutions script and add your first evolution to it. Note the rules which you should follow writing said evolutions:
- Name file
{evolution-number}-{text}.sql
. Text part-{text}
is optional and can be omitted (e.g. both1.sql
and1-create-table.sql
are correct). Note: file name should always start with number. Evolution script file with any other symbol will be ignored (e.g.*1-incorrect.sql
or-1-worng.sql
) and warning will be shown during execution. - Complement evolution file with fallback scripts. Separate evolution and fallback scripts with "#DOWN" comment as it show in example below.
- Execution will not be started in case of any conflict of numbering is found (e.g. existing any of two:
1-abba.sql
,1-baab.sql
,1.sql
,1-baba.sql
)
Folder content example:
- evolutions
- 1.sql
- 2.sql
- 3.sql
...
Evolution contents example:
CREATE TABLE Customers (
id INTEGER NOT NULL,
name VARCHAR(32) NOT NULL,
primary key (id)
);
#DOWN
DROP TABLE Customers;
Run command
yarn trona
This command will create table with information about evolutions, if it doesn't exist. After it will execute all your evolutions.
After you managed to successfully setup trona
you can run yarn trona
command. This command will automatically detect any changed or new files in your evolutions folder, run respected fallback scripts if needed and than evolve your databae schema (e. g. if you have 1.sql, 2.sql, and 3.sql evolutions already in your database, you have changed 2.sql and added 4.sql it will run fallback for 3.sql and 2.sql and then run 2.sql, 3.sql, and 4.sql scripts)
By default evolution script will ask for a confirmation to run a degrade script. You can disable this feature by -y
or --no-interactive
flag.
yarn trona -y
You can change path to trona config file by providing -c
or --config-path
option (by default trona will try to find config file .trona-config.js
in a root directory).
yarn trona -c config/.trona-my-config.js
Custom evolutions folder can be choosen by providing -d
or --evolutions-dir
option. By default evolutions
folder is being used.
yarn trona -d migrations
- Bump
version
in package.json - Fill CHANGELOG.md
- Commit changes by
git commin -m "Release X.X.X"
- Create git tag for release by
git tag -a vX.X.X -m "vX.X.X"
- Push changes to remote by
git push --follow-tags
- Release package to registry by
yarn clean-publish
- Fill release page with changelog on GitHub