Skip to content

Commit bdf2c75

Browse files
committed
fix connection problem of postgres accessory
1 parent 8ea7766 commit bdf2c75

File tree

2 files changed

+33
-36
lines changed

2 files changed

+33
-36
lines changed

config/deploy.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ env:
4040
clear:
4141
# The hostname is constructed as [service_name]-[accessory_name].
4242
DB_HOST: telegram_spam_sniper_bot-postgres
43+
DB_PORT: 5432
4344
POSTGRES_USER: rails_user
4445
POSTGRES_DB: telegram_spam_sniper_bot_production
4546
# Run the Solid Queue Supervisor inside the web server's Puma process to do jobs.
@@ -101,7 +102,7 @@ accessories:
101102
postgres:
102103
image: postgres:15
103104
host: 192.168.1.188
104-
port: "127.0.0.1:5432:5432"
105+
port: "5432:5432"
105106
env:
106107
clear:
107108
POSTGRES_USER: rails_user
@@ -145,6 +146,7 @@ accessories:
145146
- "/tmp/migration_data:/rails/db/data"
146147
# Mount the SAME storage volume as the main app to access the database
147148
- "telegram_spam_sniper_bot_storage:/rails/storage"
149+
148150
json_importer:
149151
host: 192.168.1.188
150152
image: samrayleung/telegram_spam_sniper_bot
@@ -175,6 +177,7 @@ accessories:
175177
- "/tmp/migration_data:/rails/db/data"
176178
# Mount the SAME storage volume as the main app to access the database
177179
- "telegram_spam_sniper_bot_storage:/rails/storage"
180+
178181
backfiller:
179182
host: 192.168.1.188
180183
image: samrayleung/telegram_spam_sniper_bot

lib/tasks/pg_migration.rake

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -27,49 +27,43 @@ namespace :pg_migration do
2727
end
2828

2929
data = JSON.parse(File.read(filepath))
30-
now = Time.current # Use the same timestamp for all imported records
3130

32-
# Import in dependency order
33-
if data["trained_messages"]
34-
puts "Importing #{data["trained_messages"].length} TrainedMessage records..."
31+
ActiveRecord::Base.transaction do
32+
# Import in dependency order
3533

36-
# Prepare the data for bulk insert
37-
attributes = data["trained_messages"].map do |record|
38-
record.delete("id")
39-
# Add timestamps if they don't exist in your JSON data
40-
record["created_at"] ||= now
41-
record["updated_at"] ||= now
42-
record
34+
if data["trained_messages"]
35+
puts "Importinng TrainedMessage records.."
36+
data["trained_messages"].each do |record|
37+
record.delete("id")
38+
TrainedMessage.insert!(record)
39+
print "."
40+
end
41+
puts "\nImported #{data["trained_messages"].length} TrainedMessage records"
4342
end
4443

45-
# Perform the bulk insert
46-
TrainedMessage.insert_all!(attributes)
47-
end
48-
49-
# Repeat for BannedUser
50-
if data["banned_users"]
51-
puts "Importing #{data["banned_users"].length} BannedUser records..."
52-
attributes = data["banned_users"].map do |record|
53-
record.delete("id")
54-
record["created_at"] ||= now
55-
record["updated_at"] ||= now
56-
record
44+
if data["banned_users"]
45+
puts "Importing BannedUser records..."
46+
data["banned_users"].each do |record|
47+
record.delete("id")
48+
BannedUser.insert!(record)
49+
print "."
50+
end
51+
puts "\nImported #{data["banned_users"].length} BannedUser records"
5752
end
58-
BannedUser.insert_all!(attributes)
59-
end
6053

61-
# Repeat for GroupClassifierState
62-
if data["group_classifier_states"]
63-
puts "Importing #{data["group_classifier_states"].length} GroupClassifierStates records..."
64-
attributes = data["group_classifier_states"].map do |record|
65-
record.delete("id")
66-
record["created_at"] ||= now
67-
record["updated_at"] ||= now
68-
record
54+
if data["group_classifier_states"]
55+
puts "Importing GroupClassifierStates records..."
56+
data["group_classifier_states"].each do |record|
57+
# Find the existing record by its unique group_id or prepare a new one
58+
state = GroupClassifierState.find_or_initialize_by(group_id: record["group_id"])
59+
60+
# Update its attributes with the data from the JSON file
61+
state.update!(record.except("id"))
62+
print "."
63+
end
64+
puts "\nImported #{data["group_classifier_states"].length} GroupClassifierStates records"
6965
end
70-
GroupClassifierState.insert_all!(attributes)
7166
end
72-
7367
puts "Migration complete!"
7468
end
7569
end

0 commit comments

Comments
 (0)