├── test ├── broken_dump_files │ ├── dumplink.sql │ ├── dump.sql │ └── dump_1.sql ├── sample_dump_files │ ├── more_sample_files │ │ ├── not_sql.txt │ │ ├── test4.sql │ │ └── test5.sql │ ├── test4.sql │ ├── test3.sql │ ├── test2.sql │ └── test.sql ├── test-helpers.js ├── SQLDumpGenerator.js ├── memory-stress-test.js └── test.js ├── .gitignore ├── .travis.yml ├── src ├── header.js ├── parser.js └── importer.js ├── .github └── ISSUE_TEMPLATE │ ├── bug_report.md │ └── feature_request.md ├── LICENSE ├── package.json ├── Gruntfile.js ├── CONTRIBUTING.md ├── README.md └── mysql-import.js /test/broken_dump_files/dumplink.sql: -------------------------------------------------------------------------------- 1 | dump.sql -------------------------------------------------------------------------------- /test/broken_dump_files/dump.sql: -------------------------------------------------------------------------------- 1 | 2 | import nothing into nowhere; 3 | import !@#$ into mytable; -------------------------------------------------------------------------------- /test/broken_dump_files/dump_1.sql: -------------------------------------------------------------------------------- 1 | 2 | import nothing into nowhere; 3 | import !@#$ into mytable; -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /nbproject/ 2 | /node_modules/ 3 | /package-lock.js 4 | /coverage/ 5 | /.nyc_output/ 6 | /test/large_dump.sql -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "node" 4 | services: 5 | - mysql 6 | after_success: npm run coverage -------------------------------------------------------------------------------- /test/sample_dump_files/more_sample_files/not_sql.txt: -------------------------------------------------------------------------------- 1 | select * from notarealtable; 2 | select anything form nowhere; 3 | insert wetwilly into ear; -------------------------------------------------------------------------------- /test/sample_dump_files/test4.sql: -------------------------------------------------------------------------------- 1 | DELIMITER ;; 2 | CREATE FUNCTION `testfunc`() RETURNS VARCHAR(36) 3 | BEGIN 4 | RETURN UUID(); 5 | END ;; 6 | DELIMITER ; 7 | -------------------------------------------------------------------------------- /src/header.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const mysql = require('mysql2'); 4 | const fs = require('fs'); 5 | const path = require("path"); 6 | const stream = require('stream'); 7 | -------------------------------------------------------------------------------- /test/sample_dump_files/test3.sql: -------------------------------------------------------------------------------- 1 | 2 | CREATE TABLE IF NOT EXISTS `test_table_3` ( 3 | `id` int(11) NOT NULL AUTO_INCREMENT, 4 | `somestr` VARCHAR(8) NOT NULL, 5 | PRIMARY KEY (`id`) 6 | ) ENGINE=InnoDB AUTO_INCREMENT=1; 7 | 8 | INSERT INTO `test_table_3` (`somestr`) VALUES ('a'); 9 | INSERT INTO `test_table_3` (`somestr`) VALUES ('b'); 10 | INSERT INTO `test_table_3` (`somestr`) VALUES ('c'); 11 | INSERT INTO `test_table_3` (`somestr`) VALUES ('d'); 12 | INSERT INTO `test_table_3` (`somestr`) VALUES ('e'); -------------------------------------------------------------------------------- /test/sample_dump_files/test2.sql: -------------------------------------------------------------------------------- 1 | 2 | 3 | CREATE TABLE IF NOT EXISTS `test_table_2` ( 4 | `id` int(11) NOT NULL AUTO_INCREMENT, 5 | `somestr` VARCHAR(8) NOT NULL, 6 | PRIMARY KEY (`id`) 7 | ) ENGINE=InnoDB AUTO_INCREMENT=1; 8 | 9 | INSERT INTO `test_table_2` (`somestr`) VALUES ('a'); 10 | INSERT INTO `test_table_2` (`somestr`) VALUES ('b'); 11 | INSERT INTO `test_table_2` (`somestr`) VALUES ('c'); 12 | INSERT INTO `test_table_2` (`somestr`) VALUES ('d'); 13 | INSERT INTO `test_table_2` (`somestr`) VALUES ('e'); -------------------------------------------------------------------------------- /test/sample_dump_files/more_sample_files/test4.sql: -------------------------------------------------------------------------------- 1 | 2 | CREATE TABLE IF NOT EXISTS `test_table_4` ( 3 | `id` int(11) NOT NULL AUTO_INCREMENT, 4 | `somestr` VARCHAR(8) NOT NULL, 5 | PRIMARY KEY (`id`) 6 | ) ENGINE=InnoDB AUTO_INCREMENT=1; 7 | 8 | INSERT INTO `test_table_4` (`somestr`) VALUES ('a'); 9 | INSERT INTO `test_table_4` (`somestr`) VALUES ('b'); 10 | INSERT INTO `test_table_4` (`somestr`) VALUES ('c'); 11 | INSERT INTO `test_table_4` (`somestr`) VALUES ('d'); 12 | INSERT INTO `test_table_4` (`somestr`) VALUES ('e'); -------------------------------------------------------------------------------- /test/sample_dump_files/more_sample_files/test5.sql: -------------------------------------------------------------------------------- 1 | 2 | 3 | CREATE TABLE IF NOT EXISTS `test_table_5` ( 4 | `id` int(11) NOT NULL AUTO_INCREMENT, 5 | `somestr` VARCHAR(8) NOT NULL, 6 | PRIMARY KEY (`id`) 7 | ) ENGINE=InnoDB AUTO_INCREMENT=1; 8 | 9 | INSERT INTO `test_table_5` (`somestr`) VALUES ('a'); 10 | INSERT INTO `test_table_5` (`somestr`) VALUES ('b'); 11 | INSERT INTO `test_table_5` (`somestr`) VALUES ('c'); 12 | INSERT INTO `test_table_5` (`somestr`) VALUES ('d'); 13 | INSERT INTO `test_table_5` (`somestr`) VALUES ('e'); -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **Code To Reproduce** 14 | Show your code! 15 | 16 | ```js 17 | const Importer = require('mysql-import'); 18 | // Your code here... 19 | ``` 20 | 21 | **Expected behavior** 22 | A clear and concise description of what you expected to happen. 23 | 24 | **Screenshots** 25 | If applicable, add screenshots to help explain your problem. 26 | 27 | **Additional context** 28 | Add any other context about the problem here. 29 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context or screenshots about the feature request here. 21 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2018 Rob Parham 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "author": "Rob Parham", 3 | "bugs": { 4 | "url": "https://github.com/pamblam/mysql-import/issues" 5 | }, 6 | "deprecated": false, 7 | "description": "Import .sql into a MySQL database with Node.", 8 | "devDependencies": { 9 | "chai": "^4.2.0", 10 | "coveralls": "^3.0.9", 11 | "grunt": "^1.0.4", 12 | "grunt-contrib-concat": "^1.0.1", 13 | "grunt-string-replace": "^1.3.1", 14 | "mocha": "^7.1.0", 15 | "nyc": "^15.0.0" 16 | }, 17 | "engines": { 18 | "node": ">5.0.0" 19 | }, 20 | "homepage": "https://github.com/pamblam/mysql-import#readme", 21 | "keywords": [ 22 | "nodejs", 23 | "mysql", 24 | "textfiles", 25 | "import", 26 | "sql" 27 | ], 28 | "license": "MIT", 29 | "main": "mysql-import.js", 30 | "name": "mysql-import", 31 | "repository": { 32 | "type": "git", 33 | "url": "git+https://github.com/pamblam/mysql-import.git" 34 | }, 35 | "scripts": { 36 | "build": "grunt", 37 | "test": "node_modules/.bin/nyc --reporter=html --reporter=text node_modules/.bin/mocha ./test/test.js --timeout 15000", 38 | "memory-test": "node_modules/.bin/mocha ./test/memory-stress-test.js --timeout 0", 39 | "coverage": "nyc report --reporter=text-lcov | coveralls" 40 | }, 41 | "version": "5.1.1", 42 | "dependencies": { 43 | "mysql2": "^2.3.3" 44 | } 45 | } -------------------------------------------------------------------------------- /Gruntfile.js: -------------------------------------------------------------------------------- 1 | 2 | module.exports = function(grunt) { 3 | 4 | var pkg = grunt.file.readJSON('package.json'); 5 | pkg.version = pkg.version.split("."); 6 | var subversion = pkg.version.pop(); 7 | subversion++; 8 | pkg.version.push(subversion); 9 | pkg.version = pkg.version.join("."); 10 | grunt.file.write('package.json', JSON.stringify(pkg, null, 2)); 11 | 12 | console.log("---------------------------------------"); 13 | console.log(" Building mysql-import Version "+pkg.version); 14 | console.log("---------------------------------------"); 15 | 16 | grunt.initConfig({ 17 | pkg: pkg, 18 | concat: { 19 | options: { 20 | banner: '/**\n * <%= pkg.name %> - v<%= pkg.version %>' + 21 | '\n * <%= pkg.description %>' + 22 | '\n * @author <%= pkg.author %>' + 23 | '\n * @website <%= pkg.homepage %>' + 24 | '\n * @license <%= pkg.license %>' + 25 | '\n */\n\n' 26 | }, 27 | dist: { 28 | src: [ 29 | 'src/header.js', 30 | 'src/importer.js', 31 | 'src/parser.js' 32 | ], 33 | dest: 'mysql-import.js', 34 | }, 35 | }, 36 | 'string-replace': { 37 | source: { 38 | files: { 39 | "mysql-import.js": "mysql-import.js" 40 | }, 41 | options: { 42 | replacements: [{ 43 | pattern: /{{ VERSION }}/g, 44 | replacement: '<%= pkg.version %>' 45 | }] 46 | } 47 | }, 48 | readme: { 49 | files: { 50 | "README.md": "README.md" 51 | }, 52 | options: { 53 | replacements: [{ 54 | pattern: /\d+\.\d+\.\d+/g, 55 | replacement: '<%= pkg.version %>' 56 | }] 57 | } 58 | } 59 | } 60 | }); 61 | 62 | grunt.loadNpmTasks('grunt-contrib-concat'); 63 | grunt.loadNpmTasks('grunt-string-replace'); 64 | 65 | grunt.registerTask('default', [ 66 | 'concat', 67 | 'string-replace' 68 | ]); 69 | 70 | }; -------------------------------------------------------------------------------- /test/test-helpers.js: -------------------------------------------------------------------------------- 1 | const mysql = require('mysql2'); 2 | 3 | var con; 4 | var log_bin_trust_function_creators; 5 | 6 | /** 7 | * Handle Errors and kill the test script. 8 | * @param {Error} err 9 | * @returns {undefined} 10 | */ 11 | function errorHandler(err){ 12 | console.log("\n\nsomething went wrong: ", err.message); 13 | console.error(err); 14 | process.exit(1); 15 | } 16 | 17 | /** 18 | * Run a query 19 | * @param {type} sql 20 | * @returns {Promise} 21 | */ 22 | function query(sql){ 23 | return new Promise(done=>{ 24 | con.query(sql, (err, result)=>{ 25 | if(err) errorHandler(err); 26 | else done(result); 27 | }); 28 | }); 29 | } 30 | 31 | /** 32 | * Create a fresh MySQL connection 33 | * @param {config object} config 34 | * @returns {Connection} 35 | */ 36 | async function mysqlConnect(config){ 37 | con = mysql.createConnection({ 38 | host: config.host, 39 | user: config.user, 40 | password: config.password 41 | }); 42 | var res = await query("SHOW GLOBAL VARIABLES LIKE 'log_bin_trust_function_creators';"); 43 | log_bin_trust_function_creators = res[0].Value 44 | await query("SET GLOBAL log_bin_trust_function_creators = 1;"); 45 | } 46 | 47 | /** 48 | * Create a database to test with 49 | * @returns {undefined} 50 | */ 51 | async function createTestDB(db){ 52 | await query("DROP DATABASE IF EXISTS `"+db+"`;"); 53 | await query("CREATE DATABASE `"+db+"`;"); 54 | } 55 | 56 | /** 57 | * Destroy the testing DB 58 | * @returns {undefined} 59 | */ 60 | async function destroyTestDB(db){ 61 | await query("DROP DATABASE IF EXISTS `"+db+"`;"); 62 | } 63 | 64 | async function closeConnection(){ 65 | await query("SET GLOBAL log_bin_trust_function_creators = '"+log_bin_trust_function_creators+"';"); 66 | con.end(); 67 | } 68 | 69 | module.exports = { 70 | errorHandler, 71 | query, 72 | mysqlConnect, 73 | createTestDB, 74 | destroyTestDB, 75 | closeConnection 76 | }; -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 |
2 |
3 |
4 |
5 |