Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
Merge branch 'master' into conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
wtrocki committed Jul 8, 2020
2 parents ab6b3b1 + 6723140 commit 747ea66
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 5 deletions.
20 changes: 20 additions & 0 deletions docs/graphql-migrations/db-design.md
Expand Up @@ -342,6 +342,26 @@ type Note {
}
```

### Rename

Table or columns can be renamed using the `@db(oldNames: ['old'])` annotation as shown below:

```graphql
"""
@db(oldNames: ['task'])
@model
"""
type Note {
id: ID!
"""
@db(oldNames: ['text'])
"""
title: String!
}
```

This will rename the `text` to `title` and the table name to `note` without data loss.

### Ignore a field

Sometimes our business model can contain more fields and some of these fields may only be there for representation purposes.
Expand Down
126 changes: 125 additions & 1 deletion docs/graphql-migrations/plugins.md
Expand Up @@ -4,4 +4,128 @@ title: GraphQL Migrations Plugins
sidebar_label: Plugins
---

## TODO
# Custom logic with Plugins

It's possible to write custom queries to be executed during migrations using Plugins.

Currently a plugin can only declare tap on the Writer system, with the `write` and `tap` methods:

```ts
import { MigratePlugin, WriteParams } from 'graphql-migrations'

class MyPlugin extends MigratePlugin {
write ({ tap }: WriteParams) {
tap('op-type', 'before', (op: any, transaction: any) => {
// or 'after'
})

return true;
}
}
```

The arguments are:

- `operation: string`, can be one of the following:
- `table.create`
- `table.rename`
- `table.comment.set`
- `table.drop`
- `table.index.create`
- `table.index.drop`
- `table.primary.set`
- `table.primary.drop`
- `table.unique.create`
- `table.unique.drop`
- `table.foreign.create`
- `table.foreign.drop`
- `column.create`
- `column.rename`
- `column.alter`
- `column.drop`
- `type: 'before' | 'after'`
- `callback: function` which get those parameters:
- `operation`: the operation object (see [Operation.ts](https://github.com/aerogear/graphback/blob/master/packages/graphql-migrations/src/diff/Operation.ts))
- `transaction`: the [Knex SQL transaction](http://knexjs.org/#Transactions)

Then, instantiate the plugin in the `plugins` option array of the `migrateDB` method.

For example, let's say we have the following schema:

```graphql
"""
@model
"""
type User {
id: ID!
fname: String
lname: String
}
```

Now we want to migrate the `user` table from two columns `fname` and `lname` into one:

```ts
fullname = fname + ' ' + lname
```

Here is the example code to achieve this:

```ts
import { migrateDB, MigratePlugin, WriteParams } from 'graphql-migrations';

const schema = `
"""
@model
"""
type User {
id: ID!
"""
db(oldNames: ['lname'])
"""
fullname: String
}
`;

class MyPlugin implements MigratePlugin {
write ({ tap }: WriteParams) {
tap('column.drop', 'before', async (op: any, transaction: any) => {
// Check the table and column
if (op.table === 'user' && op.column === 'fname') {
// Update the users lname with fname + ' ' + lname
const users = await transaction
.select('id', 'fname', 'lname')
.from('user');
for (const user of users) {
await transaction('user')
.where({ id: user.id })
.update({
lname: `${user.fname} ${user.lname}`
})
}
}
})

return true;
}
}

const dbConfig = {
// Knex database configuration
}

migrateDB(dbConfig, schema, {
plugins: [
new MyPlugin(),
]
})
```

The above code does the following:

- Remove the `fname` field from the schema.
- Rename `lname` to `fullname` in the schema.
- Annotate the `fullname` field to indicate it's the new name of `lname`.
- We declare a plugin that tap into the `column.drop` write operation.
- In this hook, we read the users and update each one of them to merge the two columns into `lname` before the `fname` column is dropped.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -17,7 +17,7 @@
"eslint-plugin-jsx-a11y": "6.3.1",
"eslint-plugin-no-null": "1.0.2",
"eslint-plugin-react": "7.20.3",
"eslint-plugin-react-hooks": "4.0.5",
"eslint-plugin-react-hooks": "4.0.6",
"husky": "4.2.5",
"lerna": "3.22.1",
"npm-run-all": "4.1.5",
Expand Down
2 changes: 1 addition & 1 deletion packages/graphback-cli/package.json
Expand Up @@ -46,7 +46,7 @@
"chalk": "2.4.2",
"chokidar": "3.4.0",
"debounce": "1.2.0",
"execa": "4.0.2",
"execa": "4.0.3",
"figlet": "1.4.0",
"glob": "7.1.6",
"graphback": "0.14.0-alpha4",
Expand Down
2 changes: 1 addition & 1 deletion packages/graphback/package.json
Expand Up @@ -39,7 +39,7 @@
"@graphql-tools/merge": "6.0.10",
"glob": "7.1.6",
"graphql-subscriptions": "1.1.0",
"pg": "8.2.1",
"pg": "8.2.2",
"pino": "6.3.2",
"tslib": "2.0.0"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql-migrations/package.json
Expand Up @@ -36,7 +36,7 @@
"graphql": "15.3.0",
"graphql-tools": "6.0.10",
"jest": "26.1.0",
"pg": "8.2.1",
"pg": "8.2.2",
"rimraf": "3.0.2",
"sqlite3": "4.2.0",
"ts-node": "8.10.2",
Expand Down

0 comments on commit 747ea66

Please sign in to comment.