Skip to content

Instantly share code, notes, and snippets.

@sj26
Last active June 4, 2021 00:16
Show Gist options
  • Save sj26/78fe5fbe3a225152ecba30ef6cd94dc8 to your computer and use it in GitHub Desktop.
Save sj26/78fe5fbe3a225152ecba30ef6cd94dc8 to your computer and use it in GitHub Desktop.
Timescale extensions for ActiveRecord
# 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