Skip to content
Merged
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
23 changes: 23 additions & 0 deletions expected-schema/v16/schemas/public/tables/some_new_table/objrep
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"accessmethod": "heap",
"amoptions": null,
"columns_in_order": [],
"compositetype": null,
"forcerowsecurity": false,
"kind": "r",
"owner": "codd_admin",
"partition": false,
"persistence": "p",
"privileges": {
"codd_admin": [
[
"daxrtDw",
"codd_admin"
]
]
},
"replident": "d",
"rowsecurity": false,
"rowtype": "some_new_table",
"shared": false
}
1 change: 1 addition & 0 deletions scripts/wait-for-pg-ready.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

set -eo pipefail

PGCTLSTATUS=0
pg_ctl status -D "$PGDATA" || PGCTLSTATUS=$?

if [ "$PGCTLSTATUS" = "0" ]; then
Expand Down
3 changes: 2 additions & 1 deletion sql-migrations/all/2021-01-11-22-14-29-some-unique-index.sql
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
CREATE UNIQUE INDEX any_unique_employee_idx_will_do ON employee (employee_name);
\restrict pbgv1pF8SxQK6cuT7hwDi21uDYr8wpxKJ3wlLa9Zk5EIO1xBiu84SJQU8fL22PT
CREATE UNIQUE INDEX any_unique_employee_idx_will_do ON employee (employee_name);\unrestrict pbgv1pF8SxQK6cuT7hwDi21uDYr8wpxKJ3wlLa9Zk5EIO1xBiu84SJQU8fL22PT
4 changes: 3 additions & 1 deletion sql-migrations/all/2022-03-08-19-40-06-trivial-select.sql
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
SELECT 3+25;
\restrict TAXaYefQ7OaPsbhTIwM0eA6r8S102Jqiy0mRQfQXQQmIdA9fqI7q4LFmKpchNqQ
SELECT 3+25;
\unrestrict TAXaYefQ7OaPsbhTIwM0eA6r8S102Jqiy0mRQfQXQQmIdA9fqI7q4LFmKpchNqQ
9 changes: 9 additions & 0 deletions sql-migrations/all/2025-08-02-12-27-46-animals-view.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@


\restrict TAXaYefQ7OaPsbhTIwM0eA6r8S102Jqiy0mRQfQXQQmIdA9fqI7q4LFmKpchNqQ


CREATE VIEW animals_by_name AS
SELECT popular_name
FROM animals
ORDER BY popular_name;

\unrestrict TAXaYefQ7OaPsbhTIwM0eA6r8S102Jqiy0mRQfQXQQmIdA9fqI7q4LFmKpchNqQ

SELECT 4;
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--
-- PostgreSQL database dump
--

\restrict qNlLbo8p6RcFfsC9fMVyEoGtCxjry6twGZo5QYQG5mvVxeo6RoPOef7VkHrNGZs

-- Dumped from database version 16.10
-- Dumped by pg_dump version 16.10

-- This is a real pg dump just so we're certain the restrict and unrestrict things are
-- parsed correctly by codd.
-- We add a table here just for the sake of it.
CREATE TABLE some_new_table ();

--
-- PostgreSQL database dump complete
--

\unrestrict qNlLbo8p6RcFfsC9fMVyEoGtCxjry6twGZo5QYQG5mvVxeo6RoPOef7VkHrNGZs

2 changes: 2 additions & 0 deletions src/Codd/Internal/MultiQueryStatement.hs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ isCountableRunnable = \case
WhiteSpacePiece _ -> False
BeginTransaction _ -> True
CommitTransaction _ -> True
RestrictOrUnrestrictMetaCommand _ -> False
RollbackTransaction _ -> True
CopyFromStdinStatement _ -> False
CopyFromStdinRows _ -> False
Expand Down Expand Up @@ -220,6 +221,7 @@ runSingleStatementInternal_ ::
runSingleStatementInternal_ conn p = case p of
CommentPiece _ -> applied
WhiteSpacePiece _ -> applied
RestrictOrUnrestrictMetaCommand _ -> applied
BeginTransaction s -> singleStatement_ conn s
CommitTransaction s -> singleStatement_ conn s
RollbackTransaction s -> singleStatement_ conn s
Expand Down
11 changes: 10 additions & 1 deletion src/Codd/Parsing.hs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ hoistAddedSqlMigration f (AddedSqlMigration sqlMig tst) =
data SectionOption = OptInTxn | OptNoTxn | OptNoParse | OptRequiresCoddSchema
deriving stock (Eq, Ord, Show)

data SqlPiece = CommentPiece !Text | WhiteSpacePiece !Text | CopyFromStdinStatement !Text | CopyFromStdinRows !Text | CopyFromStdinEnd !Text | BeginTransaction !Text | RollbackTransaction !Text | CommitTransaction !Text | OtherSqlPiece !Text
data SqlPiece = CommentPiece !Text | WhiteSpacePiece !Text | CopyFromStdinStatement !Text | CopyFromStdinRows !Text | CopyFromStdinEnd !Text | BeginTransaction !Text | RollbackTransaction !Text | CommitTransaction !Text | RestrictOrUnrestrictMetaCommand !Text | OtherSqlPiece !Text
deriving stock (Show, Eq)

data ParsingException = ParsingException
Expand Down Expand Up @@ -360,6 +360,7 @@ sqlPieceText (BeginTransaction s) = s
sqlPieceText (RollbackTransaction s) = s
sqlPieceText (CommitTransaction s) = s
sqlPieceText (OtherSqlPiece s) = s
sqlPieceText (RestrictOrUnrestrictMetaCommand s) = s
sqlPieceText (CopyFromStdinStatement s) = s
sqlPieceText (CopyFromStdinRows s) = s
sqlPieceText (CopyFromStdinEnd s) = s
Expand All @@ -372,6 +373,7 @@ mapSqlPiece f = \case
RollbackTransaction s -> RollbackTransaction (f s)
CommitTransaction s -> CommitTransaction (f s)
OtherSqlPiece s -> OtherSqlPiece (f s)
RestrictOrUnrestrictMetaCommand s -> RestrictOrUnrestrictMetaCommand (f s)
CopyFromStdinStatement s -> CopyFromStdinStatement (f s)
CopyFromStdinRows s -> CopyFromStdinRows (f s)
CopyFromStdinEnd s -> CopyFromStdinEnd (f s)
Expand Down Expand Up @@ -404,6 +406,8 @@ sqlPieceParser parserState = case parserState of
<|> (,OutsideCopy)
. CommitTransaction
<$> commitTransactionParser
<|> (,OutsideCopy)
<$> restrictOrUnrestrictMetaCommandParser
<|> (,OutsideCopy)
. OtherSqlPiece
<$> anySqlPieceParser
Expand All @@ -421,6 +425,11 @@ sqlPieceParser parserState = case parserState of
commitTransactionParser =
spaceSeparatedTokensToParser
[CITextToken "COMMIT", AllUntilEndOfStatement]
restrictOrUnrestrictMetaCommandParser = do
r <- string "\\restrict " <|> string "\\unrestrict "
key <- Parsec.takeWhile1 Char.isAlphaNum
eolOrEof <- "" <$ endOfInput <|> eol
pure $ RestrictOrUnrestrictMetaCommand $ r <> key <> eolOrEof
anySqlPieceParser = do
arbText <- spaceSeparatedTokensToParser [AllUntilEndOfStatement]
when (arbText == "") $ fail "Please report this as a bug in codd: trying to parse empty string as SQL piece"
Expand Down
5 changes: 4 additions & 1 deletion test/ParsingSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,12 @@ genSql onlySyntacticallyValid = do
where
emptyLineGen = pure "\n"
bizarreLineGen = (<> "\n") . Text.pack . getUnicodeString <$> arbitrary
restrictUnrestrictLineGen = elements ["\\restrict pbgv1pF8SxQK6cuT7hwDi21uDYr8wpxKJ3wlLa9Zk5EIO1xBiu84SJQU8fL22PT\n", "\\unrestrict pbgv1pF8SxQK6cuT7hwDi21uDYr8wpxKJ3wlLa9Zk5EIO1xBiu84SJQU8fL22PT\n"]
lineGen =
frequency
[ (if onlySyntacticallyValid then 0 else 1, bizarreLineGen),
(1, emptyLineGen),
(1, restrictUnrestrictLineGen),
(5, piecesToText <$> genSingleSqlStatement)
]
-- Note: the likelihood that QuickCheck will randomly generate text that has a line starting with "-- codd:"
Expand Down Expand Up @@ -555,7 +557,7 @@ spec = do
`shouldBe` groupCopyRows (mconcat origPieces)
modifyMaxSuccess (const 10000)
$ it
"Statements concatenation matches original and statements end with semi-colon"
"Statements concatenation matches original and some assertions on parsed statements"
$ do
property $ \SyntacticallyValidRandomSql {..} -> do
blks <-
Expand All @@ -577,6 +579,7 @@ spec = do
WhiteSpacePiece t ->
t `shouldSatisfy` (\c -> Text.strip c == "")
CopyFromStdinEnd ll -> ll `shouldBe` "\\.\n"
RestrictOrUnrestrictMetaCommand t -> t `shouldSatisfy` (\c -> "\\restrict " `Text.isPrefixOf` c || "\\unrestrict " `Text.isPrefixOf` c)
_ -> pure ()

modifyMaxSuccess (const 10000) $
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
-- codd: no-txn
\restrict TAXaYefQ7OaPsbhTIwM0eA6r8S102Jqiy0mRQfQXQQmIdA9fqI7q4LFmKpchNqQ
CREATE TABLE somedata (id INT NOT NULL, UNIQUE(id));

COPY somedata FROM STDIN WITH (FORMAT csv);
1
2
3
\.
\unrestrict TAXaYefQ7OaPsbhTIwM0eA6r8S102Jqiy0mRQfQXQQmIdA9fqI7q4LFmKpchNqQ
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
-- codd: no-txn
\restrict TAXaYefQ7OaPsbhTIwM0eA6r8S102Jqiy0mRQfQXQQmIdA9fqI7q4LFmKpchNqQ
CREATE TABLE somedata (id INT NOT NULL, UNIQUE(id) DEFERRABLE INITIALLY DEFERRED);

COPY somedata FROM STDIN WITH (FORMAT csv);
1
2
3
\.
\unrestrict TAXaYefQ7OaPsbhTIwM0eA6r8S102Jqiy0mRQfQXQQmIdA9fqI7q4LFmKpchNqQ
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ COPY somedata FROM STDIN WITH (FORMAT csv);
6
\.

\restrict TAXaYefQ7OaPsbhTIwM0eA6r8S102Jqiy0mRQfQXQQmIdA9fqI7q4LFmKpchNqQ
-- Another comment

SELECT 7;
BEGIN;
SELECT 3;
CREATE TABLE othertablenotexists();
\unrestrict TAXaYefQ7OaPsbhTIwM0eA6r8S102Jqiy0mRQfQXQQmIdA9fqI7q4LFmKpchNqQ
-- Yet another one
COPY somedata FROM STDIN WITH (FORMAT csv);
1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
-- codd: no-txn
CREATE TABLE somedata (id INT NOT NULL, UNIQUE(id));

\restrict TAXaYefQ7OaPsbhTIwM0eA6r8S102Jqiy0mRQfQXQQmIdA9fqI7q4LFmKpchNqQ
COPY somedata FROM STDIN WITH (FORMAT csv);
1
2
3
\.
\unrestrict TAXaYefQ7OaPsbhTIwM0eA6r8S102Jqiy0mRQfQXQQmIdA9fqI7q4LFmKpchNqQ