From 45dc3ac55bca04b14cbcf71643613fe75ae99ee4 Mon Sep 17 00:00:00 2001 From: Michael Ziegler Date: Sun, 21 Feb 2010 13:20:38 +0100 Subject: [PATCH] implement data conversion and add scripts to convert the SQLITE db. todo: mysql/pgsql. --- .../sqlite/01-schema-mumble-server_id.sql | 2 + .../sqlite/02-schema-mumble_mumbleuser.sql | 2 + .../sqlite/11-data-mumble_mumbleserver.sql | 3 + .../sqlite/12-data-mumble_mumble.sql | 6 ++ pyweb/mumble/management/__init__.py | 38 ++++++++++-- pyweb/mumble/management/update_schema.py | 62 +++++++++++++++++++ 6 files changed, 109 insertions(+), 4 deletions(-) create mode 100644 pyweb/mumble/conversionsql/sqlite/01-schema-mumble-server_id.sql create mode 100644 pyweb/mumble/conversionsql/sqlite/02-schema-mumble_mumbleuser.sql create mode 100644 pyweb/mumble/conversionsql/sqlite/11-data-mumble_mumbleserver.sql create mode 100644 pyweb/mumble/conversionsql/sqlite/12-data-mumble_mumble.sql create mode 100644 pyweb/mumble/management/update_schema.py diff --git a/pyweb/mumble/conversionsql/sqlite/01-schema-mumble-server_id.sql b/pyweb/mumble/conversionsql/sqlite/01-schema-mumble-server_id.sql new file mode 100644 index 0000000..5785218 --- /dev/null +++ b/pyweb/mumble/conversionsql/sqlite/01-schema-mumble-server_id.sql @@ -0,0 +1,2 @@ +ALTER TABLE "mumble_mumble" + ADD "server_id" integer; diff --git a/pyweb/mumble/conversionsql/sqlite/02-schema-mumble_mumbleuser.sql b/pyweb/mumble/conversionsql/sqlite/02-schema-mumble_mumbleuser.sql new file mode 100644 index 0000000..bc902fd --- /dev/null +++ b/pyweb/mumble/conversionsql/sqlite/02-schema-mumble_mumbleuser.sql @@ -0,0 +1,2 @@ +ALTER TABLE "mumble_mumble" + ADD "display" varchar(200); diff --git a/pyweb/mumble/conversionsql/sqlite/11-data-mumble_mumbleserver.sql b/pyweb/mumble/conversionsql/sqlite/11-data-mumble_mumbleserver.sql new file mode 100644 index 0000000..f53f7eb --- /dev/null +++ b/pyweb/mumble/conversionsql/sqlite/11-data-mumble_mumbleserver.sql @@ -0,0 +1,3 @@ +INSERT INTO "mumble_mumbleserver" ( "dbus", "secret" ) +SELECT DISTINCT "dbus", '' +FROM "mumble_mumble"; diff --git a/pyweb/mumble/conversionsql/sqlite/12-data-mumble_mumble.sql b/pyweb/mumble/conversionsql/sqlite/12-data-mumble_mumble.sql new file mode 100644 index 0000000..04ed3b4 --- /dev/null +++ b/pyweb/mumble/conversionsql/sqlite/12-data-mumble_mumble.sql @@ -0,0 +1,6 @@ +UPDATE "mumble_mumble" +SET "server_id"=( + SELECT "id" + FROM "mumble_mumbleserver" + WHERE "mumble_mumbleserver"."dbus" = "mumble_mumble"."dbus" + ); diff --git a/pyweb/mumble/management/__init__.py b/pyweb/mumble/management/__init__.py index b2c1b0f..21cc8a1 100644 --- a/pyweb/mumble/management/__init__.py +++ b/pyweb/mumble/management/__init__.py @@ -14,9 +14,39 @@ * GNU General Public License for more details. """ -from server_detect import find_existing_instances -from django.db.models import signals -from mumble import models +from django.db import connection, transaction +from django.db.models import signals + +from mumble import models + +from update_schema import update_schema +from server_detect import find_existing_instances + + +if not transaction.is_managed(): + managed_before = False + transaction.enter_transaction_management(True) + transaction.managed(True) +else: + managed_before = True + + +cursor = connection.cursor() +try: + cursor.execute( "SELECT server_id FROM mumble_mumble;" ) + +except cursor.db.connection.Error: + # server_id field does not exist -> DB needs to be updated. + transaction.rollback() + signals.post_syncdb.connect( update_schema, sender=models ); + +else: + transaction.rollback() + signals.post_syncdb.connect( find_existing_instances, sender=models ); + +finally: + if not managed_before: + transaction.managed(False) + transaction.leave_transaction_management() -signals.post_syncdb.connect( find_existing_instances, sender=models ); diff --git a/pyweb/mumble/management/update_schema.py b/pyweb/mumble/management/update_schema.py new file mode 100644 index 0000000..6505fac --- /dev/null +++ b/pyweb/mumble/management/update_schema.py @@ -0,0 +1,62 @@ +# -*- coding: utf-8 -*- + +""" + * Copyright © 2010, Michael "Svedrin" Ziegler + * + * Mumble-Django is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This package is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. +""" + +import os +from os.path import join + +from django.db import connection, transaction +from django.db.models import signals +from django.conf import settings + +from mumble import models +from mumble.management.server_detect import find_existing_instances + + +def update_schema( **kwargs ): + cursor = connection.cursor() + + scriptpath = join( + settings.MUMBLE_DJANGO_ROOT, "pyweb", "mumble", "conversionsql", { + 'postgresql_psycopg2': 'pgsql', + 'postgresql': 'pgsql', + 'mysql': 'mysql', + 'sqlite3': 'sqlite', + }[settings.DATABASE_ENGINE] + ) + + scripts = [ filename for filename in os.listdir( scriptpath ) if filename.endswith( ".sql" ) ] + scripts.sort() + + for filename in scripts: + scriptfile = open( os.path.join( scriptpath, filename ), "r" ) + try: + stmt = scriptfile.read() + cursor.execute( stmt ) + transaction.commit() + + except IOError, err: + print "Error reading file '%s':" % filename + print err + + except cursor.db.connection.Error, err: + print "Error executing file '%s':" % filename + print err + transaction.rollback() + + finally: + scriptfile.close() + + find_existing_instances( **kwargs )