Skip to content

Commit e1b1293

Browse files
committed
Fix idempotency of migration on MySQL
- Add quotes to usernames in error message
1 parent 4ccb6f6 commit e1b1293

File tree

4 files changed

+10
-4
lines changed

4 files changed

+10
-4
lines changed

app/models/runtime/constraints/process_user_policy.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ def validate
1111
return if @process.user.blank?
1212
return if @allowed_users.map(&:downcase).include?(@process.user.downcase)
1313

14-
@errors.add(:user, sprintf(ERROR_MSG, requested_user: @process.user, allowed_users: @allowed_users.join(', ')))
14+
@errors.add(:user, sprintf(ERROR_MSG, requested_user: @process.user, allowed_users: formatted_users_for_error))
15+
end
16+
17+
private
18+
19+
def formatted_users_for_error
20+
@allowed_users.map { |u| "'#{u}'" }.join(', ')
1521
end
1622
end

db/migrations/20250610212414_add_user_to_processes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Sequel.migration do
22
up do
33
alter_table :processes do
4-
add_column :user, String, null: true, default: nil, size: 255, if_not_exists: true
4+
add_column :user, String, null: true, default: nil, size: 255 unless @db.schema(:processes).map(&:first).include?(:user)
55
end
66
end
77

spec/migrations/20250610212414_add_user_to_processes_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
describe 'idempotency of up' do
1717
context '`user` column already exists' do
1818
before do
19-
db.add_column :processes, :user, String, size: 255
19+
db.add_column :processes, :user, String, size: 255, if_not_exists: true
2020
end
2121

2222
it 'does not fail' do

spec/unit/models/runtime/constraints/process_user_policy_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
let(:process_user) { 'vcarp' }
4141

4242
it 'is not valid' do
43-
expect(validator).to validate_with_error(process, :user, sprintf(ProcessUserPolicy::ERROR_MSG, requested_user: process.user, allowed_users: allowed_users.join(', ')))
43+
expect(validator).to validate_with_error(process, :user, sprintf(ProcessUserPolicy::ERROR_MSG, requested_user: process.user, allowed_users: "'vcap', 'ContainerUser'"))
4444
end
4545
end
4646

0 commit comments

Comments
 (0)