# Smart Database Initialization

Nord Crafting includes a database initializer and migration system.

## First start

When automatic creation is enabled, the resource:

1. Waits for `oxmysql`.
2. Ensures the settings table exists.
3. Creates required Crafting tables.
4. Inserts default data where required.
5. Runs migrations.
6. Stores the schema version.
7. Writes local state to `server/db_state.json`.

## Smart restart behavior

`server/db_state.json` stores the current schema version. When the local state and database are already current, Crafting can skip unnecessary heavy CREATE/ALTER work on normal restarts.

## Manual SQL

A full SQL file remains available at:

```text
install/sql.sql
```

Use it when your deployment policy requires manual schema management.

## Readiness export

```lua
local ready = exports['nord_crafting']:IsNordDBReady()
```

This is useful for another server resource that must wait for Crafting's database layer before calling database-backed APIs.
