Skip to content

Commit 189e0df

Browse files
committed
Added autoPolicy behaviour from IHP backend
1 parent 36718c8 commit 189e0df

File tree

3 files changed

+94
-1
lines changed

3 files changed

+94
-1
lines changed

IHP/IDE/SchemaDesigner/Controller/Columns.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ instance Controller ColumnsController where
4949
, referenceTable = paramOrNothing "referenceTable"
5050
, primaryKey = param "primaryKey"
5151
, withIndex = paramOrDefault False "withIndex"
52+
, autoPolicy = paramOrDefault False "autoPolicy"
5253
}
5354
updateSchema $ SchemaOperations.addColumn options
5455

IHP/IDE/SchemaDesigner/SchemaOperations.hs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ data AddColumnOptions = AddColumnOptions
4343
, referenceTable :: !(Maybe Text)
4444
, primaryKey :: !Bool
4545
, withIndex :: !Bool
46+
, autoPolicy :: !Bool
4647
}
4748
addColumn :: AddColumnOptions -> Schema -> Schema
4849
addColumn options@(AddColumnOptions { .. }) =
@@ -62,12 +63,34 @@ addColumn options@(AddColumnOptions { .. }) =
6263

6364
foreignKeyConstraint = newForeignKeyConstraint tableName columnName (fromJust referenceTable)
6465
index = newColumnIndex tableName columnName
66+
67+
handleAutoPolicy statements =
68+
if autoPolicy
69+
then
70+
let
71+
isTable (StatementCreateTable CreateTable { name }) = name == tableName
72+
isTable otherwise = False
73+
(Just table) = find isTable statements
74+
suggestedPolicy = suggestPolicy statements table
75+
in if (get #name suggestedPolicy /= "" && not (doesHaveExistingPolicies statements tableName))
76+
then
77+
statements
78+
|> enableRowLevelSecurity tableName
79+
|> addPolicy AddPolicyOptions
80+
{ tableName = tableName
81+
, name = get #name suggestedPolicy
82+
, using = get #using suggestedPolicy
83+
, check = get #check suggestedPolicy
84+
}
85+
else statements
86+
else statements
6587
in
6688
if isReference then
6789
\statements -> statements
6890
|> addTableOp
6991
|> appendStatement index
7092
|> appendStatement foreignKeyConstraint
93+
|> handleAutoPolicy
7194
else
7295
addTableOp
7396
. (if withIndex
@@ -465,3 +488,10 @@ isIndexStatementReferencingTableColumn statement tableName columnName = isRefere
465488
LessThanOrEqualToExpression a b -> expressionReferencesColumn a || expressionReferencesColumn b
466489
GreaterThanExpression a b -> expressionReferencesColumn a || expressionReferencesColumn b
467490
GreaterThanOrEqualToExpression a b -> expressionReferencesColumn a || expressionReferencesColumn b
491+
492+
doesHaveExistingPolicies :: [Statement] -> Text -> Bool
493+
doesHaveExistingPolicies statements tableName = statements
494+
|> find \case
495+
CreatePolicy { tableName = tableName' } -> tableName' == tableName
496+
otherwise -> False
497+
|> isJust

Test/IDE/SchemaDesigner/SchemaOperationsSpec.hs

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ tests = do
210210
, referenceTable = Nothing
211211
, primaryKey = False
212212
, withIndex = True
213+
, autoPolicy = False
213214
}
214215

215216
(SchemaOperations.addColumn options inputSchema) `shouldBe` expectedSchema
@@ -266,6 +267,68 @@ tests = do
266267
, referenceTable = Nothing
267268
, primaryKey = False
268269
, withIndex = False
270+
, autoPolicy = False
271+
}
272+
273+
(SchemaOperations.addColumn options inputSchema) `shouldBe` expectedSchema
274+
275+
it "should add a policy if autoPolicy = true" do
276+
let inputSchema = [tableA]
277+
278+
let tableAWithCreatedAt = StatementCreateTable CreateTable
279+
{ name = "a"
280+
, columns = [
281+
Column
282+
{ name = "user_id"
283+
, columnType = PUUID
284+
, defaultValue = Nothing
285+
, notNull = True
286+
, isUnique = False
287+
, generator = Nothing
288+
}
289+
]
290+
, primaryKeyConstraint = PrimaryKeyConstraint []
291+
, constraints = []
292+
}
293+
294+
let index = CreateIndex
295+
{ indexName = "a_user_id_index"
296+
, unique = False
297+
, tableName = "a"
298+
, columns = [IndexColumn { column = VarExpression "user_id", columnOrder = [] }]
299+
, whereClause = Nothing
300+
, indexType = Nothing
301+
}
302+
let constraint = AddConstraint
303+
{ tableName = "a"
304+
, constraint = ForeignKeyConstraint { name = Just "a_ref_user_id", columnName = "user_id", referenceTable = "users", referenceColumn = Just "id", onDelete = Just NoAction }
305+
, deferrable = Nothing
306+
, deferrableType = Nothing
307+
}
308+
let enableRLS = EnableRowLevelSecurity { tableName = "a" }
309+
let policy = CreatePolicy
310+
{ name = "Users can manage their a"
311+
, tableName = "a"
312+
, action = Nothing
313+
, using = Just (EqExpression (VarExpression "user_id") (CallExpression "ihp_user_id" []))
314+
, check = Just (EqExpression (VarExpression "user_id") (CallExpression "ihp_user_id" []))
315+
}
316+
317+
let expectedSchema = [tableAWithCreatedAt, index, constraint, enableRLS, policy]
318+
319+
let options = SchemaOperations.AddColumnOptions
320+
{ tableName = "a"
321+
, columnName = "user_id"
322+
, columnType = PUUID
323+
, defaultValue = Nothing
324+
, isArray = False
325+
, allowNull = False
326+
, isUnique = False
327+
, isReference = True
328+
, referenceTable = Just "users"
329+
, primaryKey = False
330+
, withIndex = False
331+
, autoPolicy = True
269332
}
270333

271334
(SchemaOperations.addColumn options inputSchema) `shouldBe` expectedSchema
@@ -301,7 +364,6 @@ tests = do
301364
(SchemaOperations.deleteColumn options inputSchema) `shouldBe` expectedSchema
302365

303366
it "should delete a updated_at trigger" do
304-
305367
let tableAWithCreatedAt = StatementCreateTable CreateTable
306368
{ name = "a"
307369
, columns = [

0 commit comments

Comments
 (0)