Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow send payload data to migration files #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 34 additions & 0 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- [x] quickly rollback recent migrations
- [x] redo feature: rollback and migrate again for quick testing
- [x] runs migrations in transactions
- [x] allow send data to migration files (payload)
- [x] friendly ui 🌹

## Installation
Expand Down Expand Up @@ -99,6 +100,39 @@ async function run() {
run()
```

Example with payload:

```es6
const flags = {
payload: {
schemaBranch: 'devel'
},
config: {
client: "postgresql",
connection: {
host: process.env.PGHOST,
port: process.env.PGPORT,
database: process.env.PGDATABASE,
user: process.env.PGUSER,
password: process.env.PGPASSWORD
},
migrations: {
extension: "ts",
loadExtensions: [".ts"],
directory: "./migrations/data_branch",
tableName: `public.migration_branch_devel`,
}
}
};
const result = await knexMigrate('up', flags);
```
and migration file:
```es6
exports.up = async (knex, promise, payload) => {
return knex.raw(`CREATE OR REPLACE VIEW "${payload.schemaBranch}".invoice_type AS SELECT 1`);
};
```

## Thank you

- [@marcbachmann](https://github.com/marcbachmann) for inspiration and starting point ([knex-umzug](https://github.com/marcbachmann/knex-umzug) and [umzug-cli](https://github.com/marcbachmann/umzug-cli))
Expand Down
5 changes: 3 additions & 2 deletions src/index.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function normalizeFlags (flags) {
flags.cwd = dirname(flags.migrations)
}

flags.payload = flags.payload || null;
flags.cwd = flags.cwd || process.cwd()
flags.knexfile = flags.knexfile || 'knexfile.js'

Expand Down Expand Up @@ -115,9 +116,9 @@ function umzugKnex (flags, connection) {
pattern: /^\d+_.+\.[j|t]s$/,
wrap: fn => (knex, Promise) => {
if (flags.raw) {
return Promise.resolve(fn(knex, Promise))
return Promise.resolve(fn(knex, Promise, flags.payload))
} else {
return knex.transaction(tx => Promise.resolve(fn(tx, Promise)))
return knex.transaction(tx => Promise.resolve(fn(tx, Promise, flags.payload)))
}
}
}
Expand Down