Knex adapter drops all tables if no table exists for the first list created
See original GitHub issueThe Knex DB adapters postConnect()
function seeds an initial DB for you, based on the lists defined, if it detects the DB hasn’t been setup yet. However the check it uses is fairly naive – it checks whether the first list created (ie. first call to keystone.createList()
) has a table in the DB. If no table is found the dropDatabase()
function is called which explicitly drops any other list tables before recreating them based on the list schemas.
adapter-knex/lib/adapter-knex.js#L71-L77
const isSetup = await this.schema().hasTable(Object.keys(this.listAdapters)[0]);
if (this.config.dropDatabase || !isSetup) {
console.log('Knex adapter: Dropping database');
await this.dropDatabase();
} else {
return [];
}
This code was intended to be useful behaviour early in app development – it allows iterating on list schemas without needing any schema migrations and seeding DB structure when first starting work on an app. The problem occurs if a new list is added to the app (and happens to be the first call to keystone.createList()
) without a migration that adds the table. Starting an app in this state will cause data loss.
Needless to say, the current check is insufficient for protecting production data.
An argument could be made this oversteps the mandate given to a DB adapter and should be removed entirely. If this or similar behaviour is still desired, it might be better structure in a separate package that could be added as a dev dependency.
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (5 by maintainers)
Top GitHub Comments
We’ve released
@keystonejs/adapter-knex@6.1.0
which includes a change to make the check safer.We’re still working on getting a better fix (there are issues with the first-start developer experience we want to make sure we address too), but at the least this will stop dropping the database except for explicitly setting
dropDatabase
on the Keystone config object.@molomby / @MadeByMike Makes sense, thanks!