use crate::parser::SqlDialect;
use crate::schema::{Column, Schema, TableSchema};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum SqlTypeFamily {
Integer,
BigInteger,
Decimal,
Boolean,
Text,
Bytes,
Uuid,
DateTime,
Json,
Other,
}
impl SqlTypeFamily {
fn from_column(column: &Column) -> Self {
use crate::schema::ColumnType;
match &column.col_type {
ColumnType::Int => SqlTypeFamily::Integer,
ColumnType::BigInt => SqlTypeFamily::BigInteger,
ColumnType::Decimal => SqlTypeFamily::Decimal,
ColumnType::Bool => SqlTypeFamily::Boolean,
ColumnType::Text => SqlTypeFamily::Text,
ColumnType::Uuid => SqlTypeFamily::Uuid,
ColumnType::DateTime => SqlTypeFamily::DateTime,
ColumnType::Other(_) => Self::from_unclassified_source_type(&column.source_type),
}
}
fn from_source_type(source_type: &str) -> Self {
use crate::schema::ColumnType;
match ColumnType::from_sql_type(source_type) {
ColumnType::Int => SqlTypeFamily::Integer,
ColumnType::BigInt => SqlTypeFamily::BigInteger,
ColumnType::Decimal => SqlTypeFamily::Decimal,
ColumnType::Bool => SqlTypeFamily::Boolean,
ColumnType::Text => SqlTypeFamily::Text,
ColumnType::Uuid => SqlTypeFamily::Uuid,
ColumnType::DateTime => SqlTypeFamily::DateTime,
ColumnType::Other(_) => Self::from_unclassified_source_type(source_type),
}
}
fn from_unclassified_source_type(source_type: &str) -> Self {
let lower = source_type.to_lowercase();
if lower.contains("json") {
SqlTypeFamily::Json
} else if lower.contains("blob") || lower.contains("binary") || lower.contains("bytea") {
SqlTypeFamily::Bytes
} else {
SqlTypeFamily::Other
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(try_from = "PortableColumnInput")]
pub struct PortableColumn {
pub name: String,
pub source_type: String,
pub family: SqlTypeFamily,
pub nullable: bool,
#[serde(default)]
pub primary_key: bool,
#[serde(default)]
pub unique: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub default_sql: Option<String>,
#[serde(default)]
pub generated: bool,
#[serde(default)]
pub identity: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub collation: Option<String>,
}
impl JsonSchema for PortableColumn {
fn schema_name() -> std::borrow::Cow<'static, str> {
"PortableColumn".into()
}
fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
let family_schema = generator.subschema_for::<SqlTypeFamily>();
schemars::json_schema!({
"type": "object",
"properties": {
"name": { "type": "string" },
"source_type": { "type": "string" },
"type": {
"type": "string",
"description": "Shorthand alias for `source_type`; `family` is derived automatically when omitted."
},
"family": family_schema,
"nullable": { "type": "boolean" },
"primary_key": { "type": "boolean" },
"unique": { "type": "boolean" },
"default_sql": { "type": ["string", "null"] },
"generated": { "type": "boolean" },
"identity": { "type": "boolean" },
"collation": { "type": ["string", "null"] }
},
"required": ["name", "nullable"],
"anyOf": [
{ "required": ["source_type"] },
{ "required": ["type"] }
],
"additionalProperties": false
})
}
}
impl PortableColumn {
fn from_runtime(column: &Column) -> Self {
Self {
name: column.name.clone(),
source_type: column.source_type.clone(),
family: SqlTypeFamily::from_column(column),
nullable: column.is_nullable,
primary_key: column.is_primary_key,
unique: column.is_unique,
default_sql: column.default_sql.clone(),
generated: column.is_generated,
identity: column.is_identity,
collation: column.collation.clone(),
}
}
}
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct PortableColumnInput {
name: String,
#[serde(alias = "type")]
source_type: String,
#[serde(default)]
family: Option<SqlTypeFamily>,
nullable: bool,
#[serde(default)]
primary_key: bool,
#[serde(default)]
unique: bool,
#[serde(default)]
default_sql: Option<String>,
#[serde(default)]
generated: bool,
#[serde(default)]
identity: bool,
#[serde(default)]
collation: Option<String>,
}
impl From<PortableColumnInput> for PortableColumn {
fn from(input: PortableColumnInput) -> Self {
let family = input
.family
.unwrap_or_else(|| SqlTypeFamily::from_source_type(&input.source_type));
Self {
name: input.name,
source_type: input.source_type,
family,
nullable: input.nullable,
primary_key: input.primary_key,
unique: input.unique,
default_sql: input.default_sql,
generated: input.generated,
identity: input.identity,
collation: input.collation,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct PortableUniqueConstraint {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
pub columns: Vec<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct PortableCheckConstraint {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
pub expression: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct PortableIndex {
pub name: String,
pub columns: Vec<String>,
#[serde(default)]
pub unique: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub index_type: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct PortableRelationship {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
pub columns: Vec<String>,
pub referenced_table: String,
pub referenced_columns: Vec<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct PortableTable {
pub name: String,
pub columns: Vec<PortableColumn>,
#[serde(default)]
pub primary_key: Vec<String>,
#[serde(default)]
pub unique_constraints: Vec<PortableUniqueConstraint>,
#[serde(default)]
pub check_constraints: Vec<PortableCheckConstraint>,
#[serde(default)]
pub indexes: Vec<PortableIndex>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub create_statement: Option<String>,
#[serde(default)]
pub relationships: Vec<PortableRelationship>,
}
impl PortableTable {
fn from_runtime(table: &TableSchema) -> Self {
let columns: Vec<PortableColumn> = table
.columns
.iter()
.map(PortableColumn::from_runtime)
.collect();
let primary_key = table
.primary_key
.iter()
.filter_map(|&id| table.column(id))
.map(|c| c.name.clone())
.collect();
let unique_constraints = table
.unique_constraints
.iter()
.map(|uc| PortableUniqueConstraint {
name: uc.name.clone(),
columns: uc.columns.clone(),
})
.collect();
let check_constraints = table
.check_constraints
.iter()
.map(|cc| PortableCheckConstraint {
name: cc.name.clone(),
expression: cc.expression.clone(),
})
.collect();
let indexes = table
.indexes
.iter()
.map(|idx| PortableIndex {
name: idx.name.clone(),
columns: idx.columns.clone(),
unique: idx.is_unique,
index_type: idx.index_type.clone(),
})
.collect();
let relationships = table
.foreign_keys
.iter()
.map(|fk| PortableRelationship {
name: fk.name.clone(),
columns: fk.column_names.clone(),
referenced_table: fk.referenced_table.clone(),
referenced_columns: fk.referenced_columns.clone(),
})
.collect();
Self {
name: table.name.clone(),
columns,
primary_key,
unique_constraints,
check_constraints,
indexes,
create_statement: table.create_statement.clone(),
relationships,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct PortableSchema {
pub dialect: String,
pub tables: BTreeMap<String, PortableTable>,
}
impl PortableSchema {
pub fn from_runtime(schema: &Schema, dialect: SqlDialect) -> Self {
let tables = schema
.iter()
.map(|table| (table.name.clone(), PortableTable::from_runtime(table)))
.collect();
Self {
dialect: dialect.to_string(),
tables,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn portable_column_accepts_type_shorthand_and_derives_family() {
let yaml = "{ name: id, type: bigint, nullable: false, primary_key: true }";
let column: PortableColumn = serde_yaml_ng::from_str(yaml).unwrap();
assert_eq!(column.source_type, "bigint");
assert_eq!(column.family, SqlTypeFamily::BigInteger);
assert!(column.primary_key);
}
#[test]
fn portable_column_canonical_form_round_trips() {
let column = PortableColumn {
name: "id".to_string(),
source_type: "bigint".to_string(),
family: SqlTypeFamily::BigInteger,
nullable: false,
primary_key: true,
unique: false,
default_sql: None,
generated: false,
identity: false,
collation: None,
};
let rendered = serde_yaml_ng::to_string(&column).unwrap();
assert!(rendered.contains("source_type: bigint"));
assert!(rendered.contains("family: big_integer"));
assert!(!rendered.contains("\ntype:"));
let reparsed: PortableColumn = serde_yaml_ng::from_str(&rendered).unwrap();
assert_eq!(reparsed, column);
}
#[test]
fn portable_column_rejects_unknown_fields_even_with_shorthand() {
let yaml = "{ name: id, type: bigint, nullable: false, bogus: true }";
let err = serde_yaml_ng::from_str::<PortableColumn>(yaml).unwrap_err();
assert!(err.to_string().contains("unknown field"));
}
}