WhatsApp/Signal/Formstack/admin updates

This commit is contained in:
Darren Clarke 2025-11-21 14:55:28 +01:00
parent bcecf61a46
commit d0cc5a21de
451 changed files with 16139 additions and 39623 deletions

View file

@ -0,0 +1,158 @@
# Bridge Migrations
Database migration management for the CDR Link bridge system.
## Overview
Bridge Migrations handles database schema versioning and migrations for all bridge-related tables using Kysely migration framework. It manages the database structure for authentication, messaging channels, webhooks, and settings.
## Features
- **Schema Versioning**: Track and apply database schema changes
- **Up/Down Migrations**: Support for rolling forward and backward
- **Type-Safe Migrations**: TypeScript-based migration files
- **Migration History**: Track applied migrations in the database
- **Multiple Migration Strategies**: Run all, run one, or rollback migrations
## Migration Files
Current migrations in order:
1. **0001-add-next-auth.ts** - NextAuth.js authentication tables
2. **0002-add-signal.ts** - Signal messenger integration
3. **0003-add-whatsapp.ts** - WhatsApp integration
4. **0004-add-voice.ts** - Voice/Twilio integration
5. **0005-add-facebook.ts** - Facebook Messenger integration
6. **0006-add-webhooks.ts** - Webhook configuration
7. **0007-add-settings.ts** - Application settings
8. **0008-add-user-role.ts** - User role management
## Development
### Prerequisites
- Node.js >= 20
- npm >= 10
- PostgreSQL database
- Database connection credentials
### Setup
```bash
# Install dependencies
npm install
# Run all pending migrations
npm run migrate:latest
# Check migration status
npm run migrate:list
```
### Environment Variables
Required environment variables:
- `DATABASE_URL` - PostgreSQL connection string
- `DATABASE_HOST` - Database host
- `DATABASE_NAME` - Database name
- `DATABASE_USER` - Database username
- `DATABASE_PASSWORD` - Database password
### Available Scripts
- `npm run migrate:latest` - Run all pending migrations
- `npm run migrate:up` - Run next pending migration
- `npm run migrate:down` - Rollback last migration
- `npm run migrate:up:all` - Run all migrations (alias)
- `npm run migrate:up:one` - Run one migration
- `npm run migrate:down:all` - Rollback all migrations
- `npm run migrate:down:one` - Rollback one migration
- `npm run migrate:list` - List migration status
- `npm run migrate:make <name>` - Create new migration file
## Creating New Migrations
To create a new migration:
```bash
npm run migrate:make add-new-feature
```
This creates a new timestamped migration file in the `migrations/` directory.
Example migration structure:
```typescript
import { Kysely } from 'kysely'
export async function up(db: Kysely<any>): Promise<void> {
await db.schema
.createTable('new_table')
.addColumn('id', 'serial', (col) => col.primaryKey())
.addColumn('name', 'varchar', (col) => col.notNull())
.addColumn('created_at', 'timestamp', (col) =>
col.defaultTo('now()').notNull()
)
.execute()
}
export async function down(db: Kysely<any>): Promise<void> {
await db.schema.dropTable('new_table').execute()
}
```
## Database Schema
### Core Tables
- **users** - User accounts with roles
- **accounts** - OAuth account connections
- **sessions** - User sessions
- **verification_tokens** - Email verification
### Communication Tables
- **bots** - Bot configurations for each service
- **signal_messages** - Signal message history
- **whatsapp_messages** - WhatsApp message history
- **voice_messages** - Voice/call records
- **facebook_messages** - Facebook message history
### Configuration Tables
- **webhooks** - External webhook endpoints
- **settings** - Application settings
## Best Practices
1. **Test Migrations**: Always test migrations in development first
2. **Backup Database**: Create backups before running migrations in production
3. **Review Changes**: Review migration files before applying
4. **Atomic Operations**: Keep migrations focused and atomic
5. **Rollback Plan**: Ensure down() methods properly reverse changes
## Troubleshooting
### Common Issues
1. **Migration Failed**: Check error logs and database permissions
2. **Locked Migrations**: Check for concurrent migration processes
3. **Missing Tables**: Ensure all previous migrations have run
4. **Connection Issues**: Verify DATABASE_URL and network access
### Recovery
If migrations fail:
1. Check migration history table
2. Manually verify database state
3. Run specific migrations as needed
4. Use rollback if necessary
## Integration
Migrations are used by:
- **bridge-frontend** - Requires migrated schema
- **bridge-worker** - Depends on message tables
- **bridge-whatsapp** - Uses bot configuration tables

View file

@ -10,6 +10,9 @@ import {
CamelCasePlugin,
} from "kysely";
import pkg from "pg";
import { createLogger } from "@link-stack/logger";
const logger = createLogger('bridge-migrations-migrate');
const { Pool } = pkg;
import * as dotenv from "dotenv";
@ -72,17 +75,17 @@ export const migrate = async (arg: string) => {
results?.forEach((it) => {
if (it.status === "Success") {
console.log(
logger.info(
`Migration "${it.migrationName} ${it.direction.toLowerCase()}" was executed successfully`,
);
} else if (it.status === "Error") {
console.error(`Failed to execute migration "${it.migrationName}"`);
logger.error(`Failed to execute migration "${it.migrationName}"`);
}
});
if (error) {
console.error("Failed to migrate");
console.error(error);
logger.error("Failed to migrate");
logger.error(error);
process.exit(1);
}

View file

@ -1,6 +1,6 @@
{
"name": "@link-stack/bridge-migrations",
"version": "2.2.0",
"version": "3.3.0",
"type": "module",
"scripts": {
"migrate:up:all": "tsx migrate.ts up:all",
@ -9,16 +9,17 @@
"migrate:down:one": "tsx migrate.ts down:one"
},
"dependencies": {
"dotenv": "^16.4.5",
"kysely": "0.26.1",
"pg": "^8.13.0",
"tsx": "^4.19.1"
"@link-stack/logger": "workspace:*",
"dotenv": "^17.2.3",
"kysely": "0.27.5",
"pg": "^8.16.3",
"tsx": "^4.20.6"
},
"devDependencies": {
"@types/node": "^22",
"@types/pg": "^8.11.10",
"@link-stack/eslint-config": "*",
"@link-stack/typescript-config": "*",
"@types/node": "^24",
"@types/pg": "^8.15.5",
"@link-stack/eslint-config": "workspace:*",
"@link-stack/typescript-config": "workspace:*",
"typescript": "^5"
}
}