-- SQLite does not support DROP COLUMN directly; recreate the table without last_used_at.
CREATE TABLE runner_api_keys_backup (
	id INTEGER PRIMARY KEY AUTOINCREMENT,
	key_prefix TEXT NOT NULL,
	key_hash TEXT NOT NULL,
	name TEXT NOT NULL,
	description TEXT,
	scope TEXT NOT NULL DEFAULT 'user',
	is_active INTEGER NOT NULL DEFAULT 1,
	created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
	created_by INTEGER,
	FOREIGN KEY (created_by) REFERENCES users(id),
	UNIQUE(key_prefix)
);

INSERT INTO runner_api_keys_backup SELECT id, key_prefix, key_hash, name, description, scope, is_active, created_at, created_by FROM runner_api_keys;

DROP TABLE runner_api_keys;

ALTER TABLE runner_api_keys_backup RENAME TO runner_api_keys;

CREATE INDEX idx_runner_api_keys_prefix ON runner_api_keys(key_prefix);
CREATE INDEX idx_runner_api_keys_active ON runner_api_keys(is_active);
CREATE INDEX idx_runner_api_keys_created_by ON runner_api_keys(created_by);
