From 895edfff52862a07bc1ee92a4ea1cef56a1f4626 Mon Sep 17 00:00:00 2001 From: Marc Scholten Date: Thu, 9 Jun 2022 10:37:12 +0200 Subject: [PATCH] diffAppDatabase should work with the configured database url instead of the hardcoded dev db --- IHP/IDE/CodeGen/MigrationGenerator.hs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/IHP/IDE/CodeGen/MigrationGenerator.hs b/IHP/IDE/CodeGen/MigrationGenerator.hs index f2d65bd46..d1976be46 100644 --- a/IHP/IDE/CodeGen/MigrationGenerator.hs +++ b/IHP/IDE/CodeGen/MigrationGenerator.hs @@ -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 @@ -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 @@ -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 @@ -312,9 +314,9 @@ 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 @@ -322,10 +324,10 @@ getAppDBSchema = do -- | 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 =