From a4291d64cea2c9190ef190d85b4a858e111554e2 Mon Sep 17 00:00:00 2001 From: Ciaran Gultnieks Date: Mon, 26 Jan 2009 20:36:02 +0000 Subject: [PATCH 1/2] PostgreSQL - remaining changes to fix db create script up to laconica 0.6.4 level --- db/laconica_pg.sql | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/db/laconica_pg.sql b/db/laconica_pg.sql index eb0dfc0ee0..c3efd9daa7 100644 --- a/db/laconica_pg.sql +++ b/db/laconica_pg.sql @@ -49,7 +49,7 @@ create table "user" ( emailnotifysub integer default 1 /* comment 'Notify by email of subscriptions' */, emailnotifyfav integer default 1 /* comment 'Notify by email of favorites' */, emailnotifynudge integer default 1 /* comment 'Notify by email of nudges' */, - emailnotifymsg integer default 1 / * comment 'Notify by email of direct messages' */, + emailnotifymsg integer default 1 /* comment 'Notify by email of direct messages' */, emailmicroid integer default 1 /* comment 'whether to publish email microid' */, language varchar(50) /* comment 'preferred language' */, timezone varchar(50) /* comment 'timezone' */, @@ -88,8 +88,8 @@ create table remote_profile ( create table subscription ( subscriber integer not null /* comment 'profile listening' */, subscribed integer not null /* comment 'profile being listened to' */, - jabber integer default 1 /* comment 'deliver jabber messages', - sms integer default 1 comment 'deliver sms messages', + jabber integer default 1 /* comment 'deliver jabber messages' */, + sms integer default 1 /* comment 'deliver sms messages' */, token varchar(255) /* comment 'authorization token' */, secret varchar(255) /* comment 'token secret' */, created timestamp not null /* comment 'date this record was created' */, @@ -271,7 +271,7 @@ create table foreign_service ( ); create table foreign_user ( - id int not null /* comment 'unique numeric key on foreign service' */, + id int not null unique /* comment 'unique numeric key on foreign service' */, service int not null /* comment 'foreign key to service' */ references foreign_service(id) , uri varchar(255) not null unique /* comment 'identifying URI' */, nickname varchar(255) /* comment 'nickname on foreign service' */, @@ -280,15 +280,14 @@ create table foreign_user ( primary key (id, service) ); -create index foreign_user_user_id_idx on foreign_user using btree(user_id); create table foreign_link ( user_id int /* comment 'link to user on this system, if exists' */ references "user" (id), foreign_id int /* comment 'link' */ references foreign_user (id), service int not null /* comment 'foreign key to service' */ references foreign_service (id), - credentials varchar(255) /* comment 'authc credentials, typically a password', + credentials varchar(255) /* comment 'authc credentials, typically a password' */, noticesync int not null default 1 /* comment 'notice synchronisation, bit 1 = sync outgoing, bit 2 = sync incoming, bit 3 = filter local replies' */, - friendsync int not null default 2 /* comment 'friend synchronisation, bit 1 = sync outgoing, bit 2 = sync incoming + friendsync int not null default 2 /* comment 'friend synchronisation, bit 1 = sync outgoing, bit 2 = sync incoming */, created timestamp not null /* comment 'date this record was created' */, modified timestamp not null /* comment 'date this record was modified' */, @@ -300,7 +299,7 @@ create table foreign_subscription ( service int not null /* comment 'service where relationship happens' */ references foreign_service(id) , subscriber int not null /* comment 'subscriber on foreign service' */ , subscribed int not null /* comment 'subscribed user' */ , - created timestamp not null /* comment 'date this record was created' /, + created timestamp not null /* comment 'date this record was created' */, primary key (service, subscriber, subscribed) ); @@ -338,14 +337,14 @@ create index message_created_idx on message using btree(created); create table notice_inbox ( - user_id integer not null /* comment 'user receiving the message' */ references user (id), + user_id integer not null /* comment 'user receiving the message' */ references "user" (id), notice_id integer not null /* comment 'notice received' */ references notice (id), - created datetime not null /* comment 'date the notice was created' */, + created timestamp not null /* comment 'date the notice was created' */, source integer default 1 /* comment 'reason it is in the inbox; 1=subscription' */, primary key (user_id, notice_id) ); -create index notice_inbox_notice_id_idx (notice_id) on notice_inbox using btree(notice_id); +create index notice_inbox_notice_id_idx on notice_inbox using btree(notice_id); create table profile_tag ( tagger integer not null /* comment 'user making the tag' */ references "user" (id), @@ -360,7 +359,7 @@ create index profile_tag_tagger_tag_idx on profile_tag using btree(tagger,tag); create table profile_block ( - blocker integer not null i/* comment 'user making the block' */ references user (id), + blocker integer not null /* comment 'user making the block' */ references "user" (id), blocked integer not null /* comment 'profile that is blocked' */ references profile (id), modified timestamp /* comment 'date of blocking' */, From 53274a6d9347c256430187a22347ca4a0910a395 Mon Sep 17 00:00:00 2001 From: Ciaran Gultnieks Date: Mon, 26 Jan 2009 21:10:32 +0000 Subject: [PATCH 2/2] PostgreSQL - code changes to avoid problems where user table is referenced in ad-hoc queries --- classes/Notice.php | 22 +++++++++++++--------- classes/User.php | 9 +++++---- lib/jabber.php | 27 ++++++++++++++------------- lib/mail.php | 9 +++++---- 4 files changed, 37 insertions(+), 30 deletions(-) diff --git a/classes/Notice.php b/classes/Notice.php index 2cdf80f1c0..3299883686 100644 --- a/classes/Notice.php +++ b/classes/Notice.php @@ -273,8 +273,10 @@ class Notice extends Memcached_DataObject if ($cache) { $user = new User(); + $UT = common_config('db','type')=='pgsql'?'"user"':'user'; $user->query('SELECT id ' . - 'FROM user JOIN subscription ON user.id = subscription.subscriber ' . + + "FROM $UT JOIN subscription ON $UT.id = subscription.subscriber " . 'WHERE subscription.subscribed = ' . $this->profile_id); while ($user->fetch()) { @@ -568,16 +570,17 @@ class Notice extends Memcached_DataObject if ($enabled === true || $enabled === 'transitional') { $inbox = new Notice_inbox(); + $UT = common_config('db','type')=='pgsql'?'"user"':'user'; $qry = 'INSERT INTO notice_inbox (user_id, notice_id, created) ' . - 'SELECT user.id, ' . $this->id . ', "' . $this->created . '" ' . - 'FROM user JOIN subscription ON user.id = subscription.subscriber ' . + "SELECT $UT.id, " . $this->id . ', "' . $this->created . '" ' . + "FROM $UT JOIN subscription ON $UT.id = subscription.subscriber " . 'WHERE subscription.subscribed = ' . $this->profile_id . ' ' . 'AND NOT EXISTS (SELECT user_id, notice_id ' . 'FROM notice_inbox ' . - 'WHERE user_id = user.id ' . + "WHERE user_id = $UT.id " . 'AND notice_id = ' . $this->id . ' )'; if ($enabled === 'transitional') { - $qry .= ' AND user.inboxed = 1'; + $qry .= " AND $UT.inboxed = 1"; } $inbox->query($qry); } @@ -628,16 +631,17 @@ class Notice extends Memcached_DataObject // FIXME: do this in an offline daemon $inbox = new Notice_inbox(); + $UT = common_config('db','type')=='pgsql'?'"user"':'user'; $qry = 'INSERT INTO notice_inbox (user_id, notice_id, created, source) ' . - 'SELECT user.id, ' . $this->id . ', "' . $this->created . '", 2 ' . - 'FROM user JOIN group_member ON user.id = group_member.profile_id ' . + "SELECT $UT.id, " . $this->id . ', "' . $this->created . '", 2 ' . + "FROM $UT JOIN group_member ON $UT.id = group_member.profile_id " . 'WHERE group_member.group_id = ' . $group->id . ' ' . 'AND NOT EXISTS (SELECT user_id, notice_id ' . 'FROM notice_inbox ' . - 'WHERE user_id = user.id ' . + "WHERE user_id = $UT.id " . 'AND notice_id = ' . $this->id . ' )'; if ($enabled === 'transitional') { - $qry .= ' AND user.inboxed = 1'; + $qry .= " AND $UT.inboxed = 1"; } $result = $inbox->query($qry); } diff --git a/classes/User.php b/classes/User.php index b1bae88351..b1c061c18f 100644 --- a/classes/User.php +++ b/classes/User.php @@ -338,11 +338,12 @@ class User extends Memcached_DataObject { # 3-way join; probably should get cached - $qry = 'SELECT user.* ' . - 'FROM subscription sub1 JOIN user ON sub1.subscribed = user.id ' . - 'JOIN subscription sub2 ON user.id = sub2.subscriber ' . + $UT = common_config('db','type')=='pgsql'?'"user"':'user'; + $qry = "SELECT $UT.* " . + "FROM subscription sub1 JOIN $UT ON sub1.subscribed = $UT.id " . + "JOIN subscription sub2 ON $UT.id = sub2.subscriber " . 'WHERE sub1.subscriber = %d and sub2.subscribed = %d ' . - 'ORDER BY user.nickname'; + "ORDER BY $UT.nickname"; $user = new User(); $user->query(sprintf($qry, $this->id, $this->id)); diff --git a/lib/jabber.php b/lib/jabber.php index 6196bc9e1d..f1be577681 100644 --- a/lib/jabber.php +++ b/lib/jabber.php @@ -354,12 +354,13 @@ function jabber_broadcast_notice($notice) // First, get users to whom this is a direct reply $user = new User(); - $user->query('SELECT user.id, user.jabber ' . - 'FROM user JOIN reply ON user.id = reply.profile_id ' . + $UT = common_config('db','type')=='pgsql'?'"user"':'user'; + $user->query("SELECT $UT.id, $UT.jabber " . + "FROM $UT JOIN reply ON $UT.id = reply.profile_id " . 'WHERE reply.notice_id = ' . $notice->id . ' ' . - 'AND user.jabber is not null ' . - 'AND user.jabbernotify = 1 ' . - 'AND user.jabberreplies = 1 '); + "AND $UT.jabber is not null " . + "AND $UT.jabbernotify = 1 " . + "AND $UT.jabberreplies = 1 "); while ($user->fetch()) { common_log(LOG_INFO, @@ -375,12 +376,12 @@ function jabber_broadcast_notice($notice) // Now, get users subscribed to this profile $user = new User(); - $user->query('SELECT user.id, user.jabber ' . - 'FROM user JOIN subscription ' . - 'ON user.id = subscription.subscriber ' . + $user->query("SELECT $UT.id, $UT.jabber " . + "FROM $UT JOIN subscription " . + "ON $UT.id = subscription.subscriber " . 'WHERE subscription.subscribed = ' . $notice->profile_id . ' ' . - 'AND user.jabber is not null ' . - 'AND user.jabbernotify = 1 ' . + "AND $UT.jabber is not null " . + "AND $UT.jabbernotify = 1 " . 'AND subscription.jabber = 1 '); while ($user->fetch()) { @@ -399,9 +400,9 @@ function jabber_broadcast_notice($notice) // Now, get users who have it in their inbox because of groups $user = new User(); - $user->query('SELECT user.id, user.jabber ' . - 'FROM user JOIN notice_inbox ' . - 'ON user.id = notice_inbox.user_id ' . + $user->query("SELECT $UT.id, $UT.jabber " . + "FROM $UT JOIN notice_inbox " . + "ON $UT.id = notice_inbox.user_id " . 'WHERE notice_inbox.notice_id = ' . $notice->id . ' ' . 'AND notice_inbox.source = 2 ' . 'AND user.jabber is not null ' . diff --git a/lib/mail.php b/lib/mail.php index 1c6a10a8a5..b424d579fe 100644 --- a/lib/mail.php +++ b/lib/mail.php @@ -331,12 +331,13 @@ function mail_broadcast_notice_sms($notice) $user = new User(); + $UT = common_config('db','type')=='pgsql'?'"user"':'user'; $user->query('SELECT nickname, smsemail, incomingemail ' . - 'FROM user JOIN subscription ' . - 'ON user.id = subscription.subscriber ' . + "FROM $UT JOIN subscription " . + "ON $UT.id = subscription.subscriber " . 'WHERE subscription.subscribed = ' . $notice->profile_id . ' ' . - 'AND user.smsemail IS NOT null ' . - 'AND user.smsnotify = 1 ' . + "AND $UT.smsemail IS NOT null " . + "AND $UT.smsnotify = 1 " . 'AND subscription.sms = 1 '); while ($user->fetch()) {