{"id":14974123,"url":"https://github.com/mattilehtinen/postgrator-cli","last_synced_at":"2025-04-04T23:10:08.925Z","repository":{"id":37270295,"uuid":"64583319","full_name":"MattiLehtinen/postgrator-cli","owner":"MattiLehtinen","description":"PostgreSQL, MySQL and MS SQL database schema migration tool using plain SQL and JS files","archived":false,"fork":false,"pushed_at":"2025-04-01T21:42:05.000Z","size":2400,"stargazers_count":41,"open_issues_count":7,"forks_count":18,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-04T23:10:01.840Z","etag":null,"topics":["migrate","migration-tool","migrations","migrator","mssql","mysql","mysql-migrations","postgres","postgres-migrations","postgresql","schema-migrations","sql","sql-migrate","sql-migrations"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MattiLehtinen.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-07-31T07:59:14.000Z","updated_at":"2025-04-01T21:41:01.000Z","dependencies_parsed_at":"2023-10-02T02:51:50.717Z","dependency_job_id":"20975e59-622f-4801-a683-76bc0efdc5ad","html_url":"https://github.com/MattiLehtinen/postgrator-cli","commit_stats":{"total_commits":344,"total_committers":8,"mean_commits":43.0,"dds":"0.45639534883720934","last_synced_commit":"c82bfa777ef92863218a603be952abc51a4fee88"},"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MattiLehtinen%2Fpostgrator-cli","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MattiLehtinen%2Fpostgrator-cli/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MattiLehtinen%2Fpostgrator-cli/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MattiLehtinen%2Fpostgrator-cli/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MattiLehtinen","download_url":"https://codeload.github.com/MattiLehtinen/postgrator-cli/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247261612,"owners_count":20910108,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["migrate","migration-tool","migrations","migrator","mssql","mysql","mysql-migrations","postgres","postgres-migrations","postgresql","schema-migrations","sql","sql-migrate","sql-migrations"],"created_at":"2024-09-24T13:49:59.825Z","updated_at":"2025-04-04T23:10:08.907Z","avatar_url":"https://github.com/MattiLehtinen.png","language":"JavaScript","readme":"# Postgrator CLI\n\n[![Build Status][build-badge]][build]\n[![npm package][npm-badge]][npm]\n[![Coverage Status][coveralls-badge]][coveralls]\n\nCommand line SQL database migration tool using SQL scripts. For PostgreSQL, MySQL and SQL Server.\n\nVersion control your SQL database using plain old SQL files.\n\nSupports also undoing migrations.\n\nUses [Postgrator](https://github.com/rickbergfalk/postgrator) node.js library developed by [Rick Bergfalk](https://github.com/rickbergfalk).\n\n## Installation\n\n*As of postgrator-cli 6 Node.js version 14 or greater is required*\n\n```\nnpm install -g postgrator-cli\n```\n\nOr if you prefer to use it locally on your project using npm scripts of package.json:\n\n```\nnpm install postgrator-cli --save-dev\n```\n\nAnd install the appropriate DB engine(s) if not installed yet:\n\n```\nnpm install pg@8\nnpm install mysql@2\nnpm install mssql@6\nnpm install sqlite3@5\n```\n\nSee the [Postgrator](https://github.com/rickbergfalk/postgrator) documentation for more information about the supported engines.\n\n## Usage\n\n### SQL Files\n\nCreate a folder and stick some SQL scripts in there that change your database in some way. It might look like:\n\n```\nmigrations/\n  |- 001.do.sql\n  |- 001.undo.sql\n  |- 002.do.optional-description-of-script.sql\n  |- 002.undo.optional-description-of-script.sql\n  |- 003.do.sql\n  |- 003.undo.sql\n  |- 004.do.js\n  |- 004.undo.js\n  |- ... and so on\n```\n\nThe files must follow the convention\n[version].[action].[optional-description].sql or\n[version].[action].[optional-description].js\n\n**Version** must be a number, but you may start and increment the numbers in any\nway you'd like. If you choose to use a purely sequential numbering scheme\ninstead of something based off a timestamp, you will find it helpful to start\nwith 000s or some large number for file organization purposes.\n\n**Action** must be either \"do\" or \"undo\". Do implements the version, and undo\nundoes it.\n\n**Optional-description** can be a label or tag to help keep track of what\nhappens inside the script. Descriptions should not contain periods.\n\n**SQL or JS** You have a choice of either using a plain SQL file or you can also\ngenerate your SQL via a javascript module. The javascript module should export a\nfunction called generateSql() that returns back a string representing the SQL.\nFor example:\n\n```js\nmodule.exports.generateSql = function () {\n  return (\n    \"CREATE USER transaction_user WITH PASSWORD '\" +\n    process.env.TRANSACTION_USER_PASSWORD +\n    \"'\"\n  )\n}\n```\n\nYou might want to choose the JS file approach, in order to make use (secret)\nenvironment variables such as the above.\n\n### The tool\n\nYou can specify all the parameters from command line (see below) but the easiest way is to:\n\n* Create `.postgratorrc.json`, or any config file supported by [lilconfig](https://github.com/antonk52/lilconfig). We also support `.yml` and `.yaml` files, and `esm` config files. For example:\n\n```\n{\n    \"migrationPattern\": \"migrations/*\",\n    \"driver\": \"pg\",\n    \"host\": \"127.0.0.1\",\n    \"port\": 5432,\n    \"database\": \"myDatabaseName\",\n    \"username\": \"user\",\n    \"password\": \"pass\"\n}\n```\n\nYou can also set these settings with all ENV vars that [node-postgres](https://node-postgres.com/features/connecting#environment-variables) supports, though this may vary with your driver. (e.g. `PGHOST` `PGHOST` etc)\n\n* Migrate to latest version (it looks settings by default from `.postgratorrc.json`, etc):\n```\n$ postgrator\n```\n\n* Migrate to version 004 (it knows current version and migrates up/down automatically):\n```\n$ postgrator 4\n```\n\n\n### Synopsis\n\n```\npostgrator [[--to=]\u003cversion\u003e] --database=\u003cdb\u003e [--driver=\u003cdriver\u003e] [--host=\u003chost\u003e] [--port=\u003cport\u003e] [--username=\u003cusername\u003e] [--password=\u003cpassword\u003e] [--no-config]\n\npostgrator [[--to=]\u003cversion\u003e] [--config=\u003cconfig\u003e]\n\npostgrator migrate [[--to=]version]\n\npostgrator drop-schema [--config=\u003cconfig\u003e]\n```\n\n### Options\n\n```\n  --to version                          Version number of the file to migrate to or 'max'. Default: 'max'\n  -r, --driver pg|mysql|mssql|sqlite3   Database driver. Default: 'pg'.\n  -h, --host hostname                   Host.\n  -o, --port port                       Port.\n  -d, --database database               Database name. (in the case of 'sqlite3' this is the filename, defaults to `:memory:`)\n  -u, --username database               Username.\n  -p, --password password               Password. If parameter without value is given, password will be asked.\n  -m, --migration-pattern pattern       A pattern matching files to run migration files from. Default: 'migrations/*'\n  -t --schema-table                     Table created to track schema version.\n  --validate-checksum                   Validates checksum of existing SQL migration files already run prior to executing migrations.\n  -s, --ssl                             Enables ssl connections. When using the mysql driver it expects a string containing name of ssl profile.\n  -c, --config file                     Explicitly set the location of the config file to load.\n  --no-config                           Do not load options from a configuration file.\n  -v, --version                         Print version.\n  -?, --help                            Print this usage guide.\n\nExamples\n\n  1. Specify parameters on command line                       postgrator 23 --host 127.0.0.1 --database sampledb\n                                                              --username testuser --password testpassword\n  2. Explicitly disable loading configuration file            postgrator 2 --no-config\n  3. Use default configuration file to migrate to version 5   postgrator 5\n  4. Migrate to latest version using default configuration    postgrator\n  file (.postgratorrc.json, etc)\n  5. Drop the schema table using configuration files          postgrator drop-schema\n```\n\n## Tests\nTo run postgrator tests locally, run `docker-compose up` and then `npm test`.\n\n[build-badge]: https://img.shields.io/github/actions/workflow/status/MattiLehtinen/postgrator-cli/test.yml?event=push\u0026style=flat-square\n[build]: https://github.com/MattiLehtinen/postgrator-cli/actions\n\n[npm-badge]: https://img.shields.io/npm/v/postgrator-cli.svg?style=flat-square\n[npm]: https://www.npmjs.org/package/postgrator-cli\n\n[coveralls-badge]: https://img.shields.io/coveralls/MattiLehtinen/postgrator-cli/master.svg?style=flat-square\n[coveralls]: https://coveralls.io/r/MattiLehtinen/postgrator-cli\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmattilehtinen%2Fpostgrator-cli","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmattilehtinen%2Fpostgrator-cli","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmattilehtinen%2Fpostgrator-cli/lists"}