coalesceColumn :: TypeDefaults -> TableName -> Column -> ScalarExpr
coalesceColumn defaults tName Column{..} =
  if columnNullable == Null
    then app "coalesce" [fqColName, num $ defVal columnType]
    else fqColName
  where
    fqColName = eqi tName columnName

    defVal colType =
      maybe (error $ "Default value not known for column type: " ++ Text.unpack colType) snd
      . find (\(k, _) -> k `Text.isPrefixOf` colType)
      . Map.toList
      $ defaults
coalesceColumn :: TypeDefaults -> TableName -> Column -> ScalarExpr
coalesceColumn defaults tName Column{..} =
  if columnNullable == Null
    then app "coalesce" [fqColName, num $ defVal columnType]
    else fqColName
  where
    fqColName = eqi tName columnName

    defVal colType =
      maybe (error $ "Default value not known for column type: " ++ Text.unpack colType) snd
      . find (\(k, _) -> k `Text.isPrefixOf` colType)
      . Map.toList
      $ defaults
coalesceColumn defaults tName Column{..} =
  if columnNullable == Null
    then app "coalesce" [fqColName, num $ defVal columnType]
    else fqColName
  where
    fqColName = eqi tName columnName

    defVal colType =
      maybe (error $ "Default value not known for column type: " ++ Text.unpack colType) snd
      . find (\(k, _) -> k `Text.isPrefixOf` colType)
      . Map.toList
      $ defaults