Schema Management

Declarative schema management with automatic migrations in Akron ORM.

NEW FEATURE

Schema Management

Akron features powerful declarative schema management with automatic migration generation and database synchronization. Define your schema in JSON and let Akron handle the implementation.

✨ Key Features

  • Declarative schema definition in JSON
  • Automatic migration generation
  • Schema change detection
  • Migration history tracking
  • Multi-database support
  • Foreign key relationships

šŸš€ Quick Workflow

1

Initialize Project

Run akron db init to create akron.json

2

Define Schema

Edit akron.json to define your database structure

3

Generate Migrations

Run akron db makemigrations to create migration files

4

Apply Changes

Run akron db migrate to update your database

Getting Started

1. Initialize a New Project

Start by initializing a new Akron project. This creates the akron.json schema file and .akron/ directory for migrations.

1# Initialize with SQLite (default)
2akron db init
3
4# Initialize with specific database
5akron db init --provider postgresql --url "postgres://user:pass@localhost:5432/mydb"
6akron db init --provider mysql --url "mysql://user:pass@localhost:3306/mydb"
7akron db init --provider mongodb --url "mongodb://localhost:27017/mydb"
Expected Output
āœ… Initialized Akron project
   Provider: sqlite
   Database: sqlite:///app.db
   Schema file: akron.json

šŸ“ Next steps:
   1. Edit akron.json to define your schema
   2. Run 'akron db makemigrations' to generate migrations
   3. Run 'akron db migrate' to apply migrations

2. Define Your Schema

Edit the generated akron.json file to define your database structure with tables, columns, and relationships.

1{
2 "database": {
3 "provider": "sqlite",
4 "url": "sqlite:///app.db"
5 },
6 "tables": {
7 "users": {
8 "columns": {
9 "id": {
10 "type": "int",
11 "primary_key": true,
12 "auto_increment": true
13 },
14 "email": {
15 "type": "str",
16 "unique": true,
17 "nullable": false,
18 "max_length": 255
19 },
20 "username": {
21 "type": "str",
22 "unique": true,
23 "nullable": false,
24 "max_length": 50
25 },
26 "created_at": {
27 "type": "datetime",
28 "default": "CURRENT_TIMESTAMP"
29 }
30 }
31 },
32 "posts": {
33 "columns": {
34 "id": {
35 "type": "int",
36 "primary_key": true,
37 "auto_increment": true
38 },
39 "title": {
40 "type": "str",
41 "nullable": false,
42 "max_length": 200
43 },
44 "content": {
45 "type": "text",
46 "nullable": true
47 },
48 "author_id": {
49 "type": "int",
50 "nullable": false
51 },
52 "published": {
53 "type": "bool",
54 "default": false
55 }
56 },
57 "foreign_keys": {
58 "author_id": {
59 "references": "users",
60 "column": "id",
61 "on_delete": "CASCADE"
62 }
63 }
64 }
65 }
66}

3. Generate Migrations

After defining your schema, generate migration files that describe the changes needed to transform your database.

1# Generate migrations with auto-generated name
2akron db makemigrations
3
4# Generate migrations with custom name
5akron db makemigrations --name "add_user_posts"
Expected Output
āœ… Generated migration: add_user_posts
   File: .akron/add_user_posts.json
   Steps: 2

šŸ“‹ Migration preview:
   1. Create table 'users'
   2. Create table 'posts'

4. Apply Migrations

Apply the pending migrations to update your database schema.

1# Preview what will be migrated
2akron db migrate --dry-run
3
4# Apply all pending migrations
5akron db migrate
Expected Output
šŸ“¦ Applying 1 migration(s)...
   Applying add_user_posts.json...
   āœ… Applied add_user_posts.json
āœ… All migrations applied successfully!

5. Check Status

Monitor your schema and migration status at any time.

1akron db status
Expected Output
šŸ“Š Akron Status
==================================================
Schema file: akron.json
Database: sqlite
URL: sqlite:///app.db
Tables: 2

āœ… Schema is up to date

Applied migrations: 1
Pending migrations: 0

Schema Format Reference

Database Configuration

1{
2 "database": {
3 "provider": "sqlite|mysql|postgresql|mongodb",
4 "url": "connection_string"
5 }
6}

Supported Data Types

Basic Types

  • int - Integer numbers
  • str - String/VARCHAR
  • text - Long text content
  • bool - Boolean values
  • float - Floating point numbers

Date & Special Types

  • datetime - Date and time
  • date - Date only
  • time - Time only
  • json - JSON data

Column Constraints

ConstraintTypeDescription
primary_keybooleanMark column as primary key
auto_incrementbooleanAuto-increment values (integers only)
uniquebooleanEnforce unique constraint
nullablebooleanAllow NULL values
defaultanyDefault value for column
max_lengthintegerMaximum length (strings)

Foreign Key Relationships

1"foreign_keys": {
2 "author_id": {
3 "references": "users",
4 "column": "id",
5 "on_delete": "CASCADE|SET_NULL|RESTRICT",
6 "on_update": "CASCADE|SET_NULL|RESTRICT"
7 }
8}

CLI Commands

akron db init

Initialize a new Akron project with schema configuration.

akron db init --provider sqlite

akron db makemigrations

Generate migration files from schema changes.

akron db makemigrations --name "add_tables"

akron db migrate

Apply pending migrations to the database.

akron db migrate --dry-run

akron db status

Show current schema and migration status.

akron db status

Best Practices

šŸ“ Schema Design

  • Use meaningful table and column names
  • Define appropriate constraints (nullable, unique, max_length)
  • Always define foreign key relationships

šŸ”„ Migration Workflow

  • Make small, incremental changes
  • Use descriptive migration names
  • Always test with --dry-run first