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 f6daff9
Show file tree
Hide file tree
Showing 15 changed files with 192 additions and 41 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.
29 changes: 29 additions & 0 deletions beam-postgres/examples/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Copyright (c) 2024, Travis Athougies


Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.

* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
File renamed without changes.
File renamed without changes.
47 changes: 47 additions & 0 deletions beam-postgres/examples/pagila.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
cabal-version: 3.0
name: pagila
version: 0.1.0.0
license: BSD-3-Clause
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
-- other-modules:
-- other-extensions:
build-depends: base ^>=4.20.0.0,
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 ^>=4.20.0.0,
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
File renamed without changes.
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 f6daff9

Please sign in to comment.