Created
September 2, 2018 15:37
-
-
Save scorredoira/e9003637159941cc0d47081c7b393203 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "stdlib/native" | |
/** | |
* ------------------------------------------------------------------ | |
* Extract databases and tables from dump files | |
* | |
* usage: amura extract -f foo.sql -d bardb | |
* ------------------------------------------------------------------ | |
*/ | |
function main(file: string, database: string, table?: string) { | |
let cmd; | |
if (filepath.ext(file) == "") { | |
cmd = "bzip2 -cd " + file + " | sed -n \"/^-- Current Database: \`" + database + "\`/,/^-- Current Database: \`/p\" " | |
} else { | |
cmd = "sed -n \"/^-- Current Database: \`" + database + "\`/,/^-- Current Database: \`/p\" " + file; | |
} | |
if (table) { | |
cmd += " | sed -n '/^-- Table structure for table `" + table + "`/,/^-- Table structure for table `/p' " | |
} | |
cmd += " > tmp.sql" | |
let fs = os.fileSystem; | |
if (table) { | |
// disable constraints | |
fs.write("out.sql", ` | |
SET @BACKUP_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS; | |
SET @@FOREIGN_KEY_CHECKS=0; | |
`) | |
} else { | |
fs.write("out.sql", "") | |
} | |
let out = os.exec("bash", "-c", cmd) | |
if (out) { | |
fmt.println(out) | |
} | |
out = os.exec("bash", "-c", "cat tmp.sql >> out.sql") | |
if (out) { | |
fmt.println(out) | |
} | |
if (table) { | |
// enable constraints | |
fs.append("out.sql", ` | |
SET @@FOREIGN_KEY_CHECKS=@BACKUP_FOREIGN_KEY_CHECKS | |
`) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment