Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make genericSerial generic over Integral #534

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions beam-migrate/Database/Beam/Migrate/SQL/BeamExtensions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
module Database.Beam.Migrate.SQL.BeamExtensions where

import Database.Beam.Backend.SQL
import Database.Beam.Migrate.SQL.Types
import Database.Beam.Migrate.SQL.Tables
import Database.Beam.Migrate.SQL.Types

import Data.Text (Text)

Expand All @@ -29,6 +29,6 @@ import Data.Text (Text)
-- 'genericSerial' may appear and may fail at run-time if these conditions
-- aren't met. Please refer to the backend of your choice for more
-- information.
class BeamMigrateSqlBackend be =>
BeamSqlBackendHasSerial be where
genericSerial :: FieldReturnType 'True 'False be (SqlSerial Int) a => Text -> a
class (BeamMigrateSqlBackend be, Integral n) =>
BeamSqlBackendHasSerial n be where
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be in favor of removing the Integral constraint here. Integral implies Num which implies the ability to do arithmetic. However, I can imagine wanting to add support for an opaque ID type which prohibits arithmetic. Backends already have the ability to constrain the valid ns in their instances.

genericSerial :: FieldReturnType 'True 'False be (SqlSerial n) a => Text -> a
2 changes: 1 addition & 1 deletion beam-postgres/Database/Beam/Postgres/Migrate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -452,5 +452,5 @@ instance Db.FieldReturnType 'True 'False Postgres resTy a =>
field' _ _ nm ty _ collation constraints PgHasDefault =
Db.field' (Proxy @'True) (Proxy @'False) nm ty Nothing collation constraints

instance BeamSqlBackendHasSerial Postgres where
instance (Integral n) => BeamSqlBackendHasSerial n Postgres where
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This instance should be split for Int16, Int32, and Int64 and use smallserial, serial, and bigserial, respectively.

genericSerial nm = Db.field nm serial PgHasDefault
2 changes: 1 addition & 1 deletion beam-sqlite/Database/Beam/Sqlite/Connection.hs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ instance FieldReturnType 'True 'False Sqlite resTy a =>
field' _ _ nm ty _ collation constraints SqliteHasDefault =
field' (Proxy @'True) (Proxy @'False) nm ty Nothing collation constraints

instance BeamSqlBackendHasSerial Sqlite where
instance (Integral n) => BeamSqlBackendHasSerial n Sqlite where
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should have a IsSqliteSerialIntegerType constraint.

genericSerial nm = Beam.field nm (DataType sqliteSerialType) SqliteHasDefault

-- | 'MonadBeam' instance inside whiche SQLite queries are run. See the
Expand Down