Reputation: 712
I would like to do a rails migration and a rails seed with this postgresql query. How can I do that ?
DROP TABLE IF EXISTS grid_light;
CREATE TABLE grid_light AS
SELECT DISTINCT g.ogc_fid AS id, g.geog4326 AS geog
FROM grid g, tracks t
WHERE t.path_buff_geog && g.geog4326 AND ST_Intersects(t.path_buff_geog, g.geog4326);
Upvotes: 1
Views: 167
Reputation: 150
You should be create a rake task ( exp: create grids.rake in your lib/task )
namespace :grids do
desc 'explain your rake here'
task create_grid: :environment do
drop_sql_cmd = <<-SQL
DROP TABLE IF EXISTS grid_light;
SQL
create_sql_cmd = <<-SQL
CREATE TABLE grid_light AS
SELECT DISTINCT g.ogc_fid AS id, g.geog4326 AS geog
FROM grid g, tracks t
WHERE t.path_buff_geog && g.geog4326 AND ST_Intersects(t.path_buff_geog, g.geog4326);
SQL
ActiveRecord::Base.transaction do
ActiveRecord::Base.connection.execute(drop_sql_cmd)
ActiveRecord::Base.connection.execute(create_sql_cmd)
end
end
end
and if call rake as
bundle exec rake grids:create_grid
Upvotes: 2