text: # -*- coding: utf-8 -*-
  # Generated by Django 1.10 on 2017-04-08 22:26
  from django.db import migrations
  
  import base
  
  
  def forwards_func(apps, schema_editor):
      """
      Convert Tweet's and TwitterAccount's fields storing ID values to fit UnsignedBigIntegerField.
  
      This applies to Tweet's tweet_id and TwitterAccount's account_id and last_known_tweet_id.
      Do so by substracting 2^63 to have full 64-bit value range.
      """
      Tweet = apps.get_model('twitter', 'Tweet')
      TwitterAccount = apps.get_model('twitter', 'TwitterAccount')
  
      db_alias = schema_editor.connection.alias
  
      for tweet in Tweet.objects.using(db_alias).all():
          tweet.tweet_id = int(tweet.tweet_id) - 2 ** 63
          tweet.save()
  
      for account in TwitterAccount.objects.using(db_alias).all():
          account.account_id = int(account.account_id) - 2 ** 63
          if account.last_known_tweet_id == '':
              account.last_known_tweet_id = 0 - 2 ** 63
          else:
              account.last_known_tweet_id = int(account.last_known_tweet_id) - 2 ** 63
          account.save()
  
  
  def reverse_func(apps, schema_editor):
      """
      Convert Tweet's and TwitterAccount's fields storing ID values to signed 64-bit value range again.
  
      This applies to Tweet's tweet_id and TwitterAccount's account_id and last_known_tweet_id.
      Do so by adding 2^63.
      """
      Tweet = apps.get_model('twitter', 'Tweet')
      TwitterAccount = apps.get_model('twitter', 'TwitterAccount')
  
      db_alias = schema_editor.connection.alias
  
      for tweet in Tweet.objects.using(db_alias).all():
          tweet.tweet_id = int(tweet.tweet_id) + 2 ** 63
          tweet.save()
  
      for account in TwitterAccount.objects.using(db_alias).all():
          account.account_id = int(account.account_id) + 2 ** 63
          account.last_known_tweet_id = int(account.last_known_tweet_id) + 2 ** 63
          account.save()
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('twitter', '0005_auto_20170226_1348'),
      ]
  
      operations = [
          migrations.RunPython(forwards_func, reverse_func),
          migrations.AlterField(
              model_name='tweet',
              name='tweet_id',
              field=base.fields.UnsignedBigIntegerField(unique=True),
          ),
          migrations.AlterField(
              model_name='twitteraccount',
              name='account_id',
              field=base.fields.UnsignedBigIntegerField(unique=True),
          ),
          migrations.AlterField(
              model_name='twitteraccount',
              name='last_known_tweet_id',
              field=base.fields.UnsignedBigIntegerField(default=0),
          ),
      ]
  