Skip to content

Commit 36718c8

Browse files
committed
Added tests
1 parent 0836c6e commit 36718c8

File tree

2 files changed

+173
-0
lines changed

2 files changed

+173
-0
lines changed

IHP/IDE/SchemaDesigner/SchemaOperations.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ deleteColumn DeleteColumnOptions { .. } schema =
405405
|> (filter \case
406406
AddConstraint { tableName = fkTable, constraint = ForeignKeyConstraint { columnName = fkColumn } } | fkTable == tableName && fkColumn == columnName -> False
407407
index@(CreateIndex {}) | isIndexStatementReferencingTableColumn index tableName columnName -> False
408+
otherwise -> True
408409
)
409410
|> (if columnName == "updated_at"
410411
then deleteTriggerIfExists (updatedAtTriggerName tableName)

Test/IDE/SchemaDesigner/SchemaOperationsSpec.hs

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,178 @@ tests = do
175175
}
176176

177177
SchemaOperations.suggestPolicy schema tasksTable `shouldBe` expectedPolicy
178+
describe "addColumn" do
179+
it "should add an index if withIndex = true" do
180+
let inputSchema = [tableA]
181+
182+
let tableAWithCreatedAt = StatementCreateTable CreateTable
183+
{ name = "a"
184+
, columns = [
185+
Column
186+
{ name = "created_at"
187+
, columnType = PTimestampWithTimezone
188+
, defaultValue = Just (CallExpression "NOW" [])
189+
, notNull = True
190+
, isUnique = False
191+
, generator = Nothing
192+
}
193+
]
194+
, primaryKeyConstraint = PrimaryKeyConstraint []
195+
, constraints = []
196+
}
197+
let index = CreateIndex { indexName = "a_created_at_index", unique = False, tableName = "a", columns = [IndexColumn { column = VarExpression "created_at", columnOrder = [] }], whereClause = Nothing, indexType = Nothing }
198+
199+
let expectedSchema = [tableAWithCreatedAt, index]
200+
201+
let options = SchemaOperations.AddColumnOptions
202+
{ tableName = "a"
203+
, columnName = "created_at"
204+
, columnType = PTimestampWithTimezone
205+
, defaultValue = Just (CallExpression "NOW" [])
206+
, isArray = False
207+
, allowNull = False
208+
, isUnique = False
209+
, isReference = False
210+
, referenceTable = Nothing
211+
, primaryKey = False
212+
, withIndex = True
213+
}
214+
215+
(SchemaOperations.addColumn options inputSchema) `shouldBe` expectedSchema
216+
217+
it "should add a trigger to updated_at columns" do
218+
let inputSchema = [tableA]
219+
220+
let tableAWithCreatedAt = StatementCreateTable CreateTable
221+
{ name = "a"
222+
, columns = [
223+
Column
224+
{ name = "updated_at"
225+
, columnType = PTimestampWithTimezone
226+
, defaultValue = Just (CallExpression "NOW" [])
227+
, notNull = True
228+
, isUnique = False
229+
, generator = Nothing
230+
}
231+
]
232+
, primaryKeyConstraint = PrimaryKeyConstraint []
233+
, constraints = []
234+
}
235+
236+
let function = CreateFunction
237+
{ functionName = "set_updated_at_to_now"
238+
, functionArguments = []
239+
, functionBody = "\nBEGIN\n NEW.updated_at = NOW();\n RETURN NEW;\nEND;\n"
240+
, orReplace = False
241+
, returns = PTrigger
242+
, language = "plpgsql"
243+
}
244+
let trigger = CreateTrigger
245+
{ name = "update_a_updated_at"
246+
, eventWhen = Before
247+
, event = TriggerOnUpdate
248+
, tableName = "a"
249+
, for = ForEachRow
250+
, whenCondition = Nothing
251+
, functionName = "set_updated_at_to_now"
252+
, arguments = []
253+
}
254+
255+
let expectedSchema = [function, tableAWithCreatedAt, trigger]
256+
257+
let options = SchemaOperations.AddColumnOptions
258+
{ tableName = "a"
259+
, columnName = "updated_at"
260+
, columnType = PTimestampWithTimezone
261+
, defaultValue = Just (CallExpression "NOW" [])
262+
, isArray = False
263+
, allowNull = False
264+
, isUnique = False
265+
, isReference = False
266+
, referenceTable = Nothing
267+
, primaryKey = False
268+
, withIndex = False
269+
}
270+
271+
(SchemaOperations.addColumn options inputSchema) `shouldBe` expectedSchema
272+
273+
describe "deleteColumn" do
274+
it "should delete an referenced index" do
275+
let tableAWithCreatedAt = StatementCreateTable CreateTable
276+
{ name = "a"
277+
, columns = [
278+
Column
279+
{ name = "created_at"
280+
, columnType = PTimestampWithTimezone
281+
, defaultValue = Just (CallExpression "NOW" [])
282+
, notNull = True
283+
, isUnique = False
284+
, generator = Nothing
285+
}
286+
]
287+
, primaryKeyConstraint = PrimaryKeyConstraint []
288+
, constraints = []
289+
}
290+
let index = CreateIndex { indexName = "a_created_at_index", unique = False, tableName = "a", columns = [IndexColumn { column = VarExpression "created_at", columnOrder = [] }], whereClause = Nothing, indexType = Nothing }
291+
292+
let inputSchema = [tableAWithCreatedAt, index]
293+
let expectedSchema = [tableA]
294+
295+
let options = SchemaOperations.DeleteColumnOptions
296+
{ tableName = "a"
297+
, columnName = "created_at"
298+
, columnId = 0
299+
}
300+
301+
(SchemaOperations.deleteColumn options inputSchema) `shouldBe` expectedSchema
302+
303+
it "should delete a updated_at trigger" do
304+
305+
let tableAWithCreatedAt = StatementCreateTable CreateTable
306+
{ name = "a"
307+
, columns = [
308+
Column
309+
{ name = "updated_at"
310+
, columnType = PTimestampWithTimezone
311+
, defaultValue = Just (CallExpression "NOW" [])
312+
, notNull = True
313+
, isUnique = False
314+
, generator = Nothing
315+
}
316+
]
317+
, primaryKeyConstraint = PrimaryKeyConstraint []
318+
, constraints = []
319+
}
320+
321+
let function = CreateFunction
322+
{ functionName = "set_updated_at_to_now"
323+
, functionArguments = []
324+
, functionBody = "\nBEGIN\n NEW.updated_at = NOW();\n RETURN NEW;\nEND;\n"
325+
, orReplace = False
326+
, returns = PTrigger
327+
, language = "plpgsql"
328+
}
329+
let trigger = CreateTrigger
330+
{ name = "update_a_updated_at"
331+
, eventWhen = Before
332+
, event = TriggerOnUpdate
333+
, tableName = "a"
334+
, for = ForEachRow
335+
, whenCondition = Nothing
336+
, functionName = "set_updated_at_to_now"
337+
, arguments = []
338+
}
339+
340+
let inputSchema = [function, tableAWithCreatedAt, trigger]
341+
let expectedSchema = [function, tableA]
342+
343+
let options = SchemaOperations.DeleteColumnOptions
344+
{ tableName = "a"
345+
, columnName = "updated_at"
346+
, columnId = 0
347+
}
348+
349+
(SchemaOperations.deleteColumn options inputSchema) `shouldBe` expectedSchema
178350

179351
parseSqlStatements :: Text -> [Statement]
180352
parseSqlStatements sql =

0 commit comments

Comments
 (0)