diff --git a/psl/psl-core/src/builtin_connectors/sqlite_datamodel_connector.rs b/psl/psl-core/src/builtin_connectors/sqlite_datamodel_connector.rs index b995505b97e8..0393513858e9 100644 --- a/psl/psl-core/src/builtin_connectors/sqlite_datamodel_connector.rs +++ b/psl/psl-core/src/builtin_connectors/sqlite_datamodel_connector.rs @@ -33,7 +33,8 @@ pub const CAPABILITIES: ConnectorCapabilities = enumflags2::make_bitflags!(Conne Json | JsonFiltering | JsonFilteringJsonPath | - AdvancedJsonNullability + AdvancedJsonNullability | + Enums }); pub struct SqliteDatamodelConnector; diff --git a/schema-engine/connectors/sql-schema-connector/src/sql_schema_calculator.rs b/schema-engine/connectors/sql-schema-connector/src/sql_schema_calculator.rs index eb66f4ce5b78..d08bedabe0dd 100644 --- a/schema-engine/connectors/sql-schema-connector/src/sql_schema_calculator.rs +++ b/schema-engine/connectors/sql-schema-connector/src/sql_schema_calculator.rs @@ -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, diff --git a/schema-engine/connectors/sql-schema-connector/src/sql_schema_calculator/sql_schema_calculator_flavour.rs b/schema-engine/connectors/sql-schema-connector/src/sql_schema_calculator/sql_schema_calculator_flavour.rs index 2112e44fceed..eaecc07de17a 100644 --- a/schema-engine/connectors/sql-schema-connector/src/sql_schema_calculator/sql_schema_calculator_flavour.rs +++ b/schema-engine/connectors/sql-schema-connector/src/sql_schema_calculator/sql_schema_calculator_flavour.rs @@ -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 { + None + } + fn column_default_value_for_autoincrement(&self) -> Option { None } diff --git a/schema-engine/connectors/sql-schema-connector/src/sql_schema_calculator/sql_schema_calculator_flavour/mysql.rs b/schema-engine/connectors/sql-schema-connector/src/sql_schema_calculator/sql_schema_calculator_flavour/mysql.rs index 6ed0301bfb4a..f579fe7925fc 100644 --- a/schema-engine/connectors/sql-schema-connector/src/sql_schema_calculator/sql_schema_calculator_flavour/mysql.rs +++ b/schema-engine/connectors/sql-schema-connector/src/sql_schema_calculator/sql_schema_calculator_flavour/mysql.rs @@ -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<'_>) { @@ -23,4 +26,8 @@ impl SqlSchemaCalculatorFlavour for MysqlFlavour { } } } + + fn column_type_for_enum(&self, enm: EnumWalker<'_>, ctx: &Context<'_>) -> Option { + ctx.enum_ids.get(&enm.id).map(|id| sql::ColumnTypeFamily::Enum(*id)) + } } diff --git a/schema-engine/connectors/sql-schema-connector/src/sql_schema_calculator/sql_schema_calculator_flavour/postgres.rs b/schema-engine/connectors/sql-schema-connector/src/sql_schema_calculator/sql_schema_calculator_flavour/postgres.rs index 017f018733dc..1084930d6043 100644 --- a/schema-engine/connectors/sql-schema-connector/src/sql_schema_calculator/sql_schema_calculator_flavour/postgres.rs +++ b/schema-engine/connectors/sql-schema-connector/src/sql_schema_calculator/sql_schema_calculator_flavour/postgres.rs @@ -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}; @@ -29,6 +29,10 @@ impl SqlSchemaCalculatorFlavour for PostgresFlavour { } } + fn column_type_for_enum(&self, enm: EnumWalker<'_>, ctx: &Context<'_>) -> Option { + ctx.enum_ids.get(&enm.id).map(|id| sql::ColumnTypeFamily::Enum(*id)) + } + fn column_default_value_for_autoincrement(&self) -> Option { if self.is_cockroachdb() { Some(sql::DefaultValue::unique_rowid()) diff --git a/schema-engine/connectors/sql-schema-connector/src/sql_schema_calculator/sql_schema_calculator_flavour/sqlite.rs b/schema-engine/connectors/sql-schema-connector/src/sql_schema_calculator/sql_schema_calculator_flavour/sqlite.rs index 9e7695f926ff..3b8c2737beb1 100644 --- a/schema-engine/connectors/sql-schema-connector/src/sql_schema_calculator/sql_schema_calculator_flavour/sqlite.rs +++ b/schema-engine/connectors/sql-schema-connector/src/sql_schema_calculator/sql_schema_calculator_flavour/sqlite.rs @@ -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. @@ -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 { + Some(ColumnTypeFamily::String) + } } diff --git a/schema-engine/sql-introspection-tests/tests/enums/mod.rs b/schema-engine/sql-introspection-tests/tests/enums/mod.rs index 7308a444ae49..6ac02f66dcae 100644 --- a/schema-engine/sql-introspection-tests/tests/enums/mod.rs +++ b/schema-engine/sql-introspection-tests/tests/enums/mod.rs @@ -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(); @@ -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(); @@ -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(); diff --git a/schema-engine/sql-migration-tests/tests/migrations/enums.rs b/schema-engine/sql-migration-tests/tests/migrations/enums.rs index 26f3c70005e1..06488609d04f 100644 --- a/schema-engine/sql-migration-tests/tests/migrations/enums.rs +++ b/schema-engine/sql-migration-tests/tests/migrations/enums.rs @@ -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#" diff --git a/schema-engine/sql-schema-describer/src/lib.rs b/schema-engine/sql-schema-describer/src/lib.rs index 8f65c175b2a3..538014c0bd6e 100644 --- a/schema-engine/sql-schema-describer/src/lib.rs +++ b/schema-engine/sql-schema-describer/src/lib.rs @@ -654,7 +654,7 @@ pub enum ColumnTypeFamily { Json, /// UUID types. Uuid, - ///Enum + /// Enum Enum(EnumId), /// Unsupported Unsupported(String),