upgrade_20180514_agent_income_report.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # -*- coding: utf-8 -*-
  2. #!/usr/bin/env python
  3. """
  4. 更改agentIncomeReport 字段名,为了与其他model统一使用query 对象
  5. """
  6. import os
  7. import sys
  8. from bson.objectid import ObjectId
  9. #: current_dir - 2
  10. PROJECT_ROOT = os.path.join(os.path.abspath(os.path.split(os.path.realpath(__file__))[0] + "/.."), '..')
  11. sys.path.insert(0, PROJECT_ROOT)
  12. from script.base import init_env, get_logger
  13. logger = get_logger(__name__)
  14. init_env(interactive=True)
  15. from apps.web.agent.models import AgentIncomeReport
  16. agentIncomeReportCollection = AgentIncomeReport.get_collection()
  17. reports = agentIncomeReportCollection.find({'createdTime': {'$ne': ''}})
  18. if not reports: print 'no reports to operate on'
  19. for report in reports:
  20. print 'updating report=%s' % (str(report['_id']),)
  21. updated0 = agentIncomeReportCollection.update({'_id': ObjectId(report['_id'])},
  22. {'$set': {'dateTimeAdded': report['createdTime']}})
  23. assert updated0, 'updated0 failed, report=%s' % (str(report['_id']),)
  24. updated1 = agentIncomeReportCollection.update({'_id': ObjectId(report['_id'])},
  25. {'$unset': {'createdTime': ''}})
  26. assert updated1, 'updated1 failed, report=%s' % (str(report['_id']),)
  27. print 'finished!'