Created
February 27, 2015 01:46
-
-
Save timonwong/d9f746e5580ce66ff5b9 to your computer and use it in GitHub Desktop.
django mysqldump management command
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
# -*- coding: utf-8 -*- | |
import os | |
import sys | |
from optparse import make_option | |
from django.core.management.base import BaseCommand | |
from django.core.management.base import CommandError | |
from django.db import connections | |
from django.db import DEFAULT_DB_ALIAS | |
from django.db.backends import BaseDatabaseClient | |
class MysqlDumpClient(BaseDatabaseClient): | |
executable_name = 'mysqldump' | |
def runshell(self): | |
settings_dict = self.connection.settings_dict | |
args = [self.executable_name] | |
db = settings_dict['OPTIONS'].get('db', settings_dict['NAME']) | |
user = settings_dict['OPTIONS'].get('user', settings_dict['USER']) | |
passwd = settings_dict['OPTIONS'].get('passwd', settings_dict['PASSWORD']) | |
host = settings_dict['OPTIONS'].get('host', settings_dict['HOST']) | |
port = settings_dict['OPTIONS'].get('port', settings_dict['PORT']) | |
defaults_file = settings_dict['OPTIONS'].get('read_default_file') | |
# Seems to be no good way to set sql_mode with CLI. | |
if defaults_file: | |
args += ["--defaults-file=%s" % defaults_file] | |
if user: | |
args += ["--user=%s" % user] | |
if passwd: | |
args += ["--password=%s" % passwd] | |
if host: | |
if '/' in host: | |
args += ["--socket=%s" % host] | |
else: | |
args += ["--host=%s" % host] | |
if port: | |
args += ["--port=%s" % port] | |
if db: | |
args += [db] | |
if os.name == 'nt': | |
sys.exit(os.system(" ".join(args))) | |
else: | |
os.execvp(self.executable_name, args) | |
class Command(BaseCommand): | |
help = ("Runs the command-line client for specified database, or the " | |
"default database if none is provided.") | |
option_list = BaseCommand.option_list + ( | |
make_option('--database', action='store', dest='database', | |
default=DEFAULT_DB_ALIAS, | |
help='Nominates a database onto which to ' | |
'open a shell. Defaults to the "default" database.'), | |
) | |
requires_model_validation = False | |
def handle(self, **options): | |
connection = connections[options.get('database')] | |
client = MysqlDumpClient(connection) | |
try: | |
client.runshell() | |
except OSError: | |
# Note that we're assuming OSError means that the client program | |
# isn't installed. There's a possibility OSError would be raised | |
# for some other reason, in which case this error message would be | |
# inaccurate. Still, this message catches the common case. | |
raise CommandError( | |
'You appear not to have the %r program installed or on your ' | |
'path.' % client.executable_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment