Skip to content

Instantly share code, notes, and snippets.

@scottwb
Created December 1, 2009 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottwb/246201 to your computer and use it in GitHub Desktop.
Save scottwb/246201 to your computer and use it in GitHub Desktop.
Ruby script to drop all tables in a MySQL database.
#!/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