1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-24 21:07:17 +00:00

Add command to migrate file comment

This commit is contained in:
zhengxie
2018-04-20 17:02:31 +08:00
parent 624c0593b0
commit 0e7f2722e6
2 changed files with 108 additions and 0 deletions

View File

@@ -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.
'''

View File

@@ -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()