I have a node js api that connects with sequelize to a mySQL database. I wrote a script to reset the database every time tests are ran. I am building a CI/CD pipeline and whenever the script is ran i get the following message:
throw new SequelizeErrors.ConnectionError(err);
^
ConnectionError [SequelizeConnectionError]: Connection lost: The server closed the connection.
...
parent: Error: Connection lost: The server closed the connection.
...
at TCP.<anonymous> (node:net:709:12) {
fatal: true,
code: 'PROTOCOL_CONNECTION_LOST'
},
original: Error: Connection lost: The server closed the connection.
This is the part of the pipeline that fails:
tests:
runs-on: ubuntu-20.04
env:
MYSQL_DEVELOPMENT_USER: root
MYSQL_DEVELOPMENT_PASSWORD: ${{ secrets.RootPassword }}
MYSQL_DEVELOPMENT_HOST: localhost
MYSQL_DEVELOPMENT_DATABASE: test
...
- uses: mirromutth/mysql-action@v1.1
with:
mysql root password: ${{ secrets.RootPassword }}
mysql database: test
- name: api testing
run: cd api && npm run test
...
This is the npm run test
"test": "npm run resetDevDb && cross-env NODE_ENV=test jest --verbose"
"resetDevDb" : "cross-env NODE_ENV=development node db/scripts/reset-development-db.js"
The reset database Script:
const resetDb = async () => {
await sequelize.authenticate();
await Book.sync({ force: true });
await Author.sync({ force: true });
await User.sync({ force: true });
await Promise.all(
users.map(async (user) => {
await User.create(user);
})
);
await Promise.all(
books.map(async (book) => {
await Book.create(book);
})
);
await Promise.all(
authors.map(async (author) => {
await Author.create(author);
})
);
console.log("Database reseted");
};
The sequelize config:
const developmentConfig = {
dialect: "mysql",
database: process.env.MYSQL_DEVELOPMENT_DATABASE,
username: process.env.MYSQL_DEVELOPMENT_USER,
password: process.env.MYSQL_DEVELOPMENT_PASSWORD,
host: process.env.MYSQL_DEVELOPMENT_HOST,
logging: console.log,
};
And initialization
const { database, username, password, ...options } = require("./config.js");
const { Sequelize } = require("sequelize");
const sequelize = new Sequelize(database, username, password, options);
module.exports = sequelize;
I have ran this exact pipeline with (I believe) the exact same config and was working fine. After changing the remote repository a few times (And adding the secret.RootPassword) to the new repository I started getting this connection error. From some by hand debugging I figure the error ocurrs during the Book.sync({ force: true }) but I'm not sure if it has something to do with the miromutth/mysql action. Thanks!
Note: When I run npm run test locally everything works great, all the tables drop and are created again and filled, and the tests run and pass normally.