Use ${table}_id for primary keys
This commit is contained in:
+5
-5
@@ -27,13 +27,13 @@ getTables :: Opium.Connection -> IO (Either Opium.Error [Table])
|
||||
getTables = Opium.fetch_ "SELECT table_schema AS schema, table_name AS name FROM information_schema.tables"
|
||||
|
||||
data DayIndex = DayIndex
|
||||
{ id :: Int
|
||||
{ day_index_id :: Int
|
||||
, day :: Day
|
||||
} deriving (Show, Generic, Opium.FromRow)
|
||||
|
||||
data DayFile = DayFile
|
||||
{ id :: Int
|
||||
, day_id :: Int
|
||||
{ day_file_id :: Int
|
||||
, day_index_id :: Int
|
||||
, relative_path :: String
|
||||
} deriving (Show, Generic, Opium.FromRow)
|
||||
|
||||
@@ -46,7 +46,7 @@ createDayIndex date = ex runIdentity .
|
||||
createDayFile :: Int -> FilePath -> Opium.Connection -> IO (Either Opium.Error ())
|
||||
createDayFile dayId path =
|
||||
Opium.execute
|
||||
"INSERT INTO yore.day_file (day_id, relative_path) VALUES ($1, $2)" (dayId, path)
|
||||
"INSERT INTO yore.day_file (day_index_id, relative_path) VALUES ($1, $2)" (dayId, path)
|
||||
|
||||
readDayIndex :: Day -> Opium.Connection -> IO (Either Opium.Error (Maybe DayIndex))
|
||||
readDayIndex date =
|
||||
@@ -57,7 +57,7 @@ readDayIndex date =
|
||||
readDayPaths :: Day -> Opium.Connection -> IO (Either Opium.Error [(DayIndex, DayFile)])
|
||||
readDayPaths date =
|
||||
Opium.fetch
|
||||
"SELECT * FROM yore.day_index LEFT JOIN yore.day_file ON day_index.id = day_id WHERE day = $1"
|
||||
"SELECT * FROM yore.day_index LEFT JOIN yore.day_file USING (day_index_id) WHERE day = $1"
|
||||
(Identity date)
|
||||
|
||||
ex :: (a -> b) -> IO (Either e a) -> IO (Either e b)
|
||||
|
||||
+1
-1
@@ -15,7 +15,7 @@ createEntries :: Day -> [FilePath] -> Opium.Connection -> IO ()
|
||||
createEntries date urls conn = do
|
||||
confidently $ Opium.execute_ "BEGIN" conn
|
||||
dayIndex <- confidently $ DB.createDayIndex date conn
|
||||
forM_ urls $ \url -> confidently $ DB.createDayFile dayIndex.id url conn
|
||||
forM_ urls $ \url -> confidently $ DB.createDayFile dayIndex.day_index_id url conn
|
||||
confidently $ Opium.execute_ "COMMIT" conn
|
||||
|
||||
confidently :: Exception e => IO (Either e a) -> IO a
|
||||
|
||||
Reference in New Issue
Block a user