Derive schema from table definition - #264
Conversation
|
Will this allow things like generating insert statements / validating table schema on boot? On a related note I've always wondered if it would be possible to model things like migrations as a type-level list, such that the resulting list can be reflected and checked for equality with the table definition / schema. ie: m1 = Migr [AddColumn "A"]
m2 = Migr [AddColumn "B"]
data T1 = T1 { a :: _, b :: _ } deriving ... -- table definition
migrate $ m1 `after` m2 -- somehow assert that result will have 2 columns a and b |
|
Yes, It should allow it. During the table schema definition, user will specify all column info with |
I don't think this a good idea but it's not relevant right now anyways. |
|
Thanks for this @ryskajakub! Can you rebase on master so that the tests pass? |
In order to be able to only project columns from Table, it's read type must not have any available functions that can modify it like Column does.
|
I fixed all errors but the one which uses If that would be needed, the Or it could be designed differently. |
|
Having looked at this again I think that the changes are too invasive. I don't like the idea of adding Instead I suggest that we add another field to data TableProperties writerColumns viewColumns = TableProperties
{ tablePropertiesWriter :: Writer writerColumns viewColumns
, tablePropertiesView :: View viewColumns
, tableColumns :: [(ColumnName, ColumnType)]
}
type ColumnName = String
type ColumnType = StringThe we will have to restrict required :: String -> TableProperties (Column a) (Column a)
required columnName = T.TableProperties
(T.required columnName)
(View (Column (HPQ.BaseTableAttrExpr columnName)))
[(columnName, showPGType (Proxy :: Proxy a))]
optional :: String -> TableProperties (Maybe (Column a)) (Column a)
optional columnName = T.TableProperties
(T.optional columnName)
(View (Column (HPQ.BaseTableAttrExpr columnName)))
[(columnName, showPGType (Proxy :: Proxy a))]Then I'm pretty sure we can get all or most of the table creation info we need out of that. |
Hi sorry for a bit of inactivity, I adapted this PR to changes in master and added the typefunction that will replace
TableColumnwithColumnfor better inference. If this is OK, we can this typefunction - probably the generic version you talk about - to template haskell that is generatedproduct-profunctorsalong with adapter andDefaultinstance.