From 53927503484e08ac7602f7f96aa45da916c86f7a Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Tue, 27 Nov 2012 16:10:17 +0100 Subject: [PATCH] fix inital database creation --- README | 2 ++ pandora/annotation/migrations/0001_initial.py | 6 +++- pandora/app/config.py | 23 +++++++----- .../item/management/commands/sync_itemsort.py | 35 +++++++++++++++++-- pandora/item/migrations/0001_initial.py | 4 +++ 5 files changed, 58 insertions(+), 12 deletions(-) diff --git a/README b/README index 23c47af5..418226ab 100644 --- a/README +++ b/README @@ -97,7 +97,9 @@ Important: "use_your_own" is a password and you have to use the same value here su pandora cd /srv/pandora/pandora ./manage.py syncdb --noinput + ./manage.py migrate ./manage.py sqlfindindex | ./manage.py dbshell + ./manage.py sync_itemsort ./manage.py collectstatic -l --noinput ./manage.py update_static ./manage.py compile_pyc diff --git a/pandora/annotation/migrations/0001_initial.py b/pandora/annotation/migrations/0001_initial.py index 6d6a957c..747b4e59 100644 --- a/pandora/annotation/migrations/0001_initial.py +++ b/pandora/annotation/migrations/0001_initial.py @@ -6,6 +6,10 @@ from django.db import models class Migration(SchemaMigration): + depends_on = ( + ("item", "0001_initial"), + ("clip", "0001_initial"), + ) def forwards(self, orm): # Adding model 'Annotation' @@ -179,4 +183,4 @@ class Migration(SchemaMigration): } } - complete_apps = ['annotation'] \ No newline at end of file + complete_apps = ['annotation'] diff --git a/pandora/app/config.py b/pandora/app/config.py index 2851945a..5c293363 100644 --- a/pandora/app/config.py +++ b/pandora/app/config.py @@ -51,15 +51,20 @@ def load_config(): settings.CONFIG = config admin = len(settings.CONFIG['userLevels']) - 1 - if not 'syncdb' in sys.argv and not 'sqldiff' in sys.argv: - if User.objects.filter(profile__level=admin).count() == 0: - for u in User.objects.filter(is_superuser=True): - p = u.get_profile() - p.level = admin - p.save() - settings.ADMIN = tuple([(u.username, u.email) - for u in User.objects.filter(profile__level=admin)]) - settings.MANAGERS = settings.ADMINS + if not 'syncdb' in sys.argv \ + and not 'sqldiff' in sys.argv \ + and not 'migrate' in sys.argv: + try: + if User.objects.filter(profile__level=admin).count() == 0: + for u in User.objects.filter(is_superuser=True): + p = u.get_profile() + p.level = admin + p.save() + settings.ADMIN = tuple([(u.username, u.email) + for u in User.objects.filter(profile__level=admin)]) + settings.MANAGERS = settings.ADMINS + except: + pass def reloader_thread(): _config_mtime = 0 diff --git a/pandora/item/management/commands/sync_itemsort.py b/pandora/item/management/commands/sync_itemsort.py index dd9e44e3..16640286 100644 --- a/pandora/item/management/commands/sync_itemsort.py +++ b/pandora/item/management/commands/sync_itemsort.py @@ -1,12 +1,14 @@ # -*- coding: utf-8 -*- # vi:si:et:sw=4:sts=4:ts=4 +from optparse import make_option from django.core.management.base import BaseCommand from django.db import connection, transaction +from django.db.models import fields import monkey_patch.models from ... import models - +import clip.models class Command(BaseCommand): """ @@ -14,6 +16,10 @@ class Command(BaseCommand): """ help = 'alter table to match itemKeys in site.json.' args = '' + option_list = BaseCommand.option_list + ( + make_option('--debug', action='store_true', dest='debug', + default=False, help='print sql commans'), + ) def handle(self, **options): table_name = models.ItemSort._meta.db_table @@ -56,10 +62,35 @@ class Command(BaseCommand): sql = 'ALTER TABLE "%s" ALTER COLUMN "%s" %s NOT NULL' % (table_name, name, f.null and "DROP" or "SET") changes.append(sql) + + #also update clip index + table_name = clip.models.Clip._meta.db_table + db_rows = connection.introspection.get_table_description(cursor, table_name) + db_fields = dict([(row[0], row) for row in db_rows]) + db_types = dict([(row[0], + connection.introspection.data_types_reverse[row[1]]) for row in db_rows]) + model_fields = ['item_id', 'sort_id'] + [f.name for f in clip.models.Clip._meta.fields] + for name in db_types: + if name not in model_fields: + sql = 'ALTER TABLE "%s" DROP COLUMN "%s"' % (table_name, name) + changes.append(sql) + for f in clip.models.Clip._meta.fields: + if not f.primary_key and not isinstance(f, fields.related.ForeignKey): + name = f.name + col_type = f.db_type(connection) + if name not in db_fields: + sql = 'ALTER TABLE "%s" ADD COLUMN "%s" %s' % (table_name, name, col_type) + changes.append(sql) + sql = 'CREATE INDEX "%s_%s_idx" ON "%s" ("%s")' % (table_name, name, + table_name, name) + changes.append(sql) + sql = 'ALTER TABLE "%s" ALTER COLUMN "%s" SET NOT NULL' % (table_name, name) + changes.append(sql) if changes: print "Database needs to be updated, plase wait..." for sql in changes: - print sql + if options['debug']: + print sql cursor.execute(sql) transaction.commit_unless_managed() diff --git a/pandora/item/migrations/0001_initial.py b/pandora/item/migrations/0001_initial.py index 4c02eb06..3a2f6950 100644 --- a/pandora/item/migrations/0001_initial.py +++ b/pandora/item/migrations/0001_initial.py @@ -42,6 +42,10 @@ class Migration(SchemaMigration): )) db.create_unique('item_item_groups', ['item_id', 'group_id']) + # Adding model 'ItemSort' + db.create_table('item_itemsort', ( + ('item', self.gf('django.db.models.fields.related.ForeignKey')(related_name='sort', to=orm['item.Item'],primary_key=True)), + )) # Adding model 'ItemFind' db.create_table('item_itemfind', ( ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),