File tree Expand file tree Collapse file tree 3 files changed +39
-0
lines changed
20250422134621_add_pos_to_db
20250422175330_add_db_trigger_for_pos Expand file tree Collapse file tree 3 files changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ -- AlterTable
2
+ ALTER TABLE " Resource" ADD COLUMN " pos" DECIMAL (1000 ,95 );
Original file line number Diff line number Diff line change
1
+ -- NOTE: this is a custo migration to add a db trigger on
2
+ -- insertion of any `null` values into the `Resource`.`pos` row
3
+ CREATE OR REPLACE FUNCTION set_default_pos ()
4
+ RETURNS TRIGGER AS $$
5
+ BEGIN
6
+ -- Check if the parent resource is of type 'Folder'
7
+ IF EXISTS (
8
+ SELECT 1
9
+ FROM " Resource"
10
+ WHERE " id" = NEW." parentId"
11
+ AND " type" = ' Folder'
12
+ ) THEN
13
+ -- If the pos is NULL, calculate the next available pos for this parentId
14
+ IF NEW." pos" IS NULL THEN
15
+ SELECT COALESCE(MAX (" pos" ), 0 ) + 1
16
+ INTO NEW." pos"
17
+ FROM " Resource"
18
+ WHERE " parentId" = NEW." parentId" ;
19
+ END IF;
20
+ END IF;
21
+
22
+ -- Return the modified row
23
+ RETURN NEW;
24
+ END;
25
+ $$ LANGUAGE plpgsql;
26
+
27
+ CREATE TRIGGER set_pos_default
28
+ BEFORE INSERT ON " Resource"
29
+ FOR EACH ROW
30
+ EXECUTE FUNCTION set_default_pos();
Original file line number Diff line number Diff line change @@ -68,6 +68,13 @@ model Resource {
68
68
state ResourceState ? @default (Draft )
69
69
type ResourceType
70
70
71
+ // NOTE: refer to the postgres docs here
72
+ // https://www.postgresql.org/docs/current/datatype-numeric.html#DATATYPE-NUMERIC-TABLE
73
+ // this creates a numeric type that has a scale of 1000 (can store up to 1000 digits)
74
+ // of which 96 are after the decimal point.
75
+ // This means that our users can create up to 10e5 pages (probably way more than required)
76
+ pos Decimal ? @db.Decimal (1000 , 95 )
77
+
71
78
createdAt DateTime @default (now () )
72
79
updatedAt DateTime @default (now () ) @updatedAt
73
80
ResourcePermission ResourcePermission []
You can’t perform that action at this time.
0 commit comments