Skip to content

Commit

Permalink
feat: sqlite enums
Browse files Browse the repository at this point in the history
  • Loading branch information
jacek-prisma committed Dec 31, 2024
1 parent aef02d5 commit 07c0b50
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ pub const CAPABILITIES: ConnectorCapabilities = enumflags2::make_bitflags!(Conne
Json |
JsonFiltering |
JsonFilteringJsonPath |
AdvancedJsonNullability
AdvancedJsonNullability |
Enums
});

pub struct SqliteDatamodelConnector;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,9 @@ fn push_column_for_model_enum_scalar_field(
let column = sql::Column {
name: field.database_name().to_owned(),
tpe: sql::ColumnType::pure(
sql::ColumnTypeFamily::Enum(ctx.enum_ids[&r#enum.id]),
ctx.flavour
.column_type_for_enum(r#enum, ctx)
.expect("should have a column type for enum"),
column_arity(field.ast_field().arity),
),
auto_increment: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ use sql_schema_describer::{self as sql, ColumnArity, ColumnType, ColumnTypeFamil
pub(crate) trait SqlSchemaCalculatorFlavour {
fn calculate_enums(&self, _ctx: &mut super::Context<'_>) {}

fn column_type_for_enum(&self, _enm: EnumWalker<'_>, _ctx: &super::Context<'_>) -> Option<sql::ColumnTypeFamily> {
None
}

fn column_default_value_for_autoincrement(&self) -> Option<sql::DefaultValue> {
None
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use psl::parser_database::walkers::EnumWalker;

use super::{super::Context, SqlSchemaCalculatorFlavour};
use crate::flavour::MysqlFlavour;
use sql_schema_describer as sql;

impl SqlSchemaCalculatorFlavour for MysqlFlavour {
fn calculate_enums(&self, ctx: &mut Context<'_>) {
Expand All @@ -23,4 +26,8 @@ impl SqlSchemaCalculatorFlavour for MysqlFlavour {
}
}
}

fn column_type_for_enum(&self, enm: EnumWalker<'_>, ctx: &Context<'_>) -> Option<sql::ColumnTypeFamily> {
ctx.enum_ids.get(&enm.id).map(|id| sql::ColumnTypeFamily::Enum(*id))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use either::Either;
use psl::{
builtin_connectors::{cockroach_datamodel_connector::SequenceFunction, PostgresDatasourceProperties},
datamodel_connector::walker_ext_traits::IndexWalkerExt,
parser_database::{IndexAlgorithm, OperatorClass},
parser_database::{walkers::EnumWalker, IndexAlgorithm, OperatorClass},
};
use sql::postgres::DatabaseExtension;
use sql_schema_describer::{self as sql, postgres::PostgresSchemaExt};
Expand All @@ -29,6 +29,10 @@ impl SqlSchemaCalculatorFlavour for PostgresFlavour {
}
}

fn column_type_for_enum(&self, enm: EnumWalker<'_>, ctx: &Context<'_>) -> Option<sql::ColumnTypeFamily> {
ctx.enum_ids.get(&enm.id).map(|id| sql::ColumnTypeFamily::Enum(*id))
}

fn column_default_value_for_autoincrement(&self) -> Option<sql::DefaultValue> {
if self.is_cockroachdb() {
Some(sql::DefaultValue::unique_rowid())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::SqlSchemaCalculatorFlavour;
use crate::flavour::SqliteFlavour;
use crate::{flavour::SqliteFlavour, sql_schema_calculator::Context};
use psl::parser_database::{walkers::*, ScalarType};
use sql_schema_describer::ColumnTypeFamily;

impl SqlSchemaCalculatorFlavour for SqliteFlavour {
// Integer primary keys on SQLite are automatically assigned the rowid, which means they are automatically autoincrementing.
Expand All @@ -12,4 +13,8 @@ impl SqlSchemaCalculatorFlavour for SqliteFlavour {
.unwrap_or(false)
&& field.scalar_type() == Some(ScalarType::Int)
}

fn column_type_for_enum(&self, _enm: EnumWalker<'_>, _ctx: &Context<'_>) -> Option<ColumnTypeFamily> {
Some(ColumnTypeFamily::String)
}
}
6 changes: 3 additions & 3 deletions schema-engine/sql-introspection-tests/tests/enums/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use quaint::prelude::Queryable;
use sql_introspection_tests::{test_api::*, TestResult};
use test_macros::test_connector;

#[test_connector(exclude(CockroachDb), capabilities(Enums))]
#[test_connector(exclude(CockroachDb, Sqlite), capabilities(Enums))]
async fn a_table_with_enums(api: &mut TestApi) -> TestResult {
let sql_family = api.sql_family();

Expand Down Expand Up @@ -67,7 +67,7 @@ async fn a_table_with_enums(api: &mut TestApi) -> TestResult {
Ok(())
}

#[test_connector(exclude(CockroachDb), capabilities(Enums))]
#[test_connector(exclude(CockroachDb, Sqlite), capabilities(Enums))]
async fn a_table_enums_should_return_alphabetically_even_when_in_different_order(api: &mut TestApi) -> TestResult {
let sql_family = api.sql_family();

Expand Down Expand Up @@ -129,7 +129,7 @@ async fn a_table_enums_should_return_alphabetically_even_when_in_different_order
Ok(())
}

#[test_connector(exclude(CockroachDb), capabilities(Enums))]
#[test_connector(exclude(CockroachDb, Sqlite), capabilities(Enums))]
async fn a_table_with_enum_default_values(api: &mut TestApi) -> TestResult {
let sql_family = api.sql_family();

Expand Down
28 changes: 28 additions & 0 deletions schema-engine/sql-migration-tests/tests/migrations/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,34 @@ fn changing_all_values_of_enums_used_in_defaults_works(api: TestApi) {
api.schema_push_w_datasource(dm2).force(true).send().assert_no_steps();
}

#[test_connector(tags(Sqlite))]
fn sqlite_enums_are_picked_up(api: TestApi) {
let sql = r#"
CREATE TABLE "Band" (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
genre TEXT NOT NULL
);
"#;

api.raw_cmd(sql);

let dm = r#"
enum Genre {
SKA
PUNK
}
model Band {
id Int @id @default(autoincrement())
name String
genre Genre
}
"#;

api.schema_push_w_datasource(dm).send().assert_green().assert_no_steps();
}

#[test_connector(tags(Postgres))]
fn existing_enums_are_picked_up(api: TestApi) {
let sql = r#"
Expand Down
2 changes: 1 addition & 1 deletion schema-engine/sql-schema-describer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ pub enum ColumnTypeFamily {
Json,
/// UUID types.
Uuid,
///Enum
/// Enum
Enum(EnumId),
/// Unsupported
Unsupported(String),
Expand Down

0 comments on commit 07c0b50

Please sign in to comment.