Created
August 27, 2020 09:44
-
-
Save mmichaa/185aa1078a4adeb4d22c1e0b97feb166 to your computer and use it in GitHub Desktop.
Hack: PostgreSQL unlogged tables with Rails v5.2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rails.application.configure do | |
# … | |
# SEE: https://prathamesh.tech/2020/08/10/creating-unlogged-tables-in-rails/ | |
# SEE: config/initializers/active_record_postgresql_create_unlogged_tables_patch.rb | |
config.to_prepare do | |
ActiveSupport.on_load(:active_record) do | |
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.create_unlogged_tables = true | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ActiveSupport.on_load(:active_record) do | |
break if Rails.env.production? | |
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.class_eval do | |
# SOURCE: https://github.com/rails/rails/blob/6-0-stable/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb | |
class_attribute :create_unlogged_tables, default: false | |
end | |
ActiveRecord::ConnectionAdapters::PostgreSQL::TableDefinition.class_eval do | |
attr_reader :unlogged | |
# SOURCE: https://github.com/rails/rails/blob/6-0-stable/activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb | |
def initialize(*, **) | |
super | |
@unlogged = ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.create_unlogged_tables | |
end | |
end | |
ActiveRecord::ConnectionAdapters::PostgreSQL::SchemaCreation.class_eval do | |
private | |
# SOURCE: https://github.com/rails/rails/blob/5-2-stable/activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb | |
def visit_TableDefinition(o) | |
create_sql = "CREATE#{table_modifier_in_create(o)} TABLE #{quote_table_name(o.name)} ".dup | |
statements = o.columns.map { |c| accept c } | |
statements << accept(o.primary_keys) if o.primary_keys | |
if supports_indexes_in_create? | |
statements.concat(o.indexes.map { |column_name, options| index_in_create(o.name, column_name, options) }) | |
end | |
if supports_foreign_keys_in_create? | |
statements.concat(o.foreign_keys.map { |to_table, options| foreign_key_in_create(o.name, to_table, options) }) | |
end | |
create_sql << "(#{statements.join(', ')})" if statements.present? | |
add_table_options!(create_sql, table_options(o)) | |
create_sql << " AS #{to_sql(o.as)}" if o.as | |
create_sql | |
end | |
# SOURCE: https://github.com/rails/rails/blob/6-0-stable/activerecord/lib/active_record/connection_adapters/postgresql/schema_creation.rb | |
def table_modifier_in_create(o) | |
if o.temporary | |
" TEMPORARY" | |
elsif o.unlogged | |
" UNLOGGED" | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment