]> Untitled Git - lemmy.git/blob - migrations/00000000000000_diesel_initial_setup/up.sql
Isomorphic docker (#1124)
[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
5
6
7
8 -- Sets up a trigger for the given table to automatically set a column called
9 -- `updated_at` whenever the row is modified (unless `updated_at` was included
10 -- in the modified columns)
11 --
12 -- # Example
13 --
14 -- ```sql
15 -- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW());
16 --
17 -- SELECT diesel_manage_updated_at('users');
18 -- ```
19 CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$
20 BEGIN
21     EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s
22                     FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl);
23 END;
24 $$ LANGUAGE plpgsql;
25
26 CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$
27 BEGIN
28     IF (
29         NEW IS DISTINCT FROM OLD AND
30         NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at
31     ) THEN
32         NEW.updated_at := current_timestamp;
33     END IF;
34     RETURN NEW;
35 END;
36 $$ LANGUAGE plpgsql;