@@ -18,6 +18,9 @@ module Pagila.Schema.V0002
18
18
import qualified Pagila.Schema.V0001 as V0001
19
19
import qualified Pagila.Schema.V0001 as V0001' hiding (PagilaDb , migration )
20
20
21
+ import Data.Int (Int32 )
22
+ import Data.Text (Text )
23
+ import Data.ByteString (ByteString )
21
24
import Database.Beam
22
25
( Generic ,
23
26
Columnar ,
@@ -26,12 +29,13 @@ import Database.Beam
26
29
Table (.. ),
27
30
TableEntity ,
28
31
Database ,
29
- smallint )
32
+ smallint ,
33
+ val_ )
30
34
import Database.Beam.Postgres ( Postgres )
31
35
import Database.Beam.Migrate.Types
32
- ( CheckedDatabaseSettings , Migration )
36
+ ( CheckedDatabaseSettings , CheckedDatabaseEntity , Migration )
33
37
import Database.Beam.Migrate.SQL.Tables
34
- ( field , notNull , createTable , preserve )
38
+ ( field , notNull , createTable , preserve , addColumn , alterTable , defaultTo_ )
35
39
36
40
import Data.Time.LocalTime (LocalTime )
37
41
@@ -58,6 +62,32 @@ deriving instance Eq FilmActorId; deriving instance Show FilmActorId
58
62
instance Beamable FilmActorT
59
63
instance Beamable (PrimaryKey FilmActorT )
60
64
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
+
61
91
data PagilaDb f
62
92
= PagilaDb
63
93
{ actor :: f (TableEntity V0001. ActorT )
@@ -71,10 +101,30 @@ data PagilaDb f
71
101
, filmActor :: f (TableEntity FilmActorT )
72
102
, language :: f (TableEntity V0001. LanguageT )
73
103
, store :: f (TableEntity V0001. StoreT )
74
- , staff :: f (TableEntity V0001. StaffT )
104
+ , staff :: f (TableEntity NewStaffT )
75
105
} deriving Generic
76
106
instance Database Postgres PagilaDb
77
107
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
+
78
128
migration :: CheckedDatabaseSettings Postgres V0001. PagilaDb
79
129
-> Migration Postgres (CheckedDatabaseSettings Postgres PagilaDb )
80
130
migration oldDb =
@@ -93,4 +143,4 @@ migration oldDb =
93
143
V0001. lastUpdateField)
94
144
<*> preserve (V0001. language oldDb)
95
145
<*> preserve (V0001. store oldDb)
96
- <*> preserve ( V0001. staff oldDb)
146
+ <*> migrateToNewStaffWithSalary oldDb
0 commit comments