From f6daff9155ef68108fd46f415807a40cb5fa1af9 Mon Sep 17 00:00:00 2001 From: Peter Becich Date: Sat, 7 Dec 2024 16:52:16 -0800 Subject: [PATCH] fix Postgres example --- beam-postgres/examples/CHANGELOG.md | 5 ++ beam-postgres/examples/LICENSE | 29 +++++++++ .../examples/{pagila-0.10.1 => data}/README | 0 .../{pagila-0.10.1 => data}/pagila-data.sql | 0 .../pagila-insert-data.sql | 0 .../{pagila-0.10.1 => data}/pagila-schema.sql | 0 beam-postgres/examples/pagila.cabal | 47 ++++++++++++++ .../examples/{ => src}/Pagila/Schema.hs | 7 ++- .../Pagila/Schema/CustomMigrateExample.hs | 61 ++++++++++++++----- .../examples/{ => src}/Pagila/Schema/V0001.hs | 52 ++++++++++++---- .../examples/{ => src}/Pagila/Schema/V0002.hs | 25 +++++--- .../examples/{ => src}/Pagila/Test.hs | 0 beam-postgres/examples/test.hs | 4 -- .../examples/{ => test}/pagilatest.hs | 0 cabal.project | 3 +- 15 files changed, 192 insertions(+), 41 deletions(-) create mode 100644 beam-postgres/examples/CHANGELOG.md create mode 100644 beam-postgres/examples/LICENSE rename beam-postgres/examples/{pagila-0.10.1 => data}/README (100%) rename beam-postgres/examples/{pagila-0.10.1 => data}/pagila-data.sql (100%) rename beam-postgres/examples/{pagila-0.10.1 => data}/pagila-insert-data.sql (100%) rename beam-postgres/examples/{pagila-0.10.1 => data}/pagila-schema.sql (100%) create mode 100644 beam-postgres/examples/pagila.cabal rename beam-postgres/examples/{ => src}/Pagila/Schema.hs (81%) rename beam-postgres/examples/{ => src}/Pagila/Schema/CustomMigrateExample.hs (74%) rename beam-postgres/examples/{ => src}/Pagila/Schema/V0001.hs (93%) rename beam-postgres/examples/{ => src}/Pagila/Schema/V0002.hs (85%) rename beam-postgres/examples/{ => src}/Pagila/Test.hs (100%) delete mode 100644 beam-postgres/examples/test.hs rename beam-postgres/examples/{ => test}/pagilatest.hs (100%) diff --git a/beam-postgres/examples/CHANGELOG.md b/beam-postgres/examples/CHANGELOG.md new file mode 100644 index 000000000..d6a112882 --- /dev/null +++ b/beam-postgres/examples/CHANGELOG.md @@ -0,0 +1,5 @@ +# Revision history for pagila + +## 0.1.0.0 -- YYYY-mm-dd + +* First version. Released on an unsuspecting world. diff --git a/beam-postgres/examples/LICENSE b/beam-postgres/examples/LICENSE new file mode 100644 index 000000000..315cef025 --- /dev/null +++ b/beam-postgres/examples/LICENSE @@ -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. diff --git a/beam-postgres/examples/pagila-0.10.1/README b/beam-postgres/examples/data/README similarity index 100% rename from beam-postgres/examples/pagila-0.10.1/README rename to beam-postgres/examples/data/README diff --git a/beam-postgres/examples/pagila-0.10.1/pagila-data.sql b/beam-postgres/examples/data/pagila-data.sql similarity index 100% rename from beam-postgres/examples/pagila-0.10.1/pagila-data.sql rename to beam-postgres/examples/data/pagila-data.sql diff --git a/beam-postgres/examples/pagila-0.10.1/pagila-insert-data.sql b/beam-postgres/examples/data/pagila-insert-data.sql similarity index 100% rename from beam-postgres/examples/pagila-0.10.1/pagila-insert-data.sql rename to beam-postgres/examples/data/pagila-insert-data.sql diff --git a/beam-postgres/examples/pagila-0.10.1/pagila-schema.sql b/beam-postgres/examples/data/pagila-schema.sql similarity index 100% rename from beam-postgres/examples/pagila-0.10.1/pagila-schema.sql rename to beam-postgres/examples/data/pagila-schema.sql diff --git a/beam-postgres/examples/pagila.cabal b/beam-postgres/examples/pagila.cabal new file mode 100644 index 000000000..85f7ac4c5 --- /dev/null +++ b/beam-postgres/examples/pagila.cabal @@ -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: travis@athougies.net +-- 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 diff --git a/beam-postgres/examples/Pagila/Schema.hs b/beam-postgres/examples/src/Pagila/Schema.hs similarity index 81% rename from beam-postgres/examples/Pagila/Schema.hs rename to beam-postgres/examples/src/Pagila/Schema.hs index 0170ed98d..233e39486 100644 --- a/beam-postgres/examples/Pagila/Schema.hs +++ b/beam-postgres/examples/src/Pagila/Schema.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE OverloadedStrings #-} module Pagila.Schema ( module Pagila.Schema.V0002 , migration, db ) where @@ -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) diff --git a/beam-postgres/examples/Pagila/Schema/CustomMigrateExample.hs b/beam-postgres/examples/src/Pagila/Schema/CustomMigrateExample.hs similarity index 74% rename from beam-postgres/examples/Pagila/Schema/CustomMigrateExample.hs rename to beam-postgres/examples/src/Pagila/Schema/CustomMigrateExample.hs index 490dcfff3..023d21269 100644 --- a/beam-postgres/examples/Pagila/Schema/CustomMigrateExample.hs +++ b/beam-postgres/examples/src/Pagila/Schema/CustomMigrateExample.hs @@ -3,7 +3,12 @@ {-# 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 @@ -11,26 +16,54 @@ -- 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 @@ -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 @@ -88,7 +121,7 @@ data PagilaDb f { address :: f (TableEntity AddressT) } deriving Generic -instance Database PagilaDb +instance Database Postgres PagilaDb -- Beamable instances diff --git a/beam-postgres/examples/Pagila/Schema/V0001.hs b/beam-postgres/examples/src/Pagila/Schema/V0001.hs similarity index 93% rename from beam-postgres/examples/Pagila/Schema/V0001.hs rename to beam-postgres/examples/src/Pagila/Schema/V0001.hs index dc1e13c0c..64b319ae6 100644 --- a/beam-postgres/examples/Pagila/Schema/V0001.hs +++ b/beam-postgres/examples/src/Pagila/Schema/V0001.hs @@ -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) @@ -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 diff --git a/beam-postgres/examples/Pagila/Schema/V0002.hs b/beam-postgres/examples/src/Pagila/Schema/V0002.hs similarity index 85% rename from beam-postgres/examples/Pagila/Schema/V0002.hs rename to beam-postgres/examples/src/Pagila/Schema/V0002.hs index f382ee512..f2ccb389e 100644 --- a/beam-postgres/examples/Pagila/Schema/V0002.hs +++ b/beam-postgres/examples/src/Pagila/Schema/V0002.hs @@ -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' @@ -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 diff --git a/beam-postgres/examples/Pagila/Test.hs b/beam-postgres/examples/src/Pagila/Test.hs similarity index 100% rename from beam-postgres/examples/Pagila/Test.hs rename to beam-postgres/examples/src/Pagila/Test.hs diff --git a/beam-postgres/examples/test.hs b/beam-postgres/examples/test.hs deleted file mode 100644 index fbd2b8674..000000000 --- a/beam-postgres/examples/test.hs +++ /dev/null @@ -1,4 +0,0 @@ -module Main where - -main :: IO () -main = putStrLn "Hello world" diff --git a/beam-postgres/examples/pagilatest.hs b/beam-postgres/examples/test/pagilatest.hs similarity index 100% rename from beam-postgres/examples/pagilatest.hs rename to beam-postgres/examples/test/pagilatest.hs diff --git a/cabal.project b/cabal.project index 6dda0fc42..e84a2cd4a 100644 --- a/cabal.project +++ b/cabal.project @@ -2,6 +2,7 @@ packages: beam-core beam-migrate beam-postgres + beam-postgres/examples beam-sqlite -- For testing @@ -10,4 +11,4 @@ allow-newer: aeson-optics:base source-repository-package type: git location: https://github.com/testcontainers/testcontainers-hs - tag: 7775add7ff62d3f97e42d546152e909e38316081 \ No newline at end of file + tag: 7775add7ff62d3f97e42d546152e909e38316081