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

dbup-downtown

v0.0.13

Published

Lightweight database migration command-line tool

Downloads

16

Readme

dbup-downtown

Lightweight database migration command-line tool.

Hold your horses

Currently, this project only supports Sqlite3 database files, however the actual migration code doesn't care, and if you'd like to contribute and write a MySQL or PostgreSQL provider, we are open for business.

Quick start

Provided your project has a migrations folder with at least one migration file in it and a valid Sqlite3 database file:

npm install
npm run build
MIGRATIONS_FOLDER=../relative/path/to/migrations/folder/in/your/project PATH_TO_DB=../relative/path/to/sqlite/database/file npm start migrate

Impact to your project

This script will make real changes to your database, however none that you don't explicitly code into your migration files. There is no custom query language or any other DSL to imply changes or be interpreted otherwise. WYSIWYG.

The script creates a single table migrations in which it keeps its state. Not the migration files and indeed no other file in your project or anywhere else are modified by this script.

Environment

MIGRATIONS_FOLDER

A folder, reasonably within your project, that holds migration files, or files that describe intended alterations to your database. The script will consume these files, ignoring any without a .json file extension.

PATH_TO_DB

A path to the (for now, only) Sqlite database file.

Usage

node migrate.js <migrate|fake [up|down|NUM] [NUM] | list | new [description]>

To perform migrations or fakes, the first token after npm start or node migrate.js is either migrate or fake.

If no other tokens, the assumption is migrate up by one.

The next token after migrate can be up or down to control the direction, or any integer to control the number of migrations performed. If a direction, the number of migrations remains assumed at one. If an integer, the direction remains assumed at up.

If yet another token is included, it must be an integer and it will override whatever count determination was made from the previous token. Any determinations made about direction from the previous token remain in effect.

| command | result | |---|---| | migrate | migrate up by one | | migrate up | migrate up by one | | migrate 2 | migrate up by two | | migrate -1 | migrate down by one | | migrate down | migrate down by one | | migrate up 3| migrate up by three | | migrate down -1 | migrate down by one | | migrate 1 -1 | nonsensical, but will attempt migrate "up" by "minus one" | | migrate 1 down | error (parsing non-numerical string as integer) |

Similarly, fake will accept the same positional arguments.

list will show all migration files found and their status with respect to history found in the migrations folder.

new will create a blank migration in the migrations folder with an appropriate UTC timestamp name.

Migration files

A migration file must follow the timestamp naming pattern YYYYMMDDHHMM.json and be placed within MIGRATIONS_FOLDER.

The format is as follows:

{
  "name": "some description for the human",
  "up": "some SQL statement to alter the database, likely in accordance with the migration name",
  "down": "some SQL statement to reverse the SQL statement for 'up'"
}

This is a real-world example:

{
  "name": "add column filename to archives",
  "up": "alter table archives add column filename char(255)",
  "down": "BEGIN; CREATE TABLE archives_temp as select id, target_id, created_at, size_kb, is_remote, remote_push_at from archives; DROP TABLE archives; ALTER TABLE archives_temp RENAME TO archives; END TRANSACTION;"
}

The following are command templates for common operations. (syntax seen here is for Sqlite3)

Add column

  • up: alter table orig add column;
  • down: BEGIN; alter table orig rename to temp; create table orig (leave, out, removed, column); insert into orig(leave, out, removed, column) select (leave, out, removed, column) from temp; END TRANSACTION;

Remove column

  • up/down: reverse of add column

Rename column

  • up: BEGIN; alter table orig rename to temp; create table orig (a2, b2); insert (a2, b2) into origin select (a, b) from temp; END TRANSACTION;
  • down: reverse values from up

Future development

  • support databases other than sqlite
  • some better installation (node-wrapper script in /usr/local/bin)
  • better input validation
  • maybe some non-action reporting/status commands
  • -h for help