Commit c158cba6 authored by Alex Kalderimis's avatar Alex Kalderimis

Add timestamps for wiki page metadata

parent 56b298f5
......@@ -74,9 +74,9 @@ class WikiPage
if persisted?
transaction do
slugs.update_all(canonical: false)
slugs.canonical.update_all(updated_at: Time.now, canonical: false)
page_slug = slugs.create_with(canonical: true).find_or_create_by(slug: slug)
page_slug.update_column(:canonical, true) unless page_slug.canonical?
page_slug.update_columns(canonical: true, updated_at: Time.now) unless page_slug.canonical?
end
else
slugs.new(slug: slug, canonical: true)
......@@ -94,12 +94,19 @@ class WikiPage
private
def update_wiki_page_attributes(page)
update_column(:title, page.title) unless page.title == title
update_columns(title: page.title, updated_at: Time.now) unless page.title == title
end
def insert_slugs(strings, is_new, canonical_slug)
creation = Time.now
slug_attrs = strings.map do |slug|
{ wiki_page_meta_id: id, slug: slug, canonical: (is_new && slug == canonical_slug) }
{
wiki_page_meta_id: id,
slug: slug,
canonical: (is_new && slug == canonical_slug),
created_at: creation
}
end
slugs.insert_all(slug_attrs) unless !is_new && slug_attrs.size == 1
......
......@@ -6,12 +6,14 @@ class AddWikiSlug < ActiveRecord::Migration[6.0]
def change
create_table :wiki_page_meta, id: :serial do |t|
t.references :project, index: true, foreign_key: { on_delete: :cascade }, null: false
t.timestamps_with_timezone null: false, default: -> { 'NOW()' }
t.string :title, null: false, limit: 255
end
create_table :wiki_page_slugs, id: :serial do |t|
t.boolean :canonical, default: false, null: false
t.references :wiki_page_meta, index: true, foreign_key: { on_delete: :cascade }, null: false
t.timestamps_with_timezone null: false, default: -> { 'NOW()' }
t.string :slug, null: false, limit: 2048
t.index [:slug, :wiki_page_meta_id], unique: true
t.index [:wiki_page_meta_id], name: 'one_canonical_wiki_page_slug_per_metadata', unique: true, where: "(canonical = true)"
......
......@@ -4669,6 +4669,8 @@ ActiveRecord::Schema.define(version: 2020_03_13_123934) do
create_table "wiki_page_meta", id: :serial, force: :cascade do |t|
t.bigint "project_id", null: false
t.datetime_with_timezone "created_at", default: -> { "now()" }, null: false
t.datetime_with_timezone "updated_at", default: -> { "now()" }, null: false
t.string "title", limit: 255, null: false
t.index ["project_id"], name: "index_wiki_page_meta_on_project_id"
end
......@@ -4676,6 +4678,8 @@ ActiveRecord::Schema.define(version: 2020_03_13_123934) do
create_table "wiki_page_slugs", id: :serial, force: :cascade do |t|
t.boolean "canonical", default: false, null: false
t.bigint "wiki_page_meta_id", null: false
t.datetime_with_timezone "created_at", default: -> { "now()" }, null: false
t.datetime_with_timezone "updated_at", default: -> { "now()" }, null: false
t.string "slug", limit: 2048, null: false
t.index ["slug", "wiki_page_meta_id"], name: "index_wiki_page_slugs_on_slug_and_wiki_page_meta_id", unique: true
t.index ["wiki_page_meta_id"], name: "index_wiki_page_slugs_on_wiki_page_meta_id"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment