MySQL to Ruby Converter

Transform MySQL database dumps into Ruby data structures including arrays, hashes, and Struct objects for Rails and Ruby applications

MySQL Input

Ruby Output

About MySQL to Ruby Converter

Convert MySQL database dumps (CREATE TABLE and INSERT statements) to Ruby data structures including arrays, hashes, and Struct objects. Perfect for Rails seed data, testing fixtures, Factory Bot, and Ruby applications.

Key Features

  • Multiple Output Formats: Array of arrays, array of hashes, or Struct objects
  • Symbol or String Keys: Choose between :symbol or "string" keys for hashes
  • Automatic Type Detection: Detects numbers, booleans, nil, and strings
  • String Escaping: Properly escapes special characters in strings
  • Rails Compatible: Perfect for db/seeds.rb and test fixtures
  • Smart Parsing: Extracts column names and data from MySQL dumps
  • File Download: Save as .rb file

How to Use

  1. Input MySQL Data: Paste your MySQL CREATE TABLE and INSERT statements or upload a .sql file
  2. Choose Output Format: Select array of hashes, array of arrays, or Struct objects
  3. Configure Options: Toggle symbol keys and header row inclusion
  4. Copy or Download: Use the Copy or Download button to save your Ruby code

Output Formats

  • Array of Hashes: Most common format, easy to work with, supports symbol or string keys
  • Array of Arrays: Simple format, optional header row, minimal memory footprint
  • Struct Objects: Type-safe format with attribute accessors, great for OOP

Example Conversion

MySQL Input:

CREATE TABLE employees (
  id INT,
  name VARCHAR(100),
  age INT,
  active BOOLEAN
);

INSERT INTO employees VALUES (1, 'John Doe', 28, true);
INSERT INTO employees VALUES (2, 'Jane Smith', 34, false);

Ruby Output (Array of Hashes with Symbols):

# Array of hashes format
data = [
  {
    :id => 1,
    :name => "John Doe",
    :age => 28,
    :active => true
  },
  {
    :id => 2,
    :name => "Jane Smith",
    :age => 34,
    :active => false
  }
]

# Access example:
# data[0][:id]  # First row, id

Ruby Output (Struct Objects):

# Struct-based format
Record = Struct.new(:id, :name, :age, :active)

# Create instances
data = [
  Record.new(1, "John Doe", 28, true),
  Record.new(2, "Jane Smith", 34, false)
]

# Access examples:
# data[0].id  # First row, id
# data.map(&:id)  # All id values

Common Use Cases

  • Rails Seed Data: Populate db/seeds.rb with MySQL data
  • Test Fixtures: Create test data for RSpec, Minitest, or Cucumber
  • Factory Bot: Generate factory definitions from MySQL data
  • Data Migration: Migrate data from MySQL to Ruby applications
  • API Mocking: Create mock data for API testing
  • Ruby Scripts: Use MySQL data in standalone Ruby scripts

Rails Integration

Use the generated Ruby code in Rails:

# db/seeds.rb
data = [
  { name: "John Doe", age: 28, city: "New York" },
  { name: "Jane Smith", age: 34, city: "London" }
]

data.each do |record|
  Employee.create!(record)
end

Type Detection

  • Numbers: Integer and decimal values (28, 3.14)
  • Booleans: true/false and 1/0 converted to Ruby booleans
  • Nil: NULL and empty values converted to nil
  • Strings: All other values with proper escaping

Symbol vs String Keys

Symbols (:key):

  • More idiomatic Ruby style
  • Better performance (immutable)
  • Commonly used in Rails

Strings ("key"):

  • Compatible with JSON parsing
  • Better for dynamic keys
  • Works with external APIs

Supported MySQL Syntax

  • CREATE TABLE: Extracts column names for Ruby keys
  • INSERT INTO: Parses data values from INSERT statements
  • Data Types: Handles all MySQL data types (VARCHAR, INT, BOOLEAN, etc.)
  • Quoted Strings: Handles single and double quotes with proper escaping

Privacy & Security

All conversions happen locally in your browser. Your MySQL data is never uploaded to any server, ensuring complete privacy and security.