fix inital database creation

This commit is contained in:
j 2012-11-27 16:10:17 +01:00
parent ab9b5adc92
commit 5392750348
5 changed files with 58 additions and 12 deletions

2
README
View file

@ -97,7 +97,9 @@ Important: "use_your_own" is a password and you have to use the same value here
su pandora su pandora
cd /srv/pandora/pandora cd /srv/pandora/pandora
./manage.py syncdb --noinput ./manage.py syncdb --noinput
./manage.py migrate
./manage.py sqlfindindex | ./manage.py dbshell ./manage.py sqlfindindex | ./manage.py dbshell
./manage.py sync_itemsort
./manage.py collectstatic -l --noinput ./manage.py collectstatic -l --noinput
./manage.py update_static ./manage.py update_static
./manage.py compile_pyc ./manage.py compile_pyc

View file

@ -6,6 +6,10 @@ from django.db import models
class Migration(SchemaMigration): class Migration(SchemaMigration):
depends_on = (
("item", "0001_initial"),
("clip", "0001_initial"),
)
def forwards(self, orm): def forwards(self, orm):
# Adding model 'Annotation' # Adding model 'Annotation'

View file

@ -51,15 +51,20 @@ def load_config():
settings.CONFIG = config settings.CONFIG = config
admin = len(settings.CONFIG['userLevels']) - 1 admin = len(settings.CONFIG['userLevels']) - 1
if not 'syncdb' in sys.argv and not 'sqldiff' in sys.argv: if not 'syncdb' in sys.argv \
if User.objects.filter(profile__level=admin).count() == 0: and not 'sqldiff' in sys.argv \
for u in User.objects.filter(is_superuser=True): and not 'migrate' in sys.argv:
p = u.get_profile() try:
p.level = admin if User.objects.filter(profile__level=admin).count() == 0:
p.save() for u in User.objects.filter(is_superuser=True):
settings.ADMIN = tuple([(u.username, u.email) p = u.get_profile()
for u in User.objects.filter(profile__level=admin)]) p.level = admin
settings.MANAGERS = settings.ADMINS 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(): def reloader_thread():
_config_mtime = 0 _config_mtime = 0

View file

@ -1,12 +1,14 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4 # vi:si:et:sw=4:sts=4:ts=4
from optparse import make_option
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from django.db import connection, transaction from django.db import connection, transaction
from django.db.models import fields
import monkey_patch.models import monkey_patch.models
from ... import models from ... import models
import clip.models
class Command(BaseCommand): class Command(BaseCommand):
""" """
@ -14,6 +16,10 @@ class Command(BaseCommand):
""" """
help = 'alter table to match itemKeys in site.json.' help = 'alter table to match itemKeys in site.json.'
args = '' args = ''
option_list = BaseCommand.option_list + (
make_option('--debug', action='store_true', dest='debug',
default=False, help='print sql commans'),
)
def handle(self, **options): def handle(self, **options):
table_name = models.ItemSort._meta.db_table table_name = models.ItemSort._meta.db_table
@ -57,9 +63,34 @@ class Command(BaseCommand):
f.null and "DROP" or "SET") f.null and "DROP" or "SET")
changes.append(sql) 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: if changes:
print "Database needs to be updated, plase wait..." print "Database needs to be updated, plase wait..."
for sql in changes: for sql in changes:
print sql if options['debug']:
print sql
cursor.execute(sql) cursor.execute(sql)
transaction.commit_unless_managed() transaction.commit_unless_managed()

View file

@ -42,6 +42,10 @@ class Migration(SchemaMigration):
)) ))
db.create_unique('item_item_groups', ['item_id', 'group_id']) 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' # Adding model 'ItemFind'
db.create_table('item_itemfind', ( db.create_table('item_itemfind', (
('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),