peppa/packages/db/migrations/0003_subscriptions.sql
2026-11-14 03:48:25 +05:00

54 lines
3.3 KiB
SQL

CREATE TABLE `subscription_pool_members` (
`pool_id` integer NOT NULL,
`subscription_id` integer NOT NULL,
PRIMARY KEY(`pool_id`, `subscription_id`),
FOREIGN KEY (`pool_id`) REFERENCES `subscription_pools`(`id`) ON UPDATE no action ON DELETE cascade,
FOREIGN KEY (`subscription_id`) REFERENCES `subscriptions`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `subscription_pools` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`name` text NOT NULL,
`created_by` integer,
`created_at` integer DEFAULT (cast(unixepoch('subsec') * 1000 as integer)) NOT NULL,
FOREIGN KEY (`created_by`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE set null
);
--> statement-breakpoint
CREATE UNIQUE INDEX `subscription_pools_name_unique` ON `subscription_pools` (`name`);--> statement-breakpoint
CREATE TABLE `subscriptions` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`provider` text NOT NULL,
`name` text NOT NULL,
`auth_kind` text NOT NULL,
`secret_enc` text,
`key_provider` text,
`status` text DEFAULT 'pending' NOT NULL,
`limited_until` integer,
`last_error` text,
`checked_at` integer,
`activated_at` integer,
`created_by` integer,
`created_at` integer DEFAULT (cast(unixepoch('subsec') * 1000 as integer)) NOT NULL,
FOREIGN KEY (`created_by`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE set null
);
--> statement-breakpoint
ALTER TABLE `projects` ADD `subscription_pool_id` integer REFERENCES subscription_pools(id) ON DELETE set null;--> statement-breakpoint
ALTER TABLE `sessions` ADD `subscription_id` integer REFERENCES subscriptions(id) ON DELETE set null;--> statement-breakpoint
-- PEP-67 data: the PEP-13 Claude token of every user becomes a subscription «Claude — <login>»
-- (same ciphertext: same PEPPA_SECRET key), a pool of the same name, and the default pool of the
-- projects that user owns — sessions keep running on the same account. Then the token is cleared.
INSERT INTO `subscriptions` (`provider`, `name`, `auth_kind`, `secret_enc`, `status`, `activated_at`, `created_by`)
SELECT 'claude', 'Claude — ' || `login`, 'oauth', `claude_token_enc`, 'active', coalesce(`claude_token_set_at`, cast(unixepoch('subsec') * 1000 as integer)), `id`
FROM `users` WHERE `claude_token_enc` IS NOT NULL;--> statement-breakpoint
INSERT OR IGNORE INTO `subscription_pools` (`name`, `created_by`)
SELECT 'Claude — ' || `login`, `id` FROM `users` WHERE `claude_token_enc` IS NOT NULL;--> statement-breakpoint
INSERT OR IGNORE INTO `subscription_pool_members` (`pool_id`, `subscription_id`)
SELECT p.`id`, s.`id` FROM `users` u
JOIN `subscription_pools` p ON p.`name` = 'Claude — ' || u.`login`
JOIN `subscriptions` s ON s.`name` = 'Claude — ' || u.`login` AND s.`created_by` = u.`id` AND s.`provider` = 'claude'
WHERE u.`claude_token_enc` IS NOT NULL;--> statement-breakpoint
UPDATE `projects` SET `subscription_pool_id` = (
SELECT p.`id` FROM `users` u JOIN `subscription_pools` p ON p.`name` = 'Claude — ' || u.`login`
WHERE u.`id` = `projects`.`owner_id` AND u.`claude_token_enc` IS NOT NULL
) WHERE `subscription_pool_id` IS NULL AND `owner_id` IN (SELECT `id` FROM `users` WHERE `claude_token_enc` IS NOT NULL);--> statement-breakpoint
UPDATE `users` SET `claude_token_enc` = NULL, `claude_token_set_at` = NULL WHERE `claude_token_enc` IS NOT NULL;