discourse/db/fixtures/009_users.rb
Sam 6a3c8fe69c FEATURE: protect against accidental column or table drops
Often we need to amend our schema, it is tempting to use
drop_table, rename_column and drop_column to amned schema
trouble though is that existing code that is running in production
can depend on the existance of previous schema leading to application
breaking until new code base is deployed.

The commit enforces new rules to ensure we can never drop tables or
columns in migrations and instead use Migration::ColumnDropper and
Migration::TableDropper to defer drop the db objects
2018-03-21 15:43:32 +11:00

126 lines
2.6 KiB
Ruby

# kind of odd, but we need it, we also need to nuke usage of User from inside migrations
# very poor form
user = User.find_by("id <> -1 and username_lower = 'system'")
if user
user.username = UserNameSuggester.suggest("system")
user.save
end
UserEmail.seed do |ue|
ue.id = -1
ue.email = "no_email"
ue.primary = true
ue.user_id = -1
end
User.seed do |u|
u.id = -1
u.name = "system"
u.username = "system"
u.username_lower = "system"
u.password = SecureRandom.hex
u.active = true
u.admin = true
u.moderator = true
u.approved = true
u.trust_level = TrustLevel[4]
end
UserOption.where(user_id: -1).update_all(
email_private_messages: false,
email_direct: false
)
Group.user_trust_level_change!(-1, TrustLevel[4])
Migration::ColumnDropper.drop(
table: 'users',
after_migration: 'DropEmailFromUsers',
columns: %w[
email
email_always
mailing_list_mode
email_digests
email_direct
email_private_messages
external_links_in_new_tab
enable_quoting
dynamic_favicon
disable_jump_reply
edit_history_public
automatically_unpin_topics
digest_after_days
auto_track_topics_after_msecs
new_topic_duration_minutes
last_redirected_to_top_at
auth_token
auth_token_updated_at
],
on_drop: ->() {
STDERR.puts 'Removing superflous users columns!'
}
)
Migration::ColumnDropper.drop(
table: 'users',
after_migration: 'RenameBlockedSilence',
columns: %w[
blocked
],
on_drop: ->() {
STDERR.puts 'Removing user blocked column!'
}
)
Migration::ColumnDropper.drop(
table: 'users',
after_migration: 'AddSilencedTillToUsers',
columns: %w[
silenced
],
on_drop: ->() {
STDERR.puts 'Removing user silenced column!'
}
)
Migration::ColumnDropper.drop(
table: 'users',
after_migration: 'AddTrustLevelLocksToUsers',
columns: %w[
trust_level_locked
],
on_drop: ->() {
STDERR.puts 'Removing user trust_level_locked!'
}
)
# User for the smoke tests
if ENV["SMOKE"] == "1"
UserEmail.seed do |ue|
ue.id = 0
ue.email = "smoke_user@discourse.org"
ue.primary = true
ue.user_id = 0
end
smoke_user = User.seed do |u|
u.id = 0
u.name = "smoke_user"
u.username = "smoke_user"
u.username_lower = "smoke_user"
u.password = "P4ssw0rd"
u.active = true
u.approved = true
u.approved_at = Time.now
u.trust_level = TrustLevel[3]
end.first
UserOption.where(user_id: smoke_user.id).update_all(
email_direct: false,
email_digests: false,
email_private_messages: false,
)
EmailToken.where(user_id: smoke_user.id).update_all(confirmed: true)
end