QvitterNotification.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * Table Definition for qvitternotification
  4. */
  5. class QvitterNotification extends Managed_DataObject
  6. {
  7. public $__table = 'qvitternotification'; // table name
  8. public $id; // int(4) primary_key not_null
  9. public $to_profile_id; // int(4)
  10. public $from_profile_id; // int(4)
  11. public $ntype; // varchar(7)
  12. public $notice_id; // int(4)
  13. public $is_seen; // int(tiny)
  14. public $created; // datetime multiple_key not_null
  15. public static function schemaDef()
  16. {
  17. return array(
  18. 'fields' => array(
  19. 'id' => array('type' => 'serial', 'not null' => true),
  20. 'to_profile_id' => array('type' => 'int', 'not null'=>true, 'description' => 'the profile being notified'),
  21. 'from_profile_id' => array('type' => 'int', 'not null'=>true, 'description' => 'the profile that is notifying'),
  22. 'ntype' => array('type' => 'varchar', 'length' => 7, 'description' => 'reply, like, mention or follow'),
  23. 'notice_id' => array('type' => 'int', 'description' => 'id for the reply or mention or notice being faved'),
  24. 'is_seen' => array('type' => 'int', 'size' => 'tiny', 'default' => 0, 'description' => 'if the notification has been seen'),
  25. 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created')
  26. ),
  27. 'primary key' => array('id'),
  28. 'foreign keys' => array(
  29. 'qvitternotification_to_profile_id_fkey' => array('profile', array('to_profile_id' => 'id')),
  30. 'qvitternotification_from_profile_id_fkey' => array('profile', array('from_profile_id' => 'id')),
  31. // removing this because there can be rows without related notice_id
  32. //'qvitternotification_notice_id_fkey' => array('notice', array('notice_id' => 'id')),
  33. ),
  34. 'indexes' => array(
  35. 'qvitternotification_created_idx' => array('created'),
  36. 'qvitternotification_to_profile_id_idx' => array('to_profile_id'),
  37. 'qvitternotification_from_profile_id_idx' => array('from_profile_id'),
  38. ),
  39. );
  40. }
  41. /**
  42. * Wrapper for record insertion to update related caches
  43. */
  44. function insert()
  45. {
  46. $result = parent::insert();
  47. if ($result) {
  48. self::blow('qvitternotification:stream:%d', $this->to_profile_id);
  49. }
  50. return $result;
  51. }
  52. static public function beforeSchemaUpdate()
  53. {
  54. $table = strtolower(get_called_class());
  55. $schema = Schema::get();
  56. try {
  57. $schemadef = $schema->getTableDef($table);
  58. } catch (SchemaTableMissingException $e) {
  59. printfnq("\nTable '$table' not created yet, so nothing to do with it before Schema Update... DONE.");
  60. return;
  61. }
  62. printfnq("\nEnsuring no NULL values for foreign keys in QvitterNotification...");
  63. // Because constraints to profile and notice table assume not null, we must
  64. // remove any values in these columns that are NULL (or 0), because they
  65. // are invalid anyway.
  66. $qn = new QvitterNotification();
  67. foreach (['to_profile_id', 'from_profile_id'] as $field) {
  68. $qn->whereAdd(sprintf('%s is NULL', $field), 'OR');
  69. $qn->whereAdd(sprintf('%s = 0', $field), 'OR');
  70. }
  71. if ($qn->find()) {
  72. printfnq(" removing {$qn->N} rows...");
  73. while ($qn->fetch()) {
  74. $qn->delete();
  75. }
  76. }
  77. printfnq("DONE.\n");
  78. printfnq("Ensuring no dead profile or notice IDs are stored in QvitterNotification...");
  79. // We could probably build a single statement for this but I just want it done...
  80. // or maybe be smart and take it directly from our defined 'foreign keys'... but oh well!
  81. $constraints = ['to_profile_id' => 'profile:id',
  82. 'from_profile_id' => 'profile:id'];
  83. foreach ($constraints as $field=>$foreign) {
  84. $qn = new QvitterNotification();
  85. $qn->selectAdd();
  86. $qn->selectAdd('qvitternotification.id'); // to avoid confusion with profile.id, also we only need the primary key
  87. $qn->joinAdd([$field, $foreign], 'LEFT');
  88. $qn->whereAdd(str_replace(':', '.', $foreign).' IS NULL');
  89. if ($qn->find()) {
  90. printfnq("\n{$field}: {$qn->N} rows...");
  91. while ($qn->fetch()) {
  92. $qn->delete();
  93. }
  94. }
  95. }
  96. printfnq("DONE.\n");
  97. }
  98. }