Victor Shelepen
Victor Shelepen

Reputation: 2266

Restore MySQL sql dump by nodejs without mysqldump

There is sql file. It has to be used for test cases. I need to restore it. I would like to restore it without third party utils. I've made few attempts. I receive new limitations these I can not resolve easy. An easier solution has to exist. Another solutions I have seen from networks do not satisfy requirements. I can not restore functions because of semicolons in sql text.

async function rawSQLFromFile(connection, path) {
  const text = fs.readFileSync(path).toString()
  const queries = text.split(';')
  for(const query of queries) {
    await runQuery(connection, query)
  }
}

I use mysql library But it does not have the functionality.

Upvotes: 1

Views: 1859

Answers (1)

Stock Overflaw
Stock Overflaw

Reputation: 3331

Untested, although this should work with your lib:

const connection = mysql.createConnection({
  // described at https://www.npmjs.com/package/mysql#multiple-statement-queries
  multipleStatements: true,
  // your connection options follow here
});
connection.query(yourSqlFileContent, (err) => { console.log(err ? err : 'restored!') });

Upvotes: 1

Related Questions