CREATE TABLE `__new_articles` (
	`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
	`feed_id` integer NOT NULL,
	`guid` text,
	`title` text,
	`link` text NOT NULL,
	`description` text,
	`content` text,
	`author` text,
	`categories` text,
	`published_at` integer,
	`is_read` integer DEFAULT false NOT NULL,
	`is_starred` integer DEFAULT false NOT NULL,
	`is_broken` integer DEFAULT false NOT NULL,
	`broken_reason` text,
	`link_fixed` text,
	`content_fetched_at` integer,
	`created_at` integer,
	`updated_at` integer
);
--> statement-breakpoint
INSERT INTO `__new_articles`("id", "feed_id", "guid", "title", "link", "description", "content", "author", "categories", "published_at", "is_read", "is_starred", "is_broken", "broken_reason", "link_fixed", "content_fetched_at", "created_at", "updated_at") SELECT "id", "feed_id", "guid", "title", "link", "description", "content", "author", "categories", "published_at", coalesce("is_read", false), coalesce("is_starred", false), coalesce("is_broken", false), "broken_reason", "link_fixed", "content_fetched_at", "created_at", "updated_at" FROM `articles`;--> statement-breakpoint
DROP TABLE `articles`;--> statement-breakpoint
ALTER TABLE `__new_articles` RENAME TO `articles`;--> statement-breakpoint
CREATE TABLE `__new_feeds` (
	`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
	`title` text NOT NULL,
	`title_overridden` integer DEFAULT false NOT NULL,
	`xml_url` text NOT NULL,
	`html_url` text,
	`description` text,
	`category` text,
	`last_fetched_at` integer,
	`last_modified` text,
	`etag` text,
	`fetch_error` text,
	`error_count` integer DEFAULT 0 NOT NULL,
	`is_active` integer DEFAULT true NOT NULL,
	`created_at` integer,
	`updated_at` integer
);
--> statement-breakpoint
INSERT INTO `__new_feeds`("id", "title", "title_overridden", "xml_url", "html_url", "description", "category", "last_fetched_at", "last_modified", "etag", "fetch_error", "error_count", "is_active", "created_at", "updated_at") SELECT "id", "title", coalesce("title_overridden", false), "xml_url", "html_url", "description", "category", "last_fetched_at", "last_modified", "etag", "fetch_error", coalesce("error_count", 0), coalesce("is_active", true), "created_at", "updated_at" FROM `feeds`;--> statement-breakpoint
DROP TABLE `feeds`;--> statement-breakpoint
ALTER TABLE `__new_feeds` RENAME TO `feeds`;--> statement-breakpoint
CREATE UNIQUE INDEX `feeds_xml_url_unique` ON `feeds` (`xml_url`);--> statement-breakpoint
CREATE INDEX `feeds_category_idx` ON `feeds` (`category`);--> statement-breakpoint
CREATE INDEX `feeds_active_idx` ON `feeds` (`is_active`);--> statement-breakpoint
CREATE TABLE `__new_opml_imports` (
	`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
	`file_path` text NOT NULL,
	`file_name` text,
	`feeds_imported` integer DEFAULT 0 NOT NULL,
	`feeds_skipped` integer DEFAULT 0 NOT NULL,
	`imported_at` integer
);
--> statement-breakpoint
INSERT INTO `__new_opml_imports`("id", "file_path", "file_name", "feeds_imported", "feeds_skipped", "imported_at") SELECT "id", "file_path", "file_name", coalesce("feeds_imported", 0), coalesce("feeds_skipped", 0), "imported_at" FROM `opml_imports`;--> statement-breakpoint
DROP TABLE `opml_imports`;--> statement-breakpoint
ALTER TABLE `__new_opml_imports` RENAME TO `opml_imports`;--> statement-breakpoint
CREATE TABLE `__new_articles_with_fk` (
	`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
	`feed_id` integer NOT NULL,
	`guid` text,
	`title` text,
	`link` text NOT NULL,
	`description` text,
	`content` text,
	`author` text,
	`categories` text,
	`published_at` integer,
	`is_read` integer DEFAULT false NOT NULL,
	`is_starred` integer DEFAULT false NOT NULL,
	`is_broken` integer DEFAULT false NOT NULL,
	`broken_reason` text,
	`link_fixed` text,
	`content_fetched_at` integer,
	`created_at` integer,
	`updated_at` integer,
	FOREIGN KEY (`feed_id`) REFERENCES `feeds`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
INSERT INTO `__new_articles_with_fk`("id", "feed_id", "guid", "title", "link", "description", "content", "author", "categories", "published_at", "is_read", "is_starred", "is_broken", "broken_reason", "link_fixed", "content_fetched_at", "created_at", "updated_at") SELECT "id", "feed_id", "guid", "title", "link", "description", "content", "author", "categories", "published_at", "is_read", "is_starred", "is_broken", "broken_reason", "link_fixed", "content_fetched_at", "created_at", "updated_at" FROM `articles`;--> statement-breakpoint
DROP TABLE `articles`;--> statement-breakpoint
ALTER TABLE `__new_articles_with_fk` RENAME TO `articles`;--> statement-breakpoint
CREATE INDEX `articles_feed_idx` ON `articles` (`feed_id`);--> statement-breakpoint
CREATE INDEX `articles_guid_idx` ON `articles` (`guid`);--> statement-breakpoint
CREATE INDEX `articles_link_idx` ON `articles` (`link`);--> statement-breakpoint
CREATE INDEX `articles_published_idx` ON `articles` (`published_at`);--> statement-breakpoint
CREATE INDEX `articles_read_idx` ON `articles` (`is_read`);--> statement-breakpoint
CREATE INDEX `articles_broken_idx` ON `articles` (`is_broken`);--> statement-breakpoint
CREATE INDEX `articles_feed_read_idx` ON `articles` (`feed_id`,`is_read`);--> statement-breakpoint
CREATE INDEX `articles_feed_pub_idx` ON `articles` (`feed_id`,`published_at`);--> statement-breakpoint
CREATE INDEX `articles_author_idx` ON `articles` (`author`);--> statement-breakpoint
CREATE INDEX `articles_starred_idx` ON `articles` (`is_starred`);--> statement-breakpoint
CREATE INDEX `articles_content_fetched_at_idx` ON `articles` (`content_fetched_at`);--> statement-breakpoint
PRAGMA foreign_key_check;
