npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tqt/pg-migrate

v0.2.9

Published

Migrate Postgres database with SQL scripts

Downloads

25

Readme

pg-migrate

pg-migrate is a tool which manages databases' schema & data with SQL scripts.

npm (scoped) Typed with TypeScript Eslint code style: prettier tested with jest GitHub GitHub repo size GitHub code size in bytes

main: CI-main codecov Quality Gate Status Maintainability Rating Security Rating

develop: CI-develop codecov Quality Gate Status Maintainability Rating Security Rating

1. Use pg-migrate as a global command

npm install -g @tqt/pg-migrate

You need to have a migration folder structured as below. You can name it whatever you want however its 2 sub-folders up and down are required. Put your main scripts in the up folder and name them in the alphabetical order (the order you want it to run). In case you want to downgrade, you need to place their counterparts in the down folder with the same name.

- migration-folder
  - up
    - 001-add-sample-1.sql
    - 002-add-sample-2.sql
    - 003-add-sample-3.sql
  - down
    - 001-add-sample-1.sql
    - 002-add-sample-2.sql
    - 003-add-sample-3.sql

Then run the script with the up command

pg-migrate up --migration-folder your-migration-folder --host host-name --database database-name --port port --user user-name --password password

For example

pg-migrate up --migration-folder ./db-migration --host localhost --database sample --port 5432 --user postgres --password postgres

After the command executes, a table named migration is created in your current database with all executed scripts.

| id | version | createdAt | | :-- | :--------------- | ------------: | | 0 | 001-add-sample-1 | 1622278220790 | | 1 | 002-add-sample-2 | 1622279735989 | | 2 | 003-add-sample-3 | 1622279766950 |

In case you want to migrate to a specific version but not the latest one, run

pg-migrate up 002-add-sample-2 --migration-folder ./db-migration --host localhost --database sample --port 5432 --user postgres --password postgres

To downgrade to a specific version, run the script with the down command

pg-migrate down 002-add-sample-2 --migration-folder ./db-migration --host localhost --database sample --port 5432 --user postgres --password postgres

Instead of using parameters, you can use environment variables. You also may use a connection string. Here is the list of all parameters:

| param | alternative environment variable | sample | | :----------------- | :------------------------------- | :--------------------------------------------------------------- | | --migration-folder | POSTGRES_MIGRATION_FOLDER | ./db-migration | | --host | POSTGRES_HOST | localhost | | --port | "003-add-sample-3" | 5432 | | --database | POSTGRES_DATABASE | postgres | | --user | POSTGRES_USER | user | | --password | POSTGRES_PASSWORD | password | | --ssl | POSTGRES_SSL | true | | --connectionString | POSTGRES_CONNECTION_STRING | postgresql://dbuser:[email protected]:3211/mydb |

2. Use pg-migrate as a local command

Install the package as a dep dependency in your project

npm install --save-dev @tqt/pg-migrate

or using yarn

yarn add -D @tqt/pg-migrate

Then run

npx pg-migrate up --migration-folder your-migration-folder --host host-name --database database-name --port port --user user-name --password password

3: Run it in your code

Install the package as a dep dependency in your project

npm install --save-dev @tqt/pg-migrate

or using yarn

yarn add -D @tqt/pg-migrate

Then import and run it in your code

import {migrate} from '@tqt/pg-migrate';

migrate({
  migrationFolder: './db-migration',
  mode: 'up',
  poolConfig: {
    host: 'host',
    database: 'database',
    port: 5432,
    user: 'user',
    password: 'password',
    ssl: true,
  },
});