@elizaos/plugin-sql 1.6.1-alpha.3 → 1.6.1-alpha.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -130,119 +130,186 @@ Default pool configuration:
130
130
 
131
131
  ## Migration Support
132
132
 
133
- The adapter supports two approaches to managing database schema:
133
+ ElizaOS v1.0.0 introduces **dynamic runtime migrations** - automatic schema management that runs at startup without manual intervention. Plugins can define their schemas and the system handles all migrations automatically.
134
134
 
135
- ### 1. Initial Setup
135
+ ### TLDR: What Changed?
136
136
 
137
- Migrations are automatically run during initialization if:
137
+ **Before (v0.x):** Manual migrations with `drizzle-kit generate` → `drizzle-kit push` → restart
138
+ **Now (v1.0.0):** Define schema in plugin → Start agent → Migrations run automatically ✨
138
139
 
139
- - Database tables don't exist
140
- - Vector extension is not found
140
+ ### Key Features
141
141
 
142
- This is handled internally by:
142
+ - **Zero-Config Migrations**: No more manual migration commands
143
+ - **Plugin Isolation**: Each plugin gets its own schema namespace
144
+ - **Safety First**: Destructive changes blocked by default in production
145
+ - **Concurrent Safety**: Built-in locks prevent race conditions
146
+ - **Rollback Protection**: All migrations run in transactions
143
147
 
144
- ```typescript
145
- await runMigrations(pgPool);
146
- ```
148
+ ### How It Works
147
149
 
148
- ### 2. Schema Updates
150
+ 1. **Plugin defines schema** using Drizzle ORM:
149
151
 
150
- To update the schema:
152
+ ```typescript
153
+ // In your plugin's schema.ts
154
+ import { pgTable, text, uuid } from 'drizzle-orm/pg-core';
151
155
 
152
- 1. Install drizzle-kit (if not already installed):
156
+ export const myTable = pgTable('my_table', {
157
+ id: uuid('id').primaryKey(),
158
+ name: text('name').notNull(),
159
+ });
153
160
 
154
- ```bash
155
- bun add -D drizzle-kit
161
+ // Export schema in your plugin
162
+ export const plugin = {
163
+ name: '@your-org/plugin-name',
164
+ schema: schema, // Your Drizzle schema object
165
+ // ... rest of plugin
166
+ };
156
167
  ```
157
168
 
158
- 2. Create or update your schema files (e.g., `schema/account.ts`):
169
+ 2. **Runtime detects changes** at startup:
159
170
 
160
- ```typescript
161
- import { pgTable, text, uuid } from 'drizzle-orm/pg-core';
162
- import { sql } from 'drizzle-orm';
163
-
164
- export const accountTable = pgTable('accounts', {
165
- id: uuid('id').primaryKey().notNull(),
166
- name: text('name'),
167
- email: text('email').notNull(),
168
- // Add new fields here
169
- newField: text('newField'),
170
- });
171
+ ```bash
172
+ [RuntimeMigrator] Starting migration for plugin: @your-org/plugin-name
173
+ [RuntimeMigrator] Executing 2 SQL statements...
174
+ [RuntimeMigrator] Migration completed successfully
171
175
  ```
172
176
 
173
- 3. Generate migrations:
177
+ 3. **Automatic safety checks**:
174
178
 
175
179
  ```bash
176
- npx drizzle-kit generate:pg
180
+ # Destructive changes are blocked
181
+ [RuntimeMigrator] Destructive migration blocked
182
+ [RuntimeMigrator] Destructive operations detected:
183
+ [RuntimeMigrator] - Column "email" will be dropped from table "users"
184
+ [RuntimeMigrator] To proceed:
185
+ [RuntimeMigrator] 1. Set ELIZA_ALLOW_DESTRUCTIVE_MIGRATIONS=true
186
+ [RuntimeMigrator] 2. Or use { force: true } option
177
187
  ```
178
188
 
179
- This will create SQL migration files in your migrations directory.
180
-
181
- 4. Apply migrations using one of these methods:
189
+ ### Migration Controls
182
190
 
183
- a. Using drizzle-kit:
191
+ Control migration behavior via environment variables:
184
192
 
185
193
  ```bash
186
- npx drizzle-kit push:pg
194
+ # Allow destructive migrations (drops, type changes)
195
+ ELIZA_ALLOW_DESTRUCTIVE_MIGRATIONS=true
196
+
197
+ # Development vs Production
198
+ NODE_ENV=production # Stricter checks, verbose off by default
199
+ NODE_ENV=development # More permissive, verbose on
187
200
  ```
188
201
 
189
- b. Through your application code:
202
+ Or programmatically:
190
203
 
191
204
  ```typescript
192
- import { migrate } from 'drizzle-orm/node-postgres/migrator';
193
-
194
- await migrate(db, { migrationsFolder: './drizzle' });
205
+ await databaseAdapter.runPluginMigrations(plugins, {
206
+ verbose: true, // Show SQL statements
207
+ force: true, // Allow destructive changes
208
+ dryRun: true, // Preview without applying
209
+ });
195
210
  ```
196
211
 
197
- c. Using the provided migration script:
212
+ ### Transitioning from Manual Migrations
198
213
 
199
- ```bash
200
- npm run migrate
201
- # or
202
- bun migrate
203
- ```
214
+ If you have existing manual Drizzle migrations:
204
215
 
205
- d. Using drizzle-kit migrate command:
216
+ 1. **Keep existing migrations** - They remain compatible
217
+ 2. **Add schema to plugin** - Export your Drizzle schema
218
+ 3. **First run** - Runtime migrator detects current state
219
+ 4. **Future changes** - Just update schema and restart
206
220
 
207
- ```bash
208
- npx drizzle-kit migrate
221
+ Example transition:
222
+
223
+ ```typescript
224
+ // Before: Manual migrations
225
+ // 1. Edit schema
226
+ // 2. Run: bunx drizzle-kit generate
227
+ // 3. Run: bunx drizzle-kit push
228
+ // 4. Restart agent
229
+
230
+ // After: Runtime migrations
231
+ // 1. Edit schema in plugin
232
+ // 2. Restart agent (migrations run automatically)
209
233
  ```
210
234
 
211
- This command will read the configuration from `drizzle.config.ts` and pull the PostgreSQL URI from the `.env` file. Make sure your `.env` file contains the `POSTGRES_URL` variable with the correct connection string.
235
+ ### Schema Namespacing
212
236
 
213
- ### Migration Configuration
237
+ Plugins automatically get namespaced schemas for isolation:
214
238
 
215
- The plugin uses a `drizzle.config.ts` file to configure migrations:
239
+ - `@elizaos/plugin-sql` Uses `public` schema (core tables)
240
+ - `@your-org/plugin-name` → Uses `your_org_plugin_name` schema
241
+ - Prevents table name conflicts between plugins
242
+ - Clean separation of concerns
243
+
244
+ To use a custom schema:
216
245
 
217
246
  ```typescript
218
- import { config } from 'dotenv';
219
- import { defineConfig } from 'drizzle-kit';
220
-
221
- config({ path: '../../.env' });
222
-
223
- export default defineConfig({
224
- dialect: 'postgresql',
225
- schema: './src/schema/index.ts',
226
- out: './drizzle/migrations',
227
- dbCredentials: {
228
- url: process.env.POSTGRES_URL || 'file:../../.elizadb',
229
- },
230
- breakpoints: true,
247
+ import { pgSchema } from 'drizzle-orm/pg-core';
248
+
249
+ const mySchema = pgSchema('my_custom_schema');
250
+ export const myTable = mySchema.table('my_table', {
251
+ // ... columns
231
252
  });
232
253
  ```
233
254
 
255
+ ### Debugging Migrations
256
+
257
+ Check migration status:
258
+
259
+ ```typescript
260
+ const migrator = migrationService.getMigrator();
261
+ const status = await migrator.getStatus('@your-org/plugin-name');
262
+ console.log(status);
263
+ // {
264
+ // hasRun: true,
265
+ // lastMigration: { hash: "...", timestamp: ... },
266
+ // journal: [...],
267
+ // snapshots: 3
268
+ // }
269
+ ```
270
+
271
+ Preview changes without applying:
272
+
273
+ ```typescript
274
+ const check = await migrator.checkMigration('@your-org/plugin-name', schema);
275
+ if (check?.hasDataLoss) {
276
+ console.log('Warning: Destructive changes:', check.warnings);
277
+ }
278
+ ```
279
+
234
280
  ### Database Support
235
281
 
236
- The plugin supports two database backends:
282
+ The plugin supports two database backends with automatic migration support:
283
+
284
+ 1. **PostgreSQL**: Production-ready with full feature support
285
+ 2. **PGlite**: Embedded database for development/testing
286
+
287
+ Both use identical migration systems - develop locally with PGlite, deploy to PostgreSQL.
288
+
289
+ ### Troubleshooting
290
+
291
+ **"Destructive migration blocked"**
292
+
293
+ - Set `ELIZA_ALLOW_DESTRUCTIVE_MIGRATIONS=true` for development
294
+ - For production, review changes carefully before enabling
295
+
296
+ **"Migration already in progress"**
297
+
298
+ - Another instance is running migrations
299
+ - System will wait for lock automatically
237
300
 
238
- 1. **PostgreSQL**: Used when `POSTGRES_URL` environment variable is provided
239
- 2. **PGlite**: Used as a fallback when no PostgreSQL URL is provided
301
+ **"No changes detected"**
240
302
 
241
- Both backends use the same migration files, ensuring consistent schema across environments.
303
+ - Schema matches database state
304
+ - No migration needed
242
305
 
243
- ### Note on Vector Support
306
+ **Manual migration needed?**
244
307
 
245
- Make sure the PostgreSQL vector extension is installed before running migrations. The adapter will validate vector setup during initialization.
308
+ - Use standard Drizzle Kit for complex scenarios:
309
+ ```bash
310
+ bunx drizzle-kit generate
311
+ bunx drizzle-kit migrate
312
+ ```
246
313
 
247
314
  ## Clean Shutdown
248
315