Add sailors log remote database

This commit is contained in:
Max Wofford
2025-02-22 23:12:00 -05:00
parent 025e11aaa7
commit 4720cd7a74
6 changed files with 52 additions and 8 deletions

View File

@@ -0,0 +1,21 @@
class SailorsLog::SlackNotificationPreference < ::ApplicationRecord
self.abstract_class = true
connects_to database: { reading: :sailors_log, writing: :sailors_log }
self.table_name = "SlackNotificationPreference"
scope :enabled, -> { where(enabled: true) }
end
class OneTime::ImportFromSailorsLogJob < ApplicationJob
queue_as :default
def perform
# Import from SailorsLog
SailorsLog::SlackNotificationPreference.enabled.each do |preference|
slnp = ::SailorsLogNotificationPreference.find_or_create_by(
slack_uid: preference.slack_user_id,
slack_channel_id: preference.slack_channel_id
)
end
end
end

View File

@@ -2,7 +2,7 @@ class SailorsLog < ApplicationRecord
validates :slack_uid, presence: true, uniqueness: true
validates :projects_summary, presence: true
after_create :initialize_projects_summary
before_create :initialize_projects_summary
has_many :notification_preferences,
class_name: "SailorsLogNotificationPreference",
@@ -17,10 +17,9 @@ class SailorsLog < ApplicationRecord
private
def initialize_projects_summary
self.projects_summary = {}
return unless projects_summary.blank?
Heartbeat.where(user_id: slack_uid).distinct.pluck(:project).each do |project|
self.projects_summary[project] = Heartbeat.where(user_id: slack_uid, project: project).duration_seconds
end
save! if changed?
end
end

View File

@@ -1,14 +1,23 @@
class SailorsLogNotificationPreference < ApplicationRecord
after_create :ensure_sailors_log_exists
before_validation :ensure_sailors_log_exists
belongs_to :sailors_log,
class_name: "SailorsLog",
foreign_key: :slack_uid,
primary_key: :slack_uid,
optional: true
primary_key: :slack_uid
validates :slack_uid, uniqueness: {
scope: :slack_channel_id,
message: "already has a notification preference for this channel"
}
private
def ensure_sailors_log_exists
SailorsLog.find_or_create_by(slack_uid: slack_uid)
return if sailors_log.present?
sailors_log = SailorsLog.find_or_create_by(slack_uid: slack_uid)
self.sailors_log = sailors_log
sailors_log.send(:initialize_projects_summary)
end
end

View File

@@ -18,6 +18,9 @@ development:
wakatime:
url: <%= ENV['WAKATIME_DATABASE_URL'] %>
replica: true
sailors_log:
url: <%= ENV['SAILORS_LOG_DATABASE_URL'] %>
replica: true
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
@@ -43,6 +46,9 @@ production:
wakatime:
url: <%= ENV['WAKATIME_DATABASE_URL'] %>
replica: true
sailors_log:
url: <%= ENV['SAILORS_LOG_DATABASE_URL'] %>
replica: true
cache:
<<: *default
database: storage/production_cache.sqlite3

View File

@@ -0,0 +1,8 @@
class AddUniqueIndexToSailorsLogNotificationPreferences < ActiveRecord::Migration[8.0]
def change
add_index :sailors_log_notification_preferences,
[ :slack_uid, :slack_channel_id ],
unique: true,
name: 'idx_sailors_log_notification_preferences_unique_user_channel'
end
end

3
db/schema.rb generated
View File

@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[8.0].define(version: 2025_02_22_032930) do
ActiveRecord::Schema[8.0].define(version: 2025_02_22_082500) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_catalog.plpgsql"
@@ -128,6 +128,7 @@ ActiveRecord::Schema[8.0].define(version: 2025_02_22_032930) do
t.boolean "enabled", default: true, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["slack_uid", "slack_channel_id"], name: "idx_sailors_log_notification_preferences_unique_user_channel", unique: true
end
create_table "sailors_log_slack_notifications", force: :cascade do |t|