Skip to content

Commit

Permalink
Use "Active Record" when referring to library name
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima committed Jul 18, 2023
1 parent 76bdf62 commit ce390da
Show file tree
Hide file tree
Showing 22 changed files with 71 additions and 71 deletions.
32 changes: 16 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ You can also add [custom checks](#custom-checks) or [disable specific checks](#d

:x: **Bad**

ActiveRecord caches database columns at runtime, so if you drop a column, it can cause exceptions until your app reboots.
Active Record caches database columns at runtime, so if you drop a column, it can cause exceptions until your app reboots.

```ruby
class RemoveNameFromUsers < ActiveRecord::Migration[7.0]
Expand All @@ -174,12 +174,12 @@ end
1. Ignore the column:

```ruby
# For ActiveRecord 5+
# For Active Record 5+
class User < ApplicationRecord
self.ignored_columns = ["name"]
end

# For ActiveRecord < 5
# For Active Record < 5
class User < ActiveRecord::Base
def self.columns
super.reject { |c| c.name == "name" }
Expand Down Expand Up @@ -243,7 +243,7 @@ end

:x: **Bad**

ActiveRecord wraps each migration in a transaction, and backfilling in the same transaction that alters a table keeps the table locked for the [duration of the backfill](https://wework.github.io/data/2015/11/05/add-columns-with-default-values-to-large-tables-in-rails-postgres/).
Active Record wraps each migration in a transaction, and backfilling in the same transaction that alters a table keeps the table locked for the [duration of the backfill](https://wework.github.io/data/2015/11/05/add-columns-with-default-values-to-large-tables-in-rails-postgres/).

```ruby
class AddAdminToUsers < ActiveRecord::Migration[7.0]
Expand Down Expand Up @@ -407,7 +407,7 @@ The technique is built on top of database views, using the following steps:

1. Rename the table to some temporary name
2. Create a VIEW using the old table name with addition of a new column as an alias of the old one
3. Add a workaround for ActiveRecord's schema cache
3. Add a workaround for Active Record's schema cache
For the previous example, to rename `name` column to `first_name` of the `users` table, we can run:
Expand All @@ -418,9 +418,9 @@ CREATE VIEW users AS SELECT *, first_name AS name FROM users_column_rename;
COMMIT;
```
As database views do not expose the underlying table schema (default values, not null constraints, indexes, etc), further steps are needed to update the application to use the new table name. ActiveRecord heavily relies on this data, for example, to initialize new models.
As database views do not expose the underlying table schema (default values, not null constraints, indexes, etc), further steps are needed to update the application to use the new table name. Active Record heavily relies on this data, for example, to initialize new models.
To work around this limitation, we need to tell ActiveRecord to acquire this information from original table using the new table name.
To work around this limitation, we need to tell Active Record to acquire this information from original table using the new table name.
**Online Migrations** provides several helpers to implement column renaming:
Expand Down Expand Up @@ -462,12 +462,12 @@ end
(is disabled by default in Active Record >= 7), then you need to ignore old column:
```ruby
# For ActiveRecord 5+
# For Active Record 5+
class User < ApplicationRecord
self.ignored_columns = ["name"]
end
# For ActiveRecord < 5
# For Active Record < 5
class User < ActiveRecord::Base
def self.columns
super.reject { |c| c.name == "name" }
Expand Down Expand Up @@ -523,7 +523,7 @@ The technique is built on top of database views, using the following steps:

1. Rename the database table
2. Create a VIEW using the old table name by pointing to the new table name
3. Add a workaround for ActiveRecord's schema cache
3. Add a workaround for Active Record's schema cache
For the previous example, to rename `name` column to `first_name` of the `users` table, we can run:
Expand All @@ -534,9 +534,9 @@ CREATE VIEW clients AS SELECT * FROM users;
COMMIT;
```
As database views do not expose the underlying table schema (default values, not null constraints, indexes, etc), further steps are needed to update the application to use the new table name. ActiveRecord heavily relies on this data, for example, to initialize new models.
As database views do not expose the underlying table schema (default values, not null constraints, indexes, etc), further steps are needed to update the application to use the new table name. Active Record heavily relies on this data, for example, to initialize new models.
To work around this limitation, we need to tell ActiveRecord to acquire this information from original table using the new table name.
To work around this limitation, we need to tell Active Record to acquire this information from original table using the new table name.
**Online Migrations** provides several helpers to implement table renaming:
Expand Down Expand Up @@ -964,7 +964,7 @@ Add a non-generated column and use callbacks or triggers instead.
:x: **Bad**
When using short integer types as primary key types, [there is a risk](https://m.signalvnoise.com/update-on-basecamp-3-being-stuck-in-read-only-as-of-nov-8-922am-cst/) of running out of IDs on inserts. The default type in ActiveRecord < 5.1 for primary and foreign keys is `INTEGER`, which allows a little over of 2 billion records. Active Record 5.1 changed the default type to `BIGINT`.
When using short integer types as primary key types, [there is a risk](https://m.signalvnoise.com/update-on-basecamp-3-being-stuck-in-read-only-as-of-nov-8-922am-cst/) of running out of IDs on inserts. The default type in Active Record < 5.1 for primary and foreign keys is `INTEGER`, which allows a little over of 2 billion records. Active Record 5.1 changed the default type to `BIGINT`.
```ruby
class CreateUsers < ActiveRecord::Migration[7.0]
Expand Down Expand Up @@ -1154,12 +1154,12 @@ A safer approach is to:
1. ignore the column:

```ruby
# For ActiveRecord 5+
# For Active Record 5+
class User < ApplicationRecord
self.ignored_columns = ["type"]
end
# For ActiveRecord < 5
# For Active Record < 5
class User < ActiveRecord::Base
def self.columns
super.reject { |c| c.name == "type" }
Expand Down Expand Up @@ -1203,7 +1203,7 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/fatkod
## Development
After checking out the repo, run `bundle install` to install dependencies. Run `createdb online_migrations_test` to create a test database. Then, run `bundle exec rake test` to run the tests. This project uses multiple Gemfiles to test against multiple versions of ActiveRecord; you can run the tests against the specific version with `BUNDLE_GEMFILE=gemfiles/activerecord_61.gemfile bundle exec rake test`.
After checking out the repo, run `bundle install` to install dependencies. Run `createdb online_migrations_test` to create a test database. Then, run `bundle exec rake test` to run the tests. This project uses multiple Gemfiles to test against multiple versions of Active Record; you can run the tests against the specific version with `BUNDLE_GEMFILE=gemfiles/activerecord_61.gemfile bundle exec rake test`.
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
Expand Down
4 changes: 2 additions & 2 deletions docs/configuring.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ To mark migrations as safe that were created before installing this gem, configu

config.start_after = 20220101000000

# or if you use multiple databases (ActiveRecord 6+)
# or if you use multiple databases (Active Record 6+)
config.start_after = { primary: 20211112000000, animals: 20220101000000 }
```

Expand All @@ -129,7 +129,7 @@ If your development database version is different from production, you can speci

config.target_version = 10 # or "12.9" etc

# or if you use multiple databases (ActiveRecord 6+)
# or if you use multiple databases (Active Record 6+)
config.target_version = { primary: 10, animals: 14.1 }
```

Expand Down
2 changes: 1 addition & 1 deletion lib/online_migrations/background_migrations/copy_column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def process_batch(relation)
old_value = arel_table[from_column]
if (type_cast_function = type_cast_functions[from_column])
if Utils.ar_version <= 5.2
# ActiveRecord <= 5.2 does not support quoting of Arel::Nodes::NamedFunction
# Active Record <= 5.2 does not support quoting of Arel::Nodes::NamedFunction
old_value = Arel.sql("#{type_cast_function}(#{connection.quote_column_name(from_column)})")
else
old_value = Arel::Nodes::NamedFunction.new(type_cast_function, [old_value])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ def initialize(model_name, associations, _options = {})
end

def relation
# For ActiveRecord 6.1+ we can use `where.missing`
# For Active Record 6.1+ we can use `where.missing`
# https://github.com/rails/rails/pull/34727
associations.inject(model.unscoped) do |relation, association|
reflection = model.reflect_on_association(association)
unless reflection
raise ArgumentError, "'#{model.name}' has no association called '#{association}'"
end

# left_joins was added in ActiveRecord 5.0 - https://github.com/rails/rails/pull/12071
# left_joins was added in Active Record 5.0 - https://github.com/rails/rails/pull/12071
relation
.left_joins(association)
.where(reflection.table_name => { reflection.association_primary_key => nil })
Expand All @@ -31,7 +31,7 @@ def process_batch(relation)
if Utils.ar_version > 5.0
relation.delete_all
else
# Older ActiveRecord generates incorrect query when running delete_all
# Older Active Record generates incorrect query when running delete_all
primary_key = model.primary_key
model.unscoped.where(primary_key => relation.select(primary_key)).delete_all
end
Expand Down
2 changes: 1 addition & 1 deletion lib/online_migrations/background_migrations/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def next_batch_range
# rubocop:disable Lint/UnreachableLoop
iterator.each_batch(of: batch_size, column: batch_column_name, start: next_min_value) do |relation|
if Utils.ar_version <= 4.2
# ActiveRecord <= 4.2 does not support pluck with Arel nodes
# Active Record <= 4.2 does not support pluck with Arel nodes
quoted_column = self.class.connection.quote_column_name(batch_column_name)
batch_range = relation.pluck("MIN(#{quoted_column}), MAX(#{quoted_column})").first
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def copy_columns_in_background(table_name, copy_from, copy_to, model_name: nil,
# @see https://api.rubyonrails.org/classes/ActiveRecord/CounterCache/ClassMethods.html#method-i-reset_counters
#
# @note This method is better suited for large tables (10/100s of millions of records).
# For smaller tables it is probably better and easier to use `reset_counters` from the ActiveRecord.
# For smaller tables it is probably better and easier to use `reset_counters` from the Active Record.
#
def reset_counters_in_background(model_name, *counters, touch: nil, **options)
model_name = model_name.name if model_name.is_a?(Class)
Expand Down Expand Up @@ -224,7 +224,7 @@ def reset_counters_in_background(model_name, *counters, touch: nil, **options)
#
def delete_orphaned_records_in_background(model_name, *associations, **options)
if Utils.ar_version <= 4.2
raise "#{__method__} does not support ActiveRecord <= 4.2 yet"
raise "#{__method__} does not support Active Record <= 4.2 yet"
end

model_name = model_name.name if model_name.is_a?(Class)
Expand Down
4 changes: 2 additions & 2 deletions lib/online_migrations/background_migrations/migration_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class MigrationJob < ActiveRecord::Base

self.table_name = :background_migration_jobs

# For ActiveRecord <= 4.2 needs to fully specify enum values
# For Active Record <= 4.2 needs to fully specify enum values
scope :active, -> { where(status: [statuses[:enqueued], statuses[:running]]) }
scope :completed, -> { where(status: [statuses[:failed], statuses[:succeeded]]) }
scope :stuck, -> do
Expand Down Expand Up @@ -45,7 +45,7 @@ class MigrationJob < ActiveRecord::Base

belongs_to :migration

# For ActiveRecord 5.0+ this is validated by default from belongs_to
# For Active Record 5.0+ this is validated by default from belongs_to
validates :migration, presence: true

validates :min_value, :max_value, presence: true, numericality: { greater_than: 0 }
Expand Down
4 changes: 2 additions & 2 deletions lib/online_migrations/background_migrations/reset_counters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def process_batch(relation)
names = Array.wrap(names)
options = names.extract_options!
touch_updates = touch_attributes_with_time(*names, **options)
# In ActiveRecord 4.2 sanitize_sql_for_assignment is protected
# In Active Record 4.2 sanitize_sql_for_assignment is protected
updates << model.send(:sanitize_sql_for_assignment, touch_updates)
end

Expand All @@ -65,7 +65,7 @@ def has_many_association(counter_association) # rubocop:disable Naming/Predicate
has_many_association = has_many.find do |association|
counter_cache_column = association.counter_cache_column

# ActiveRecord <= 4.2 is able to return only explicitly provided `counter_cache` column.
# Active Record <= 4.2 is able to return only explicitly provided `counter_cache` column.
if !counter_cache_column && Utils.ar_version <= 4.2
counter_cache_column = "#{association.name}_count"
end
Expand Down
4 changes: 2 additions & 2 deletions lib/online_migrations/command_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ def add_unique_key(table_name, column_name = nil, **options)
remove_code: command_str(:remove_unique_key, table_name, column_name)
end

# Implementation is from ActiveRecord
# Implementation is from Active Record
def index_name(table_name, column_name)
max_index_name_size = 62
name = "index_#{table_name}_on_#{Array(column_name) * '_and_'}"
Expand Down Expand Up @@ -805,7 +805,7 @@ def column_for(table_name, column_name)
connection.columns(table_name).find { |column| column.name == column_name.to_s }
end

# From ActiveRecord
# From Active Record
def derive_join_table_name(table1, table2)
[table1.to_s, table2.to_s].sort.join("\0").gsub(/^(.*_)(.+)\0\1(.+)/, '\1\2_\3').tr("\0", "_")
end
Expand Down
2 changes: 1 addition & 1 deletion lib/online_migrations/error_messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def change
end
end
<% else %>
ActiveRecord caches database columns at runtime, so if you drop a column, it can cause exceptions until your app reboots.
Active Record caches database columns at runtime, so if you drop a column, it can cause exceptions until your app reboots.
A safer approach is to:
1. Ignore the column(s):
Expand Down
2 changes: 1 addition & 1 deletion lib/online_migrations/lock_retrier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def with_lock_retries(&block)
else
yield
end
# ActiveRecord::LockWaitTimeout can be used for ActiveRecord 5.2+
# ActiveRecord::LockWaitTimeout can be used for Active Record 5.2+
rescue ActiveRecord::StatementInvalid => e
if lock_timeout_error?(e) && current_attempt <= attempts
current_delay = delay(current_attempt)
Expand Down
10 changes: 5 additions & 5 deletions lib/online_migrations/schema_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,16 @@ def column_rename_table(table_name)
def duplicate_column(old_column_name, new_column_name, columns)
old_column = columns.find { |column| column.name == old_column_name }
new_column = old_column.dup
# ActiveRecord defines only reader for :name
# Active Record defines only reader for :name
new_column.instance_variable_set(:@name, new_column_name)
# Correspond to the ActiveRecord freezing of each column
# Correspond to the Active Record freezing of each column
columns << new_column.freeze
end
end

# @private
module SchemaCache7
# ActiveRecord >= 7.1 changed signature of the methods,
# Active Record >= 7.1 changed signature of the methods,
# see https://github.com/rails/rails/pull/48716.
def primary_keys(connection, table_name)
if (renamed_table = renamed_table?(connection, table_name))
Expand Down Expand Up @@ -154,9 +154,9 @@ def column_rename_table(table_name)
def duplicate_column(old_column_name, new_column_name, columns)
old_column = columns.find { |column| column.name == old_column_name }
new_column = old_column.dup
# ActiveRecord defines only reader for :name
# Active Record defines only reader for :name
new_column.instance_variable_set(:@name, new_column_name)
# Correspond to the ActiveRecord freezing of each column
# Correspond to the Active Record freezing of each column
columns << new_column.freeze
end
end
Expand Down
Loading

0 comments on commit ce390da

Please sign in to comment.