Commit c4fbaa06 authored by Alex Kalderimis's avatar Alex Kalderimis

Remove DB timestamp defaults

This removes the DB timestamp column defaults, and manages those values
in the model domain object instead.
parent c158cba6
...@@ -74,9 +74,9 @@ class WikiPage ...@@ -74,9 +74,9 @@ class WikiPage
if persisted? if persisted?
transaction do transaction do
slugs.canonical.update_all(updated_at: Time.now, canonical: false) slugs.canonical.update_all(canonical: false)
page_slug = slugs.create_with(canonical: true).find_or_create_by(slug: slug) page_slug = slugs.create_with(canonical: true).find_or_create_by(slug: slug)
page_slug.update_columns(canonical: true, updated_at: Time.now) unless page_slug.canonical? page_slug.update_columns(canonical: true) unless page_slug.canonical?
end end
else else
slugs.new(slug: slug, canonical: true) slugs.new(slug: slug, canonical: true)
...@@ -91,21 +91,30 @@ class WikiPage ...@@ -91,21 +91,30 @@ class WikiPage
self.canonical_slug = wiki_page.slug self.canonical_slug = wiki_page.slug
end end
def update_columns(attrs = {})
super(attrs.reverse_merge(updated_at: Time.now.utc))
end
def self.update_all(attrs = {})
super(attrs.reverse_merge(updated_at: Time.now.utc))
end
private private
def update_wiki_page_attributes(page) def update_wiki_page_attributes(page)
update_columns(title: page.title, updated_at: Time.now) unless page.title == title update_columns(title: page.title) unless page.title == title
end end
def insert_slugs(strings, is_new, canonical_slug) def insert_slugs(strings, is_new, canonical_slug)
creation = Time.now creation = Time.now.utc
slug_attrs = strings.map do |slug| slug_attrs = strings.map do |slug|
{ {
wiki_page_meta_id: id, wiki_page_meta_id: id,
slug: slug, slug: slug,
canonical: (is_new && slug == canonical_slug), canonical: (is_new && slug == canonical_slug),
created_at: creation created_at: creation,
updated_at: creation
} }
end end
slugs.insert_all(slug_attrs) unless !is_new && slug_attrs.size == 1 slugs.insert_all(slug_attrs) unless !is_new && slug_attrs.size == 1
......
...@@ -14,5 +14,13 @@ class WikiPage ...@@ -14,5 +14,13 @@ class WikiPage
} }
scope :canonical, -> { where(canonical: true) } scope :canonical, -> { where(canonical: true) }
def update_columns(attrs = {})
super(attrs.reverse_merge(updated_at: Time.now.utc))
end
def self.update_all(attrs = {})
super(attrs.reverse_merge(updated_at: Time.now.utc))
end
end end
end end
...@@ -6,14 +6,14 @@ class AddWikiSlug < ActiveRecord::Migration[6.0] ...@@ -6,14 +6,14 @@ class AddWikiSlug < ActiveRecord::Migration[6.0]
def change def change
create_table :wiki_page_meta, id: :serial do |t| create_table :wiki_page_meta, id: :serial do |t|
t.references :project, index: true, foreign_key: { on_delete: :cascade }, null: false t.references :project, index: true, foreign_key: { on_delete: :cascade }, null: false
t.timestamps_with_timezone null: false, default: -> { 'NOW()' } t.timestamps_with_timezone null: false
t.string :title, null: false, limit: 255 t.string :title, null: false, limit: 255
end end
create_table :wiki_page_slugs, id: :serial do |t| create_table :wiki_page_slugs, id: :serial do |t|
t.boolean :canonical, default: false, null: false t.boolean :canonical, default: false, null: false
t.references :wiki_page_meta, index: true, foreign_key: { on_delete: :cascade }, 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.timestamps_with_timezone null: false
t.string :slug, null: false, limit: 2048 t.string :slug, null: false, limit: 2048
t.index [:slug, :wiki_page_meta_id], unique: true 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)" t.index [:wiki_page_meta_id], name: 'one_canonical_wiki_page_slug_per_metadata', unique: true, where: "(canonical = true)"
......
...@@ -4669,8 +4669,8 @@ ActiveRecord::Schema.define(version: 2020_03_13_123934) do ...@@ -4669,8 +4669,8 @@ ActiveRecord::Schema.define(version: 2020_03_13_123934) do
create_table "wiki_page_meta", id: :serial, force: :cascade do |t| create_table "wiki_page_meta", id: :serial, force: :cascade do |t|
t.bigint "project_id", null: false t.bigint "project_id", null: false
t.datetime_with_timezone "created_at", default: -> { "now()" }, null: false t.datetime_with_timezone "created_at", null: false
t.datetime_with_timezone "updated_at", default: -> { "now()" }, null: false t.datetime_with_timezone "updated_at", null: false
t.string "title", limit: 255, null: false t.string "title", limit: 255, null: false
t.index ["project_id"], name: "index_wiki_page_meta_on_project_id" t.index ["project_id"], name: "index_wiki_page_meta_on_project_id"
end end
...@@ -4678,8 +4678,8 @@ ActiveRecord::Schema.define(version: 2020_03_13_123934) do ...@@ -4678,8 +4678,8 @@ ActiveRecord::Schema.define(version: 2020_03_13_123934) do
create_table "wiki_page_slugs", id: :serial, force: :cascade do |t| create_table "wiki_page_slugs", id: :serial, force: :cascade do |t|
t.boolean "canonical", default: false, null: false t.boolean "canonical", default: false, null: false
t.bigint "wiki_page_meta_id", null: false t.bigint "wiki_page_meta_id", null: false
t.datetime_with_timezone "created_at", default: -> { "now()" }, null: false t.datetime_with_timezone "created_at", null: false
t.datetime_with_timezone "updated_at", default: -> { "now()" }, null: false t.datetime_with_timezone "updated_at", null: false
t.string "slug", limit: 2048, 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 ["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" 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