FlowForge/backend/migrations/20250607_initial_schema.js

63 lines
2.2 KiB
JavaScript

/**
* Initial database schema for FlowForge
*/
exports.up = function(knex) {
return knex.schema
// Users table
.createTable('users', function(table) {
table.uuid('id').primary();
table.string('email').notNullable().unique();
table.string('password').notNullable();
table.timestamp('created_at').defaultTo(knex.fn.now());
table.timestamp('updated_at').defaultTo(knex.fn.now());
})
// Workflows table
.createTable('workflows', function(table) {
table.uuid('id').primary();
table.uuid('user_id').notNullable().references('id').inTable('users').onDelete('CASCADE');
table.string('name').notNullable();
table.jsonb('nodes').notNullable().defaultTo('[]');
table.jsonb('connections').notNullable().defaultTo('[]');
table.timestamp('created_at').defaultTo(knex.fn.now());
table.timestamp('updated_at').defaultTo(knex.fn.now());
// Index for faster user-based queries
table.index('user_id');
})
// Workflow logs table
.createTable('workflow_logs', function(table) {
table.uuid('id').primary();
table.uuid('workflow_id').notNullable().references('id').inTable('workflows').onDelete('CASCADE');
table.jsonb('logs').notNullable().defaultTo('[]');
table.timestamp('created_at').defaultTo(knex.fn.now());
// Index for faster workflow-based queries
table.index('workflow_id');
})
// Webhook registrations table
.createTable('webhooks', function(table) {
table.uuid('id').primary();
table.uuid('workflow_id').notNullable().references('id').inTable('workflows').onDelete('CASCADE');
table.uuid('node_id').notNullable();
table.string('path').notNullable().unique();
table.string('method').notNullable().defaultTo('POST');
table.timestamp('created_at').defaultTo(knex.fn.now());
table.timestamp('updated_at').defaultTo(knex.fn.now());
// Indexes
table.index('workflow_id');
table.index('path');
});
};
exports.down = function(knex) {
return knex.schema
.dropTableIfExists('webhooks')
.dropTableIfExists('workflow_logs')
.dropTableIfExists('workflows')
.dropTableIfExists('users');
};