Skip to content

Commit

Permalink
Rails V7.1.0 (#5)
Browse files Browse the repository at this point in the history
* Update CI and gemspec

* Rails 7.1 Updates

* Test against 7.1.2 as well

* Bump version
  • Loading branch information
malomalo authored Dec 5, 2023
1 parent a36d7ac commit a990e6e
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 40 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

strategy:
matrix:
rails: ['7.0.8']
rails: ['7.1.0', '7.1.2']
ruby-version: ['3.1']
postgres-version: ['15']

Expand Down Expand Up @@ -50,7 +50,7 @@ jobs:

strategy:
matrix:
rails: ['v7.0.8']
rails: ['v7.1.0', 'v7.1.2']
ruby-version: ['3.1']
postgres-version: ['15']

Expand Down Expand Up @@ -102,7 +102,7 @@ jobs:

strategy:
matrix:
rails: ['v7.0.8']
rails: ['v7.1.0', 'v7.1.2']
ruby-version: ['3.1']
postgres-version: ['15']

Expand Down Expand Up @@ -141,7 +141,7 @@ jobs:

strategy:
matrix:
rails: ['v7.0.8']
rails: ['v7.1.0', 'v7.1.2']
ruby-version: ['3.1']
postgres-version: ['15']

Expand Down
26 changes: 20 additions & 6 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,28 @@ Bundler.require(:development)
require 'fileutils'
require "rake/testtask"

# Test Task
Rake::TestTask.new do |t|
t.libs << 'lib' << 'test'
t.test_files = FileList[ARGV[1] ? ARGV[1] : 'test/**/*_test.rb']
t.warning = true
# t.verbose = true
# ==== Test Tasks =============================================================
ADAPTERS = %w(postgres sqlite)

namespace :test do
ADAPTERS.each do |adapter|
Rake::TestTask.new(adapter => "#{adapter}:setup") do |t|
t.libs << 'lib' << 'test'
t.test_files = FileList[ARGV[1] ? ARGV[1] : 'test/**/*_test.rb']
t.warning = false
t.verbose = true
end

namespace adapter do
task(:setup) { ENV["AR_ADAPTER"] = adapter }
end
end

desc "Run test with all adapters"
task all: ADAPTERS.shuffle.map{ |e| "test:#{e}" }
end

task test: "test:all"

# # require "sdoc"
# RDoc::Task.new do |rdoc|
Expand Down
3 changes: 2 additions & 1 deletion activerecord-cached_at.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ Gem::Specification.new do |spec|
spec.test_files = `git ls-files -- {test}/*`.split("\n")
spec.require_paths = ["lib"]

spec.add_runtime_dependency 'activerecord', '>= 7.0.0'
spec.add_runtime_dependency 'activerecord', '>= 7.1.0'

spec.add_development_dependency "bundler"
spec.add_development_dependency "rake"
spec.add_development_dependency 'minitest'
spec.add_development_dependency 'minitest-reporters'
spec.add_development_dependency "pg"
spec.add_development_dependency "sqlite3"
spec.add_development_dependency "simplecov"
spec.add_development_dependency "byebug"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ module ActiveRecord
module ConnectionAdapters #:nodoc:

class TableDefinition

def internal_table?
@name == "#{ActiveRecord::Base.table_name_prefix}#{ActiveRecord::Base.internal_metadata_table_name}#{ActiveRecord::Base.table_name_suffix}"
end

def timestamps(**options)
options[:null] = false if options[:null].nil?

Expand All @@ -11,7 +16,7 @@ def timestamps(**options)

column(:created_at, :datetime, **options)
column(:updated_at, :datetime, **options)
column(:cached_at, :datetime, **options) if @name != ActiveRecord::InternalMetadata.table_name
column(:cached_at, :datetime, **options) if !internal_table?
end
end

Expand Down
38 changes: 19 additions & 19 deletions ext/active_record/connection_adapters/abstract/schema_statements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,8 @@ module ActiveRecord
module ConnectionAdapters #:nodoc:

module SchemaStatements
def add_timestamps(table_name, **options)
options[:null] = false if options[:null].nil?

if !options.key?(:precision) && supports_datetime_with_precision?
options[:precision] = 6
end

add_column table_name, :created_at, :datetime, **options
add_column table_name, :updated_at, :datetime, **options
add_column table_name, :cached_at, :datetime, **options
end

def remove_timestamps(table_name, **options)
remove_column table_name, :updated_at
remove_column table_name, :created_at
remove_column table_name, :cached_at
remove_columns table_name, :updated_at, :created_at, :cached_at
end

def add_timestamps_for_alter(table_name, **options)
Expand All @@ -33,12 +19,26 @@ def add_timestamps_for_alter(table_name, **options)
add_column_for_alter(table_name, :cached_at, :datetime, **options)
]
end
end

def remove_timestamps_for_alter(table_name, **options)
remove_columns_for_alter(table_name, :updated_at, :created_at, :cached_at)
end
end
end

end

ActiveSupport.on_load(:active_record_sqlite3adapter) do
class ActiveRecord::ConnectionAdapters::SQLite3Adapter
def add_timestamps(table_name, **options)
options[:null] = false if options[:null].nil?

if !options.key?(:precision)
options[:precision] = 6
end

alter_table(table_name) do |definition|
definition.column :created_at, :datetime, **options
definition.column :updated_at, :datetime, **options
definition.column :cached_at, :datetime, **options
end
end
end
end
2 changes: 1 addition & 1 deletion lib/cached_at/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def cache_version(*includes)
timestamp = timestamp_keys.map { |attr| self[attr]&.to_time }.compact.max
end

timestamp.utc.to_s(:usec)
timestamp.utc.to_fs(:usec)
end

# TODO
Expand Down
2 changes: 1 addition & 1 deletion lib/cached_at/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module CachedAt
VERSION = '7.0.0'
VERSION = '7.1.0'
end
8 changes: 4 additions & 4 deletions test/cached_at_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ class Region < ActiveRecord::Base
account = Account.create
account.update_columns(cached_at: t1, organization_cached_at: t2, organization_images_cached_at: t3)

assert_equal "cached_at_test/accounts/#{account.id}@#{t1.utc.to_s(:usec)}", account.cache_key
assert_equal "cached_at_test/accounts/#{account.id}+b4c1948c087fafc89a88450fcbb64c77@#{t2.utc.to_s(:usec)}", account.cache_key(:organization)
assert_equal "cached_at_test/accounts/#{account.id}+b471431f7777fe57283f68842e724add@#{t3.utc.to_s(:usec)}", account.cache_key(organization: :images)
assert_equal "cached_at_test/accounts/#{account.id}+b471431f7777fe57283f68842e724add@#{t3.utc.to_s(:usec)}", account.cache_key([organization: [:images]])
assert_equal "cached_at_test/accounts/#{account.id}@#{t1.utc.to_fs(:usec)}", account.cache_key
assert_equal "cached_at_test/accounts/#{account.id}+b4c1948c087fafc89a88450fcbb64c77@#{t2.utc.to_fs(:usec)}", account.cache_key(:organization)
assert_equal "cached_at_test/accounts/#{account.id}+b471431f7777fe57283f68842e724add@#{t3.utc.to_fs(:usec)}", account.cache_key(organization: :images)
assert_equal "cached_at_test/accounts/#{account.id}+b471431f7777fe57283f68842e724add@#{t3.utc.to_fs(:usec)}", account.cache_key([organization: [:images]])

with_cache_versioning do
assert_equal "cached_at_test/accounts/#{account.id}", account.cache_key
Expand Down
30 changes: 27 additions & 3 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,38 @@ def self.schema(&block)
end

set_callback(:setup, :before) do
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
case ENV["AR_ADAPTER"]
when 'postgres'
ActiveRecord::Base.establish_connection({
adapter: "postgresql",
database: "activerecord_cached_at_test",
encoding: "utf8"
})

db_config = ActiveRecord::Base.connection_db_config
task = ActiveRecord::Tasks::PostgreSQLDatabaseTasks.new(db_config)
task.drop
task.create
when 'sqlite'
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
end


if !instance_variable_defined?(:@suite_setup_run) && self.class.class_variable_defined?(:@@schema)
ActiveRecord::Migration.suppress_messages do
ActiveRecord::Schema.define(&self.class.class_variable_get(:@@schema))
ActiveRecord::Base.connection.data_sources.each do |table|
next if table == 'ar_internal_metadata'
ActiveRecord::Migration.execute("INSERT INTO SQLITE_SEQUENCE (name,seq) VALUES ('#{table}', #{rand(50_000)})")
next if %w(schema_migrations ar_internal_metadata).include?(table)
case ENV["AR_ADAPTER"]
when 'postgres'
ActiveRecord::Migration.execute(<<~SQL)
ALTER SEQUENCE IF EXISTS #{table}_id_seq RESTART WITH #{rand(50_000)}
SQL
when 'sqlite'
ActiveRecord::Migration.execute(<<~SQL)
INSERT INTO SQLITE_SEQUENCE (name,seq) VALUES ('#{table}', #{rand(50_000)})
SQL
end
end
end
end
Expand Down

0 comments on commit a990e6e

Please sign in to comment.