question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Knex adapter drops all tables if no table exists for the first list created

See original GitHub issue

The 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:closed
  • Created 4 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
jesstelfordcommented, Dec 16, 2019

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.

0reactions
thekevinbrowncommented, Dec 18, 2019

@molomby / @MadeByMike Makes sense, thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Express knex migrations do not create database tables
no migrations table or any tables .. i have even delete the database and recreate another one ... it seems like the cli...
Read more >
Knex cheatsheet - Devhints
One-page guide to Knex: usage, examples, and more. Knex is an SQL query builder for Node.js.This guide targets v0.13.0.
Read more >
Installation | Knex.js
Knex can be used as an SQL query builder in both Node.JS and the browser, limited to WebSQL's constraints (like the inability to...
Read more >
Database Migrations with Knex - Perk
Creating a migration file. Perk uses Knex for all database related functionality. ... If exports.up created a table, then exports.down will drop that...
Read more >
Bookshelf.js | Home
The issue here is that Knex, the database abstraction layer used by Bookshelf, uses connection pooling and thus keeps the database connection open....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found