fastladderのテーブル一覧
sqlite> .tables crawl_statuses folders pins favicons items schema_migrations feeds members subscriptions
この中でおぼえておくべきテーブルは"feeds"と"items"。feedsテーブルに登録されているURLをベースにデータを取得して、itemsテーブルに格納することになる。
各テーブルスキーマ
feedsのテーブルスキーマはこちら。(migrationファイルで確認)
cat 002_create_feeds.rb
class CreateFeeds < ActiveRecord::Migration
def self.up
create_table :feeds do |t|
t.string :feedlink, null: false
t.string :link, null: false
t.text :title, null: false
t.text :description, null: false
t.integer :subscribers_count, default: 0, null: false
t.string :image
t.string :icon
t.datetime :modified_on
t.datetime :created_on, null: false
t.datetime :updated_on, null: false
end
add_index :feeds, :feedlink, unique: true
end
itemテーブルのスキーマはこちら。
$ cat 006_create_items.rb
class CreateItems < ActiveRecord::Migration
def self.up
create_table :items do |t|
t.integer :feed_id, default: 0, null: false
t.string :link, default: "", null: false
t.text :title, null: false
t.text :body
t.string :author
t.string :category
t.string :enclosure
t.string :enclosure_type
t.string :digest
t.integer :version, default: 1, null: false
t.datetime :stored_on
t.datetime :modified_on
t.datetime :created_on, null: false
t.datetime :updated_on, null: false
end
add_index :items, [:feed_id, :link], unique: true
end
んんん?ちょっとまて?itemsのfeed_idの情報はどこに入っているんだ?
migrationファイルではなくて実際のSQLを見てみた。
sqlite> .schema feeds
CREATE TABLE "feeds" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "feedlink" varchar NOT NULL, "link" varchar NOT NULL, "title" text NOT NULL, "description" text NOT NULL, "subscribers_count" integer DEFAULT 0 NOT NULL, "image" varchar, "icon" varchar, "modified_on" datetime, "created_on" datetime NOT NULL, "updated_on" datetime NOT NULL);
CREATE UNIQUE INDEX "index_feeds_on_feedlink" ON "feeds" ("feedlink");
sqlite>
sqlite>
sqlite> .schema items
CREATE TABLE "items" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "feed_id" integer DEFAULT 0 NOT NULL, "link" varchar DEFAULT '' NOT NULL, "title" text NOT NULL, "body" text, "author" varchar, "category" varchar, "enclosure" varchar, "enclosure_type" varchar, "digest" varchar, "version" integer DEFAULT 1 NOT NULL, "stored_on" datetime, "modified_on" datetime, "created_on" datetime NOT NULL, "updated_on" datetime NOT NULL, "guid" varchar);
CREATE INDEX "index_items_on_digest" ON "items" ("digest");
CREATE UNIQUE INDEX "index_items_on_feed_id_and_guid" ON "items" ("feed_id", "guid");
CREATE INDEX "items_search_index" ON "items" ("feed_id", "stored_on", "created_on", "id");
sqlite>
・・・・・・ない。どういうこっちゃ。
feedlinkにindexつけてそれをユニークにしているからきっとコレなんだろうとは思うけど。
selectで見た時は見えないんだな。
ActiveRecordはいまいち理解できてないからな〜。おそらく動きからするとfeedsのidカラムがitemsだとfeed_idに相当するもので間違いないと思うんだけど、これが命名規則によるものなのかどうかがわからない・・・
とりあえず覚書として残しておこう。
