@@ -0,0 +1,168 @@
1+{-# LANGUAGE
2+ DataKinds ,
3+ DeriveGeneric ,
4+ OverloadedLabels,
5+ OverloadedStrings ,
6+ TypeApplications ,
7+ TypeOperators #-}
8+
9+module Data.Schema (
10+ Schema,
11+ createTables
12+) where
13+
14+import Squeal.PostgreSQL
15+import Squeal.PostgreSQL.Expression
16+import Squeal.PostgreSQL.Schema
17+import Data.User
18+import Data.Post
19+
20+type Schema =
21+ '[
22+ "users" ::: 'Table ( '[
23+ "pk_users" ::: 'PrimaryKey '["userId"]] :=>
24+ '[
25+ "userId" ::: 'Def :=> 'NotNull 'PGint8,
26+ "firstName" ::: 'NoDef :=> 'NotNull 'PGtext,
27+ "secondName" ::: 'NoDef :=> 'NotNull 'PGtext
28+ ]),
29+ "posts" ::: 'Table ( '[
30+ "pk_posts" ::: 'PrimaryKey '["postRowId"],
31+ "fk_author_id" ::: 'ForeignKey '["postRowAuthorId"] "users" '["userId"]] :=>
32+ '[
33+ "postRowId" ::: 'Def :=> 'NotNull 'PGint8,
34+ "postRowAuthorId" ::: 'NoDef :=> 'NotNull 'PGint8
35+ ]),
36+ "quotes" ::: 'Table (
37+ '[
38+ "pk_post_quotes" ::: 'PrimaryKey '["quoteRowId"],
39+ "fk_quote_post_id" ::: 'ForeignKey '["quoteRowPostId"] "posts" '["postRowId"] ] :=>
40+ '[
41+ "quoteRowId" ::: 'Def :=> 'NotNull 'PGint8,
42+ "quoteRowPostId" ::: 'NoDef :=> 'NotNull 'PGint8
43+ ]),
44+ "drafts" ::: 'Table ( '[
45+ "pk_drafts" ::: 'PrimaryKey '["postRowId"],
46+ "fk_draft_author_id" ::: 'ForeignKey '["postRowAuthorId"] "users" '["userId"]] :=>
47+ '[
48+ "postRowId" ::: 'Def :=> 'NotNull 'PGint8,
49+ "postRowAuthorId" ::: 'NoDef :=> 'NotNull 'PGint8
50+ ]),
51+ "postElements" ::: 'Table (
52+ '[
53+ "fk_post_quote_self_id" ::: 'ForeignKey '["rowElementId"] "posts" '["postRowId"],
54+ "fk_post_quote_id" ::: 'ForeignKey '["rowElementQuote"] "quotes" '["quoteRowId"]
55+ -- ,
56+ -- "unique_post_quote" ::: 'Check '["rowElementOrd", "rowElementMarkdown", "rowElementLatex", "rowElementImage", "rowElementQuote", "rowElementAttachment"]
57+ ] :=>
58+ '[
59+ "rowElementId" ::: 'NoDef :=> 'NotNull 'PGint8,
60+ "rowElementOrd" ::: 'NoDef :=> 'NotNull 'PGint8,
61+ "rowElementMarkdown" ::: 'NoDef :=> 'Null 'PGtext,
62+ "rowElementLatex" ::: 'NoDef :=> 'Null 'PGtext,
63+ "rowElementImage" ::: 'NoDef :=> 'Null 'PGtext,
64+ "rowElementQuote" ::: 'NoDef :=> 'Null 'PGint8,
65+ "rowElementAttachment" ::: 'NoDef :=> 'Null 'PGtext
66+ ]),
67+ "quoteElements" ::: 'Table (
68+ '[
69+ "fk_quotes_quote_self_id" ::: 'ForeignKey '["rowElementId"] "quotes" '["quoteRowId"],
70+ "fk_quote_quote_id" ::: 'ForeignKey '["rowElementQuote"] "quotes" '["quoteRowId"]
71+ -- ,
72+ -- "unique_quote_quote" ::: 'Check '["rowElementOrd", "rowElementMarkdown", "rowElementLatex", "rowElementImage", "rowElementQuote", "rowElementAttachment"]
73+ ] :=>
74+ '[
75+ "rowElementId" ::: 'NoDef :=> 'NotNull 'PGint8,
76+ "rowElementOrd" ::: 'NoDef :=> 'NotNull 'PGint8,
77+ "rowElementMarkdown" ::: 'NoDef :=> 'Null 'PGtext,
78+ "rowElementLatex" ::: 'NoDef :=> 'Null 'PGtext,
79+ "rowElementImage" ::: 'NoDef :=> 'Null 'PGtext,
80+ "rowElementQuote" ::: 'NoDef :=> 'Null 'PGint8,
81+ "rowElementAttachment" ::: 'NoDef :=> 'Null 'PGtext
82+ ]),
83+ "draftElements" ::: 'Table (
84+ '[
85+ "fk_draft_quote_self_id" ::: 'ForeignKey '["rowElementId"] "drafts" '["postRowId"],
86+ "fk_draft_quote_id" ::: 'ForeignKey '["rowElementQuote"] "quotes" '["quoteRowId"]
87+ -- ,
88+ -- "unique_post_quote" ::: 'Check '["rowElementOrd", "rowElementMarkdown", "rowElementLatex", "rowElementImage", "rowElementQuote", "rowElementAttachment"]
89+ ] :=>
90+ '[
91+ "rowElementId" ::: 'NoDef :=> 'NotNull 'PGint8,
92+ "rowElementOrd" ::: 'NoDef :=> 'NotNull 'PGint8,
93+ "rowElementMarkdown" ::: 'NoDef :=> 'Null 'PGtext,
94+ "rowElementLatex" ::: 'NoDef :=> 'Null 'PGtext,
95+ "rowElementImage" ::: 'NoDef :=> 'Null 'PGtext,
96+ "rowElementQuote" ::: 'NoDef :=> 'Null 'PGint8,
97+ "rowElementAttachment" ::: 'NoDef :=> 'Null 'PGtext
98+ ])
99+
100+ ]
101+
102+
103+createTables :: Definition '[] Schema
104+createTables = createTable #users (
105+ serial8 `as` #userId :*
106+ notNullable text `as` #firstName :*
107+ notNullable text `as` #secondName
108+ ) (
109+ primaryKey #userId `as` #pk_users
110+ ) >>> createTable #posts (
111+ serial8 `as` #postRowId :*
112+ notNullable int8 `as` #postRowAuthorId
113+ ) (
114+ primaryKey #postRowId `as` #pk_posts :*
115+ foreignKey #postRowAuthorId #users #userId OnDeleteCascade OnUpdateCascade `as` #fk_author_id
116+ ) >>> createTable #quotes (
117+ serial8 `as` #quoteRowId :*
118+ notNullable int8 `as` #quoteRowPostId
119+ ) (
120+ primaryKey #quoteRowId `as` #pk_post_quotes :*
121+ foreignKey #quoteRowPostId #posts #postRowId OnDeleteCascade OnUpdateCascade `as` #fk_quote_post_id
122+ ) >>> createTable #drafts (
123+ serial8 `as` #postRowId :*
124+ notNullable int8 `as` #postRowAuthorId
125+ ) (
126+ primaryKey #postRowId `as` #pk_drafts :*
127+ foreignKey #postRowAuthorId #users #userId OnDeleteCascade OnUpdateCascade `as` #fk_draft_author_id
128+ ) >>> createTable #postElements (
129+ notNullable int8 `as` #rowElementId :*
130+ notNullable int8 `as` #rowElementOrd :*
131+ nullable text `as` #rowElementMarkdown :*
132+ nullable text `as` #rowElementLatex :*
133+ nullable text `as` #rowElementImage :*
134+ nullable int8 `as` #rowElementQuote :*
135+ nullable text `as` #rowElementAttachment
136+ ) (
137+ foreignKey #rowElementId #posts #postRowId OnDeleteCascade OnUpdateCascade `as` #fk_post_quote_self_id :*
138+ foreignKey #rowElementQuote #quotes #quoteRowId OnDeleteCascade OnUpdateCascade `as` #fk_post_quote_id
139+ ) >>> createTable #quoteElements (
140+ notNullable int8 `as` #rowElementId :*
141+ notNullable int8 `as` #rowElementOrd :*
142+ nullable text `as` #rowElementMarkdown :*
143+ nullable text `as` #rowElementLatex :*
144+ nullable text `as` #rowElementImage :*
145+ nullable int8 `as` #rowElementQuote :*
146+ nullable text `as` #rowElementAttachment
147+ ) (
148+ foreignKey #rowElementId #quotes #quoteRowId OnDeleteCascade OnUpdateCascade `as` #fk_quotes_quote_self_id :*
149+ foreignKey #rowElementQuote #quotes #quoteRowId OnDeleteCascade OnUpdateCascade `as` #fk_quote_quote_id
150+ ) >>> createTable #draftElements (
151+ notNullable int8 `as` #rowElementId :*
152+ notNullable int8 `as` #rowElementOrd :*
153+ nullable text `as` #rowElementMarkdown :*
154+ nullable text `as` #rowElementLatex :*
155+ nullable text `as` #rowElementImage :*
156+ nullable int8 `as` #rowElementQuote :*
157+ nullable text `as` #rowElementAttachment
158+ ) (
159+ foreignKey #rowElementId #drafts #postRowId OnDeleteCascade OnUpdateCascade `as` #fk_draft_quote_self_id :*
160+ foreignKey #rowElementQuote #quotes #quoteRowId OnDeleteCascade OnUpdateCascade `as` #fk_draft_quote_id
161+ )
162+-- quoteUniquenessCheck :: TableConstraintExpression Schema _ (Check '["rowElementOrd", "rowElementMarkdown", "rowElementLatex", "rowElementImage", "rowElementQuote", "rowElementAttachment"])
163+-- quoteUniquenessCheck = check (#rowElementOrd :* #rowElementMarkdown :* #rowElementLatex :* #rowElementImage :* #rowElementQuote :* #rowElementAttachment) $
164+-- ((ifThenElse (isNull #rowElementOrd) 1 0) +
165+-- (ifThenElse (isNull #rowElementOrd) 1 0) +
166+-- (ifThenElse (isNull #rowElementOrd) 1 0) +
167+-- (ifThenElse (isNull #rowElementOrd) 1 0) +
168+-- (ifThenElse (isNull #rowElementOrd) 1 0)) .== 0