sqiff 0.1.3.1 → 0.1.4.0
raw patch · 5 files changed
+25/−9 lines, 5 files
Files
- sqiff.cabal +1/−1
- src/Sqiff/Plan.hs +6/−2
- src/Sqiff/Sqitch/Units.hs +5/−5
- src/Sqiff/Sql.hs +10/−1
- test/Unit.hs +3/−0
sqiff.cabal view
@@ -1,6 +1,6 @@ cabal-version: 3.0 name: sqiff-version: 0.1.3.1+version: 0.1.4.0 synopsis: Describe and diff PostgreSQL schemas from their catalogs description: sqiff compares PostgreSQL schemas and writes the difference as sqitch
src/Sqiff/Plan.hs view
@@ -27,7 +27,7 @@ import Sqiff.Graph (schedule, topoOrder) import Sqiff.Model import Sqiff.Recipe-import Sqiff.Sql (terminator)+import Sqiff.Sql (lowerCode, terminator) -- | Objects whose steps depend on each other in a circle, so no order works.@@ -190,12 +190,16 @@ isDropped = \case Dropped _ -> True; _ -> False +-- | A step as the plan prints it. Its code is lowercased here, where it is printed and+-- not where it is built: steps carry statements copied from the catalog+-- (@pg_get_viewdef@, @pg_get_indexdef@, …) in its upper case, and the recipes match on+-- that text. renderStep :: Step -> Text renderStep s = T.unlines $ ["-- " <> render s.object] <> ["-- " <> note | note <- txnNote <> map hazardText s.hazards]- <> [s.sql <> terminator s.sql]+ <> [lowerCode s.sql <> terminator s.sql] where txnNote = case s.txn of Shared -> []
src/Sqiff/Sqitch/Units.hs view
@@ -38,7 +38,7 @@ import Sqiff.Plan (invert) import Sqiff.Recipe import Sqiff.Sqitch.Plan (SqitchPlan (..))-import Sqiff.Sql (doBlock, literal, replaceAllInCode, terminator)+import Sqiff.Sql (doBlock, literal, lowerCode, replaceAllInCode, terminator) data UnitKind = AddChange | ReworkChange@@ -397,12 +397,12 @@ [s] | s.txn == None -> statement (idempotent s.sql) s _ -> T.intercalate "\n\n"- $ ["BEGIN;"]+ $ ["begin;"] <> [" -- Do not remove this line\n select set_config('lock_timeout', '50ms', false);" | placement == Migration] <> map (\s -> if safeToIndent s.sql then indent (statement s.sql s) else statement s.sql s) group'- <> ["COMMIT;"]+ <> ["commit;"] statement sql s =- T.intercalate "\n" (map ("-- " <>) (hazardNotes s) <> [tidy s sql <> terminator sql] <> stopUntilWritten s)+ T.intercalate "\n" (map ("-- " <>) (hazardNotes s) <> [lowerCode (tidy s sql) <> terminator sql] <> stopUntilWritten s) -- A step sqiff cannot write stops the deploy until someone writes it, rather than -- deploying as a change that does nothing. stopUntilWritten s =@@ -498,7 +498,7 @@ -- types print as the plan has them. verifyScript :: Text -> [Step] -> Text verifyScript header steps =- T.intercalate "\n\n" ([header, "BEGIN;", " set local search_path = '';"] <> map (" " <>) selects <> assertBlock <> ["ROLLBACK;"]) <> "\n"+ T.intercalate "\n\n" ([header, "begin;", " set local search_path = '';"] <> map (" " <>) selects <> assertBlock <> ["rollback;"]) <> "\n" where final = Map.elems (Map.fromList [(s.object, s) | s <- steps, s.key.aspect == Definition]) gone :: Step -> Bool
src/Sqiff/Sql.hs view
@@ -17,13 +17,14 @@ splitTopLevel, replaceInCode, replaceAllInCode,+ lowerCode, literal, dollarQuoted, doBlock, terminator, ) where -import Data.Char (isAlpha, isAlphaNum, isDigit, isSpace)+import Data.Char (isAlpha, isAlphaNum, isAsciiUpper, isDigit, isSpace, toLower) import Data.String.Interpolate (i) import Data.Text qualified as T import RIO@@ -158,6 +159,14 @@ in if inCode at then before <> new <> go (at + T.length old) (T.drop (T.length old) match) else before <> T.take 1 match <> go (at + 1) (T.drop 1 match)+++-- | The text with its code in lower case, as sqiff writes SQL, keeping quotes and+-- comments as they are. It reads the same to PostgreSQL, which folds unquoted words to+-- lower case: catalog functions such as @pg_get_viewdef@ quote every name that is not+-- already lower case. Only ASCII letters are folded, as PostgreSQL folds them.+lowerCode :: Text -> Text+lowerCode t = T.pack [if r == Code && isAsciiUpper c then toLower c else c | (c, r) <- regions t] -- | Each pair's replacement, in order, in code only.
test/Unit.hs view
@@ -77,6 +77,9 @@ [ testCase "replaceInCode leaves literals, quoted names and comments alone" $ Sql.replaceInCode " WHERE " " where " "x WHERE y = ' WHERE ' -- WHERE \n and \" WHERE \" WHERE z" @?= "x where y = ' WHERE ' -- WHERE \n and \" WHERE \" where z"+ , testCase "lowerCode lowers code and keeps literals, quoted names and comments"+ $ Sql.lowerCode "SELECT \"Total\", 'ABC', E'X\\nY' AS t -- KEEP\n FROM T WHERE f($$ABC$$) AND g($q$DEF$q$)"+ @?= "select \"Total\", 'ABC', E'X\\nY' as t -- KEEP\n from t where f($$ABC$$) and g($q$DEF$q$)" , testCase "replaceInCode leaves dollar-quoted bodies alone" $ Sql.replaceInCode "concurrently " "" "create index concurrently i on t (x) where f($$concurrently $$)" @?= "create index i on t (x) where f($$concurrently $$)"