
Security News
Socket Releases Free Certified Patches for Critical vm2 Sandbox Escape
A critical vm2 sandbox escape can allow untrusted JavaScript to break isolation and execute commands on the host Node.js process.
@oliasoft-open-source/node-postgresql-migrator
Advanced tools
A Node.js utility for PostgreSQL database migrations
npxWhat about rollback (revert)?
This is not supported. Instead, thorough change testing, and a "roll-forward" strategy is advised (if a breaking change gets released, create a new migration script to correct the problem). While some other tools do claim "rollback" support, usually this means overhead of writing 'revert scripts', which in practice can be flaky/unreliable.
The database must already exist and have permissions granted.
Install the tool directly from Gitlab (published NPM packages may be available in future):
npm install --save git+https://gitlab.com/oliasoft-open-source/node-postgresql-migrator.git
Then run the tool with npx:
npx migrator --db=postgres://username:password@host:port/database --dir=./path/to/migrations
Where the db option is the PostgreSQL connections string, and the dir option is the path to the
migrations directory, from the Node project root.
In future, we may support environment variables. For now, you can use a wrapper script.
In package.json:
"scripts": {
"migrator": "npx babel-node --presets @babel/env sql-migrator.js"
}
In sql-migrator.js (wrapper script):
import { spawn } from 'child_process';
import { getDbConnectionConfiguration } from '../server/db/connectionConf';
const run = async () => {
const connectionConf = await getDbConnectionConfiguration();
const migrationsPath = './server/db/migrations';
const command = `npx migrator --db='${connectionConf}' --dir='${migrationsPath}'`;
const child = spawn(command, ['--color=always'], { shell: true });
child.stdout.on('data', (data) => console.log(data.toString()));
child.stderr.on('data', (data) => console.error(data.toString()));
child.on('exit', (code) => process.exit(code));
};
run().then();
Then invoke with npm run migrator.
A change script (migration file) should be written for each logical changeset (for example, when adding a new product feature).
dir option)schema and seed directories are sensible at the top-level
(to distinguish between structural schema changes and seed values)YYYY-MM-DDTHHmmss-description.sql (ISO 8601 without colons :)###Rules
Using transactions is not allowed in migrations (COMMIT and ROLLBACK are forbidden).
This is because the migrator tool uses a top-level transaction to make the entire migration operation atomic
(and sub-transactions would interfere with this).
--force option, which will re-execute altered scripts*.once.sql will
never be re-executed by the --force optionIt is good practice to make change scripts repeatable. This means they should not throw errors if executed twice. This is achieved by adding guards to SQL commands. We do this as a good practice even though the migrator tool will not re-execute scripts under normal circumstances. Some example guards are provided below.
For schema migrations:
CREATE TABLE IF NOT EXISTS ...CREATE INDEX IF NOT EXISTS ON ...CREATE OR REPLACE FUNCTION ...ALTER TABLE ... ADD COLUMN IF NOT EXISTS ...ALTER TABLE ... DROP COLUMN IF EXISTS ...DO $$ BEGIN
IF EXISTS (SELECT column_name
FROM information_schema.columns
WHERE table_name='foo' and column_name='bar')
THEN
UPDATE foo SET bar = 123;
END IF;
END $$;
DO $$ BEGIN
IF EXISTS(SELECT column_name
FROM information_schema.columns
WHERE table_name='foo' and column_name='bar')
THEN
ALTER TABLE "foo" RENAME COLUMN "bar" TO "baz";
END IF;
END $$;
DO $$ BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname 'fk_foo_barid_bar_id' ) THEN
ALTER TABLE foo
ADD CONSTRAINT fk_foo_barid_bar_id
FOREIGN KEY (barid)
REFERENCES bar(barid);
END IF;
END $$;
For data (seed) migrations:
INSERT INTO ... ON CONFLICT DO NOTHINGINSERT INTO ... SELECT ... WHERE NOT EXISTS (...)Contribution is welcome via issue tickets and merge requests.
npx babel-node --presets @babel/env src/migrator.js --db=postgres://username:password@host:port/db --dir=./test/__testdata__/migrationsnpm run buld (this transpile and bundles a production build to the dist directory)
dist directory should be committednode ./dist/cli.js --db=postgres://username:password@host:port/db --dir=./test/__testdata__/migrationsnpm run test
test/coverage (not committed)FAQs
A Node.js utility for PostgreSQL database migrations
The npm package @oliasoft-open-source/node-postgresql-migrator receives a total of 1,451 weekly downloads. As such, @oliasoft-open-source/node-postgresql-migrator popularity was classified as popular.
We found that @oliasoft-open-source/node-postgresql-migrator demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
A critical vm2 sandbox escape can allow untrusted JavaScript to break isolation and execute commands on the host Node.js process.

Research
Five malicious NuGet packages impersonate Chinese .NET libraries to deploy a stealer targeting browser credentials, crypto wallets, SSH keys, and local files.

Security News
pnpm 11 turns on a 1-day Minimum Release Age and blocks exotic subdeps by default, adding safeguards against fast-moving supply chain attacks.