Skip to content

Instantly share code, notes, and snippets.

@phildionne
Last active August 29, 2015 14:16
Show Gist options
  • Save phildionne/6c5cdd210f4e91bd43bb to your computer and use it in GitHub Desktop.
Save phildionne/6c5cdd210f4e91bd43bb to your computer and use it in GitHub Desktop.
Rails + Apartment + PostGIS + activerecord-postgis-adapter
Apartment.configure do |config|
config.persistent_schemas = %w{ shared_extensions }
end
defaults: &defaults
adapter: postgis
postgis_extension: true
schema_search_path: "public,shared_extensions"
encoding: unicode
host: 127.0.0.1
pool: 5
timeout: 5000
development:
<<: *defaults
database: development
username:
password:
CREATE TABLE "sampling_points" ("id" serial primary key, "localisation_description" text, "notes" text, "created_at" timestamp, "updated_at" timestamp, "waterbody_id" integer, "entity_id" integer, "name" character varying, "sampling_point_justification_id" integer, "slug" character varying NOT NULL, "coordinates" geometry(POINT,0))
ActiveRecord::StatementInvalid: PG::UndefinedObject: ERROR: type "geometry" does not exist
LINE 1: ... "slug" character varying NOT NULL, "coordinates" geometry(P...
^
: CREATE TABLE "sampling_points" ("id" serial primary key, "localisation_description" text, "notes" text, "created_at" timestamp, "updated_at" timestamp, "waterbody_id" integer, "entity_id" integer, "name" character varying, "sampling_point_justification_id" integer, "slug" character varying NOT NULL, "coordinates" geometry(POINT,0))
from /Users/pdionne/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `async_exec'
namespace :db do
desc 'Creates shared_extensions Schema and enables postgis extension'
task :extensions => :environment do
# Create shared_extensions schema
ActiveRecord::Base.connection.execute 'CREATE SCHEMA IF NOT EXISTS shared_extensions;'
# Enable PostGIS
ActiveRecord::Base.connection.execute 'CREATE EXTENSION IF NOT EXISTS postgis SCHEMA shared_extensions;'
end
end
Rake::Task["db:create"].enhance do
Rake::Task["db:extensions"].invoke
end
Rake::Task["db:test:purge"].enhance do
Rake::Task["db:extensions"].invoke
end
SELECT PostGIS_full_version();
OTICE: Function postgis_topology_scripts_installed() not found. Is topology support enabled and topology.sql installed?
postgis_full_version
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
POSTGIS="2.1.5 r13152" GEOS="3.4.2-CAPI-1.8.2 r3921" PROJ="Rel. 4.8.0, 6 March 2012" GDAL="GDAL 1.11.1, released 2014/09/24" LIBXML="2.9.1" LIBJSON="UNKNOWN" RASTER
(1 row)
class UpdateSamplingPointsGeo < ActiveRecord::Migration
def change
add_column(:sampling_points, :coordinates, :st_point, has_z: true)
add_index :sampling_points, :coordinates, using: :gist
end
end
AtiveRecord::Schema.define(version: 20150228171547) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
enable_extension "postgis"
create_table "sampling_points", force: :cascade do |t|
t.geometry "coordinates", limit: {:srid=>0, :type=>"point"}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment