Repository navigation
Allow truncate for SQLite3 adapter and add rails db:seed:replant - #34779
Conversation
1abb52e to
b71e651
Compare
|
Awesome, @bogdanvlviv 🙏. I'll let someone else review the implementation, but this is great. |
|
What about to use also |
ActiveRecord::Base.connection.disable_referential_integrity do
table_names.without(*internal_table_names).each do |table_name|
ActiveRecord::Base.connection.truncate(table_name)
end
end |
e8f17e9 to
a1f9b9b
Compare
|
On PostgreSQL, this issue (test failure) could be solved by applying Diffdiff --git a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb
index 2299fc0214..deac8e9e5b 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb
@@ -135,7 +135,7 @@ def exec_delete(sql, name = nil, binds = [])
end
# Executes the truncate statement.
- def truncate(table_name, name = nil)
+ def truncate(table_name, name = nil, options = {})
raise NotImplementedError
end
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
index dbc6614b93..ad13432aaa 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
@@ -271,7 +271,7 @@ def collation
show_variable "collation_database"
end
- def truncate(table_name, name = nil)
+ def truncate(table_name, name = nil, options = {})
execute "TRUNCATE TABLE #{quote_table_name(table_name)}", name
end
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
index 381d5ab29b..7ed0c45c17 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -258,8 +258,11 @@ def clear_cache!
end
end
- def truncate(table_name, name = nil)
- exec_query "TRUNCATE TABLE #{quote_table_name(table_name)}", name, []
+ def truncate(table_name, name = nil, options = {})
+ cascade = if options[:cascade]
+ " CASCADE"
+ end
+ exec_query "TRUNCATE TABLE #{quote_table_name(table_name)}#{cascade}", name, []
end
# Is this connection alive and ready for queries?
diff --git a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
index 94754e5b86..144d1548b9 100644
--- a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
@@ -161,7 +161,7 @@ def clear_cache!
@statements.clear
end
- def truncate(table_name, name = nil)
+ def truncate(table_name, name = nil, options = {})
execute "DELETE FROM #{quote_table_name(table_name)}", name
end
diff --git a/activerecord/lib/active_record/tasks/database_tasks.rb b/activerecord/lib/active_record/tasks/database_tasks.rb
index 81a2b84e39..f852ebf328 100644
--- a/activerecord/lib/active_record/tasks/database_tasks.rb
+++ b/activerecord/lib/active_record/tasks/database_tasks.rb
@@ -191,7 +191,7 @@ def truncate_tables
ActiveRecord::Base.connection.disable_referential_integrity do
table_names.without(*internal_table_names).each do |table_name|
- ActiveRecord::Base.connection.truncate(table_name)
+ ActiveRecord::Base.connection.truncate(table_name, nil, cascade: true)
end
end
endBut I'm not completly sure that it is right solution since it changes public API( Possible related to 2812694 |
c48bd5e to
1a0bd9f
Compare
|
Since version 6.0 Rails will support mult DB, so user will be ActiveRecord::Base.connected_to(database: :primary) do
User.create!(name: "Bogdan")
end
ActiveRecord::Base.connected_to(database: :animals) do
Dog.create!(name: "Bosyi")
end
Since not every DB backend has For instance, in order to truncate a table and not crash on foreign key constraints SET FOREIGN_KEY_CHECKS = 0;
TRUNCATE TABLE `users`;on PostgreSQL: TRUNCATE TABLE "users", ...;on SQLite: PRAGMA defer_foreign_keys = ON;
PRAGMA foreign_keys = OFF;
DELETE FROM "users";Because of that, we should also provide strategy by which other DB Since we can't just rely on /cc @dhh |
Yes, we can: to be compatible with a new release, adapters need to support whatever methods we decide on. (And our downstreams are super accomodating and responsive about that.) I haven't looked at this, but we already have "empty out all the tables" behaviour for populating fixtures: unless I'm missing something, that should both prove that we have the technology, and suggest some scope for code sharing. |
That is right, I meant that
I've only found this |
Yeah, that looks right: it disables referential integrity, then empties out all the tables. We could consider switching that to truncate, if its additional constraints are okay, but I think we can treat that as a separate question. (As a side note, |
01213c8 to
b1a22c8
Compare
👍 Thanks for pointing this out, I changed the implementation for PostgreSQL to use TRUNCATE statement with a listing all the known tables. |
b1a22c8 to
f5b96a8
Compare
There was a problem hiding this comment.
Seems like this might have a need for something similar to what we're doing here: https://github.com/rails/rails/pull/33985/files#diff-18d566ee525de376b1a3741881545631R147
There was a problem hiding this comment.
There is one more place that possible should be refactored
ActiveRecord::Base.descendants.select { |model| !model._internal? }.map(&:table_name).compact here (It ignores join tables, ...) since It isn't the same as ActiveRecord::Base.connection.tables.
f5b96a8 to
d8feb78
Compare
|
@bogdanvlviv Is this ready for another review? |
d8feb78 to
c98a382
Compare
|
Yes. I just rebased with the |
There was a problem hiding this comment.
Isn't establish connection deprecated now that we have connect_to?
There was a problem hiding this comment.
ActiveRecord::Base.establish_connection isn't deprecated.
But yeah, we can give a chance ActiveRecord::Base.connected_to to be used here, see 8df9776.
fe05b0f to
8df9776
Compare
8df9776 to
d26952c
Compare
|
I just rebased this branch one more time to resolve the conflicts. Let me if you have any concerns about these changes. |
c8c4386 to
2148716
Compare
|
@bogdanvlviv You're happy with the state of things and ready to merge? |
SQLite doesn't support `TRUNCATE TABLE`, but SQLite3 adapter can support `ActiveRecord::Base.connection.truncate` by using `DELETE FROM`. `DELETE` without `WHERE` uses "The Truncate Optimization", see https://www.sqlite.org/lang_delete.html.
Extract code from the rake task. It will be easier to test this code and reuse.
…hen `truncate_tables`
Since version 6.0 Rails will support mult DB, so user will be
able to write in `db/seeds.rb` something like:
```ruby
ActiveRecord::Base.connected_to(database: :primary) do
User.create!(name: "Bogdan")
end
ActiveRecord::Base.connected_to(database: :animals) do
Dog.create!(name: "Bosyi")
end
```
`rails db:seed:replant` should truncate tables in both databases,
and replant seeds.
</hr>
Since not every DB backend has `TRUNCATE`, or if it has then
it can work in a different way than on any another DB backend.
For instance, in order to truncate a table and not crash on foreign key constraints
on MySQL we should execute something like:
```sql
SET FOREIGN_KEY_CHECKS = 0;
TRUNCATE TABLE `users`;
```
on PostgreSQL:
```sql
TRUNCATE TABLE "users", ... ;
```
on SQLite:
```sql
PRAGMA defer_foreign_keys = ON;
PRAGMA foreign_keys = OFF;
DELETE FROM "users";
```
Because of that, we should also provide strategy by which other DB
adapters, like `oracle-enhanced`, or `activerecord-sqlserver-adapter`,
will we able to make `rails db:seed:replant` work.
Since we can't just rely on `ActiveRecord::Base.connection.truncate`
because of the inconsistency of DB backends, I want to recommend the next:
Implement "ActiveRecord::Tasks::#{adapter_name}DatabaseTasks#truncate_tables(*table_names)"
to an adapter that would like to support this rake task.
This method just receives an array of table names. Your implementation
should ensure that it truncates all of those tables. Also, be sure
that it does not crash on foreign key constraints!
|
Unless there are any new implementation notes, it should be good to go. |
|
CI is terribly flaky at the moment 👎 |
Allow `truncate` for SQLite3 adapter and add `rails db:seed:replant` rails/rails#34779
Allow `truncate` for SQLite3 adapter and add `rails db:seed:replant` rails/rails#34779
Allow `truncate` for SQLite3 adapter and add `rails db:seed:replant` rails/rails#34779
Allow `truncate` for SQLite3 adapter and add `rails db:seed:replant` rails/rails#34779
Add
ActiveRecord::Base.connection.truncatefor SQLite3 adapter.SQLite doesn't support
TRUNCATE TABLE, but SQLite3 adapter can supportActiveRecord::Base.connection.truncateby usingDELETE FROM.DELETEwithoutWHEREuses "The Truncate Optimization",see https://www.sqlite.org/lang_delete.html.
Add
rails db:seed:replantthat truncates tables of each database for current environment and loads the seeds.Closes Add rails db:seed:replant #34765