A platform for community discussion. Free, open, simple.
MT: Import event timezones in generic bulk import
Gerhard Schlager72281636ab18
diff --git a/script/bulk_import/generic_bulk.rb b/script/bulk_import/generic_bulk.rbindex 64a9e7c81a4..97718b759ff 100644--- a/script/bulk_import/generic_bulk.rb+++ b/script/bulk_import/generic_bulk.rb@@ -2505,7 +2505,7 @@ class BulkImport::Generic < BulkImport::BaseWHERE id = :event_idSQL- raw.gsub!(event["placeholder"], event_bbcode(event_details)) if event_details+ raw.gsub!(event["placeholder"], event_bbcode(event_details).to_s) if event_detailsendif (quotes = placeholders&.fetch("quotes", nil))@@ -2685,6 +2685,7 @@ class BulkImport::Generic < BulkImport::Basestarts_at = to_datetime(event["starts_at"])ends_at = to_datetime(event["ends_at"])+ timezone = event_timezone(event)status = DiscourseEvents::Events::Event.statuses[event["status"]].to_sname =if (name = event["name"].presence)@@ -2696,9 +2697,10 @@ class BulkImport::Generic < BulkImport::Basecustom_fields = event["custom_fields"] ? JSON.parse(event["custom_fields"]) : niltext = +"[event"- text << %{ start="#{starts_at.utc.strftime("%Y-%m-%d %H:%M")}"} if starts_at- text << %{ end="#{ends_at.utc.strftime("%Y-%m-%d %H:%M")}"} if ends_at- text << %{ timezone="UTC"}+ # The plugin reads start and end as local times in the event's timezone.+ text << %{ start="#{format_event_time(starts_at, timezone)}"} if starts_at+ text << %{ end="#{format_event_time(ends_at, timezone)}"} if ends_at+ text << %{ timezone="#{timezone}"}text << %{ status="#{status}"} if statustext << %{ name="#{name}"} if nametext << %{ url="#{url}"} if url@@ -2708,6 +2710,15 @@ class BulkImport::Generic < BulkImport::Basetextend+ def event_timezone(event)+ timezone = event["timezone"].presence+ timezone && ActiveSupport::TimeZone[timezone] ? timezone : "UTC"+ end++ def format_event_time(time, timezone)+ time.in_time_zone(timezone).strftime("%Y-%m-%d %H:%M")+ end+def import_post_custom_fieldsputs "", "Importing post custom fields..."@@ -4329,7 +4340,6 @@ class BulkImport::Generic < BulkImport::BaseSQLdefault_custom_fields = "{}"- timezone = "UTC"public_group_invitees = "{#{DiscourseEvents::Events::Event::PUBLIC_GROUP}}"standalone_invitees = "{}"@@ -4339,6 +4349,14 @@ class BulkImport::Generic < BulkImport::Basepost_id = post_id_from_imported_id(row["post_id"])next if !post_id || existing_events.include?(post_id)+ timezone = event_timezone(row)+ if row["timezone"].present? && timezone != row["timezone"]+ log_import_issue(+ "invalid event timezone, using UTC",+ "#{row["timezone"]} (event #{row["id"]})",+ )+ end+{id: post_id,status: row["status"],@@ -4347,7 +4365,7 @@ class BulkImport::Generic < BulkImport::Basename: row["name"],url: row["url"] ? row["url"][0..999] : nil,custom_fields: row["custom_fields"] || default_custom_fields,- timezone: timezone,+ timezone:,raw_invitees:(if row["status"] == DiscourseEvents::Events::Event.statuses[:public]diff --git a/spec/script/bulk_import/generic_bulk_spec.rb b/spec/script/bulk_import/generic_bulk_spec.rbindex 41e6211539a..341cb209ccf 100644--- a/spec/script/bulk_import/generic_bulk_spec.rb+++ b/spec/script/bulk_import/generic_bulk_spec.rb@@ -656,6 +656,134 @@ if generic_import_dependencies_availableendend+ describe "event timezones" do+ fab!(:vienna_post, :post)+ fab!(:untimed_post, :post)+ fab!(:invalid_post, :post)++ let(:source_db) { SQLite3::Database.new(":memory:", results_as_hash: true) }+ let(:importer) do+ described_class.allocate.tap do |instance|+ instance.instance_variable_set(:@source_db, source_db)+ instance.instance_variable_set(+ :@posts,+ { 1 => vienna_post.id, 2 => untimed_post.id, 3 => invalid_post.id },+ )+ instance.instance_variable_set(+ :@topic_id_by_post_id,+ [vienna_post, untimed_post, invalid_post].to_h { |post| [post.id, post.topic_id] },+ )+ instance.instance_variable_set(+ :@import_issue_log_path,+ File.join(Dir.mktmpdir, "issues.log"),+ )+ instance.instance_variable_set(+ :@raw_connection,+ ActiveRecord::Base.connection.raw_connection,+ )+ instance.instance_variable_set(:@encoder, PG::TextEncoder::CopyRow.new)+ end+ end++ before { skip "requires the events plugin" unless defined?(DiscourseEvents::Events) }++ after { source_db.close }++ def create_events_table(with_timezone: true)+ source_db.execute(<<~SQL)+ CREATE TABLE events (+ id INTEGER, post_id INTEGER, status INTEGER, starts_at DATETIME, ends_at DATETIME,+ name TEXT, url TEXT, custom_fields JSON_TEXT #{", timezone TEXT" if with_timezone}+ )+ SQL+ end++ def insert_event(id, timezone = nil)+ columns = %w[id post_id status starts_at ends_at]+ values = [id, id, 0, "2026-07-01T08:00:00Z", "2026-07-01T09:30:00Z"]+ if timezone+ columns << "timezone"+ values << timezone+ end+ source_db.execute(+ "INSERT INTO events (#{columns.join(", ")}) VALUES (#{(["?"] * values.size).join(", ")})",+ values,+ )+ end++ def event_row(id)+ source_db.get_first_row("SELECT * FROM events WHERE id = ?", id)+ end++ describe "#event_bbcode" do+ it "writes start and end as local times in the event's timezone" do+ create_events_table+ insert_event(1, "Europe/Vienna")++ bbcode = importer.event_bbcode(event_row(1))++ expect(bbcode).to include(+ 'start="2026-07-01 10:00" end="2026-07-01 11:30" timezone="Europe/Vienna"',+ )+ expect(ActiveSupport::TimeZone["Europe/Vienna"].parse("2026-07-01 10:00")).to eq(+ Time.utc(2026, 7, 1, 8),+ )+ end++ it "falls back to UTC for missing and invalid timezones" do+ create_events_table+ insert_event(2)+ insert_event(3, "+01:00")++ [2, 3].each do |id|+ expect(importer.event_bbcode(event_row(id))).to include(+ 'start="2026-07-01 08:00" end="2026-07-01 09:30" timezone="UTC"',+ )+ end+ end++ it "uses UTC when the events table has no timezone column" do+ create_events_table(with_timezone: false)+ insert_event(1)++ expect(importer.event_bbcode(event_row(1))).to include('timezone="UTC"')+ end+ end++ describe "#import_post_events" do+ it "imports valid timezones and logs invalid ones" do+ create_events_table+ insert_event(1, "Europe/Vienna")+ insert_event(2)+ insert_event(3, "+01:00")++ importer.import_post_events++ expect(+ DiscourseEvents::Events::Event.where(+ id: [vienna_post.id, untimed_post.id, invalid_post.id],+ ).pluck(:id, :timezone),+ ).to contain_exactly(+ [vienna_post.id, "Europe/Vienna"],+ [untimed_post.id, "UTC"],+ [invalid_post.id, "UTC"],+ )+ expect(File.read(importer.instance_variable_get(:@import_issue_log_path))).to eq(+ "[invalid event timezone, using UTC] +01:00 (event 3)\n",+ )+ end++ it "imports events from intermediate databases without a timezone column" do+ create_events_table(with_timezone: false)+ insert_event(1)++ importer.import_post_events++ expect(DiscourseEvents::Events::Event.find(vienna_post.id).timezone).to eq("UTC")+ end+ end+ end+describe "mapping selection" dofab!(:canonical_user, :user)fab!(:other_user, :user)