Created
December 1, 2009 10:09
-
-
Save scottwb/246201 to your computer and use it in GitHub Desktop.
Ruby script to drop all tables in a MySQL database.
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
#!/usr/bin/env ruby | |
# | |
# Drops all the tables in a MySQL database, without dropping the databse. | |
# | |
############################################################################### | |
# Usage | |
############################################################################### | |
usage=<<EOT | |
Usage: | |
drop_all_tables.rb <database> | |
<database>: | |
The name of the database whose tables to drop. | |
EOT | |
############################################################################### | |
# Configuration | |
############################################################################### | |
MYSQL_CMD = "mysql -u root" | |
############################################################################### | |
# Main Script | |
############################################################################### | |
if ARGV.size != 1 | |
puts usage | |
exit -1 | |
end | |
database = ARGV[0] | |
tables = `#{MYSQL_CMD} #{database} -e "show tables;"` | |
tables = tables.split(/$/).map{|t| t.strip}.reject{|t| t.empty?} | |
tables.shift | |
sql = tables.map{|t| "drop table #{t};"}.join(' ') | |
system("#{MYSQL_CMD} #{database} -e \"#{sql}\"") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment