Last active
June 4, 2021 00:16
-
-
Save sj26/78fe5fbe3a225152ecba30ef6cd94dc8 to your computer and use it in GitHub Desktop.
Timescale extensions for ActiveRecord
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
# Put this in config/initializers/ | |
module ActiveRecord::Tasks::TimescaleExtensions | |
def structure_load(filename, extra_flags) | |
# If this structure is using timescaledb then we need to take some extra steps | |
# https://docs.timescale.com/timescaledb/latest/how-to-guides/backup-and-restore/pg-dump-and-restore#entire-database | |
if system "grep", "-Eq", "CREATE EXTENSION( IF NOT EXISTS)? timescaledb", filename | |
puts "Enabling timescale extension" | |
connection.enable_extension("timescaledb") | |
puts "Starting timescale restore" | |
connection.execute("SELECT timescaledb_pre_restore()") | |
super | |
puts "Finishing timescale restore" | |
connection.execute("SELECT timescaledb_post_restore()") | |
else | |
super | |
end | |
end | |
end | |
ActiveSupport.on_load(:active_record) do | |
ActiveRecord::Tasks::PostgreSQLDatabaseTasks.prepend ActiveRecord::Tasks::TimescaleExtensions | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment