Skip to content

feat: add support for create publication #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
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
25 changes: 25 additions & 0 deletions crates/codegen/src/get_node_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,31 @@ fn custom_handlers(node: &Node) -> TokenStream {
tokens.push(TokenProperty::from(Token::Create));
tokens.push(TokenProperty::from(Token::Database));
},
"CreatePublicationStmt" => quote! {
tokens.push(TokenProperty::from(Token::Create));
tokens.push(TokenProperty::from(Token::Publication));
if n.for_all_tables {
tokens.push(TokenProperty::from(Token::For));
tokens.push(TokenProperty::from(Token::All));
tokens.push(TokenProperty::from(Token::Tables));
}
if let Some(n) = n.pubobjects.first() {
tokens.push(TokenProperty::from(Token::For));
if let Some(NodeEnum::PublicationObjSpec(n)) = &n.node {
match n.pubobjtype() {
protobuf::PublicationObjSpecType::PublicationobjTable => {
tokens.push(TokenProperty::from(Token::Table));
},
protobuf::PublicationObjSpecType::PublicationobjTablesInSchema => {
tokens.push(TokenProperty::from(Token::Tables));
tokens.push(TokenProperty::from(Token::InP));
tokens.push(TokenProperty::from(Token::Schema));
},
_ => panic!("Unknown CreatePublicationStmt {:#?}", n.pubobjtype())
}
}
}
},
_ => quote! {},
}
}
Expand Down
17 changes: 17 additions & 0 deletions crates/parser/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,4 +281,21 @@ mod tests {
],
)
}

#[test]
fn test_create_publication() {
test_get_node_properties(
"create publication sales_publication for tables in schema marketing;",
SyntaxKind::CreatePublicationStmt,
vec![
TokenProperty::from(SyntaxKind::Create),
TokenProperty::from(SyntaxKind::Publication),
TokenProperty::from(SyntaxKind::For),
TokenProperty::from(SyntaxKind::Tables),
TokenProperty::from(SyntaxKind::InP),
TokenProperty::from(SyntaxKind::Schema),
TokenProperty::from("sales_publication".to_string()),
],
)
}
}
7 changes: 7 additions & 0 deletions crates/parser/tests/data/statements/valid/0050.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE PUBLICATION mypublication FOR TABLE users, departments;
-- CREATE PUBLICATION active_departments FOR TABLE departments WHERE (active IS TRUE);
CREATE PUBLICATION alltables FOR ALL TABLES;
-- CREATE PUBLICATION insert_only FOR TABLE mydata WITH (publish = 'insert');
-- CREATE PUBLICATION production_publication FOR TABLE users, departments, TABLES IN SCHEMA production;
CREATE PUBLICATION sales_publication FOR TABLES IN SCHEMA marketing, sales;
-- CREATE PUBLICATION users_filtered FOR TABLE users (user_id, firstname);
39 changes: 39 additions & 0 deletions crates/parser/tests/snapshots/statements/valid/0050@1.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
source: crates/parser/tests/statement_parser_test.rs
description: "-- CREATE PUBLICATION mypublication FOR TABLE users, departments;\n-- CREATE PUBLICATION active_departments FOR TABLE departments WHERE (active IS TRUE);\nCREATE PUBLICATION alltables FOR ALL TABLES;"
---
Parse {
cst: SourceFile@0..197
SqlComment@0..65 "-- CREATE PUBLICATION ..."
Newline@65..66 "\n"
SqlComment@66..152 "-- CREATE PUBLICATION ..."
Newline@152..153 "\n"
CreatePublicationStmt@153..197
Create@153..159 "CREATE"
Whitespace@159..160 " "
Publication@160..171 "PUBLICATION"
Whitespace@171..172 " "
Ident@172..181 "alltables"
Whitespace@181..182 " "
For@182..185 "FOR"
Whitespace@185..186 " "
All@186..189 "ALL"
Whitespace@189..190 " "
Tables@190..196 "TABLES"
Ascii59@196..197 ";"
,
errors: [],
stmts: [
RawStmt {
stmt: CreatePublicationStmt(
CreatePublicationStmt {
pubname: "alltables",
options: [],
pubobjects: [],
for_all_tables: true,
},
),
range: 152..196,
},
],
}
74 changes: 74 additions & 0 deletions crates/parser/tests/snapshots/statements/valid/0050@2.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
source: crates/parser/tests/statement_parser_test.rs
description: "\n-- CREATE PUBLICATION insert_only FOR TABLE mydata WITH (publish = 'insert');\n-- CREATE PUBLICATION production_publication FOR TABLE users, departments, TABLES IN SCHEMA production;\nCREATE PUBLICATION sales_publication FOR TABLES IN SCHEMA marketing, sales;"
---
Parse {
cst: SourceFile@0..258
Newline@0..1 "\n"
SqlComment@1..78 "-- CREATE PUBLICATION ..."
Newline@78..79 "\n"
SqlComment@79..182 "-- CREATE PUBLICATION ..."
Newline@182..183 "\n"
CreatePublicationStmt@183..258
Create@183..189 "CREATE"
Whitespace@189..190 " "
Publication@190..201 "PUBLICATION"
Whitespace@201..202 " "
Ident@202..219 "sales_publication"
Whitespace@219..220 " "
For@220..223 "FOR"
Whitespace@223..224 " "
Tables@224..230 "TABLES"
Whitespace@230..231 " "
InP@231..233 "IN"
Whitespace@233..234 " "
Schema@234..240 "SCHEMA"
Whitespace@240..241 " "
PublicationObjSpec@241..250
Ident@241..250 "marketing"
Ascii44@250..251 ","
Whitespace@251..252 " "
PublicationObjSpec@252..257
Ident@252..257 "sales"
Ascii59@257..258 ";"
,
errors: [],
stmts: [
RawStmt {
stmt: CreatePublicationStmt(
CreatePublicationStmt {
pubname: "sales_publication",
options: [],
pubobjects: [
Node {
node: Some(
PublicationObjSpec(
PublicationObjSpec {
pubobjtype: PublicationobjTablesInSchema,
name: "marketing",
pubtable: None,
location: 58,
},
),
),
},
Node {
node: Some(
PublicationObjSpec(
PublicationObjSpec {
pubobjtype: PublicationobjTablesInSchema,
name: "sales",
pubtable: None,
location: 69,
},
),
),
},
],
for_all_tables: false,
},
),
range: 182..257,
},
],
}