Skip to content

Commit

Permalink
feat: Add non-pk versions of uuid schema helper methods (#296)
Browse files Browse the repository at this point in the history
Closes #295
  • Loading branch information
spencewenski authored Jul 26, 2024
1 parent f06bdcc commit 0ca8553
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 16 deletions.
90 changes: 80 additions & 10 deletions src/migration/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,31 +127,74 @@ pub fn pk_uuid<T>(name: T) -> ColumnDef
where
T: IntoIden,
{
ColumnDef::new(name).uuid().primary_key().to_owned()
uuid(name).primary_key().to_owned()
}

/// Create a column using [Uuid][sea_orm::sea_query::ColumnType::Uuid] as the column
/// type. A new v4 UUID will be generated as the default if no value is provided by the application.
///
/// Note: This requires that your database supports generating v4 UUIDs using a method named
/// `uuid_generate_v4()`.
pub fn uuid_v4<T>(name: T) -> ColumnDef
where
T: IntoIden,
{
uuid_default(name, Expr::cust("uuid_generate_v4()"))
}

#[deprecated(since = "0.5.11", note = "Renamed as `pk_uuid_v4`.")]
pub use pk_uuid_v4 as pk_uuidv4;

/// Create a primary key column using [Uuid][sea_orm::sea_query::ColumnType::Uuid] as the column
/// type. A new v4 UUID will be generated as the default if no value is provided by the application.
///
/// Note: This requires that your database supports generating v4 UUIDs using a method named
/// `uuid_generate_v4()`.
pub fn pk_uuidv4<T>(name: T) -> ColumnDef
pub fn pk_uuid_v4<T>(name: T) -> ColumnDef
where
T: IntoIden,
{
uuid_v4(name).primary_key().to_owned()
}

/// Create a column using [Uuid][sea_orm::sea_query::ColumnType::Uuid] as the column
/// type. A new v7 UUID will be generated as the default if no value is provided by the application.
///
/// Note: This requires that your database supports generating v7 UUIDs using a method named
/// `uuid_generate_v7()`.
pub fn uuid_v7<T>(name: T) -> ColumnDef
where
T: IntoIden,
{
pk_uuid_default(name, Expr::cust("uuid_generate_v4()"))
uuid_default(name, Expr::cust("uuid_generate_v7()"))
}

#[deprecated(since = "0.5.11", note = "Renamed as `pk_uuid_v7`.")]
pub use pk_uuid_v7 as pk_uuidv7;

/// Create a primary key column using [Uuid][sea_orm::sea_query::ColumnType::Uuid] as the column
/// type. A new v7 UUID will be generated as the default if no value is provided by the application.
///
/// Note: This requires that your database supports generating v7 UUIDs using a method named
/// `uuid_generate_v7()`.
pub fn pk_uuidv7<T>(name: T) -> ColumnDef
pub fn pk_uuid_v7<T>(name: T) -> ColumnDef
where
T: IntoIden,
{
pk_uuid_default(name, Expr::cust("uuid_generate_v7()"))
uuid_v7(name).primary_key().to_owned()
}

/// Create a column using [Uuid][sea_orm::sea_query::ColumnType::Uuid] as the column
/// type.
///
/// Provide a `default` expression in order to define how a default value is generated if no value
/// is not provided by the application.
pub fn uuid_default<T, D>(name: T, default: D) -> ColumnDef
where
T: IntoIden,
D: Into<SimpleExpr>,
{
uuid(name).default(default).to_owned()
}

/// Create a primary key column using [Uuid][sea_orm::sea_query::ColumnType::Uuid] as the column
Expand All @@ -164,7 +207,7 @@ where
T: IntoIden,
D: Into<SimpleExpr>,
{
pk_uuid(name).default(default).to_owned()
uuid_default(name, default).primary_key().to_owned()
}

#[cfg(test)]
Expand Down Expand Up @@ -265,16 +308,43 @@ mod tests {

#[rstest]
#[cfg_attr(coverage_nightly, coverage(off))]
fn pk_uuidv4(_case: TestCase, mut table_stmt: TableCreateStatement) {
table_stmt.col(super::pk_uuidv4(Foo::Bar));
fn uuid_v4(_case: TestCase, mut table_stmt: TableCreateStatement) {
table_stmt.col(super::uuid_v4(Foo::Bar));

assert_snapshot!(table_stmt.to_string(PostgresQueryBuilder));
}

#[rstest]
#[cfg_attr(coverage_nightly, coverage(off))]
fn pk_uuidv7(_case: TestCase, mut table_stmt: TableCreateStatement) {
table_stmt.col(super::pk_uuidv7(Foo::Bar));
fn pk_uuid_v4(_case: TestCase, mut table_stmt: TableCreateStatement) {
table_stmt.col(super::pk_uuid_v4(Foo::Bar));

assert_snapshot!(table_stmt.to_string(PostgresQueryBuilder));
}

#[rstest]
#[cfg_attr(coverage_nightly, coverage(off))]
fn uuid_v7(_case: TestCase, mut table_stmt: TableCreateStatement) {
table_stmt.col(super::uuid_v7(Foo::Bar));

assert_snapshot!(table_stmt.to_string(PostgresQueryBuilder));
}

#[rstest]
#[cfg_attr(coverage_nightly, coverage(off))]
fn pk_uuid_v7(_case: TestCase, mut table_stmt: TableCreateStatement) {
table_stmt.col(super::pk_uuid_v7(Foo::Bar));

assert_snapshot!(table_stmt.to_string(PostgresQueryBuilder));
}

#[rstest]
#[cfg_attr(coverage_nightly, coverage(off))]
fn uuid_default(_case: TestCase, mut table_stmt: TableCreateStatement) {
table_stmt.col(super::uuid_default(
Foo::Bar,
Expr::cust("custom_uuid_fn()"),
));

assert_snapshot!(table_stmt.to_string(PostgresQueryBuilder));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
source: src/migration/schema.rs
expression: table_stmt.to_string(PostgresQueryBuilder)
---
CREATE TABLE "foo" ( "bar" uuid PRIMARY KEY )
CREATE TABLE "foo" ( "bar" uuid NOT NULL PRIMARY KEY )
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
source: src/migration/schema.rs
expression: table_stmt.to_string(PostgresQueryBuilder)
---
CREATE TABLE "foo" ( "bar" uuid PRIMARY KEY DEFAULT custom_uuid_fn() )
CREATE TABLE "foo" ( "bar" uuid NOT NULL DEFAULT custom_uuid_fn() PRIMARY KEY )
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/migration/schema.rs
expression: table_stmt.to_string(PostgresQueryBuilder)
---
CREATE TABLE "foo" ( "bar" uuid NOT NULL DEFAULT uuid_generate_v4() PRIMARY KEY )
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/migration/schema.rs
expression: table_stmt.to_string(PostgresQueryBuilder)
---
CREATE TABLE "foo" ( "bar" uuid NOT NULL DEFAULT uuid_generate_v7() PRIMARY KEY )
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
source: src/migration/schema.rs
expression: table_stmt.to_string(PostgresQueryBuilder)
---
CREATE TABLE "foo" ( "bar" uuid PRIMARY KEY DEFAULT uuid_generate_v4() )
CREATE TABLE "foo" ( "bar" uuid NOT NULL DEFAULT custom_uuid_fn() )
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
source: src/migration/schema.rs
expression: table_stmt.to_string(PostgresQueryBuilder)
---
CREATE TABLE "foo" ( "bar" uuid PRIMARY KEY DEFAULT uuid_generate_v7() )
CREATE TABLE "foo" ( "bar" uuid NOT NULL DEFAULT uuid_generate_v4() )
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/migration/schema.rs
expression: table_stmt.to_string(PostgresQueryBuilder)
---
CREATE TABLE "foo" ( "bar" uuid NOT NULL DEFAULT uuid_generate_v7() )
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: src/migration/user/create_table.rs
source: src/migration/user/create_and_drop_table.rs
expression: query.to_string(PostgresQueryBuilder)
---
CREATE TABLE IF NOT EXISTS "user" ( "created_at" timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP, "updated_at" timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP, "id" uuid PRIMARY KEY, "name" varchar NOT NULL CHECK (CHAR_LENGTH("name") > 0), "username" varchar NOT NULL UNIQUE CHECK (CHAR_LENGTH("username") > 0), "email" varchar NOT NULL UNIQUE CHECK (CHAR_LENGTH("email") > 0), "password" varchar NOT NULL )
CREATE TABLE IF NOT EXISTS "user" ( "created_at" timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP, "updated_at" timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP, "id" uuid NOT NULL PRIMARY KEY, "name" varchar NOT NULL CHECK (CHAR_LENGTH("name") > 0), "username" varchar NOT NULL UNIQUE CHECK (CHAR_LENGTH("username") > 0), "email" varchar NOT NULL UNIQUE CHECK (CHAR_LENGTH("email") > 0), "password" varchar NOT NULL )

0 comments on commit 0ca8553

Please sign in to comment.