dynamic-active-model-rails 0.15.0 → 0.15.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c00e8c89d4f3d145711f0ec10472dda48b3dfd38ab11e921a2efa26641cf71e3
4
- data.tar.gz: 13900a4926179764ed30e642f44fd19dca06cbbe5742226292aafc8c06537ebb
3
+ metadata.gz: 72bf2f4e81912203cc8fa972baec98b8e25fbca4565272a9e5bd7a915bb5bbd3
4
+ data.tar.gz: be83fdc8eaa6cb83c83d6298d54be005779ecd4dc4931a2c3216d3a5c5aa2342
5
5
  SHA512:
6
- metadata.gz: 1d0a57c73c5976963edec4c2d9b9c928907a95476219085cca20ab8d2a57b8399f329b29c67e91ae00ca885da623f8a36680e025199e21aaab032066e10c124b
7
- data.tar.gz: 5dd9266a94e120c327f54a2677a42a88094bbb6caf54a66b1e129641352048609d9dd56bdcd43b44ecf74a9941597c9b08b5dee5a6e8e04de57cdccca6d0b709
6
+ metadata.gz: 40a0d18557415731182cd67634f5dfac07e9394ce6a8715d437ab1c0a28f85f1dd0177d41b043bb3610f76aa1688dd58b49e9f5a9546508d8a0412c0b5466638
7
+ data.tar.gz: 3dc87829e2442bceb70c63127cc3a7ef00a43c3741166c721e4e8100a6468e6ad881dfc6b87dd4718dc30f2c984cadf76393d128d732cdb359e44f86e656a3ef
data/README.md CHANGED
@@ -158,6 +158,26 @@ Avoid `Rails.application.config.after_initialize { GrantDB.database... }`. It ru
158
158
  - **Migrations.** After migrations run or a schema is loaded (`db:migrate`, `db:rollback`, `db:prepare`, `db:schema:load`, `maintain_test_schema!`), models are reset and rebuild on next use. That means `bin/rails db:prepare` followed by seeding works in one process.
159
159
  - **Reloading.** In development, models are rebuilt whenever the app reloads, including after edits to `.ext.rb` files or to anything under `db/`, such as a migration rewriting `db/schema.rb`.
160
160
 
161
+ ## Schema Cache
162
+
163
+ Models read columns, primary keys and indexes through Rails' schema cache. Dump it at deploy time, and building models no longer queries every table:
164
+
165
+ ```bash
166
+ bin/rails db:schema:cache:dump # db/schema_cache.yml, db/<name>_schema_cache.yml
167
+ ```
168
+
169
+ | | Queries to build 5 tables (SQLite) |
170
+ |---|---|
171
+ | No dump | 28 |
172
+ | Dump, Rails defaults | 4 |
173
+ | Dump, `config.active_record.check_schema_cache_dump_version = false` | 2 |
174
+
175
+ The table list is always one live query. The dump holds each table's indexes (name, uniqueness, columns), so associations come out the same as from live queries, including `has_one` from unique indexes and join tables.
176
+
177
+ A dump is only as fresh as the last `db:schema:cache:dump`. By default, Rails compares the dump's schema version with the database, and ignores the dump after a migration. That version only tracks migrations, though. For a database whose schema changes outside Rails migrations, such as a legacy or externally managed database, regenerate the dump on every deploy or don't use one. Otherwise new columns, indexes and associations are missed. With `check_schema_cache_dump_version = false`, any stale dump is used.
178
+
179
+ When models are reset after a code reload or schema change, the gem clears the database's schema cache too. That matters because Rails only clears the primary database's cache on reload.
180
+
161
181
  ## Migrations
162
182
 
163
183
  Migrations stay standard Rails. The gem reads the schema; it never owns it. For a secondary database, give its database.yml entry a `migrations_paths`:
@@ -40,11 +40,15 @@ module DynamicActiveModel
40
40
  !@database.nil?
41
41
  end
42
42
 
43
- # Removes the built models so the next #load! rebuilds them
43
+ # Removes the built models, and the schema cache their columns and indexes
44
+ # came from, so the next #load! rebuilds them against the current schema
44
45
  # @return [void]
45
46
  def reset!
46
47
  @monitor.synchronize do
47
- @database&.reset!
48
+ if @database
49
+ clear_schema_cache
50
+ @database.reset!
51
+ end
48
52
  @database = nil
49
53
  end
50
54
  end
@@ -65,6 +69,13 @@ module DynamicActiveModel
65
69
  raise
66
70
  end
67
71
 
72
+ # Clears the schema cache of the pool the models use. Rails clears only the
73
+ # primary pool on code reload, and nothing on schema changes outside migrations.
74
+ # @return [void]
75
+ def clear_schema_cache
76
+ @database.models.map(&:connection_pool).uniq.each { |pool| pool.schema_cache.clear! }
77
+ end
78
+
68
79
  # @return [DynamicActiveModel::Database] A database configured from the definition
69
80
  def new_database
70
81
  Database.new(namespace, definition.connection, parent_class: parent_class).tap do |database|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dynamic-active-model-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.15.0
4
+ version: 0.15.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Doug Youch
@@ -29,14 +29,14 @@ dependencies:
29
29
  requirements:
30
30
  - - '='
31
31
  - !ruby/object:Gem::Version
32
- version: 0.15.0
32
+ version: 0.15.1
33
33
  type: :runtime
34
34
  prerelease: false
35
35
  version_requirements: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - '='
38
38
  - !ruby/object:Gem::Version
39
- version: 0.15.0
39
+ version: 0.15.1
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: railties
42
42
  requirement: !ruby/object:Gem::Requirement