-
Notifications
You must be signed in to change notification settings - Fork 309
If mtime is supposed to track modified time, then use triggers (with example code) #1498
Comments
Thanks @mw44118! I've added this to the current Infrastructure milestone. For tables that have a
Every time a user sets or resets a tip, we INSERT INTO tips
(ctime, tipper, tippee, amount)
VALUES ( COALESCE (( SELECT ctime
FROM tips
WHERE (tipper=%s AND tippee=%s)
LIMIT 1
), CURRENT_TIMESTAMP)
, %s, %s, %s
)
RETURNING ( SELECT count(*) = 0 FROM tips WHERE tipper=%s )
AS first_time_tipper We're using this pattern in a couple places in the app. Now that postgres.py is out the door (#1320) I want to write docs on our schema (#1272) and clean it up in the process. I want to abstract what we can of this pattern and others. |
So will mtime always match ctime? On Tue, Sep 24, 2013 at 10:15 AM, Chad Whitacre [email protected]:
W. Matthew Wilson |
No, it looks like this:
That's ordered by |
I'm baffled. What does the "c" in ctime stand for? |
I must say I am lost as well. Actually I am finding some of the queries really difficult to understand. For example I have no idea why don't we do just plain insert into the tips table and/or why we have two timestamps there. |
If I had to guess, |
Without an |
It is. I guess we need to get more eyes on the code. For that i'd like to think about simplifying things so that it is more obvious from the first sight what the author wanted to say/do. The data model is definitely first and most important target. That might help even more than documentation in bringing more people to it IMO. |
We have two timestamps there so that we can know when a user first created a tip, and when the user modified the tip.
Here, for example, we see that foo started tipping bar way back in June, 2012, and just yesterday dropped their gift to bar down to zero. We could of course compute the date of first tip (
I made the decision when designing the schema that it would be useful and interesting to carry this |
Correct! But since we're only selecting |
Always a good thing. :-)
How about documentation about the data model (#1272)? :-) |
I've modified the example at #1498 (comment). It should be correct now. |
@zwn :-) |
There is one thing missing for me to let this issue lay closed. What is the difference between trigger and rule? I have found a description in postgres documentation.
@mw44118 From the #1502 we know that we are using rules to create the mtimes. I believe that's conceptually the same thing (while the difference being implementation detail). |
@zwn We could do what we're doing with rules using triggers, yes. I implemented them with rules because it fit this example in the RULE docs and seemed to work well for what we want to do here. I'm open to the possibility that a trigger would work better for this. For example, perhaps we could factor out the ctime coalescing into a PL/pgSQL function and use that in multiple triggers. But perhaps we could factor that out and use it multiple rules as well? |
I see tables with a ctime timestamp and an mtime timestamp column. I'm guessing ctime is meant to track "created time" for that row, and mtime is meant to track the time that row was modified.
I prefer to use "inserted" and "updated" for these concepts, but that's not really important.
But right now, mtime is kind of useless unless app code is setting it on every update.
Anyhow, here's an example of how to use a trigger on a table to set a column like mtime (except I'm calling it "updated" but you can use your imagination):
Now, if you add another table, you don't need to redefine the function set_updated_column, but you MUST add the trigger to run set_updated_column on every update.
Here's an example:
I hope that helps!
The text was updated successfully, but these errors were encountered: