@@ -18,6 +18,9 @@ module Pagila.Schema.V0002
1818import qualified Pagila.Schema.V0001 as V0001
1919import qualified Pagila.Schema.V0001 as V0001' hiding (PagilaDb , migration )
2020
21+ import Data.Int (Int32 )
22+ import Data.Text (Text )
23+ import Data.ByteString (ByteString )
2124import Database.Beam
2225 ( Generic ,
2326 Columnar ,
@@ -26,12 +29,13 @@ import Database.Beam
2629 Table (.. ),
2730 TableEntity ,
2831 Database ,
29- smallint )
32+ smallint ,
33+ val_ )
3034import Database.Beam.Postgres ( Postgres )
3135import Database.Beam.Migrate.Types
32- ( CheckedDatabaseSettings , Migration )
36+ ( CheckedDatabaseSettings , CheckedDatabaseEntity , Migration )
3337import Database.Beam.Migrate.SQL.Tables
34- ( field , notNull , createTable , preserve )
38+ ( field , notNull , createTable , preserve , addColumn , alterTable , defaultTo_ )
3539
3640import Data.Time.LocalTime (LocalTime )
3741
@@ -58,6 +62,32 @@ deriving instance Eq FilmActorId; deriving instance Show FilmActorId
5862instance Beamable FilmActorT
5963instance Beamable (PrimaryKey FilmActorT )
6064
65+ instance Table NewStaffT where
66+ data PrimaryKey NewStaffT f = NewStaffId (Columnar f Int32 ) deriving Generic
67+ primaryKey = NewStaffId . staffId
68+ type NewStaffId = PrimaryKey NewStaffT Identity
69+ deriving instance Eq NewStaffId ; deriving instance Show NewStaffId
70+
71+ data NewStaffT f
72+ = NewStaffT
73+ { staffId :: Columnar f Int32
74+ , staffFirstName :: Columnar f Text
75+ , staffLastName :: Columnar f Text
76+ , staffAddress :: PrimaryKey V0001. AddressT f
77+ , staffEmail :: Columnar f Text
78+ , staffStore :: PrimaryKey V0001. StoreT f
79+ , staffActive :: Columnar f Bool
80+ , staffUsername :: Columnar f Text
81+ , staffPassword :: Columnar f Text -- TODO use ByteString
82+ , staffLastUpdate :: Columnar f LocalTime
83+ , staffPicture :: Columnar f (Maybe ByteString )
84+ , staffSalary :: Columnar f Int32 -- new Salary field
85+ } deriving Generic
86+ type NewStaff = NewStaffT Identity
87+ deriving instance Eq NewStaff ; deriving instance Show NewStaff
88+ instance Beamable (PrimaryKey NewStaffT )
89+ instance Beamable NewStaffT
90+
6191data PagilaDb f
6292 = PagilaDb
6393 { actor :: f (TableEntity V0001. ActorT )
@@ -71,10 +101,30 @@ data PagilaDb f
71101 , filmActor :: f (TableEntity FilmActorT )
72102 , language :: f (TableEntity V0001. LanguageT )
73103 , store :: f (TableEntity V0001. StoreT )
74- , staff :: f (TableEntity V0001. StaffT )
104+ , staff :: f (TableEntity NewStaffT )
75105 } deriving Generic
76106instance Database Postgres PagilaDb
77107
108+ migrateToNewStaffWithSalary :: CheckedDatabaseSettings Postgres V0001. PagilaDb
109+ -> Migration Postgres (CheckedDatabaseEntity Postgres db (TableEntity NewStaffT ))
110+ migrateToNewStaffWithSalary oldDb = alterTable (V0001. staff oldDb) $ \ oldStaff -> do
111+ staffSalary <- addColumn (field " salary" smallint notNull (defaultTo_ (val_ 100 )))
112+ pure $
113+ NewStaffT
114+ { staffId = V0001. staffId oldStaff,
115+ staffFirstName = V0001. staffFirstName oldStaff,
116+ staffLastName = V0001. staffLastName oldStaff,
117+ staffAddress = V0001. staffAddress oldStaff,
118+ staffEmail = V0001. staffEmail oldStaff,
119+ staffStore = V0001. staffStore oldStaff,
120+ staffActive = V0001. staffActive oldStaff,
121+ staffUsername = V0001. staffUsername oldStaff,
122+ staffPassword = V0001. staffPassword oldStaff,
123+ staffLastUpdate = V0001. staffLastUpdate oldStaff,
124+ staffPicture = V0001. staffPicture oldStaff,
125+ staffSalary = staffSalary
126+ }
127+
78128migration :: CheckedDatabaseSettings Postgres V0001. PagilaDb
79129 -> Migration Postgres (CheckedDatabaseSettings Postgres PagilaDb )
80130migration oldDb =
@@ -93,4 +143,4 @@ migration oldDb =
93143 V0001. lastUpdateField)
94144 <*> preserve (V0001. language oldDb)
95145 <*> preserve (V0001. store oldDb)
96- <*> preserve ( V0001. staff oldDb)
146+ <*> migrateToNewStaffWithSalary oldDb
0 commit comments