]> Untitled Git - lemmy.git/blob - migrations/00000000000000_diesel_initial_setup/up.sql
add enable_federated_downvotes site option
[lemmy.git] / migrations / 00000000000000_diesel_initial_setup / up.sql
1 -- This file was automatically created by Diesel to setup helper functions
2 -- and other internal bookkeeping. This file is safe to edit, any future
3 -- changes will be added to existing projects as new migrations.
4 -- Sets up a trigger for the given table to automatically set a column called
5 -- `updated_at` whenever the row is modified (unless `updated_at` was included
6 -- in the modified columns)
7 --
8 -- # Example
9 --
10 -- ```sql
11 -- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW());
12 --
13 -- SELECT diesel_manage_updated_at('users');
14 -- ```
15 CREATE OR REPLACE FUNCTION diesel_manage_updated_at (_tbl regclass)
16     RETURNS VOID
17     AS $$
18 BEGIN
19     EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s
20                     FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl);
21 END;
22 $$
23 LANGUAGE plpgsql;
24
25 CREATE OR REPLACE FUNCTION diesel_set_updated_at ()
26     RETURNS TRIGGER
27     AS $$
28 BEGIN
29     IF (NEW IS DISTINCT FROM OLD AND NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at) THEN
30         NEW.updated_at := CURRENT_TIMESTAMP;
31     END IF;
32     RETURN NEW;
33 END;
34 $$
35 LANGUAGE plpgsql;
36