Skip to content
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 27 additions & 16 deletions ihp-ide/IHP/IDE/CodeGen/MigrationGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,12 @@ diffSchemas targetSchema' actualSchema' = (drop <> create)
isCreateTableStatement (StatementCreateTable { unsafeGetCreateTable = table }) | table.name == tableName = True
isCreateTableStatement otherwise = False

(Just actualTable) = actualSchema |> find \case
actualTable = case actualSchema |> find \case
StatementCreateTable { unsafeGetCreateTable = table } -> table.name == tableName
otherwise -> False
otherwise -> False of
Just stmt@(StatementCreateTable {}) -> stmt
Just _ -> error $ "Internal error in patchTable: Expected StatementCreateTable but got different Statement type"
Nothing -> error $ "Internal error in patchTable: Could not find table " <> cs tableName <> " in actual schema"
patchTable (s:rest) = s:(patchTable rest)
patchTable [] = []

Expand All @@ -117,9 +120,12 @@ diffSchemas targetSchema' actualSchema' = (drop <> create)
isCreateEnumTypeStatement CreateEnumType { name = n } = name == n
isCreateEnumTypeStatement otherwise = False

(Just actualEnumType) = actualSchema |> find \case
actualEnumType = case actualSchema |> find \case
CreateEnumType { name = enum } -> enum == name
otherwise -> False
otherwise -> False of
Just enumType@CreateEnumType{} -> enumType
Just _ -> error $ "Internal error in patchEnumType: Expected CreateEnumType but got different Statement type"
Nothing -> error $ "Internal error in patchEnumType: Could not find enum type " <> cs name <> " in actual schema"
patchEnumType (s:rest) = s:(patchEnumType rest)
patchEnumType [] = []

Expand All @@ -134,6 +140,7 @@ diffSchemas targetSchema' actualSchema' = (drop <> create)
to = createTable'.name
in
(RenameTable { from, to }):(applyRenameTable (fixIdentifiers from to (delete createTable statements)))
Just _ -> s:(applyRenameTable statements) -- Not a StatementCreateTable, skip rename
Nothing -> s:(applyRenameTable statements)
where
createTable :: Maybe Statement
Expand All @@ -143,13 +150,14 @@ diffSchemas targetSchema' actualSchema' = (drop <> create)
isCreateTableStatement (StatementCreateTable { unsafeGetCreateTable = table }) = (table.name /= actualTable'.name) && ((actualTable' :: CreateTable) { name = "" } == (table :: CreateTable) { name = "" })
isCreateTableStatement otherwise = False

(Just actualTable) = actualSchema |> find \case
maybeActualTable = actualSchema |> find \case
StatementCreateTable { unsafeGetCreateTable = table } -> table.name == tableName
otherwise -> False

actualTable' :: CreateTable
actualTable' = case actualTable of
StatementCreateTable { unsafeGetCreateTable = table } -> table
actualTable' = case maybeActualTable of
Just (StatementCreateTable { unsafeGetCreateTable = table }) -> table
_ -> error $ "Internal error in applyRenameTable: Could not find table " <> cs tableName <> " in actual schema"

fixIdentifiers :: Text -> Text -> [Statement] -> [Statement]
fixIdentifiers tableFrom tableTo statements = map fixIdentifier statements
Expand Down Expand Up @@ -252,9 +260,10 @@ migrateTable StatementCreateTable { unsafeGetCreateTable = targetTable } Stateme
Nothing -> s:(applyMakeUnique statements)
where
dropColumn :: Column
(Just dropColumn) = actualColumns
|> find \case
Column { name } -> name == columnName
dropColumn = case actualColumns |> find \case
Column { name } -> name == columnName of
Just col -> col
Nothing -> error $ "Internal error in applyMakeUnique: Could not find column " <> cs columnName <> " in actual columns"

updateConstraint = if dropColumn.isUnique
then DropConstraint { tableName, constraintName = tableName <> "_" <> (dropColumn.name) <> "_key" }
Expand Down Expand Up @@ -289,9 +298,10 @@ migrateTable StatementCreateTable { unsafeGetCreateTable = targetTable } Stateme
Nothing -> s:(applySetDefault statements)
where
dropColumn :: Column
(Just dropColumn) = actualColumns
|> find \case
Column { name } -> name == columnName
dropColumn = case actualColumns |> find \case
Column { name } -> name == columnName of
Just col -> col
Nothing -> error $ "Internal error in applySetDefault: Could not find column " <> cs columnName <> " in actual columns"

matchingCreateColumn :: Maybe Statement
matchingCreateColumn = find isMatchingCreateColumn statements
Expand All @@ -318,9 +328,10 @@ migrateTable StatementCreateTable { unsafeGetCreateTable = targetTable } Stateme
Nothing -> s:(applyToggleNull statements)
where
dropColumn :: Column
(Just dropColumn) = actualColumns
|> find \case
Column { name } -> name == columnName
dropColumn = case actualColumns |> find \case
Column { name } -> name == columnName of
Just col -> col
Nothing -> error $ "Internal error in applyToggleNull: Could not find column " <> cs columnName <> " in actual columns"

updateConstraint = if dropColumn.notNull
then DropNotNull { tableName, columnName = dropColumn.name }
Expand Down
4 changes: 3 additions & 1 deletion ihp-ide/data/lib/IHP/Makefile.dist
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ GHC_EXTENSIONS+= -XPartialTypeSignatures
GHC_EXTENSIONS+= -XStandaloneDeriving
GHC_EXTENSIONS+= -XDerivingVia
GHC_EXTENSIONS+= -Werror=missing-fields
GHC_EXTENSIONS+= -fwarn-incomplete-patterns
# Incomplete patterns are treated as errors to catch potential bugs at compile time
# If you need to disable this temporarily for a specific module, add {-# OPTIONS_GHC -Wno-error=incomplete-patterns #-} at the top of that module
GHC_EXTENSIONS+= -Werror=incomplete-patterns
GHC_EXTENSIONS+= -XTemplateHaskell
GHC_EXTENSIONS+= -XDeepSubsumption
GHC_EXTENSIONS+= -XOverloadedRecordDot
Expand Down
1 change: 1 addition & 0 deletions ihp/IHP/ValidationSupport/ValidateField.hs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ withCustomErrorMessage :: Text -> (value -> ValidatorResult) -> value -> Validat
withCustomErrorMessage errorMessage validator value =
case validator value of
Failure _ -> Failure errorMessage
FailureHtml _ -> FailureHtml errorMessage
Success -> Success
{-# INLINABLE withCustomErrorMessage #-}

Expand Down