From 0e7f2722e63163089451c5f12c7a08711143f485 Mon Sep 17 00:00:00 2001 From: zhengxie Date: Fri, 20 Apr 2018 17:02:31 +0800 Subject: [PATCH] Add command to migrate file comment --- seahub/base/apps.py | 11 +++ .../commands/migrate_file_comment.py | 97 +++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 seahub/base/management/commands/migrate_file_comment.py diff --git a/seahub/base/apps.py b/seahub/base/apps.py index e07f74f375..42e100aa49 100644 --- a/seahub/base/apps.py +++ b/seahub/base/apps.py @@ -8,6 +8,8 @@ class BaseConfig(AppConfig): def ready(self): super(BaseConfig, self).ready() + + # check memcache is available cache.set('test_cache', 'worked') if cache.get('test_cache') != 'worked': print ''' @@ -15,3 +17,12 @@ Warning: Cache is not working, please check memcached is running if you are usin memcached backend, otherwise, please check permission of cache directory on your file system. ''' + + # check table `base_filecomment` is ok + from seahub.base.models import FileComment + try: + _ = list(FileComment.objects.all()[:1].values('uuid_id')) + except: + print ''' +Warning: File comment has changed since version 6.3, while table `base_filecomment` is not migrated yet, please consider migrate it according to v6.3.0 release note, otherwise the file comment feature will not work correctly. + ''' diff --git a/seahub/base/management/commands/migrate_file_comment.py b/seahub/base/management/commands/migrate_file_comment.py new file mode 100644 index 0000000000..49f91554e9 --- /dev/null +++ b/seahub/base/management/commands/migrate_file_comment.py @@ -0,0 +1,97 @@ +# Copyright (c) 2012-2016 Seafile Ltd. +import uuid + +from django.conf import settings +from django.core.management.base import BaseCommand +from django.db import connection +from django.db.utils import OperationalError + +from seahub.base.models import FileComment +from seahub.tags.models import FileUUIDMap + +def random_key(): + return uuid.uuid4().hex[:6] + +class Command(BaseCommand): + help = "Migrate base_filecomment schema which is changed in version 6.3." + + def migrate_schema(self): + mysql = False + sqlite = False + engine = settings.DATABASES['default']['ENGINE'] + if 'mysql' in engine: + mysql = True + elif 'sqlite' in engine: + sqlite = True + else: + print 'Unsupported database. Exit.' + return + + print 'Start to update schema...' + + comments = list(FileComment.objects.raw('SELECT * from base_filecomment')) + + with connection.cursor() as cursor: + sql = 'ALTER TABLE base_filecomment RENAME TO base_filecomment_backup_%s' % (random_key()) + cursor.execute(sql) + print sql + + print '' + + if mysql: + sql = '''CREATE TABLE `base_filecomment` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `author` varchar(255) NOT NULL, + `comment` longtext NOT NULL, + `created_at` datetime NOT NULL, + `updated_at` datetime NOT NULL, + `uuid_id` char(32) NOT NULL, + PRIMARY KEY (`id`), + KEY `base_filecomment_uuid_id_%s_fk_tags_fileuuidmap_uuid` (`uuid_id`), + KEY `base_filecomment_author_%s` (`author`), + CONSTRAINT `base_filecomment_uuid_id_%s_fk_tags_fileuuidmap_uuid` FOREIGN KEY (`uuid_id`) REFERENCES `tags_fileuuidmap` (`uuid`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 + ''' % (random_key(), random_key(), random_key()) + + cursor.execute(sql) + print sql + + if sqlite: + sql = '''CREATE TABLE "base_filecomment" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "author" varchar(255) NOT NULL, "comment" text NOT NULL, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL, "uuid_id" char(32) NOT NULL REFERENCES "tags_fileuuidmap" ("uuid")) + ''' + cursor.execute(sql) + print sql + + sql = '''CREATE INDEX "base_filecomment_%s" ON "base_filecomment" ("author")''' % random_key() + cursor.execute(sql) + print sql + + sql = '''CREATE INDEX "base_filecomment_%s" ON "base_filecomment" ("uuid_id") ''' % random_key() + cursor.execute(sql) + print sql + + print 'Start to migate comments data...' + for c in comments: + repo_id = c.repo_id + parent_path = c.parent_path + filename = c.item_name + author = c.author + comment = c.comment + created_at = c.created_at + updated_at = c.updated_at + + uuid = FileUUIDMap.objects.get_or_create_fileuuidmap(repo_id, parent_path, filename, False) + FileComment(uuid=uuid, author=author, comment=comment, + created_at=created_at, updated_at=updated_at).save() + print 'migrated comment ID: %d' % c.pk + + print 'Done' + + def handle(self, *args, **options): + # check table column `uuid` + try: + res = FileComment.objects.raw('SELECT uuid_id from base_filecomment limit 1') + if 'uuid_id' in res.columns: + print 'base_filecomment is already migrated, exit.' + except OperationalError: + self.migrate_schema()