Skip to content

Commit

Permalink
test examples (#615)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmkal authored Aug 5, 2023
1 parent 2cfa525 commit 754b2f8
Show file tree
Hide file tree
Showing 20 changed files with 443 additions and 37 deletions.
2 changes: 2 additions & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
**/*new-migration*
**/*new-seed*
2 changes: 1 addition & 1 deletion examples/1.sequelize-typescript/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ node migrate down # revert the last migration
node migrate down --to 0 # revert all migrations
node migrate up --step 2 # run only two migrations

node migrate create --name my-migration.ts # create a new migration file
node migrate create --name new-migration.ts # create a new migration file
```
4 changes: 2 additions & 2 deletions examples/2.es-modules/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ node umzug.mjs --help
node umzug.mjs up
node umzug.mjs down

node umzug.mjs create --name my-new-migration.mjs
node umzug.mjs create --name my-new-migration.cjs
node umzug.mjs create --name new-migration-1.cjs
node umzug.mjs create --name new-migration-2.mjs
```
4 changes: 4 additions & 0 deletions examples/3.raw-sql/readme.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
This example demonstrates how to set up umzug with a raw sql client.

Note: this is really just a toy example to show how you can use pre-existing sql queries. If you want a raw sql migrator for postgres, see [@slonik/migrator](https://npmjs.com/package/@slonik/migrator). It uses umzug in a similar way, with [slonik](https://npmjs.com/package/slonik) as the underlying client, and it adds transactions, locking, supports typescript/javascript alongside sql, and some additional safety checks.

```bash
node migrate up
```
4 changes: 2 additions & 2 deletions examples/4.sequelize-seeders/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ Usage example:
node migrate --help
node seed --help

node seed up # will fail, since tables haven't been created yet
node seed up || echo failed # will fail, since tables haven't been created yet

node migrate up # creates tables
node seed up # inserts seed data

node seed down --to 0 # removes all seed data

node seed create --name more-seed-data.ts # create a placeholder migration file for inserting more seed data.
node seed create --name new-seed-data.ts # create a placeholder migration file for inserting more seed data.
```
2 changes: 1 addition & 1 deletion examples/5.custom-template/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ The implementation simply reads the template file from a predefined folder, but
Usage:

```bash
node migrate --create my-migration.ts
node migrate create --name new-migration.ts
```
4 changes: 4 additions & 0 deletions examples/6.events/readme.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
This example demonstrates how to use umzug events. In this example, an internal service is shut down before migrations running (hopefully, you wouldn't actually need to do this - it's just a demo of how you _could_ use the events emitted by the umzug instance).

```bash
node migrate up
```
5 changes: 3 additions & 2 deletions examples/7.bundling-codegen/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
project: ['./tsconfig.json'],
},
plugins: ['codegen'],
ignorePatterns: ['.eslintrc.js', 'migrate.js'],
ignorePatterns: ['.eslintrc.js', 'migrate/index.js'],
rules: {
'codegen/codegen': 'error',
}
},
}
2 changes: 1 addition & 1 deletion examples/7.bundling-codegen/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.cache
package-lock.json
yarn.lock
migrate.js
migrate/index.js
14 changes: 0 additions & 14 deletions examples/7.bundling-codegen/barrel.ts

This file was deleted.

14 changes: 14 additions & 0 deletions examples/7.bundling-codegen/migrations/barrel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// The content in this file is autogenerated by eslint. So any IDE with an eslint extension will automatically
// sync it, or a lint task such as `yarn lint --fix`.
// Ideally there should be a CI job that runs `yarn lint` - and will fail if it ever does get out of date.
// See https://npmjs.com/package/eslint-plugin-codegen for more details.

// codegen:start {preset: barrel, include: './*.ts', import: star, export: {name: migrations, keys: path}}
import * as _20201209T192431UsersTable from './2020.12.09T19.24.31.users-table'
import * as _20201209T192509RolesTable from './2020.12.09T19.25.09.roles-table'

export const migrations = {
"./2020.12.09T19.24.31.users-table": _20201209T192431UsersTable,
"./2020.12.09T19.25.09.roles-table": _20201209T192509RolesTable
}
// codegen:end
8 changes: 4 additions & 4 deletions examples/7.bundling-codegen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"name": "umzug-bundling-example",
"version": "0.0.0",
"scripts": {
"lint": "eslint .",
"build": "parcel build ./umzug.ts --no-minify --target node --out-dir . --out-file migrate.js --no-source-maps"
"lint": "eslint . --ext ts",
"build": "ncc build umzug.ts --transpile-only --out migrate"
},
"devDependencies": {
"eslint": "7.15.0",
"parcel-bundler": "1.12.4"
"@vercel/ncc": "^0.36.1",
"eslint": "7.15.0"
}
}
6 changes: 3 additions & 3 deletions examples/7.bundling-codegen/readme.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
This example shows how Umzug can be used with a bundler like webpack or parcel. This scenario isn't really what Umzug was designed for, so it depends on the help of codegen tool.
This example shows how Umzug can be used with a bundler like webpack, parcel, turbopack, ncc, bun, esbuild, swc, etc. This scenario isn't really what Umzug was designed for, so it depends on the help of codegen tool.

Since we're going to be bundling this package, (maybe with the purpose of running migrations in another environment), we can't rely on "globbing" the filesystem. Here, [eslint-plugin-codegen](https://npmjs.com/package/eslint-plugin-codegen) is used to glob for the files when the linter runs, and barrel them into an object (see [barrel.ts](./barrel.ts)). That object can then be passed to the Umzug constructor directly as a list of migrations (see [umzug.ts](./umzug.ts)).

When a new migration file is added, the linter can ensure it is added to the barrel by running `eslint . --fix`.

To try out this example, install parcel with `npm install --global parcel-bundler` (or use `npx`), then bundle and run the migrator:
To try out this example, which uses `ncc`, first install dependencies, then bundle and run the migrator:

```bash
npm install
Expand All @@ -13,7 +13,7 @@ npm run build

node migrate up # apply migrations

node migrate create --name my-migration.ts --skip-verify # create a new migration file
node migrate create --name new-migration.ts --skip-verify # create a new migration file
npm run lint -- --fix # makes sure barrel is up to date
```

Expand Down
2 changes: 1 addition & 1 deletion examples/7.bundling-codegen/umzug.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { MigrationFn } from 'umzug';
import { Umzug, SequelizeStorage } from 'umzug';
import { Sequelize } from 'sequelize';
import { migrations } from './barrel';
import { migrations } from './migrations/barrel';

const sequelize = new Sequelize({
dialect: 'sqlite',
Expand Down
2 changes: 1 addition & 1 deletion examples/readme.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
This folder contains several minimal umzug setups. You can try them out by cloning this repo, running `npm install`, then `cd`ing into the folders. Or just browse them on GitHub and copy-paste what you need. Each has a short readme.
This folder contains several minimal umzug setups. You can try them out by cloning this repo, running `npm install`, then `cd`ing into the folders. Or just browse them on GitHub and copy-paste what you need. Each has a short readme with a set of shell commands showing a sample usage.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ exports.down = async params => {};
export const ts = `
import type { MigrationFn } from 'umzug';
export const up: MigrationFn = params => {};
export const down: MigrationFn = params => {};
export const up: MigrationFn = async params => {};
export const down: MigrationFn = async params => {};
`.trimStart();

export const mjs = `
Expand Down
Loading

0 comments on commit 754b2f8

Please sign in to comment.