Tay Ray Chuan avatar Tay Ray Chuan committed 559e841

product/migrations: add support for sqlite3

Comments (0)

Files changed (2)

satchmo/apps/product/migrations/0002_add_attributeoption.py

 # encoding: utf-8
 import datetime
 from south.db import db
+from south.logger import get_logger
 from south.v2 import SchemaMigration
 from django.db import models
 
                     validation=default_validation,
                 )
 
+        if db.backend_name=='sqlite3':
+            get_logger().debug("dropping and re-creating table for ProductAttribute")
+            if db.dry_run:
+                return
+            #
+            # We re-create ProductAttribute, since sqlite does not support adding
+            # foreign key constraints on existing tables (ie. adding ForeignKey
+            # fields).
+            #
+            # We have to do 0003's work here, because we can't iterate over
+            # ProductAttribute instances there - the 'option' column has not
+            # been created and django barfs if we do so.
+            #
+
+            # Collect old data
+            old_attrs = {}
+            for attr in orm['product.ProductAttribute'].objects.all():
+                obj = {}
+                # We have already collected 'name' earlier, so we can leave it
+                # out.
+                # TODO make this more generic
+                for k in ('product', 'languagecode', 'value'):
+                    obj[k] = getattr(attr, k)
+                old_attrs[attr.id] = obj
+
+            # Deleting old 'ProductAttribute' table
+            db.delete_table('product_productattribute')
+
+            # Re-use create_table expression for old 'ProductAttribute', this
+            # time with the adding the 'option' column
+            db.create_table('product_productattribute', (
+                ('languagecode', self.gf('django.db.models.fields.CharField')(max_length=10, null=True, blank=True)),
+                ('product', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['product.Product'])),
+                ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
+                ('value', self.gf('django.db.models.fields.CharField')(max_length=255)),
+                ('name', self.gf('django.db.models.fields.SlugField')(max_length=100, db_index=True)),
+                ('option', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['product.AttributeOption']))
+            ))
+            db.send_create_signal('product', ['ProductAttribute'])
+
+            # Add back data
+            for id, attr_dict in old_attrs.items():
+                kwargs = {}
+                for field in ('product', 'languagecode', 'value'):
+                    kwargs[field] = attr_dict[field]
+                orm['product.ProductAttribute'].objects.create(
+                    id=id, **kwargs)
+
 
     def backwards(self, orm):
 
         # Deleting model 'AttributeOption'
         db.delete_table('product_attributeoption')
 
+        if db.backend_name == 'sqlite3':
+            # Since we added the 'option' column in forwards(), delete it here.
+            db.delete_column('product_productattribute', 'option_id')
+
 
     models = {
         'product.attributeoption': {

satchmo/apps/product/migrations/0003_add_productattribute_option.py

 
     def forwards(self, orm):
 
-        # Adding field 'ProductAttribute.option'
-        db.add_column('product_productattribute', 'option', self.gf('django.db.models.fields.related.ForeignKey')(default=None, to=orm['product.AttributeOption']), keep_default=False)
+        if not db.backend_name == 'sqlite3':
+            # Adding field 'ProductAttribute.option'
+            db.add_column('product_productattribute', 'option', self.gf('django.db.models.fields.related.ForeignKey')(default=None, to=orm['product.AttributeOption']), keep_default=False)
+        else:
+            # The previous migration has already created the column; nothing to
+            # do here.
+            pass
 
         if not db.dry_run:
             for attr in orm['product.ProductAttribute'].objects.all():
 
     def backwards(self, orm):
 
-        # Deleting field 'ProductAttribute.option'
-        db.delete_column('product_productattribute', 'option_id')
+        if not db.backend_name == 'sqlite3':
+            # Deleting field 'ProductAttribute.option'
+            db.delete_column('product_productattribute', 'option_id')
+        else:
+            # The previous migration will handle this.
+            pass
 
 
     models = {
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.