-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Missing relation indexes on historical tables #318
Comments
When trying to understand what was going on with chronomodel, I lost myself inside AR internals, where they use
|
Workaround# frozen_string_literal: true
namespace :chronomodel do
desc "Sync indexes from temporal to history schema"
task sync_history_indexes: :environment do
def get_tables(schema)
ActiveRecord::Base.connection.execute(<<-SQL.squish).values.flatten
SELECT table_name
FROM information_schema.tables
WHERE table_schema = '#{schema}'
ORDER BY table_name
SQL
end
def get_non_unique_indexes(schema, table)
ActiveRecord::Base.connection.execute(<<-SQL.squish).values
SELECT
i.relname AS index_name,
pg_get_indexdef(i.oid) AS index_definition
FROM
pg_index x
JOIN pg_class c ON c.oid = x.indrelid
JOIN pg_class i ON i.oid = x.indexrelid
JOIN pg_namespace n ON n.oid = c.relnamespace
LEFT JOIN pg_constraint co ON (co.conrelid = c.oid AND co.conindid = x.indexrelid)
WHERE
c.relkind = 'r'
AND n.nspname = '#{schema}'
AND c.relname = '#{table}'
AND x.indisunique = false
AND co.contype IS NULL
ORDER BY
i.relname;
SQL
end
def index_exists?(schema, table, index_name)
ActiveRecord::Base.connection.execute(<<-SQL.squish).values.flatten.first
SELECT EXISTS (
SELECT 1
FROM pg_indexes
WHERE schemaname = '#{schema}'
AND tablename = '#{table}'
AND indexname = '#{index_name}'
)
SQL
end
def create_index(schema, table, index_name, index_definition)
# Extract the part of the index definition after "USING"
index_type_and_columns = index_definition.split(' USING ').last
ActiveRecord::Base.connection.execute(<<-SQL.squish)
CREATE INDEX IF NOT EXISTS #{index_name} ON #{schema}.#{table} USING #{index_type_and_columns}
SQL
end
temporal_tables = get_tables('temporal')
temporal_tables.each do |table|
puts "Processing table: #{table}"
non_unique_indexes = get_non_unique_indexes('temporal', table)
non_unique_indexes.each do |index_name, index_definition|
if index_exists?('history', table, index_name)
puts " [SKIP] Index '#{index_name}' already exists in history.#{table}"
else
puts " [CREATE] Creating index '#{index_name}' on history.#{table}"
create_index('history', table, index_name, index_definition)
end
end
end
puts 'Index synchronization completed!'
end
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When using
t.references
ort.belongs_to
in a migration, Chronomodel does not create the reference index in the historical tableReproducible test case
The text was updated successfully, but these errors were encountered: