Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added ENCRYPTION to PostgreSQL

added CHARACTER SET and COLLATION to MySQL
added note about falling back
added warning about collations using MySQL
Thanks to MikeSandford for the patches!
  • Loading branch information...
commit 1ad3fe12acfb2e7bcf63d7375b789641ef7763b9 1 parent ae5e2e2
trbs trbs authored
31 django_extensions/management/commands/sqlcreate.py
View
@@ -1,5 +1,6 @@
from django.core.management.base import NoArgsCommand
from django.conf import settings
+import sys
class Command(NoArgsCommand):
help = """Generates the SQL to create your database for you, as specified in settings.py
@@ -7,10 +8,10 @@ class Command(NoArgsCommand):
./manage.py sqlcreate [--router=<routername>] | mysql -u <db_administrator> -p
./manage.py sqlcreate [--router=<routername>] | psql -U <db_administrator> -W"""
-
+
requires_model_validation = False
can_import_settings = True
-
+
def set_db_settings(self, *args, **options):
if django.get_version() >= "1.2":
router = options.get('router')
@@ -29,37 +30,41 @@ def set_db_settings(self, *args, **options):
else:
# settings are set for django < 1.2 no modification needed
return True
-
+
def handle_noargs(self, **options):
-
+
if django.get_version() >= "1.2":
got_db_settings = self.set_db_settings(*args, **options)
if not got_db_settings:
raise CommandError("You are using Django %s which requires to specify the db-router.\nPlease specify the router by adding --router=<routername> to this command." % django.get_version())
- return
-
-
+
#print "%s %s %s %s" % (settings.DATABASE_ENGINE, settings.DATABASE_NAME, settings.DATABASE_USER, settings.DATABASE_PASSWORD)
engine = settings.DATABASE_ENGINE
dbname = settings.DATABASE_NAME
dbuser = settings.DATABASE_USER
dbpass = settings.DATABASE_PASSWORD
dbhost = settings.DATABASE_HOST
-
+
# django settings file tells you that localhost should be specified by leaving
# the DATABASE_HOST blank
if not dbhost:
dbhost = 'localhost'
-
+
if engine == 'mysql':
- print "CREATE DATABASE %s;" % dbname
+ sys.stderr.write("""-- WARNING!: https://docs.djangoproject.com/en/dev/ref/databases/#collation-settings
+-- Please read this carefully! Collation will be set to utf8_bin to have case-sensitive data.
+""");
+ print "CREATE DATABASE %s CHARACTER SET utf8 COLLATE utf8_bin;" % dbname
print "GRANT ALL PRIVILEGES ON %s.* to '%s'@'%s' identified by '%s';" % (
dbname, dbuser, dbhost, dbpass)
elif engine == 'postgresql_psycopg2':
- print "CREATE USER %s WITH password '%s';" % (dbuser, dbpass)
- print "CREATE DATABASE %s WITH OWNER %s;" % (dbname, dbuser)
+ print "CREATE USER %s WITH ENCRYPTED PASSWORD '%s';" % (dbuser, dbpass)
+ print "CREATE DATABASE %s WITH ENCODING 'UTF-8' OWNER \"%s\";" % (dbname, dbuser)
#print "GRANT ALL PRIVILEGES ON DATABASE %s TO %s" % (dbname, dbuser)
+ elif engine == 'sqlite3':
+ sys.stderr.write("-- manage.py syncdb will automatically create a sqlite3 database file.\n")
else:
- # fallback on SQL92
+ # CREATE DATABASE is not SQL standard, but seems to be supported by most.
+ sys.stderr.write("-- Don't know how to handle '%s' falling back to SQL.\n" % engine)
print "CREATE DATABASE %s;" % dbname
print "GRANT ALL PRIVILEGES ON DATABASE %s to %s" % (dbname, dbuser)
8 docs/sqlcreate.rst
View
@@ -38,8 +38,8 @@ MySQL
Known Issues
------------
- * Falls back to what SQL standard if not MySQL or PostgreSQL but does not create
- user or set password in that case. It does try to do a GRANT to the database user.
- * Missing option to create all database in a multi-db setup automatically
- * Missing options for encodings, tablespaces, etc.
+ * CREATE DATABASE is not SQL standard so might not work everywhere.
+ * When using fallback user is not created and password is not set.
+ But it does try to do a GRANT to the database user.
+ * Missing options for tablespaces, etc.
Please sign in to comment.
Something went wrong with that request. Please try again.