Back to Repositories

Implementing Database Schema Migration for Geocoding Tests in alexreisner/geocoder

This test file implements the database schema migration for geocoding functionality testing in the Geocoder gem. It establishes test tables with geographic coordinates and address fields to support both forward and reverse geocoding scenarios.

Test Coverage Overview

The test schema provides comprehensive coverage for various geocoding scenarios including:

  • Basic places with standard geocoding fields
  • Custom lookup implementations
  • Forward and reverse geocoding combinations
  • Custom result handling cases
The schema supports testing of precision requirements with decimal columns configured for high-accuracy coordinate storage.

Implementation Analysis

The migration utilizes Rails’ ActiveRecord Migration framework to create test tables with specific geographic requirements. It implements a version-aware approach to maintain compatibility across Rails 4 and 5, using conditional superclass assignment based on Migration API version support.

The schema defines multiple table structures with varying column configurations to test different geocoding scenarios and custom implementations.

Technical Details

Key technical specifications include:

  • Decimal columns with 16 digits precision and 6 decimal places for coordinates
  • Standard string columns for addresses and location names
  • Custom columns for result handling and lookup customization
  • Rails migration framework integration
  • Cross-version compatibility handling

Best Practices Demonstrated

The test schema demonstrates several database design best practices:

  • Consistent coordinate precision across all geographic tables
  • Modular table creation with shared column definitions
  • Clear naming conventions for test-specific tables
  • Flexible schema design supporting various use cases
  • Forward-compatible migration implementation

alexreisner/geocoder

test/db/migrate/001_create_test_schema.rb

            
# CreateTestSchema creates the tables used in test_helper.rb

superclass = ActiveRecord::Migration
# TODO: Inherit from the 5.0 Migration class directly when we drop support for Rails 4.
superclass = ActiveRecord::Migration[5.0] if superclass.respond_to?(:[])

class CreateTestSchema < superclass
  def self.up
    [
      :places,
      :place_reverse_geocodeds
    ].each do |table|
      create_table table do |t|
        t.column :name, :string
        t.column :address, :string
        t.column :latitude, :decimal, :precision => 16, :scale => 6
        t.column :longitude, :decimal, :precision => 16, :scale => 6
        t.column :radius_column, :decimal, :precision => 16, :scale => 6
      end
    end

    [
      :place_with_custom_lookup_procs,
      :place_with_custom_lookups,
      :place_reverse_geocoded_with_custom_lookups
    ].each do |table|
      create_table table do |t|
        t.column :name, :string
        t.column :address, :string
        t.column :latitude, :decimal, :precision => 16, :scale => 6
        t.column :longitude, :decimal, :precision => 16, :scale => 6
        t.column :result_class, :string
      end
    end

    create_table :place_with_forward_and_reverse_geocodings do |t|
      t.column :name, :string
      t.column :location, :string
      t.column :lat, :decimal, :precision => 16, :scale => 6
      t.column :lon, :decimal, :precision => 16, :scale => 6
      t.column :address, :string
    end

    create_table :place_reverse_geocoded_with_custom_results_handlings do |t|
      t.column :name, :string
      t.column :address, :string
      t.column :latitude, :decimal, :precision => 16, :scale => 6
      t.column :longitude, :decimal, :precision => 16, :scale => 6
      t.column :country, :string
    end

    create_table :place_with_custom_results_handlings do |t|
      t.column :name, :string
      t.column :address, :string
      t.column :latitude, :decimal, :precision => 16, :scale => 6
      t.column :longitude, :decimal, :precision => 16, :scale => 6
      t.column :coords_string, :string
    end
  end
end