Skip to content

Commit

Permalink
fix Postgres example
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbecich committed Dec 8, 2024
1 parent 249cb07 commit 3d4e88e
Show file tree
Hide file tree
Showing 15 changed files with 187 additions and 54 deletions.
5 changes: 5 additions & 0 deletions beam-postgres/examples/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Revision history for pagila

## 0.1.0.0 -- YYYY-mm-dd

* First version. Released on an unsuspecting world.
8 changes: 8 additions & 0 deletions beam-postgres/examples/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
The MIT License (MIT)
Copyright (c) 2017-2018 Travis Athougies and the Beam Authors

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
File renamed without changes.
File renamed without changes.
48 changes: 48 additions & 0 deletions beam-postgres/examples/pagila.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
cabal-version: 3.0
name: pagila
version: 0.1.0.0
license: MIT
license-file: LICENSE
author: Travis Athougies
maintainer: [email protected]
-- copyright:
build-type: Simple
extra-doc-files: CHANGELOG.md
-- extra-source-files:

common warnings
ghc-options: -Wall

library
import: warnings
exposed-modules: Pagila.Schema,
Pagila.Schema.V0001,
Pagila.Schema.V0002,
Pagila.Schema.CustomMigrateExample,
Pagila.Test
-- other-modules:
-- other-extensions:
build-depends: base,
time,
scientific,
bytestring,
text,
postgresql-simple,
beam-core,
beam-postgres,
beam-migrate

hs-source-dirs: src
default-language: Haskell2010

test-suite pagila-test
import: warnings
default-language: Haskell2010
-- other-modules:
-- other-extensions:
type: exitcode-stdio-1.0
hs-source-dirs: test
main-is: pagilatest.hs
build-depends:
base,
pagila
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE OverloadedStrings #-}
module Pagila.Schema
( module Pagila.Schema.V0002
, migration, db ) where
Expand All @@ -7,16 +8,16 @@ import Pagila.Schema.V0002 hiding (migration)
import qualified Pagila.Schema.V0001 as V0001 (migration)
import qualified Pagila.Schema.V0002 as V0002 (migration)

import Control.Arrow
import Control.Arrow ( (>>>) )

import Database.Beam (DatabaseSettings)
import Database.Beam.Migrate.Types ( CheckedDatabaseSettings, MigrationSteps, unCheckDatabase
, evaluateDatabase, migrationStep)
import Database.Beam.Postgres (Postgres, PgCommandSyntax)

migration :: MigrationSteps PgCommandSyntax () (CheckedDatabaseSettings Postgres PagilaDb)
migration :: MigrationSteps PgCommandSyntax () (CheckedDatabaseSettings Postgres Pagila.Schema.V0002.PagilaDb)
migration = migrationStep "Initial commit" V0001.migration >>>
migrationStep "Add film actor, inventory, rental table" V0002.migration

db :: DatabaseSettings Postgres PagilaDb
db :: DatabaseSettings Postgres Pagila.Schema.V0002.PagilaDb
db = unCheckDatabase (evaluateDatabase migration)
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,67 @@
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE PartialTypeSignatures #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE DeriveGeneric #-}

-- | This module contains a minimal example of how to use
-- a custom data type (``ShippingCarrier`` in this example) in migration
-- Other than the use of ``ShippingCarrier`` as a part of ``AddressT``,
-- this is just a stripped down version of Pagila.Schema.V0001
module Pagila.Schema.CustomMigrateExample where

import Database.Beam
import Database.Beam.Postgres
import Database.Beam.Migrate
import Database.Beam.Postgres.Migrate
import Database.Beam.Backend.SQL
import Database.Beam.Migrate.SQL.Types (TableFieldSchema(..), DataType(..))
import Database.Beam.Backend.SQL.Types (SqlSerial)

import Data.Int
import Database.Beam
( Generic,
maybeType,
timestamp,
varchar,
FromBackendRow(fromBackendRow),
DataType(..),
Beamable,
Columnar,
Database,
Table(..),
TableEntity,
Identity )
import Database.Beam.Postgres
( smallserial,
now_,
PgCommandSyntax,
Postgres,
ResultError(ConversionFailed) )
import Database.Beam.Postgres.Syntax
( pgTextType, PgColumnSchemaSyntax, PgDataTypeSyntax )
import Database.Beam.Migrate
( createTable,
defaultTo_,
field,
notNull,
TableFieldSchema,
Migration,
CheckedDatabaseSettings )
import Database.Beam.Backend.SQL
( HasSqlValueSyntax(..),
BeamSqlBackend,
SqlSerial,
autoSqlValueSyntax )
import Database.Beam.Migrate.SQL ()

import Data.Int ( Int32 )
import qualified Data.Text as T
import Data.Time.LocalTime (LocalTime)
import Database.PostgreSQL.Simple.FromField
import Text.Read
import Database.PostgreSQL.Simple.FromField
( returnError, FromField(..) )
import Text.Read ( readMaybe )

data ShippingCarrier = USPS | FedEx | UPS | DHL
deriving (Show, Read, Eq, Ord, Enum)

instance HasSqlValueSyntax be String => HasSqlValueSyntax be ShippingCarrier where
sqlValueSyntax = autoSqlValueSyntax
instance (IsSql92ColumnSchemaSyntax be) => HasDefaultSqlDataTypeConstraints be ShippingCarrier

instance FromField ShippingCarrier where
fromField f bs = do
Expand All @@ -40,7 +73,7 @@ instance FromField ShippingCarrier where
Just x -> pure x

-- | An explicit definition of ``fromBackendRow`` is required for each custom type
instance (BeamBackend be, FromBackendRow be T.Text) => FromBackendRow be ShippingCarrier where
instance (BeamSqlBackend be, FromBackendRow be T.Text) => FromBackendRow be ShippingCarrier where
fromBackendRow = do
val <- fromBackendRow
case val :: T.Text of
Expand Down Expand Up @@ -88,7 +121,7 @@ data PagilaDb f
{
address :: f (TableEntity AddressT)
} deriving Generic
instance Database PagilaDb
instance Database Postgres PagilaDb

-- Beamable instances

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,55 @@
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE PartialTypeSignatures #-}
{-# OPTIONS_GHC -fglasgow-exts #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE DeriveGeneric #-}

module Pagila.Schema.V0001 where

import Database.Beam
( Generic,
Columnar,
Identity,
Beamable,
Table(..),
TableEntity,
Database,
SqlValable(val_),
timestamp,
varchar,
maybeType,
smallint,
boolean,
date,
numeric,
char,
binaryLargeObject )
import Database.Beam.Postgres
import Database.Beam.Postgres (PgSyntax(..))
import Database.Beam.Postgres.Migrate
import Database.Beam.Migrate.Types hiding (migrateScript)
import Database.Beam.Migrate.SQL.Tables
import Database.Beam.Migrate.SQL.Types
( Postgres,
PgCommandSyntax,
now_,
serial,
smallserial,
text,
bytea )
import Database.Beam.Postgres.Syntax (PgColumnSchemaSyntax)
import Database.Beam.Migrate.Types
( CheckedDatabaseSettings, Migration )
import Database.Beam.Migrate.SQL
( TableFieldSchema,
field,
defaultTo_,
notNull,
createTable,
unique )
import Database.Beam.Backend.SQL.Types (SqlSerial)
import qualified Database.PostgreSQL.Simple as Pg

import qualified Control.Exception as E

import Data.Int (Int32)
import Data.Text (Text)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Lazy as BL
import Data.Time.LocalTime (LocalTime)
import Data.Scientific (Scientific)

Expand Down Expand Up @@ -100,7 +130,7 @@ type Actor = ActorT Identity
deriving instance Show Actor; deriving instance Eq Actor

instance Table ActorT where
data PrimaryKey ActorT f = ActorId (Columnar f (SqlSerial Int23))
data PrimaryKey ActorT f = ActorId (Columnar f (SqlSerial Int32))
deriving Generic
primaryKey = ActorId . actorId
type ActorId = PrimaryKey ActorT Identity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE PartialTypeSignatures #-}
{-# OPTIONS_GHC -fglasgow-exts #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE DeriveGeneric #-}

module Pagila.Schema.V0002
( module V0001'
Expand All @@ -16,17 +20,22 @@ import qualified Pagila.Schema.V0001 as V0001
import qualified Pagila.Schema.V0001 as V0001' hiding (PagilaDb, migration)

import Database.Beam
import Database.Beam.Postgres
import Database.Beam.Postgres (PgSyntax(..))
import Database.Beam.Postgres.Migrate
import Database.Beam.Migrate.Types hiding (migrateScript)
( Generic,
Columnar,
Identity,
Beamable,
Table(..),
TableEntity,
Database,
smallint )
import Database.Beam.Postgres ( Postgres, PgCommandSyntax )
import Database.Beam.Migrate.Types
( CheckedDatabaseSettings, Migration )
import Database.Beam.Migrate.SQL.Tables
import Database.Beam.Migrate.SQL.Types
( field, notNull, createTable, preserve )

import qualified Database.PostgreSQL.Simple as Pg

import qualified Control.Exception as E

import Data.Text (Text)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Lazy as BL
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE DeriveGeneric #-}

module Pagila.Test where

import Control.Arrow
import Control.Arrow ()

import Data.Int
import Data.Proxy
import Data.Int ( Int32 )
import Data.Text(Text)

import Database.Beam
import Database.Beam.Backend.Types
import Database.Beam.Migrate.Types ( CheckedDatabaseSettings, MigrationSteps, unCheckDatabase
, evaluateDatabase, migrationStep)
( Generic, Beamable, Columnar, Database, Table(..), TableEntity )
import Database.Beam.Postgres
import Database.Beam.Backend.Types ()
import Database.Beam.Migrate.Generics
import Database.Beam.Migrate.SQL.SQL92
import Database.Beam.Postgres (Postgres, PgCommandSyntax)
import Database.Beam.Postgres.Migrate
import Database.Beam.Migrate.Types hiding (migrateScript)
import Database.Beam.Migrate.SQL.Tables
import Database.Beam.Migrate.SQL.Types

data SimpleTbl f
= SimpleTbl
Expand All @@ -35,7 +37,7 @@ data MyDb f =
MyDb { mydbSimpleTbl :: f (TableEntity SimpleTbl) } deriving Generic
instance Database be MyDb

myDbMigratable :: forall syntax be
myDbMigratable :: forall syntax
. IsSql92DdlCommandSyntax syntax
=> CheckedDatabaseSettings be MyDb
myDbMigratable = defaultMigratableDbSettings @syntax
=> CheckedDatabaseSettings Postgres MyDb
myDbMigratable = defaultMigratableDbSettings @Postgres @MyDb
4 changes: 0 additions & 4 deletions beam-postgres/examples/test.hs

This file was deleted.

File renamed without changes.
3 changes: 2 additions & 1 deletion cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ packages:
beam-core
beam-migrate
beam-postgres
beam-postgres/examples
beam-sqlite

-- For testing
Expand All @@ -10,4 +11,4 @@ allow-newer: aeson-optics:base
source-repository-package
type: git
location: https://github.com/testcontainers/testcontainers-hs
tag: 7775add7ff62d3f97e42d546152e909e38316081
tag: 7775add7ff62d3f97e42d546152e909e38316081

0 comments on commit 3d4e88e

Please sign in to comment.