简体   繁体   中英

How do I use the MySQL MEMORY storage engine with Knex?

I'm using Knex and MySQL for production. I've been using SQLite in-memory for testing, but there are some minor inconsistencies, so I'd like to switch over to using MySQL in memory. Can anyone recommend a good way to do this?

The way I currently do things is that my Knex object is a singleton and has a process flag when it's being run in a test context. I was thinking that I could add something like this to my migrations:

await knex
                .schema
                .createTable('organization', table => {
                    if (knex.processFlag === 'test')
                        table.engine('MEMORY');
                    table.increments('id').primary().unsigned();
                    table.string('address');
                    table.string('city');
                    table.string('state');
                    table.string('country');
                    table.string('post_code');
                });

Thoughts? Are there problems with this approach I'm not considering?

TLDR: I don't recommend it.

I was thinking the same thing myself. You can create in-memory tables using Knex as you suggested: table.engine('MEMORY');

However, it suffers the same problem as using SQLite, but worse. It's not like InnoDB stored in memory instead of disk, it's a completely different engine, and it doesn't even try to behave like InnoDB.

I tried replacing InnoDB tables as you propose, and it didn't work. When replacing just a few tables, my foreign key constraints failed. Some tables couldn't even be created because the memory engine doesn't support Blob/Text fields. There are probably many more differences, but I stopped there.

Update : After a little bit of research, the best solution I found was to have MySQL store its data on a tempfs mount (RAM disk.)

In my case, we use docker, and it was as simple as adding the following lines to docker-compose for the MySQL container.

tmpfs:
  - /var/lib/mysql

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM