-- Add migration script here
CREATE TABLE table_definitions (
    id BIGSERIAL PRIMARY KEY,
    deleted BOOLEAN NOT NULL DEFAULT FALSE,
    table_name TEXT NOT NULL UNIQUE,
    columns JSONB NOT NULL,
    indexes JSONB NOT NULL,
    created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
    profile_id BIGINT NOT NULL REFERENCES profiles(id) DEFAULT 1,
    linked_table_id BIGINT REFERENCES table_definitions(id)
);

-- Create composite unique index for profile+table combination
CREATE UNIQUE INDEX idx_table_definitions_profile_table
    ON table_definitions (profile_id, table_name);

-- Add self-referential foreign key constraint
ALTER TABLE table_definitions
    ADD CONSTRAINT fk_linked_table
    FOREIGN KEY (linked_table_id)
    REFERENCES table_definitions(id);
