1

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.

6
  • Can you add some info about I can not restore functions because of brackets and it does not have the functionality ? It is a bit unclear what you mean by that... Commented Feb 24, 2020 at 9:56
  • 1
    And did you notice this package: npmjs.com/package/mysql-import Commented Feb 24, 2020 at 10:00
  • I've corrected. It is because of semicolons. The text is divided into subqueries by semicolons. The problem is that the function definition have semicolons. Commented Feb 24, 2020 at 10:15
  • I will check the library. It looks better than I have tried before. Commented Feb 24, 2020 at 10:16
  • @Luuk I've checked the library npmjs.com/package/mysql-import. :) It also does not import the function. I have been looking at code. It does not process begin end constructions at all. So the library has the same issue as my code. Commented Feb 24, 2020 at 10:40

1 Answer 1

1

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!') });
Sign up to request clarification or add additional context in comments.

1 Comment

It has to work at core level according to the lines: // May send multiple statements per COM_QUERY and COM_STMT_PREPARE defaultFlags.push('+MULTI_STATEMENTS'); But it does not ...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.