Skip to content

Commit 8403e22

Browse files
authored
Merge pull request #14 from dev-five-git/add-uuid
Add uuid type
2 parents 5efb087 + b912686 commit 8403e22

File tree

6 files changed

+86
-31
lines changed

6 files changed

+86
-31
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"changes":{"crates/vespertide/Cargo.toml":"Patch","crates/vespertide-macro/Cargo.toml":"Patch","crates/vespertide-query/Cargo.toml":"Patch","crates/vespertide-exporter/Cargo.toml":"Patch","crates/vespertide-planner/Cargo.toml":"Patch","crates/vespertide-config/Cargo.toml":"Patch","crates/vespertide-cli/Cargo.toml":"Patch","crates/vespertide-core/Cargo.toml":"Patch"},"note":"Add UUID type","date":"2025-12-14T14:08:05.559501100Z"}

CLAUDE.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,21 @@ cargo run -p vespertide-cli -- log
5050

5151
### Crate Responsibilities
5252

53-
- **vespertide-core**: Data structures (`TableDef`, `ColumnDef`, `MigrationAction`, `MigrationPlan`, constraints, indexes)
53+
- **vespertide-core**: Data structures (`TableDef`, `ColumnDef`, `ColumnType`, `MigrationAction`, `MigrationPlan`, constraints, indexes)
54+
- `ColumnType` enum with `Simple(SimpleColumnType)` and `Complex(ComplexColumnType)` variants
55+
- `ColumnType::to_sql()` and `ColumnType::to_rust_type()` methods for type conversion
5456
- **vespertide-planner**:
5557
- `schema_from_plans()`: Replays applied migrations to reconstruct baseline schema
5658
- `diff_schemas()`: Compares two schemas and generates migration actions
5759
- `plan_next_migration()`: Combines baseline reconstruction + diffing to create the next migration
5860
- `apply_action()`: Applies a single migration action to a schema (used during replay)
5961
- `validate_*()`: Validates schemas and migration plans
6062
- **vespertide-query**: Converts `MigrationAction` → PostgreSQL SQL with bind parameters
63+
- Uses `ColumnType::to_sql()` method for SQL type conversion
6164
- **vespertide-config**: Manages `vespertide.json` (models/migrations directories, naming case preferences)
6265
- **vespertide-cli**: Command-line interface implementation
6366
- **vespertide-exporter**: Exports schemas to other formats (e.g., SeaORM entities)
67+
- Uses `ColumnType::to_rust_type(nullable)` method for Rust type conversion
6468
- **vespertide-schema-gen**: Generates JSON Schema files for validation
6569
- **vespertide-macro**: Placeholder for future runtime migration executor
6670

@@ -80,7 +84,7 @@ When creating `ColumnDef` instances in tests or code, you must initialize ALL fi
8084
```rust
8185
ColumnDef {
8286
name: "id".into(),
83-
r#type: ColumnType::Integer,
87+
r#type: ColumnType::Simple(SimpleColumnType::Integer),
8488
nullable: false,
8589
default: None,
8690
comment: None,
@@ -93,6 +97,25 @@ ColumnDef {
9397

9498
These inline fields (added recently) allow constraints to be defined directly on columns in addition to table-level `TableConstraint` definitions.
9599

100+
### ColumnType Structure
101+
`ColumnType` is an enum with two variants:
102+
- `Simple(SimpleColumnType)`: Built-in types like `Integer`, `Text`, `Boolean`, etc.
103+
- `Complex(ComplexColumnType)`: Types with parameters like `Varchar { length }` or `Custom { custom_type }`
104+
105+
**Important**: In Rust code, always use `ColumnType::Simple(SimpleColumnType::Integer)` instead of the old `ColumnType::Integer` syntax. The `From` trait is implemented for convenience:
106+
```rust
107+
// These are equivalent:
108+
ColumnType::Simple(SimpleColumnType::Integer)
109+
SimpleColumnType::Integer.into()
110+
```
111+
112+
### ColumnType Methods
113+
`ColumnType` provides two utility methods:
114+
- `to_sql()`: Returns the PostgreSQL SQL type string (e.g., `"INTEGER"`, `"VARCHAR(255)"`)
115+
- `to_rust_type(nullable: bool)`: Returns the Rust type string for SeaORM entity generation (e.g., `"i32"` or `"Option<i32>"`)
116+
117+
These methods replace the old standalone functions `column_type_sql()` and `rust_type()`.
118+
96119
### Foreign Key Definition
97120
Foreign keys can be defined inline on columns via the `foreign_key` field:
98121

Cargo.lock

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ Models are JSON files in the `models/` directory:
6363
"$schema": "https://raw.githubusercontent.com/dev-five-git/vespertide/refs/heads/main/schemas/model.schema.json",
6464
"name": "user",
6565
"columns": [
66-
{ "name": "id", "type": "Integer", "nullable": false, "primary_key": true },
67-
{ "name": "email", "type": "Text", "nullable": false, "unique": true },
68-
{ "name": "name", "type": "Text", "nullable": false },
69-
{ "name": "created_at", "type": "Timestamp", "nullable": false, "default": "NOW()" }
66+
{ "name": "id", "type": "integer", "nullable": false, "primary_key": true },
67+
{ "name": "email", "type": "text", "nullable": false, "unique": true },
68+
{ "name": "name", "type": "text", "nullable": false },
69+
{ "name": "created_at", "type": "timestamptz", "nullable": false, "default": "NOW()" }
7070
],
7171
"constraints": [],
7272
"indexes": []
@@ -75,16 +75,32 @@ Models are JSON files in the `models/` directory:
7575

7676
### Column Types
7777

78+
**Simple Types** (string values in JSON):
7879
| Type | PostgreSQL |
7980
|------|------------|
80-
| `"Integer"` | INTEGER |
81-
| `"BigInt"` | BIGINT |
82-
| `"Text"` | TEXT |
83-
| `"Boolean"` | BOOLEAN |
84-
| `"Timestamp"` | TIMESTAMP |
85-
| `{ "Custom": "UUID" }` | UUID |
86-
| `{ "Custom": "JSONB" }` | JSONB |
87-
| `{ "Custom": "DECIMAL(10,2)" }` | DECIMAL(10,2) |
81+
| `"integer"` | INTEGER |
82+
| `"big_int"` | BIGINT |
83+
| `"text"` | TEXT |
84+
| `"boolean"` | BOOLEAN |
85+
| `"timestamp"` | TIMESTAMP |
86+
| `"timestamptz"` | TIMESTAMPTZ |
87+
| `"uuid"` | UUID |
88+
| `"jsonb"` | JSONB |
89+
| `"small_int"` | SMALLINT |
90+
| `"real"` | REAL |
91+
| `"double_precision"` | DOUBLE PRECISION |
92+
| `"date"` | DATE |
93+
| `"time"` | TIME |
94+
| `"bytea"` | BYTEA |
95+
| `"json"` | JSON |
96+
| `"inet"` | INET |
97+
| `"cidr"` | CIDR |
98+
| `"macaddr"` | MACADDR |
99+
100+
**Complex Types** (object values in JSON):
101+
- `{ "kind": "varchar", "length": 255 }` → VARCHAR(255)
102+
- `{ "kind": "custom", "custom_type": "DECIMAL(10,2)" }` → DECIMAL(10,2)
103+
- `{ "kind": "custom", "custom_type": "UUID" }` → UUID
88104

89105
### Inline Constraints
90106

@@ -93,7 +109,7 @@ Constraints can be defined directly on columns:
93109
```json
94110
{
95111
"name": "user_id",
96-
"type": "Integer",
112+
"type": "integer",
97113
"nullable": false,
98114
"foreign_key": {
99115
"ref_table": "user",

SKILL.md

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ Models are JSON files in the `models/` directory:
7373

7474
## Column Types
7575

76-
### Built-in Types
76+
Column types in JSON can be either simple (string) or complex (object) values.
77+
78+
### Simple Types (Built-in)
79+
80+
Simple types are represented as strings in JSON (snake_case):
7781

7882
| Type | PostgreSQL | Use Cases |
7983
|------|------------|-----------|
@@ -96,17 +100,27 @@ Models are JSON files in the `models/` directory:
96100
| `"cidr"` | CIDR | Network address |
97101
| `"macaddr"` | MACADDR | MAC address |
98102

99-
### Custom Types
103+
**Note**: In JSON, simple types are written as lowercase strings (e.g., `"integer"`, `"text"`). The Rust enum uses `SimpleColumnType` wrapped in `ColumnType::Simple()`.
100104

101-
For types not covered above:
105+
### Complex Types
102106

107+
Complex types are represented as objects with a `kind` field:
108+
109+
**VARCHAR with length:**
103110
```json
104-
{ "custom": "DECIMAL(10,2)" }
105-
{ "custom": "VARCHAR(255)" }
106-
{ "custom": "NUMERIC(20,8)" }
107-
{ "custom": "INTERVAL" }
111+
{ "kind": "varchar", "length": 255 }
108112
```
109113

114+
**Custom types:**
115+
```json
116+
{ "kind": "custom", "custom_type": "DECIMAL(10,2)" }
117+
{ "kind": "custom", "custom_type": "NUMERIC(20,8)" }
118+
{ "kind": "custom", "custom_type": "INTERVAL" }
119+
{ "kind": "custom", "custom_type": "UUID" }
120+
```
121+
122+
**Note**: In Rust code, complex types are represented as `ColumnType::Complex(ComplexColumnType::Varchar { length })` or `ColumnType::Complex(ComplexColumnType::Custom { custom_type })`.
123+
110124
## Inline Constraints
111125

112126
### Primary Key
@@ -230,7 +244,7 @@ Reference actions: `"Cascade"`, `"Restrict"`, `"SetNull"`, `"SetDefault"`, `"NoA
230244
"columns": [
231245
{ "name": "id", "type": "uuid", "nullable": false, "primary_key": true, "default": "gen_random_uuid()" },
232246
{ "name": "customer_id", "type": "integer", "nullable": false, "foreign_key": { "ref_table": "customer", "ref_columns": ["id"], "on_delete": "Restrict" }, "index": true },
233-
{ "name": "total_amount", "type": { "custom": "DECIMAL(10,2)" }, "nullable": false },
247+
{ "name": "total_amount", "type": { "kind": "custom", "custom_type": "DECIMAL(10,2)" }, "nullable": false },
234248
{ "name": "status", "type": "text", "nullable": false, "default": "'pending'" },
235249
{ "name": "metadata", "type": "jsonb", "nullable": true },
236250
{ "name": "created_at", "type": "timestamptz", "nullable": false, "default": "NOW()" }

crates/vespertide-query/src/sql.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,7 @@ mod tests {
836836
#[case(ColumnType::Simple(SimpleColumnType::Text), "TEXT")]
837837
#[case(ColumnType::Simple(SimpleColumnType::Boolean), "BOOLEAN")]
838838
#[case(ColumnType::Simple(SimpleColumnType::Timestamp), "TIMESTAMP")]
839+
#[case(ColumnType::Simple(SimpleColumnType::Uuid), "UUID")]
839840
#[case(ColumnType::Complex(ComplexColumnType::Custom { custom_type: "VARCHAR(255)".to_string() }), "VARCHAR(255)")]
840841
fn test_column_type_sql(#[case] ty: ColumnType, #[case] expected: &str) {
841842
assert_eq!(ty.to_sql(), expected);

0 commit comments

Comments
 (0)