migratefrom01.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. from django.conf import settings
  2. from django.core.management import CommandError
  3. from django.core.management.base import BaseCommand
  4. from django.db import connections
  5. from misago.models import *
  6. class Command(BaseCommand):
  7. """
  8. Small utility that migrates data from Misago 0.1 instalation to 0.2
  9. """
  10. help = 'Updates Popular Threads ranking'
  11. def handle(self, *args, **options):
  12. self.cursor = connections['deprecated'].cursor()
  13. self.stdout.write('\nBeginning migration from Misago 0.1...\n')
  14. self.mig_settings()
  15. self.mig_monitor()
  16. self.users = {}
  17. self.forums = {}
  18. self.threads = {}
  19. self.mig_users()
  20. self.mig_users_roles()
  21. self.mig_users_relations()
  22. Karma.objects.all().delete()
  23. Change.objects.all().delete()
  24. Checkpoint.objects.all().delete()
  25. Post.objects.all().delete()
  26. Thread.objects.all().delete()
  27. self.mig_forums()
  28. self.mig_threads()
  29. self.mig_posts()
  30. self.mig_subs()
  31. self.sync_threads()
  32. self.sync_forums()
  33. self.stdout.write('\nData was migrated.\n')
  34. def mig_settings(self):
  35. self.stdout.write('Migrating Database Settings...')
  36. self.cursor.execute("SELECT setting, value FROM settings_setting");
  37. for row in self.dictfetchall():
  38. Setting.objects.filter(setting=row['setting']).update(value=row['value'])
  39. def mig_monitor(self):
  40. self.stdout.write('Migrating Forum Monitor...')
  41. self.cursor.execute("SELECT id, value, updated FROM monitor_item");
  42. for row in self.dictfetchall():
  43. MonitorItem.objects.filter(id=row['id']).update(value=row['value'], updated=row['updated'])
  44. def mig_users(self):
  45. User.objects.all().delete()
  46. self.stdout.write('Migrating Users...')
  47. self.cursor.execute("SELECT * FROM users_user");
  48. for row in self.dictfetchall():
  49. self.users[row['id']] = User.objects.create(
  50. username=row['username'],
  51. username_slug=row['username_slug'],
  52. email=row['email'],
  53. email_hash=row['email_hash'],
  54. password=row['password'],
  55. password_date=row['password_date'],
  56. avatar_type=row['avatar_type'],
  57. avatar_image=row['avatar_image'],
  58. avatar_original=row['avatar_original'],
  59. avatar_temp=row['avatar_temp'],
  60. signature=row['signature'],
  61. signature_preparsed=row['signature_preparsed'],
  62. join_date=row['join_date'],
  63. join_ip=row['join_ip'],
  64. join_agent=row['join_agent'],
  65. last_date=row['last_date'],
  66. last_ip=row['last_ip'],
  67. last_agent=row['last_agent'],
  68. hide_activity=row['hide_activity'],
  69. subscribe_start=row['subscribe_start'],
  70. subscribe_reply=row['subscribe_reply'],
  71. receive_newsletters=row['receive_newsletters'],
  72. threads=row['threads'],
  73. posts=row['posts'],
  74. votes=row['votes'],
  75. karma_given_p=row['karma_given_p'],
  76. karma_given_n=row['karma_given_n'],
  77. karma_p=row['karma_p'],
  78. karma_n=row['karma_n'],
  79. following=row['following'],
  80. followers=row['followers'],
  81. score=row['score'],
  82. ranking=row['ranking'],
  83. last_sync=row['last_sync'],
  84. title=row['title'],
  85. last_post=row['last_post'],
  86. last_search=row['last_search'],
  87. alerts=row['alerts'],
  88. alerts_date=row['alerts_date'],
  89. activation=row['activation'],
  90. token=row['token'],
  91. avatar_ban=row['avatar_ban'],
  92. avatar_ban_reason_user=row['avatar_ban_reason_user'],
  93. avatar_ban_reason_admin=row['avatar_ban_reason_admin'],
  94. signature_ban=row['signature_ban'],
  95. signature_ban_reason_user=row['signature_ban_reason_user'],
  96. signature_ban_reason_admin=row['signature_ban_reason_admin'],
  97. timezone=row['timezone'],
  98. is_team=row['is_team'],
  99. acl_key=row['acl_key'],
  100. )
  101. def mig_users_roles(self):
  102. self.stdout.write('Migrating Users Roles...')
  103. self.cursor.execute("SELECT * FROM users_user_roles");
  104. for row in self.dictfetchall():
  105. self.users[row['user_id']].roles.add(Role.objects.get(id=row['role_id']))
  106. def mig_users_relations(self):
  107. self.stdout.write('Migrating Users Relations...')
  108. self.cursor.execute("SELECT * FROM users_user_follows");
  109. for row in self.dictfetchall():
  110. self.users[row['from_user_id']].follows.add(self.users[row['to_user_id']])
  111. self.cursor.execute("SELECT * FROM users_user_ignores");
  112. for row in self.dictfetchall():
  113. self.users[row['from_user_id']].ignores.add(self.users[row['to_user_id']])
  114. def mig_forums(self):
  115. self.stdout.write('Migrating Forums...')
  116. self.forums[4] = Forum.objects.get(special='root')
  117. for forum in self.forums[4].get_descendants():
  118. forum.delete()
  119. self.forums[4] = Forum.objects.get(special='root')
  120. self.cursor.execute("SELECT * FROM forums_forum WHERE level > 0 ORDER BY lft");
  121. for row in self.dictfetchall():
  122. self.forums[row['id']] = Forum(
  123. type=row['type'],
  124. special=row['token'],
  125. name=row['name'],
  126. slug=row['slug'],
  127. description=row['description'],
  128. description_preparsed=row['description_preparsed'],
  129. redirect=row['redirect'],
  130. redirects=row['redirects'],
  131. attrs=row['attrs'],
  132. show_details=row['show_details'],
  133. style=row['style'],
  134. closed=row['closed'],
  135. )
  136. self.forums[row['id']].insert_at(self.forums[row['parent_id']], position='last-child', save=True)
  137. Forum.objects.populate_tree(True)
  138. def mig_threads(self):
  139. self.stdout.write('Migrating Threads...')
  140. self.cursor.execute("SELECT * FROM threads_thread");
  141. for row in self.dictfetchall():
  142. self.threads[row['id']] = Thread.objects.create(
  143. forum=self.forums[row['forum_id']],
  144. weight=row['weight'],
  145. name=row['name'],
  146. slug=row['slug'],
  147. merges=row['merges'],
  148. score=row['score'],
  149. upvotes=row['upvotes'],
  150. downvotes=row['downvotes'],
  151. start=row['start'],
  152. start_poster_name='a',
  153. start_poster_slug='a',
  154. last=row['last'],
  155. deleted=row['deleted'],
  156. closed=row['closed'],
  157. )
  158. def mig_posts(self):
  159. self.stdout.write('Migrating Posts...')
  160. self.cursor.execute("SELECT * FROM threads_post");
  161. for row in self.dictfetchall():
  162. post = Post.objects.create(
  163. forum=self.forums[row['forum_id']],
  164. thread=self.threads[row['thread_id']],
  165. merge=row['merge'],
  166. user=(self.users[row['user_id']] if row['user_id'] else None),
  167. user_name=row['user_name'],
  168. ip=row['ip'],
  169. agent=row['agent'],
  170. post=row['post'],
  171. post_preparsed=row['post_preparsed'],
  172. upvotes=row['upvotes'],
  173. downvotes=row['downvotes'],
  174. checkpoints=row['checkpoints'],
  175. date=row['date'],
  176. edits=row['edits'],
  177. edit_date=row['edit_date'],
  178. edit_reason=row['edit_reason'],
  179. edit_user=(self.users[row['edit_user_id']] if row['edit_user_id'] else None),
  180. edit_user_name=row['edit_user_name'],
  181. edit_user_slug=row['edit_user_slug'],
  182. reported=row['reported'],
  183. moderated=row['moderated'],
  184. deleted=row['deleted'],
  185. protected=row['protected'],
  186. )
  187. # Migrate post checkpoints
  188. self.cursor.execute("SELECT * FROM threads_checkpoint WHERE post_id = %s" % row['id']);
  189. for related in self.dictfetchall():
  190. Checkpoint.objects.create(
  191. forum=self.forums[row['forum_id']],
  192. thread=self.threads[row['thread_id']],
  193. post=post,
  194. action=related['action'],
  195. user=(self.users[related['user_id']] if related['user_id'] else None),
  196. user_name=related['user_name'],
  197. user_slug=related['user_slug'],
  198. date=related['date'],
  199. ip=related['ip'],
  200. agent=related['agent'],
  201. )
  202. # Migrate post edits
  203. self.cursor.execute("SELECT * FROM threads_change WHERE post_id = %s" % row['id']);
  204. for related in self.dictfetchall():
  205. Change.objects.create(
  206. forum=self.forums[row['forum_id']],
  207. thread=self.threads[row['thread_id']],
  208. post=post,
  209. user=(self.users[related['user_id']] if related['user_id'] else None),
  210. user_name=related['user_name'],
  211. user_slug=related['user_slug'],
  212. date=related['date'],
  213. ip=related['ip'],
  214. agent=related['agent'],
  215. reason=related['reason'],
  216. thread_name_new=related['thread_name_new'],
  217. thread_name_old=related['thread_name_old'],
  218. post_content=related['post_content'],
  219. size=related['size'],
  220. change=related['change'],
  221. )
  222. # Migrate post karmas
  223. self.cursor.execute("SELECT * FROM threads_karma WHERE post_id = %s" % row['id']);
  224. for related in self.dictfetchall():
  225. Karma.objects.create(
  226. forum=self.forums[row['forum_id']],
  227. thread=self.threads[row['thread_id']],
  228. post=post,
  229. user=(self.users[related['user_id']] if related['user_id'] else None),
  230. user_name=related['user_name'],
  231. user_slug=related['user_slug'],
  232. date=related['date'],
  233. ip=related['ip'],
  234. agent=related['agent'],
  235. score=related['score'],
  236. )
  237. # Migrate mentions
  238. self.cursor.execute("SELECT * FROM threads_post_mentions WHERE post_id = %s" % row['id']);
  239. for related in self.dictfetchall():
  240. post.mentions.add(self.users[related['user_id']])
  241. def mig_subs(self):
  242. self.stdout.write('Migrating Subscribtions...')
  243. self.cursor.execute("SELECT * FROM watcher_threadwatch");
  244. for row in self.dictfetchall():
  245. WatchedThread.objects.create(
  246. user=self.users[row['user_id']],
  247. forum=self.forums[row['forum_id']],
  248. thread=self.threads[row['thread_id']],
  249. last_read=row['last_read'],
  250. email=row['email'],
  251. )
  252. def sync_threads(self):
  253. self.stdout.write('Synchronising Threads...')
  254. for thread in Thread.objects.all():
  255. thread.sync()
  256. thread.save(force_update=True)
  257. def sync_forums(self):
  258. self.stdout.write('Synchronising Forums...')
  259. for forum in Forum.objects.all():
  260. forum.sync()
  261. forum.save(force_update=True)
  262. def dictfetchall(self):
  263. desc = self.cursor.description
  264. return [dict(zip([col[0] for col in desc], row)) for row in self.cursor.fetchall()]