Skip to content
This repository
  • 4 commits
  • 3 files changed
  • 0 comments
  • 2 contributors
112  docs/index.rst
Source Rendered
@@ -57,24 +57,6 @@ Options for upgradedb
57 57
   `./manage.py upgradedb --seed 005` will skip migrations 000 to 005 but not
58 58
   006.
59 59
 
60  
-
61  
-Configuration for comparedb
62  
----------------------------
63  
-
64  
-The `comparedb` command is available only for Postgres. It executes a few raw
65  
-postgres shell commands which you might need to customize to add user
66  
-credentials, encoding or specify database templates. This can be done through
67  
-the `NASHVEGAS` dictionnary in your setting::
68  
-
69  
-    NASHVEGAS = {
70  
-        "createdb": "createdb -U postgres -T template0 -E UTF8",
71  
-        "dropdb": "dropdb -U postgres",
72  
-        "pg_dump": "pg_dump -U postgres",
73  
-    }
74  
-
75  
-By default, nashvegas executes raw `createdb`, `dropdb` or `pg_dump` commands.
76  
-
77  
-
78 60
 Conventions
79 61
 -----------
80 62
 
@@ -110,6 +92,100 @@ product codes on next release::
110 92
             product.code = "NEW-%s" % product.code
111 93
             product.save()
112 94
 
  95
+Configuration for comparedb
  96
+---------------------------
  97
+
  98
+The `comparedb` command is available only for advanced system administrators.
  99
+It proceeds as such:
  100
+
  101
+* get the SQL structure dump of the current database
  102
+* create a new database, the "compare" database
  103
+* syncdb in the "compare" database,
  104
+* get the SQL structure dump of the "compare" database
  105
+* output the diff
  106
+
  107
+It executes a few raw shell commands which you might need to customize to add
  108
+user credentials, encoding or specify database templates. This can be done
  109
+through the `NASHVEGAS` dictionnary in your setting.
  110
+
  111
+Example for PostgreSQL
  112
+``````````````````````
  113
+
  114
+By default, nashvegas executes raw `createdb`, `dropdb` or `pg_dump` commands,
  115
+example customisation::
  116
+
  117
+    NASHVEGAS = {
  118
+        "createdb": "createdb -U postgres -T template0 -E UTF8 {dbname}",
  119
+        "dropdb": "dropdb -U postgres {dbname}",
  120
+        "dumpdb": "pg_dump -U postgres {dbname}",
  121
+    }
  122
+
  123
+
  124
+If you add a field "test" on model "Foo", comparedb will output::
  125
+
  126
+    >>> ./manage.py comparedb
  127
+    Getting schema for current database...
  128
+    Getting schema for fresh database...
  129
+    Outputing diff between the two...
  130
+    --- 
  131
+    +++ 
  132
+    @@ -515,7 +515,8 @@
  133
+     
  134
+     CREATE TABLE testapp_foo (
  135
+         id integer NOT NULL,
  136
+    -    bar character varying(100)
  137
+    +    bar character varying(100),
  138
+    +    test character varying(100)
  139
+     );
  140
+
  141
+Example for MySQL
  142
+`````````````````
  143
+
  144
+MySQL is not supported by default thought such settings do work::
  145
+
  146
+    NASHVEGAS = { 
  147
+        "createdb": "mysql -u root -p -e \"create database {dbname}\"",
  148
+        "dropdb": "mysql -u root -p -e \"drop database {dbname}\"",
  149
+        "dumpdb": "mysqldump -u root -p {dbname}",
  150
+    }
  151
+
  152
+If you add a field "test" on model "Foo", comparedb will output::
  153
+
  154
+    >>> ./manage.py comparedb       
  155
+    Getting schema for current database...
  156
+    Enter password: 
  157
+    Getting schema for fresh database...
  158
+    Enter password: 
  159
+    Enter password: 
  160
+    Enter password: 
  161
+    Outputing diff between the two...
  162
+    --- 
  163
+    +++ 
  164
+    @@ -1,6 +1,6 @@
  165
+     -- MySQL dump 10.13  Distrib 5.1.58, for debian-linux-gnu (x86_64)
  166
+     --
  167
+    --- Host: localhost    Database: testproject
  168
+    +-- Host: localhost    Database: testproject_compare
  169
+     -- ------------------------------------------------------
  170
+     -- Server version  5.1.58-1ubuntu1
  171
+     
  172
+    @@ -419,6 +419,7 @@
  173
+     CREATE TABLE `testapp_foo` (
  174
+       `id` int(11) NOT NULL AUTO_INCREMENT,
  175
+       `bar` varchar(100) DEFAULT NULL,
  176
+    +  `test` varchar(100) DEFAULT NULL,
  177
+       PRIMARY KEY (`id`)
  178
+     ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  179
+     /*!40101 SET character_set_client = @saved_cs_client */;
  180
+    @@ -441,4 +442,4 @@
  181
+     /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
  182
+     /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
  183
+     
  184
+    --- Dump completed on 2012-03-07 12:58:15
  185
+    +-- Dump completed on 2012-03-07 12:58:18
  186
+
  187
+Typicall customisation would be to setup a `$HOME/.my.cnf` that contains
  188
+credentials allowing to run this command without password prompt.
113 189
 
114 190
 Indices and tables
115 191
 ==================
42  nashvegas/management/commands/comparedb.py
@@ -9,7 +9,7 @@
9 9
 from django.core.management.base import BaseCommand
10 10
 
11 11
 
12  
-NASHVEGAS = getattr(settings, "NASHVEGAS", None)
  12
+NASHVEGAS = getattr(settings, "NASHVEGAS", {})
13 13
 
14 14
 
15 15
 class Command(BaseCommand):
@@ -24,16 +24,12 @@ class Command(BaseCommand):
24 24
     help = "Compares current database with the one that nashvegas will build from scratch."
25 25
     
26 26
     def setup_database(self):
27  
-        command = "createdb %s" % self.name
28  
-        if NASHVEGAS and "createdb" in settings.NASHVEGAS:
29  
-            command = "%s %s" % (settings.NASHVEGAS["createdb"], self.name)
30  
-        Popen(command.split()).wait()
  27
+        command = NASHVEGAS.get("createdb", "createdb {dbname}")
  28
+        Popen(command.format(dbname=self.compare_name), shell=True).wait()
31 29
     
32 30
     def teardown_database(self):
33  
-        command = "dropdb %s" % self.name
34  
-        if NASHVEGAS and "dropdb" in settings.NASHVEGAS:
35  
-            command = "%s %s" % (settings.NASHVEGAS["dropdb"], self.name)
36  
-        Popen(command.split()).wait()
  31
+        command = NASHVEGAS.get("dropdb", "dropdb {dbname}")
  32
+        Popen(command.format(dbname=self.compare_name), shell=True).wait()
37 33
     
38 34
     def handle(self, *args, **options):
39 35
         """
@@ -44,30 +40,26 @@ def handle(self, *args, **options):
44 40
         report the diffs to the user.
45 41
         """
46 42
         self.db = options.get("database", DEFAULT_DB_ALIAS)
47  
-        self.name = options.get("db_name")
48  
-        if not self.name:
49  
-            self.name = "%s_compare" % connections.databases[self.db]["NAME"]
50  
-        
51  
-        command = "pg_dump -s %s" % connections[self.db].settings_dict["NAME"]
52  
-        if NASHVEGAS and "pg_dump" in settings.NASHVEGAS:
53  
-            command = "%s -s %s" % (settings.NASHVEGAS["pg_dump"], 
54  
-                connections[self.db].settings_dict["NAME"])
55  
-        
  43
+        self.current_name = connections[self.db].settings_dict["NAME"]
  44
+        self.compare_name = options.get("db_name")
  45
+        if not self.compare_name:
  46
+            self.compare_name = "%s_compare" % self.current_name
  47
+
  48
+        command = NASHVEGAS.get("dumpdb", "pg_dump -s {dbname}")
  49
+
56 50
         print "Getting schema for current database..."
57  
-        print command
58  
-        current_sql = Popen(command.split(), stdout=PIPE).stdout.readlines()
  51
+        current_sql = Popen(command.format(dbname=self.current_name),
  52
+            shell=True, stdout=PIPE).stdout.readlines()
59 53
         
60 54
         print "Getting schema for fresh database..."
61 55
         self.setup_database()
62  
-        orig = connections[self.db].settings_dict["NAME"]
63 56
         connections[self.db].close()
64  
-        connections[self.db].settings_dict["NAME"] = self.name
  57
+        connections[self.db].settings_dict["NAME"] = self.compare_name
65 58
         call_command("syncdb", interactive=False, verbosity=0)
66  
-        print command
67  
-        new_sql = Popen(command.split() + ["-s", self.name], 
  59
+        new_sql = Popen(command.format(dbname=self.compare_name).split(),
68 60
             stdout=PIPE).stdout.readlines()
69 61
         connections[self.db].close()
70  
-        connections[self.db].settings_dict["NAME"] = orig
  62
+        connections[self.db].settings_dict["NAME"] = self.current_name
71 63
         self.teardown_database()
72 64
         
73 65
         print "Outputing diff between the two..."
2  nashvegas/utils.py
@@ -43,7 +43,7 @@ def model_installed(model):
43 43
         converter = connection.introspection.table_name_converter
44 44
         db_table_in = (converter(opts.db_table) in tables)
45 45
         auto_create_in = (
46  
-            opts.auto_created and \
  46
+            opts.auto_created and
47 47
             converter(opts.auto_created._meta.db_table) in tables
48 48
         )
49 49
         return not (db_table_in or auto_create_in)

No commit comments for this range

Something went wrong with that request. Please try again.