diff options
Diffstat (limited to 'chimere/scripts/upgrade.py')
-rwxr-xr-x | chimere/scripts/upgrade.py | 112 |
1 files changed, 107 insertions, 5 deletions
diff --git a/chimere/scripts/upgrade.py b/chimere/scripts/upgrade.py index f260a07..46b3548 100755 --- a/chimere/scripts/upgrade.py +++ b/chimere/scripts/upgrade.py @@ -13,7 +13,7 @@ from django.db import connection, transaction cursor = connection.cursor() -from main.models import Area, Route +from main.models import Area, Marker, Route, Icon, SubCategory from django.contrib.gis.geos import LineString # early versions before 0.1: urn field doesn't exist for area @@ -32,12 +32,14 @@ def slugfy(text, separator): ret = re.sub(" +", separator, ret) return ret.strip() +QUERY_CHECK_FIELD = """SELECT a.attname AS field FROM pg_class c, pg_attribute a + WHERE c.relname = '%s' AND a.attnum > 0 AND a.attrelid = c.oid + AND a.attname='%s';""" QUERY_CHECK_TABLE = """SELECT c.relname FROM pg_class c WHERE c.relname = '%s';""" -query = """SELECT a.attname AS field FROM pg_class c, pg_attribute a - WHERE c.relname = 'main_area' AND a.attnum > 0 AND a.attrelid = c.oid - AND a.attname='urn';""" + +query = QUERY_CHECK_FIELD % ('main_area', 'urn') cursor.execute(query) transaction.commit_unless_managed() @@ -93,7 +95,8 @@ if not row: row = cursor.fetchone() if row: - query_rename = """ALTER TABLE subcategory_areas RENAME TO main_subcategory_areas;""" + query_rename = "ALTER TABLE subcategory_areas RENAME TO \ +main_subcategory_areas;" cursor.execute(query_rename) transaction.commit_unless_managed() print " * subcategory_areas renamed to main_subcategory_areas" @@ -159,6 +162,104 @@ for area in areas: if changed: print " * projections of areas corrected" +# changement from version 1.0 to 1.1: version of django 1.2 +# create specific height and width for image fields + +for cls, attr in ((Icon, "image"), (Marker, "picture"), + (Route, "picture")): + table = cls._meta.db_table + query = QUERY_CHECK_FIELD % (table, 'width') + cursor.execute(query) + transaction.commit_unless_managed() + + row = cursor.fetchone() + if not row: + query_update = "ALTER TABLE "+table+" ADD COLUMN width integer" + cursor.execute(query_update) + transaction.commit_unless_managed() + query_update = "ALTER TABLE "+table+" ADD COLUMN height integer" + cursor.execute(query_update) + transaction.commit_unless_managed() + for obj in cls.objects.all(): + image = getattr(obj, attr) + if not image: + continue + obj.width = image.width + obj.height = image.height + obj.save() + print " * height and width of " + table + " corrected" + +# changement from version 1.0 to 1.1: add dated fields to markers and routes +if settings.DAYS_BEFORE_EVENT: + for cls in (Marker, Route): + table = cls._meta.db_table + query = QUERY_CHECK_FIELD % (table, 'start_date') + cursor.execute(query) + transaction.commit_unless_managed() + + row = cursor.fetchone() + if not row: + query_update = "ALTER TABLE "+table+" ADD COLUMN start_date date" + cursor.execute(query_update) + transaction.commit_unless_managed() + query_update = "ALTER TABLE "+table+" ADD COLUMN end_date date" + cursor.execute(query_update) + transaction.commit_unless_managed() + print " * start_date and end_date added to table " + table + "." + +# changement from version 1.0 to 1.1: add available_date field to marker +if 'chimere.rss' in settings.INSTALLED_APPS: + for cls in (Marker,): + table = cls._meta.db_table + query = QUERY_CHECK_FIELD % (table, 'available_date') + cursor.execute(query) + transaction.commit_unless_managed() + + row = cursor.fetchone() + if not row: + query_update = "ALTER TABLE " + table + " ADD COLUMN \ +available_date timestamp with time zone" + cursor.execute(query_update) + transaction.commit_unless_managed() + print " * available_date added to table " + table + "." + + +# changement from version 1.0 to 1.1: multiple selection of categories + +for cls in (Marker, Route): + table = cls._meta.db_table[len("main_"):] + query = QUERY_CHECK_TABLE % ('main_' + table + '_categories') + cursor.execute(query) + transaction.commit_unless_managed() + + row = cursor.fetchone() + if row: + continue + query_create = """ +CREATE TABLE "main_%s_categories" ( + "id" serial NOT NULL PRIMARY KEY, + "%s_id" integer NOT NULL REFERENCES "main_%s" ("id") DEFERRABLE INITIALLY DEFERRED, + "subcategory_id" integer NOT NULL REFERENCES "main_subcategory" ("id") DEFERRABLE INITIALLY DEFERRED, + UNIQUE ("%s_id", "subcategory_id")); +""" % (table, table, table, table) + cursor.execute(query_create) + transaction.commit_unless_managed() + for obj in cls.objects.all(): + query = "select subcategory_id from main_%s where id=%d" % (table, + obj.id) + cursor.execute(query) + transaction.commit_unless_managed() + + row = cursor.fetchone() + if row: + obj.categories.add(SubCategory.objects.get(id=row[0])) + obj.save() + query = "ALTER TABLE main_%s DROP COLUMN subcategory_id;" % table + cursor.execute(query) + transaction.commit_unless_managed() + print " * main_%s_categories created" % table + + # early versions before 0.1: save route with wrong SRID # only errors with default SRID is managed adapt the script for your SRID @@ -177,3 +278,4 @@ for route in routes: route.save() if changed: print " * projections of routes corrected" + |