use super::ddl::SqliteEntity;
use crate::snapshot::{Snapshot, SnapshotEntity};
use crate::version::SQLITE_SNAPSHOT_VERSION;
use serde::{Deserialize, Serialize};
impl SnapshotEntity for SqliteEntity {
const DIALECT: &'static str = "sqlite";
const SNAPSHOT_VERSION: &'static str = SQLITE_SNAPSHOT_VERSION;
}
pub type SQLiteSnapshot = Snapshot<SqliteEntity>;
use super::ddl::{Table, View};
use std::collections::HashMap;
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
pub struct Meta {
#[serde(default)]
pub tables: HashMap<String, String>,
#[serde(default)]
pub columns: HashMap<String, String>,
}
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
pub struct Internal {
#[serde(default)]
pub indexes: HashMap<String, IndexInternal>,
}
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
pub struct IndexInternal {
#[serde(default)]
pub columns: HashMap<String, ColumnInternal>,
}
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
#[serde(rename_all = "camelCase")]
pub struct ColumnInternal {
#[serde(skip_serializing_if = "Option::is_none")]
pub is_expression: Option<bool>,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct SQLiteSnapshotV6 {
pub version: String,
pub dialect: String,
pub id: String,
pub prev_id: String,
pub tables: HashMap<String, Table>,
#[serde(default)]
pub views: HashMap<String, View>,
#[serde(default)]
pub enums: HashMap<String, serde_json::Value>,
#[serde(rename = "_meta")]
pub meta: Meta,
#[serde(skip_serializing_if = "Option::is_none")]
pub internal: Option<Internal>,
}
#[cfg(test)]
mod tests {
use super::*;
use crate::sqlite::ddl::{Column, Table};
#[test]
fn test_new_snapshot() {
let snapshot = SQLiteSnapshot::new();
assert_eq!(snapshot.version, "7");
assert_eq!(snapshot.dialect, "sqlite");
assert_eq!(snapshot.prev_ids[0], crate::ORIGIN_UUID);
assert!(snapshot.ddl.is_empty());
}
#[test]
fn test_add_entity() {
let mut snapshot = SQLiteSnapshot::new();
let table = Table::new("users");
snapshot.add_entity(SqliteEntity::Table(table));
let id_col = Column::new("users", "id", "integer")
.not_null()
.autoincrement();
let name_col = Column::new("users", "name", "text").not_null();
snapshot.add_entity(SqliteEntity::Column(id_col));
snapshot.add_entity(SqliteEntity::Column(name_col));
assert_eq!(snapshot.ddl.len(), 3);
}
#[test]
fn test_snapshot_serialization() {
let mut snapshot = SQLiteSnapshot::new();
let table = Table::new("users");
snapshot.add_entity(SqliteEntity::Table(table));
let col = Column::new("users", "id", "integer").not_null();
snapshot.add_entity(SqliteEntity::Column(col));
let json = snapshot.to_json().unwrap();
let parsed = SQLiteSnapshot::from_json(&json).unwrap();
assert_eq!(parsed.version, "7");
assert_eq!(parsed.dialect, "sqlite");
assert_eq!(parsed.ddl.len(), 2);
let value: serde_json::Value = serde_json::from_str(&json).unwrap();
assert_eq!(value["version"], "7");
assert_eq!(value["dialect"], "sqlite");
assert_eq!(value["ddl"][0]["entityType"], "tables");
assert_eq!(value["ddl"][1]["entityType"], "columns");
}
#[test]
fn test_column_json_format_matches_drizzle_kit() {
let col = Column::new("users", "id", "integer")
.not_null()
.autoincrement();
let value: serde_json::Value = serde_json::to_value(&col).unwrap();
assert_eq!(value["autoincrement"], serde_json::json!(true));
assert_eq!(value["notNull"], serde_json::json!(true));
assert_eq!(value["type"], "integer");
assert_eq!(value["table"], "users");
assert_eq!(value["name"], "id");
assert!(
value.get("sql_type").is_none(),
"Should not contain 'sql_type'"
);
assert!(
value.get("not_null").is_none(),
"Should not contain 'not_null'"
);
}
}