2021-07-09 12:04:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
require "pg"
|
2023-04-26 03:59:20 -04:00
|
|
|
require "date"
|
2021-07-09 12:04:28 -04:00
|
|
|
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-02-28 14:50:55 -05:00
|
|
|
usage = <<-TEXT
|
2021-07-09 12:04:28 -04:00
|
|
|
Commands:
|
|
|
|
ruby db_timestamp_updater.rb yesterday <date> move all timestamps by x days so that <date> will be moved to yesterday
|
|
|
|
ruby db_timestamp_updater.rb 100 move all timestamps forward by 100 days
|
|
|
|
ruby db_timestamp_updater.rb -100 move all timestamps backward by 100 days
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-02-28 14:50:55 -05:00
|
|
|
TEXT
|
2021-07-09 12:04:28 -04:00
|
|
|
|
|
|
|
class TimestampsUpdater
|
2021-07-13 04:36:41 -04:00
|
|
|
def initialize(schema, ignore_tables)
|
|
|
|
@schema = schema
|
|
|
|
@ignore_tables = ignore_tables
|
2021-07-09 12:04:28 -04:00
|
|
|
@raw_connection =
|
|
|
|
PG.connect(
|
|
|
|
host: ENV["DISCOURSE_DB_HOST"] || "localhost",
|
|
|
|
port: ENV["DISCOURSE_DB_PORT"] || 5432,
|
|
|
|
dbname: ENV["DISCOURSE_DB_NAME"] || "discourse_development",
|
|
|
|
user: ENV["DISCOURSE_DB_USERNAME"] || "postgres",
|
|
|
|
password: ENV["DISCOURSE_DB_PASSWORD"] || "",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def move_by(days)
|
|
|
|
postgresql_date_types = ["timestamp without time zone", "timestamp with time zone", "date"]
|
|
|
|
|
|
|
|
postgresql_date_types.each do |data_type|
|
|
|
|
columns = all_columns_of_type(data_type)
|
|
|
|
columns.each do |c|
|
|
|
|
table = c["table_name"]
|
2021-07-13 04:36:41 -04:00
|
|
|
next if @ignore_tables.include? table
|
2021-07-09 12:04:28 -04:00
|
|
|
column = c["column_name"]
|
2021-07-13 04:36:41 -04:00
|
|
|
|
2021-08-12 11:24:21 -04:00
|
|
|
if has_unique_index(table, column)
|
|
|
|
move_timestamps_respect_constraints(table, column, days)
|
|
|
|
else
|
|
|
|
move_timestamps(table, column, days)
|
|
|
|
end
|
2021-07-09 12:04:28 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def move_to_yesterday(date)
|
|
|
|
days = (Date.today.prev_day - date).to_i
|
|
|
|
move_by days
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def all_columns_of_type(data_type)
|
|
|
|
sql = <<~SQL
|
|
|
|
SELECT c.column_name, c.table_name
|
|
|
|
FROM information_schema.columns AS c
|
|
|
|
JOIN information_schema.tables AS t
|
|
|
|
ON c.table_name = t.table_name
|
2021-07-13 04:36:41 -04:00
|
|
|
WHERE c.table_schema = '#{@schema}'
|
2021-07-13 09:21:15 -04:00
|
|
|
AND t.table_schema = '#{@schema}'
|
2021-07-09 12:04:28 -04:00
|
|
|
AND c.data_type = '#{data_type}'
|
|
|
|
AND t.table_type = 'BASE TABLE'
|
|
|
|
SQL
|
|
|
|
@raw_connection.exec(sql)
|
|
|
|
end
|
|
|
|
|
2021-08-12 11:24:21 -04:00
|
|
|
def has_unique_index(table, column)
|
|
|
|
# This detects unique indices created with "CREATE UNIQUE INDEX".
|
|
|
|
# This also detects unique constraints and primary keys,
|
|
|
|
# because postgresql creates unique indices for them.
|
|
|
|
sql = <<~SQL
|
|
|
|
SELECT 1
|
|
|
|
FROM pg_class t,
|
|
|
|
pg_class i,
|
|
|
|
pg_index ix,
|
|
|
|
pg_attribute a,
|
|
|
|
pg_namespace ns
|
|
|
|
WHERE t.oid = ix.indrelid
|
|
|
|
AND i.oid = ix.indexrelid
|
|
|
|
AND a.attrelid = t.oid
|
|
|
|
AND a.attnum = ANY (ix.indkey)
|
|
|
|
AND t.relnamespace = ns.oid
|
|
|
|
AND ns.nspname = '#{@schema}'
|
|
|
|
AND t.relname = '#{table}'
|
|
|
|
AND a.attname = '#{column}'
|
|
|
|
AND ix.indisunique
|
|
|
|
LIMIT 1;
|
|
|
|
SQL
|
|
|
|
result = @raw_connection.exec(sql)
|
|
|
|
result.any?
|
|
|
|
end
|
|
|
|
|
2021-07-09 12:04:28 -04:00
|
|
|
def move_timestamps(table_name, column_name, days)
|
|
|
|
operator = days < 0 ? "-" : "+"
|
2021-08-12 11:24:21 -04:00
|
|
|
interval_expression = "#{operator} INTERVAL '#{days.abs} days'"
|
|
|
|
update_table(table_name, column_name, interval_expression)
|
|
|
|
end
|
|
|
|
|
|
|
|
def move_timestamps_respect_constraints(table_name, column_name, days)
|
|
|
|
# add 1000 years to the interval to avoid uniqueness conflicts:
|
|
|
|
operator = days < 0 ? "-" : "+"
|
|
|
|
interval_expression = "#{operator} INTERVAL '1000 years #{days.abs} days'"
|
|
|
|
update_table(table_name, column_name, interval_expression)
|
|
|
|
|
|
|
|
# return back by 1000 years:
|
|
|
|
operator = days < 0 ? "+" : "-"
|
|
|
|
interval_expression = "#{operator} INTERVAL '1000 years'"
|
|
|
|
update_table(table_name, column_name, interval_expression)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_table(table_name, column_name, interval_expression)
|
2021-07-09 12:04:28 -04:00
|
|
|
sql = <<~SQL
|
|
|
|
UPDATE #{table_name}
|
2021-08-12 11:24:21 -04:00
|
|
|
SET #{column_name} = #{column_name} #{interval_expression}
|
2021-07-09 12:04:28 -04:00
|
|
|
SQL
|
|
|
|
@raw_connection.exec(sql)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def is_i?(string)
|
2023-01-07 06:53:14 -05:00
|
|
|
begin
|
2021-07-09 12:04:28 -04:00
|
|
|
true if Integer(string)
|
|
|
|
rescue StandardError
|
|
|
|
false
|
2023-01-07 06:53:14 -05:00
|
|
|
end
|
2021-07-09 12:04:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def is_date?(string)
|
2023-01-07 06:53:14 -05:00
|
|
|
begin
|
2021-07-09 12:04:28 -04:00
|
|
|
true if Date.parse(string)
|
|
|
|
rescue StandardError
|
|
|
|
false
|
2023-01-07 06:53:14 -05:00
|
|
|
end
|
2021-07-09 12:04:28 -04:00
|
|
|
end
|
|
|
|
|
2021-07-13 04:36:41 -04:00
|
|
|
def create_updater
|
2024-07-29 11:20:14 -04:00
|
|
|
ignore_tables = %w[
|
|
|
|
user_second_factors
|
|
|
|
discourse_voting_topic_vote_count
|
|
|
|
discourse_voting_votes
|
|
|
|
discourse_voting_category_settings
|
|
|
|
]
|
2021-07-13 04:36:41 -04:00
|
|
|
TimestampsUpdater.new "public", ignore_tables
|
|
|
|
end
|
|
|
|
|
2021-07-09 12:04:28 -04:00
|
|
|
if ARGV.length == 2 && ARGV[0] == "yesterday" && is_date?(ARGV[1])
|
|
|
|
date = Date.parse(ARGV[1])
|
2021-07-13 04:36:41 -04:00
|
|
|
updater = create_updater
|
|
|
|
updater.move_to_yesterday date
|
2021-07-09 12:04:28 -04:00
|
|
|
elsif ARGV.length == 1 && is_i?(ARGV[0])
|
|
|
|
days = ARGV[0].to_i
|
2021-07-13 04:36:41 -04:00
|
|
|
updater = create_updater
|
|
|
|
updater.move_by days
|
2021-07-09 12:04:28 -04:00
|
|
|
else
|
|
|
|
puts usage
|
|
|
|
exit 1
|
|
|
|
end
|