Skip to content

Commit

Permalink
Allow setting up the table and schema name for postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
czocher committed Nov 16, 2023
1 parent bb06fd2 commit 62aecdd
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions sqlx-store/src/postgres_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,36 @@ impl PostgresStore {
}
}

/// Set the session table schema name with the provided name.
pub fn with_schema_name(mut self, schema_name: impl AsRef<str>) -> Result<Self, String> {
let schema_name = schema_name.as_ref();
if !is_valid_identifier(schema_name) {
return Err(format!(
"Invalid schema name '{}'. Schema names must start with a letter or underscore (including letters with diacritical marks and non-Latin letters).\
Subsequent characters can be letters, underscores, digits (0-9), or dollar signs ($).",
schema_name
));
}

self.schema_name = schema_name.to_owned();
Ok(self)
}

/// Set the session table name with the provided name.
pub fn with_table_name(mut self, table_name: impl AsRef<str>) -> Result<Self, String> {
let table_name = table_name.as_ref();
if !is_valid_identifier(table_name) {
return Err(format!(
"Invalid table name '{}'. Table names must start with a letter or underscore (including letters with diacritical marks and non-Latin letters).\
Subsequent characters can be letters, underscores, digits (0-9), or dollar signs ($).",
table_name
));
}

self.table_name = table_name.to_owned();
Ok(self)
}

/// Migrate the session schema.
///
/// # Examples
Expand Down Expand Up @@ -169,3 +199,18 @@ impl SessionStore for PostgresStore {
Ok(())
}
}

/// A valid PostreSQL identifier must start with a letter or underscore (including letters with diacritical marks and non-Latin letters).
/// Subsequent characters in an identifier or key word can be letters, underscores, digits (0-9), or dollar signs ($).
/// See https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS for details.
fn is_valid_identifier(name: &str) -> bool {
!name.is_empty()
&& name

Check warning on line 208 in sqlx-store/src/postgres_store.rs

View check run for this annotation

Codecov / codecov/patch

sqlx-store/src/postgres_store.rs#L206-L208

Added lines #L206 - L208 were not covered by tests
.chars()
.next()
.map(|c| c.is_alphabetic() || c == '_')

Check warning on line 211 in sqlx-store/src/postgres_store.rs

View check run for this annotation

Codecov / codecov/patch

sqlx-store/src/postgres_store.rs#L211

Added line #L211 was not covered by tests
.unwrap_or_default()
&& name

Check warning on line 213 in sqlx-store/src/postgres_store.rs

View check run for this annotation

Codecov / codecov/patch

sqlx-store/src/postgres_store.rs#L213

Added line #L213 was not covered by tests
.chars()
.all(|c| c.is_alphanumeric() || c == '_' || c == '$')

Check warning on line 215 in sqlx-store/src/postgres_store.rs

View check run for this annotation

Codecov / codecov/patch

sqlx-store/src/postgres_store.rs#L215

Added line #L215 was not covered by tests
}

0 comments on commit 62aecdd

Please sign in to comment.