Skip to content

Knex Migrator

Matthew Dzaman edited this page Nov 14, 2019 · 2 revisions

Goal

Facilitate migrating from knex to erector with a codemod and a test generator to verify the erector output stays true to knex's.

Objectives

Codemod

Simple knex statements should be located in source files and rewritten as erector statements, and they should be automatically verified against inputs to ensure they produce the same results. The value for fixed knex statements is pretty minimal, but also simple to implement. It would be worth considering support for custom helper functions, which would be significantly more effort, but would provide more value as well.

Verification

Even knex queries that cannot be converted with a codemod can still be verified against conversions to erector statements, with the likely exception of queries that are composed in multiple statements or use functions either with complex logic or references to parent scopes.

In the following example, the id reference can be identified, and by setting the value in the global scope the statements can be executed and converted to string for comparison to ensure parity. It would need to be tested with a null and non-null value.

// knex
knex.select('foo').from('bar').where('id', id)

// erector
sql`SELECT "foo" FROM "bar" WHERE id=${id}`

For more dynamic queries that use custom helpers, the permutations of parameters should be defined by the executor.

knex.select('foo').from("bar").whereIfDefined('id', id).whereIfDefined('other', value)

Plan

TBD

Clone this wiki locally