Created
March 9, 2010 20:33
-
-
Save dolzenko/327085 to your computer and use it in GitHub Desktop.
extract_tables_from_dump_using_enumerable_slice_before
This file contains 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
if ARGV.size < 1 | |
puts "Use like mysqldump database_name | ./#{ $PROGRAM_NAME } table_name[s]" | |
exit(0) | |
end | |
# Dump for each table in standard mysqldump output is started with the line like: | |
# | |
# -- Table structure for table `access_rights` | |
# | |
# Use +Enumerable#slice_before+ to slice the whole dump into sections per table | |
# and then select only sections for the tables we are interested in. | |
# Create regexp which will match any of the passed table names quoted with backticks | |
quoted_table_names = /`#{ ARGV.join("|") }`/ | |
table_dump = STDIN.slice_before(/\A-- Table structure for table/).select do |table_section| | |
table_section.first =~ quoted_table_names | |
end.join | |
print(table_dump) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment