Skip to content

Commit

Permalink
diffAppDatabase should work with the configured database url instead …
Browse files Browse the repository at this point in the history
…of the hardcoded dev db
  • Loading branch information
mpscholten committed Jun 9, 2022
1 parent 052b638 commit 895edff
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions IHP/IDE/CodeGen/MigrationGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import Text.Megaparsec
import IHP.IDE.SchemaDesigner.Compiler (compileSql)
import IHP.IDE.CodeGen.Types
import qualified IHP.LibDir as LibDir
import qualified IHP.FrameworkConfig as FrameworkConfig

buildPlan :: Text -> Maybe Text -> IO (Int, [GeneratorAction])
buildPlan description sqlStatements = do
Expand All @@ -32,7 +33,8 @@ buildPlan description sqlStatements = do
migrationSql <- case sqlStatements of
Just sql -> pure sql
Nothing -> do
appDiff <- diffAppDatabase
databaseUrl <- cs <$> FrameworkConfig.defaultDatabaseUrl
appDiff <- diffAppDatabase databaseUrl
pure $ if isEmpty appDiff
then "-- Write your SQL migration code in here\n"
else compileSql appDiff
Expand All @@ -41,10 +43,10 @@ buildPlan description sqlStatements = do
, CreateFile { filePath = "Application/Migration/" <> migrationFile, fileContent = migrationSql }
])

diffAppDatabase = do
diffAppDatabase databaseUrl = do
(Right schemaSql) <- Parser.parseSchemaSql
(Right ihpSchemaSql) <- parseIHPSchema
actualSchema <- getAppDBSchema
actualSchema <- getAppDBSchema databaseUrl

let targetSchema = ihpSchemaSql <> schemaSql

Expand Down Expand Up @@ -312,20 +314,20 @@ migrateEnum CreateEnumType { name, values = targetValues } CreateEnumType { valu
addValue :: Text -> Statement
addValue value = AddValueToEnumType { enumName = name, newValue = value, ifNotExists = True }

getAppDBSchema :: IO [Statement]
getAppDBSchema = do
sql <- dumpAppDatabaseSchema
getAppDBSchema :: Text -> IO [Statement]
getAppDBSchema databaseUrl = do
sql <- dumpAppDatabaseSchema databaseUrl
case parseDumpedSql sql of
Left error -> fail (cs error)
Right result -> pure result

-- | Returns the DDL statements of the locally running dev db
--
-- Basically does the same as @make dumpdb@ but returns the output as a string
dumpAppDatabaseSchema :: IO Text
dumpAppDatabaseSchema = do
dumpAppDatabaseSchema :: Text -> IO Text
dumpAppDatabaseSchema databaseUrl = do
projectDir <- Directory.getCurrentDirectory
cs <$> Process.readProcess "pg_dump" ["-s", "--no-owner", "--no-acl", "-h", projectDir <> "/build/db", "app"] []
cs <$> Process.readProcess "pg_dump" ["-s", "--no-owner", "--no-acl", cs databaseUrl] []

parseDumpedSql :: Text -> (Either ByteString [Statement])
parseDumpedSql sql =
Expand Down

0 comments on commit 895edff

Please sign in to comment.