This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(graphql-migrations): migration failed for auto increments column
Migrations failed if the field was not "id" and the scalar was "ID"
- Loading branch information
Enda Phelan
committed
Sep 18, 2020
1 parent
2a1ffb9
commit 83a80cd
Showing
5 changed files
with
76 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import * as Knex from "knex"; | ||
|
||
import { buildSchema } from 'graphql'; | ||
import { migrateDB } from "../../packages/graphql-migrations/src"; | ||
import { Operation } from "../../packages/graphql-migrations/src/diff/Operation"; | ||
|
||
|
||
const dbConfig: Knex.Config = { | ||
client: "pg", | ||
connection: { | ||
user: "postgresql", | ||
password: "postgres", | ||
database: "users", | ||
host: "localhost", | ||
port: parseInt(process.env.POSTGRES_PORT, 10) || 5432 | ||
} | ||
} | ||
|
||
const db = Knex(dbConfig); | ||
|
||
beforeEach(async () => { | ||
await db.raw(`DROP SCHEMA public CASCADE; | ||
CREATE SCHEMA public; | ||
GRANT ALL ON SCHEMA public TO postgresql; | ||
GRANT ALL ON SCHEMA public TO public;`); | ||
}) | ||
|
||
afterAll(() => db.destroy()); | ||
|
||
test('consecutive migrations', async () => { | ||
const model = buildSchema(` | ||
"""@model""" | ||
type User { | ||
"""@id""" | ||
_id: ID | ||
name: String | ||
} | ||
`) | ||
|
||
let ops = await migrateDB(dbConfig, model) | ||
expect(ops.results).toHaveLength(3); | ||
const opsResults = ops.results.map((op: Operation) => op.type); | ||
expect(opsResults).toEqual(['table.create', 'table.primary.set', 'column.create']) | ||
|
||
ops = await migrateDB(dbConfig, model) | ||
expect(ops.results).toHaveLength(0); | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
packages/graphql-migrations/src/util/isAutoIncrementableColumn.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import Knex from 'knex'; | ||
|
||
/** | ||
* Detect if the column is incrementable based on column information | ||
* | ||
* @param {string} client - database client | ||
* @param {Knex.ColumnInfo} columnInfo - metadata for column | ||
*/ | ||
export function isAutoIncrementableColumn (client: string, columnInfo: Knex.ColumnInfo): boolean { | ||
switch (client) { | ||
case 'pg': | ||
return columnInfo.defaultValue?.toString().startsWith('nextval('); | ||
case 'sqlite3': | ||
// sqlite3 primary keys not supported | ||
return false; | ||
default: | ||
console.warn(`${client} increments column detection not supported`) | ||
|
||
return false; | ||
} | ||
} |