-
-
Notifications
You must be signed in to change notification settings - Fork 39
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
Ability to discover PostgreSQL enums #24
Merged
Merged
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
fcbff8f
Correct type from Alias::new("ploygon") to Alias::new("polygon")
charleschege a2b063a
Add type
charleschege 57e5a41
Add support for PostgreSQL types
charleschege 7e36665
Add tests for checking the correctness of PostgreSQL enum parsing and…
charleschege d2016a1
Gate `PgEnum` and its impl block under the `sqlx` feature
charleschege d70fe56
Add support for fetching enums
charleschege c41f23d
Merge branch 'SeaQL:master' into master
charleschege b57b8dd
Move types into types files under postgres::def::Type
charleschege a8d9b4d
Add method to parse a raw SQL create query for enum creation
charleschege beabb80
Test case to discover all the enums in a given database schema
charleschege 8be4741
Solve issues with github actions not detecting types
charleschege ec076a7
Add feature flag for new enum type and associated functions
charleschege c630069
Switch to using re-exported sqlx types within sea-schema
charleschege 30bb0b4
Fix CI errors
billy1624 0e83d2a
Support enums with crazy names that include special characters
charleschege 134674c
Add support for ordered Vecs
charleschege d481ed1
Add tests for discovery of enums
charleschege dbb03b0
Ensure a custom enum type is dropped first from the database so that …
charleschege 2e68190
Refactoring
billy1624 48b9ff3
Ensures the writer derives the enum name from the EnumDef
charleschege 689fe9b
Refactoring
billy1624 172f2a3
Refactoring
billy1624 3704da4
Refactoring
billy1624 37a452c
Testing enum column...
billy1624 2aac450
Refactoring
billy1624 35879c6
Refactoring
billy1624 d2ce208
Refactoring
billy1624 c6e9912
Fix test
billy1624 3b3a49d
Refactoring
billy1624 4833b9e
Refactoring
billy1624 30ae58b
Refactoring
billy1624 9c3ffc2
Cleanup
billy1624 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use super::SchemaQueryBuilder; | ||
use crate::sqlx_types::postgres::PgRow; | ||
use sea_query::{Expr, Order, Query, SelectStatement}; | ||
|
||
#[derive(Debug, sea_query::Iden)] | ||
pub enum PgType { | ||
#[iden = "pg_type"] | ||
Table, | ||
#[iden = "typname"] | ||
TypeName, | ||
#[iden = "oid"] | ||
Oid, | ||
} | ||
|
||
#[derive(Debug, sea_query::Iden)] | ||
pub enum PgEnum { | ||
#[iden = "pg_enum"] | ||
Table, | ||
#[iden = "enumlabel"] | ||
EnumLabel, | ||
#[iden = "enumtypid"] | ||
EnumTypeId, | ||
} | ||
|
||
#[derive(Debug, Default)] | ||
pub struct EnumQueryResult { | ||
pub typename: String, | ||
pub enumlabel: String, | ||
} | ||
|
||
impl SchemaQueryBuilder { | ||
pub fn query_enums(&self) -> SelectStatement { | ||
Query::select() | ||
.column((PgType::Table, PgType::TypeName)) | ||
.column((PgEnum::Table, PgEnum::EnumLabel)) | ||
.from(PgType::Table) | ||
.inner_join( | ||
PgEnum::Table, | ||
Expr::tbl(PgEnum::Table, PgEnum::EnumTypeId).equals(PgType::Table, PgType::Oid), | ||
) | ||
.order_by((PgType::Table, PgType::TypeName), Order::Asc) | ||
.order_by((PgEnum::Table, PgEnum::EnumLabel), Order::Asc) | ||
.take() | ||
} | ||
} | ||
|
||
#[cfg(feature = "sqlx-postgres")] | ||
impl From<&PgRow> for EnumQueryResult { | ||
fn from(row: &PgRow) -> Self { | ||
use crate::sqlx_types::Row; | ||
Self { | ||
typename: row.get(0), | ||
enumlabel: row.get(1), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(not(feature = "sqlx-postgres"))] | ||
impl From<&PgRow> for EnumQueryResult { | ||
fn from(row: &PgRow) -> Self { | ||
Self::default() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
pub mod char_set; | ||
pub mod column; | ||
pub mod constraints; | ||
pub mod enumeration; | ||
pub mod schema; | ||
pub mod table; | ||
|
||
pub use char_set::*; | ||
pub use column::*; | ||
pub use constraints::*; | ||
pub use enumeration::*; | ||
pub use schema::*; | ||
pub use table::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use crate::postgres::def::EnumDef; | ||
use sea_query::{ | ||
extension::postgres::{Type, TypeCreateStatement}, | ||
Alias, | ||
}; | ||
|
||
impl EnumDef { | ||
/// Converts the [EnumDef] to a [TypeCreateStatement] | ||
pub fn write(&self) -> TypeCreateStatement { | ||
Type::create() | ||
.as_enum(Alias::new(self.typename.as_str())) | ||
.values(self.values.iter().map(|val| Alias::new(val.as_str()))) | ||
.to_owned() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,8 @@ use std::collections::HashMap; | |
|
||
use sea_schema::postgres::{def::TableDef, discovery::SchemaDiscovery}; | ||
use sea_schema::sea_query::{ | ||
Alias, ColumnDef, ForeignKey, ForeignKeyAction, Index, PostgresQueryBuilder, Table, | ||
TableCreateStatement, | ||
extension::postgres::Type, Alias, ColumnDef, ForeignKey, ForeignKeyAction, Index, | ||
PostgresQueryBuilder, Table, TableCreateStatement, | ||
}; | ||
use sqlx::{PgPool, Pool, Postgres}; | ||
|
||
|
@@ -13,6 +13,22 @@ async fn main() { | |
let connection = setup("postgres://sea:sea@localhost", "sea-schema").await; | ||
let mut executor = connection.acquire().await.unwrap(); | ||
|
||
let create_enum_stmt = Type::create() | ||
.as_enum(Alias::new("crazy_enum")) | ||
.values(vec![ | ||
Alias::new("Astro0%00%8987,.!@#$%^&*()_-+=[]{}\\|.<>/? ``"), | ||
Alias::new("Biology"), | ||
Alias::new("Chemistry"), | ||
Alias::new("Math"), | ||
Alias::new("Physics"), | ||
]) | ||
.to_string(PostgresQueryBuilder); | ||
Comment on lines
+16
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The expected result should be in the same order, i.e. ascending order |
||
|
||
sqlx::query(&create_enum_stmt) | ||
.execute(&mut executor) | ||
.await | ||
.unwrap(); | ||
|
||
let tbl_create_stmts = vec![ | ||
create_bakery_table(), | ||
create_baker_table(), | ||
|
@@ -53,6 +69,20 @@ async fn main() { | |
println!(); | ||
assert_eq!(expected_sql, sql); | ||
} | ||
|
||
let enum_defs = schema_discovery.discover_enums().await; | ||
|
||
dbg!(&enum_defs); | ||
|
||
let enum_create_statements: Vec<String> = enum_defs | ||
.into_iter() | ||
.map(|enum_def| enum_def.write().to_string(PostgresQueryBuilder)) | ||
.collect(); | ||
|
||
dbg!(&create_enum_stmt); | ||
dbg!(&enum_create_statements); | ||
|
||
assert_eq!(create_enum_stmt, enum_create_statements[0]); | ||
} | ||
|
||
async fn setup(base_url: &str, db_name: &str) -> Pool<Postgres> { | ||
|
@@ -90,6 +120,7 @@ fn create_bakery_table() -> TableCreateStatement { | |
) | ||
.col(ColumnDef::new(Alias::new("name")).string()) | ||
.col(ColumnDef::new(Alias::new("profit_margin")).double()) | ||
.col(ColumnDef::new(Alias::new("crazy_enum_col")).custom(Alias::new("crazy_enum"))) | ||
billy1624 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.primary_key( | ||
Index::create() | ||
.primary() | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The generated
Vec<EnumDef>
will be in ascending order of typename and variants of it will also be in ascending order.