Skip to content

Commit e465627

Browse files
committed
First stab on implementing schema support
1 parent 6fdbf84 commit e465627

File tree

6 files changed

+33
-2
lines changed

6 files changed

+33
-2
lines changed

persistent-sqlite/Database/Persist/Sqlite.hs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ wrapConnectionInfo connInfo conn logFunc = do
299299
, connCommit = helper "COMMIT"
300300
, connRollback = ignoreExceptions . helper "ROLLBACK"
301301
, connEscapeFieldName = escape . unFieldNameDB
302-
, connEscapeTableName = escape . unEntityNameDB . getEntityDBName
302+
, connEscapeTableName = \entDef -> prependSchemaAndEscape entDef $ getEntityDBName entDef
303303
, connEscapeRawName = escape
304304
, connNoLimit = "LIMIT -1"
305305
, connRDBMS = "sqlite"
@@ -490,7 +490,7 @@ mockMigration mig = do
490490
, connCommit = helper "COMMIT"
491491
, connRollback = ignoreExceptions . helper "ROLLBACK"
492492
, connEscapeFieldName = escape . unFieldNameDB
493-
, connEscapeTableName = escape . unEntityNameDB . getEntityDBName
493+
, connEscapeTableName = \entDef -> prependSchemaAndEscape entDef $ getEntityDBName entDef
494494
, connEscapeRawName = escape
495495
, connNoLimit = "LIMIT -1"
496496
, connRDBMS = "sqlite"
@@ -695,6 +695,13 @@ escape s =
695695
go '"' = "\"\""
696696
go c = T.singleton c
697697

698+
prependSchemaAndEscape :: EntityDef -> EntityNameDB -> Text
699+
prependSchemaAndEscape entDef entDBName = case getEntityDBSchema entDef of
700+
Nothing -> escapeE entDBName
701+
Just "main" -> escapeE entDBName
702+
Just "temp" -> escape "temp" <> "." <> escapeE entDBName
703+
Just schema -> escape (schema <> "_" <> unEntityNameDB entDBName)
704+
698705
putManySql :: EntityDef -> Int -> Text
699706
putManySql ent n = putManySql' conflictColumns (toList fields) ent n
700707
where

persistent-sqlite/test/Database/Persist/Sqlite/CompositeSpec.hs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import Control.Monad.Trans.Resource (MonadResource)
2424
import qualified Data.Conduit.List as CL
2525
import Conduit
2626
import Database.Persist.Sqlite
27+
import Database.Persist.SqlBackend.Internal
28+
import Database.Persist.EntityDef.Internal
2729
import System.IO (hClose)
2830
import Control.Exception (handle, IOException, throwIO)
2931
import System.IO.Temp (withSystemTempFile)
@@ -83,6 +85,15 @@ spec = describe "CompositeSpec" $ do
8385
runSqliteInfo connInfo $ do
8486
void $ runMigrationSilent compositeMigrateTest
8587
validateForeignKeys
88+
it "test schema handling (issues #93 and #1454)" $ asIO $ runSqliteInfo (mkSqliteConnectionInfo ":memory:") $ do
89+
backend <- ask
90+
let tableName rec = connEscapeTableName backend (entityDef $ Just rec)
91+
tableNameWithSchema schema rec = connEscapeTableName backend ((entityDef (Just rec)) { entitySchema = Just schema })
92+
93+
tableName (undefined :: SimpleComposite) @== "\"simple_composite\""
94+
tableNameWithSchema "main" (undefined :: SimpleComposite) @== "\"simple_composite\""
95+
tableNameWithSchema "temp" (undefined :: SimpleComposite) @== "\"temp\".\"simple_composite\""
96+
tableNameWithSchema "foo" (undefined :: SimpleComposite) @== "\"foo_simple_composite\""
8697

8798

8899
validateForeignKeys

persistent/Database/Persist/EntityDef.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module Database.Persist.EntityDef
99
-- * Accessors
1010
, getEntityHaskellName
1111
, getEntityDBName
12+
, getEntityDBSchema
1213
, getEntityFields
1314
, getEntityFieldsDatabase
1415
, getEntityForeignDefs
@@ -86,6 +87,14 @@ getEntityDBName
8687
-> EntityNameDB
8788
getEntityDBName = entityDB
8889

90+
-- | Return the database schema name for the given entity.
91+
--
92+
-- @since 2.14.4.5
93+
getEntityDBSchema
94+
:: EntityDef
95+
-> Maybe Text
96+
getEntityDBSchema = entitySchema
97+
8998
getEntityExtra :: EntityDef -> Map Text [[Text]]
9099
getEntityExtra = entityExtra
91100

persistent/Database/Persist/Quasi/Internal.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,7 @@ mkUnboundEntityDef ps parsedEntDef =
693693
EntityDef
694694
{ entityHaskell = entNameHS
695695
, entityDB = entNameDB
696+
, entitySchema = Nothing
696697
-- idField is the user-specified Id
697698
-- otherwise useAutoIdField
698699
-- but, adjust it if the user specified a Primary

persistent/Database/Persist/Types/Base.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ data EntityDef = EntityDef
131131
-- ^ The name of the entity as Haskell understands it.
132132
, entityDB :: !EntityNameDB
133133
-- ^ The name of the database table corresponding to the entity.
134+
, entitySchema :: !(Maybe Text)
135+
-- ^ The schema name of the database table.
134136
, entityId :: !EntityIdDef
135137
-- ^ The entity's primary key or identifier.
136138
, entityAttrs :: ![Attr]

persistent/test/Database/Persist/THSpec.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ spec = describe "THSpec" $ do
318318
EntityDef
319319
{ entityHaskell = EntityNameHS "HasSimpleCascadeRef"
320320
, entityDB = EntityNameDB "HasSimpleCascadeRef"
321+
, entitySchema = Nothing
321322
, entityId =
322323
EntityIdField FieldDef
323324
{ fieldHaskell = FieldNameHS "Id"

0 commit comments

Comments
 (0)