]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - db/laconica_pg.sql
take an argument for fixup_utf8
[quix0rs-gnu-social.git] / db / laconica_pg.sql
1 /* local and remote users have profiles */\r
2 \r
3 create sequence profile_seq;\r
4 create table profile (\r
5     id bigint default nextval('profile_seq') primary key /* comment 'unique identifier' */,\r
6     nickname varchar(64) not null /* comment 'nickname or username' */,\r
7     fullname varchar(255) /* comment 'display name' */,\r
8     profileurl varchar(255) /* comment 'URL, cached so we dont regenerate' */,\r
9     homepage varchar(255) /* comment 'identifying URL' */,\r
10     bio varchar(140) /* comment 'descriptive biography' */,\r
11     location varchar(255) /* comment 'physical location' */,\r
12     created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,\r
13     modified timestamp /* comment 'date this record was modified' */,\r
14 \r
15     textsearch tsvector\r
16 );\r
17 create index profile_nickname_idx on profile using btree(nickname);\r
18 \r
19 create table avatar (\r
20     profile_id integer not null /* comment 'foreign key to profile table' */ references profile (id) ,\r
21     original integer default 0 /* comment 'uploaded by user or generated?' */,\r
22     width integer not null /* comment 'image width' */,\r
23     height integer not null /* comment 'image height' */,\r
24     mediatype varchar(32) not null /* comment 'file type' */,\r
25     filename varchar(255) null /* comment 'local filename, if local' */,\r
26     url varchar(255) unique /* comment 'avatar location' */,\r
27     created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,\r
28     modified timestamp /* comment 'date this record was modified' */,\r
29 \r
30     primary key(profile_id, width, height)\r
31 );\r
32 create index avatar_profile_id_idx on avatar using btree(profile_id);\r
33 \r
34 create sequence sms_carrier_seq;\r
35 create table sms_carrier (\r
36     id bigint default nextval('sms_carrier_seq') primary key /* comment 'primary key for SMS carrier' */,\r
37     name varchar(64) unique /* comment 'name of the carrier' */,\r
38     email_pattern varchar(255) not null /* comment 'sprintf pattern for making an email address from a phone number' */,\r
39     created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,\r
40     modified timestamp /* comment 'date this record was modified ' */\r
41 );\r
42 \r
43 /* local users */\r
44 \r
45 create table "user" (\r
46     id integer primary key /* comment 'foreign key to profile table' */ references profile (id) ,\r
47     nickname varchar(64) unique /* comment 'nickname or username, duped in profile' */,\r
48     password varchar(255) /* comment 'salted password, can be null for OpenID users' */,\r
49     email varchar(255) unique /* comment 'email address for password recovery etc.' */,\r
50     incomingemail varchar(255) unique /* comment 'email address for post-by-email' */,\r
51     emailnotifysub integer default 1 /* comment 'Notify by email of subscriptions' */,\r
52     emailnotifyfav integer default 1 /* comment 'Notify by email of favorites' */,\r
53     emailnotifynudge integer default 1 /* comment 'Notify by email of nudges' */,\r
54     emailnotifymsg integer default 1 /* comment 'Notify by email of direct messages' */,\r
55     emailnotifyattn integer default 1 /* command 'Notify by email of @-replies' */, \r
56     emailmicroid integer default 1 /* comment 'whether to publish email microid' */,\r
57     language varchar(50) /* comment 'preferred language' */,\r
58     timezone varchar(50) /* comment 'timezone' */,\r
59     emailpost integer default 1 /* comment 'Post by email' */,\r
60     jabber varchar(255) unique /* comment 'jabber ID for notices' */,\r
61     jabbernotify integer default 0 /* comment 'whether to send notices to jabber' */,\r
62     jabberreplies integer default 0 /* comment 'whether to send notices to jabber on replies' */,\r
63     jabbermicroid integer default 1 /* comment 'whether to publish xmpp microid' */,\r
64     updatefrompresence integer default 0 /* comment 'whether to record updates from Jabber presence notices' */,\r
65     sms varchar(64) unique /* comment 'sms phone number' */,\r
66     carrier integer /* comment 'foreign key to sms_carrier' */ references sms_carrier (id) ,\r
67     smsnotify integer default 0 /* comment 'whether to send notices to SMS' */,\r
68     smsreplies integer default 0 /* comment 'whether to send notices to SMS on replies' */,\r
69     smsemail varchar(255) /* comment 'built from sms and carrier' */,\r
70     uri varchar(255) unique /* comment 'universally unique identifier, usually a tag URI' */,\r
71     autosubscribe integer default 0 /* comment 'automatically subscribe to users who subscribe to us' */,\r
72     urlshorteningservice varchar(50) default 'ur1.ca' /* comment 'service to use for auto-shortening URLs' */,\r
73     inboxed integer default 0 /* comment 'has an inbox been created for this user?' */, \r
74     created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,\r
75     modified timestamp /* comment 'date this record was modified' */\r
76 \r
77 );\r
78 create index user_smsemail_idx on "user" using btree(smsemail);\r
79 \r
80 /* remote people */\r
81 \r
82 create table remote_profile (\r
83     id integer primary key /* comment 'foreign key to profile table' */ references profile (id) ,\r
84     uri varchar(255) unique /* comment 'universally unique identifier, usually a tag URI' */,\r
85     postnoticeurl varchar(255) /* comment 'URL we use for posting notices' */,\r
86     updateprofileurl varchar(255) /* comment 'URL we use for updates to this profile' */,\r
87     created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,\r
88     modified timestamp /* comment 'date this record was modified' */\r
89 );\r
90 \r
91 create table subscription (\r
92     subscriber integer not null /* comment 'profile listening' */,\r
93     subscribed integer not null /* comment 'profile being listened to' */,\r
94     jabber integer default 1 /* comment 'deliver jabber messages' */,\r
95     sms integer default 1 /* comment 'deliver sms messages' */,\r
96     token varchar(255) /* comment 'authorization token' */,\r
97     secret varchar(255) /* comment 'token secret' */,\r
98     created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,\r
99     modified timestamp /* comment 'date this record was modified' */,\r
100 \r
101     primary key (subscriber, subscribed)\r
102 );\r
103 create index subscription_subscriber_idx on subscription using btree(subscriber);\r
104 create index subscription_subscribed_idx on subscription using btree(subscribed);\r
105 \r
106 create sequence notice_seq;\r
107 create table notice (\r
108 \r
109     id bigint default nextval('notice_seq') primary key /* comment 'unique identifier' */,\r
110     profile_id integer not null /* comment 'who made the update' */ references profile (id) ,\r
111     uri varchar(255) unique /* comment 'universally unique identifier, usually a tag URI' */,\r
112     content varchar(140) /* comment 'update content' */,\r
113     rendered text /* comment 'HTML version of the content' */,\r
114     url varchar(255) /* comment 'URL of any attachment (image, video, bookmark, whatever)' */,\r
115     created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,\r
116     modified timestamp /* comment 'date this record was modified' */,\r
117     reply_to integer /* comment 'notice replied to (usually a guess)' */ references notice (id) ,\r
118     is_local integer default 0 /* comment 'notice was generated by a user' */,\r
119     source varchar(32) /* comment 'source of comment, like "web", "im", or "clientname"' */\r
120 \r
121 /*    FULLTEXT(content) */\r
122 );\r
123 create index notice_profile_id_idx on notice using btree(profile_id);\r
124 create index notice_created_idx on notice using btree(created);\r
125 \r
126 create table notice_source (\r
127      code varchar(32) primary key not null /* comment 'source code' */,\r
128      name varchar(255) not null /* comment 'name of the source' */,\r
129      url varchar(255) not null /* comment 'url to link to' */,\r
130      created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,\r
131      modified timestamp /* comment 'date this record was modified' */\r
132 );\r
133 \r
134 create table reply (\r
135 \r
136     notice_id integer not null /* comment 'notice that is the reply' */ references notice (id) ,\r
137     profile_id integer not null /* comment 'profile replied to' */ references profile (id) ,\r
138     modified timestamp /* comment 'date this record was modified' */,\r
139     replied_id integer /* comment 'notice replied to (not used, see notice.reply_to)' */,\r
140 \r
141     primary key (notice_id, profile_id)\r
142 \r
143 );\r
144 create index reply_notice_id_idx on reply using btree(notice_id);\r
145 create index reply_profile_id_idx on reply using btree(profile_id);\r
146 create index reply_replied_id_idx on reply using btree(replied_id);\r
147 \r
148 create table fave (\r
149 \r
150     notice_id integer not null /* comment 'notice that is the favorite' */ references notice (id),\r
151     user_id integer not null /* comment 'user who likes this notice' */ references "user" (id) ,\r
152     modified timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was modified' */,\r
153     primary key (notice_id, user_id)\r
154 \r
155 );\r
156 create index fave_notice_id_idx on fave using btree(notice_id);\r
157 create index fave_user_id_idx on fave using btree(user_id);\r
158 create index fave_modified_idx on fave using btree(modified);\r
159 \r
160 /* tables for OAuth */\r
161 \r
162 create table consumer (\r
163     consumer_key varchar(255) primary key /* comment 'unique identifier, root URL' */,\r
164     seed char(32) not null /* comment 'seed for new tokens by this consumer' */,\r
165 \r
166     created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,\r
167     modified timestamp /* comment 'date this record was modified' */\r
168 );\r
169 \r
170 create table token (\r
171     consumer_key varchar(255) not null /* comment 'unique identifier, root URL' */ references consumer (consumer_key),\r
172     tok char(32) not null /* comment 'identifying value' */,\r
173     secret char(32) not null /* comment 'secret value' */,\r
174     type integer not null default 0 /* comment 'request or access' */,\r
175     state integer default 0 /* comment 'for requests; 0 = initial, 1 = authorized, 2 = used' */,\r
176 \r
177     created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,\r
178     modified timestamp /* comment 'date this record was modified' */,\r
179 \r
180     primary key (consumer_key, tok)\r
181 );\r
182 \r
183 create table nonce (\r
184     consumer_key varchar(255) not null /* comment 'unique identifier, root URL' */,\r
185     tok char(32) not null /* comment 'identifying value' */,\r
186     nonce char(32) null /* comment 'buggy old value, ignored */,\r
187     ts integer not null /* comment 'timestamp sent' values are epoch, and only used internally */,\r
188 \r
189     created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,\r
190     modified timestamp /* comment 'date this record was modified' */,\r
191 \r
192     primary key (consumer_key, ts, nonce)\r
193 );\r
194 \r
195 /* One-to-many relationship of user to openid_url */\r
196 \r
197 create table user_openid (\r
198     canonical varchar(255) primary key /* comment 'Canonical true URL' */,\r
199     display varchar(255) not null unique /* comment 'URL for viewing, may be different from canonical' */,\r
200     user_id integer not null /* comment 'user owning this URL' */ references "user" (id) ,\r
201     created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,\r
202     modified timestamp /* comment 'date this record was modified' */\r
203 \r
204 );\r
205 create index user_openid_user_id_idx on user_openid using btree(user_id);\r
206 \r
207 /* These are used by JanRain OpenID library */\r
208 \r
209 create table oid_associations (\r
210     server_url varchar(2047),\r
211     handle varchar(255),\r
212     secret bytea,\r
213     issued integer,\r
214     lifetime integer,\r
215     assoc_type varchar(64),\r
216     primary key (server_url, handle)\r
217 );\r
218 \r
219 create table oid_nonces (\r
220     server_url varchar(2047),\r
221     "timestamp" integer,\r
222     salt character(40),\r
223     unique (server_url, "timestamp", salt)\r
224 );\r
225 \r
226 create table confirm_address (\r
227     code varchar(32) not null primary key /* comment 'good random code' */,\r
228     user_id integer not null /* comment 'user who requested confirmation' */ references "user" (id),\r
229     address varchar(255) not null /* comment 'address (email, Jabber, SMS, etc.)' */,\r
230     address_extra varchar(255) not null default '' /* comment 'carrier ID, for SMS' */,\r
231     address_type varchar(8) not null /* comment 'address type ("email", "jabber", "sms")' */,\r
232     claimed timestamp /* comment 'date this was claimed for queueing' */,\r
233     sent timestamp /* comment 'date this was sent for queueing' */,\r
234     modified timestamp /* comment 'date this record was modified' */\r
235 );\r
236 \r
237 create table remember_me (\r
238     code varchar(32) not null primary key /* comment 'good random code' */,\r
239     user_id integer not null /* comment 'user who is logged in' */ references "user" (id),\r
240     modified timestamp /* comment 'date this record was modified' */\r
241 );\r
242 \r
243 create table queue_item (\r
244 \r
245     notice_id integer not null /* comment 'notice queued' */ references notice (id) ,\r
246     transport varchar(8) not null /* comment 'queue for what? "email", "jabber", "sms", "irc", ...' */,\r
247     created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,\r
248     claimed timestamp /* comment 'date this item was claimed' */,\r
249 \r
250     primary key (notice_id, transport)\r
251 \r
252 );\r
253 create index queue_item_created_idx on queue_item using btree(created);\r
254 \r
255 /* Hash tags */\r
256 create table notice_tag (\r
257     tag varchar( 64 ) not null /* comment 'hash tag associated with this notice' */,\r
258     notice_id integer not null /* comment 'notice tagged' */ references notice (id) ,\r
259     created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,\r
260 \r
261     primary key (tag, notice_id)\r
262 );\r
263 create index notice_tag_created_idx on notice_tag using btree(created);\r
264 \r
265 /* Synching with foreign services */\r
266 \r
267 create table foreign_service (\r
268      id int not null primary key /* comment 'numeric key for service' */,\r
269      name varchar(32) not null unique /* comment 'name of the service' */,\r
270      description varchar(255) /* comment 'description' */,\r
271      created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,\r
272      modified timestamp /* comment 'date this record was modified' */\r
273 );\r
274 \r
275 create table foreign_user (\r
276      id int not null unique /* comment 'unique numeric key on foreign service' */,\r
277      service int not null /* comment 'foreign key to service' */ references foreign_service(id) ,\r
278      uri varchar(255) not null unique /* comment 'identifying URI' */,\r
279      nickname varchar(255) /* comment 'nickname on foreign service' */,\r
280      created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,\r
281      modified timestamp /* comment 'date this record was modified' */,\r
282      \r
283      primary key (id, service)\r
284 );\r
285 \r
286 create table foreign_link (\r
287      user_id int /* comment 'link to user on this system, if exists' */ references "user" (id),\r
288      foreign_id int /* comment 'link' */ references foreign_user (id),\r
289      service int not null /* comment 'foreign key to service' */ references foreign_service (id),\r
290      credentials varchar(255) /* comment 'authc credentials, typically a password' */,\r
291      noticesync int not null default 1 /* comment 'notice synchronisation, bit 1 = sync outgoing, bit 2 = sync incoming, bit 3 = filter local replies' */,\r
292      friendsync int not null default 2 /* comment 'friend synchronisation, bit 1 = sync outgoing, bit 2 = sync incoming */, \r
293      profilesync int not null default 1 /* comment 'profile synchronization, bit 1 = sync outgoing, bit 2 = sync incoming' */,\r
294      last_noticesync timestamp default null /* comment 'last time notices were imported' */,\r
295      last_friendsync timestamp default null /* comment 'last time friends were imported' */,\r
296      created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,\r
297      modified timestamp /* comment 'date this record was modified' */,\r
298 \r
299      primary key (user_id,foreign_id,service)\r
300 );\r
301 create index foreign_user_user_id_idx on foreign_link using btree(user_id);\r
302 \r
303 create table foreign_subscription (\r
304      service int not null /* comment 'service where relationship happens' */ references foreign_service(id) ,\r
305      subscriber int not null /* comment 'subscriber on foreign service' */ ,\r
306      subscribed int not null /* comment 'subscribed user' */ ,\r
307      created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,\r
308      \r
309      primary key (service, subscriber, subscribed)\r
310 );\r
311 create index foreign_subscription_subscriber_idx on foreign_subscription using btree(subscriber);\r
312 create index foreign_subscription_subscribed_idx on foreign_subscription using btree(subscribed);\r
313 \r
314 create table invitation (\r
315      code varchar(32) not null primary key /* comment 'random code for an invitation' */,\r
316      user_id int not null /* comment 'who sent the invitation' */ references "user" (id),\r
317      address varchar(255) not null /* comment 'invitation sent to' */,\r
318      address_type varchar(8) not null /* comment 'address type ("email", "jabber", "sms") '*/,\r
319      created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */\r
320 \r
321 );\r
322 create index invitation_address_idx on invitation using btree(address,address_type);\r
323 create index invitation_user_id_idx on invitation using btree(user_id);\r
324 \r
325 create sequence message_seq;\r
326 create table message (\r
327 \r
328     id bigint default nextval('message_seq') primary key /* comment 'unique identifier' */,\r
329     uri varchar(255) unique /* comment 'universally unique identifier' */,\r
330     from_profile integer not null /* comment 'who the message is from' */ references profile (id),\r
331     to_profile integer not null /* comment 'who the message is to' */ references profile (id),\r
332     content varchar(140) /* comment 'message content' */,\r
333     rendered text /* comment 'HTML version of the content' */,\r
334     url varchar(255) /* comment 'URL of any attachment (image, video, bookmark, whatever)' */,\r
335     created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,\r
336     modified timestamp /* comment 'date this record was modified' */,\r
337     source varchar(32) /* comment 'source of comment, like "web", "im", or "clientname"' */\r
338     \r
339 );\r
340 create index message_from_idx on message using btree(from_profile);\r
341 create index message_to_idx on message using btree(to_profile);\r
342 create index message_created_idx on message using btree(created);\r
343 \r
344 create table notice_inbox (\r
345 \r
346     user_id integer not null /* comment 'user receiving the message' */ references "user" (id),\r
347     notice_id integer not null /* comment 'notice received' */ references notice (id),\r
348     created timestamp not null default CURRENT_TIMESTAMP /* comment 'date the notice was created' */,\r
349     source integer default 1 /* comment 'reason it is in the inbox; 1=subscription' */,\r
350 \r
351     primary key (user_id, notice_id)\r
352 );\r
353 create index notice_inbox_notice_id_idx on notice_inbox using btree(notice_id);\r
354 \r
355 create table profile_tag (\r
356    tagger integer not null /* comment 'user making the tag' */ references "user" (id),\r
357    tagged integer not null /* comment 'profile tagged' */ references profile (id),\r
358    tag varchar(64) not null /* comment 'hash tag associated with this notice' */,\r
359    modified timestamp /* comment 'date the tag was added' */,\r
360 \r
361    primary key (tagger, tagged, tag)\r
362 );\r
363 create index profile_tag_modified_idx on profile_tag using btree(modified);\r
364 create index profile_tag_tagger_tag_idx on profile_tag using btree(tagger,tag);\r
365 \r
366 create table profile_block (\r
367 \r
368    blocker integer not null /* comment 'user making the block' */ references "user" (id),\r
369    blocked integer not null /* comment 'profile that is blocked' */ references profile (id),\r
370    modified timestamp /* comment 'date of blocking' */,\r
371 \r
372    primary key (blocker, blocked)\r
373 \r
374 );\r
375 \r
376 create sequence user_group_seq;\r
377 create table user_group (\r
378 \r
379     id bigint default nextval('user_group_seq') primary key /* comment 'unique identifier' */,\r
380 \r
381     nickname varchar(64) unique /* comment 'nickname for addressing' */,\r
382     fullname varchar(255) /* comment 'display name' */,\r
383     homepage varchar(255) /* comment 'URL, cached so we dont regenerate' */,\r
384     description varchar(140) /* comment 'descriptive biography' */,\r
385     location varchar(255) /* comment 'related physical location, if any' */,\r
386 \r
387     original_logo varchar(255) /* comment 'original size logo' */,\r
388     homepage_logo varchar(255) /* comment 'homepage (profile) size logo' */,\r
389     stream_logo varchar(255) /* comment 'stream-sized logo' */,\r
390     mini_logo varchar(255) /* comment 'mini logo' */,\r
391 \r
392     created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,\r
393     modified timestamp /* comment 'date this record was modified' */\r
394 \r
395 );\r
396 create index user_group_nickname_idx on user_group using btree(nickname);\r
397 \r
398 create table group_member (\r
399 \r
400     group_id integer not null /* comment 'foreign key to user_group' */ references user_group (id),\r
401     profile_id integer not null /* comment 'foreign key to profile table' */ references profile (id),\r
402     is_admin integer default 0 /* comment 'is this user an admin?' */,\r
403 \r
404     created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,\r
405     modified timestamp /* comment 'date this record was modified' */,\r
406 \r
407     primary key (group_id, profile_id)\r
408 );\r
409 \r
410 create table related_group (\r
411 \r
412     group_id integer not null /* comment 'foreign key to user_group' */ references user_group (id) ,\r
413     related_group_id integer not null /* comment 'foreign key to user_group' */ references user_group (id),\r
414 \r
415     created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,\r
416 \r
417     primary key (group_id, related_group_id)\r
418 \r
419 );\r
420 \r
421 create table group_inbox (\r
422     group_id integer not null /* comment 'group receiving the message' references user_group (id) */,\r
423     notice_id integer not null /* comment 'notice received' references notice (id) */,\r
424     created timestamp not null default CURRENT_TIMESTAMP /* comment 'date the notice was created' */,\r
425 \r
426     primary key (group_id, notice_id)\r
427 );\r
428 create index group_inbox_created_idx on group_inbox using btree(created);\r
429 \r
430 /* Textsearch stuff */\r
431 \r
432 create index textsearch_idx on profile using gist(textsearch);\r
433 create index noticecontent_idx on notice using gist(to_tsvector('english',content));\r
434 create trigger textsearchupdate before insert or update on profile for each row\r
435 execute procedure tsvector_update_trigger(textsearch, 'pg_catalog.english', nickname, fullname, location, bio, homepage);\r
436 \r