Skip to content

tarellel/activerecord-duckdb

Repository files navigation

Activerecord::Duckdb

This gem is a DuckDB database adapter for ActiveRecord.

Description

Activerecord::Duckdb providers DuckDB database access for Ruby on Rails applications.

~ NOTE: This gem is still a work in progress, so it might not work exactly as expected just yet. Some ActiveRecord features haven’t been added and/or fully tested.

Requirements

This gem relies on the ruby-duckdb ruby gem as its database adapter. Thus it provides a seamless integration with the DuckDB database.

Both gems requires that you have duckdb. DuckDB has many installation options available that can be found on their installation page.

# OSx
brew install duckdb

# Most Linux distributions
curl https://install.duckdb.org | sh

Installation

Install the gem and add to the application's Gemfile by executing:

bundle add "activerecord-duckdb"

If bundler is not being used to manage dependencies, install the gem by executing:

gem install activerecord-duckdb

Usage

Configuration

Adjust your database.yml file to use the duckdb adapter.

development:
  adapter: duckdb
  database: db/development.duckdb

test:
  adapter: duckdb
  database: db/test.duckdb

production:
  adapter: duckdb
  database: db/production.duckdb

Run some migrations to ensure the database is ready.

rails g model Notice name:string email:string content:string
Notice.create(name: 'John Doe', email: 'john@example.com', content: 'Something happened at work today!')
Notice.find_by(email: 'john@example.com')
Notice.all
Notice.last.delete

~ At the moment using an in-memory database is very limited and still in development. NOTE: When using a memory database, any transactional operations will be lost when the process exits. The only reason I can think of is that you might want to use an in-memory database for testing purposes, data analysis, or some sort of quick calculations where the data is not critical.

temporary_database:
  adapter: duckdb
  database: :memory
class User < ApplicationRecord
  establish_connection(:temporary_database)
end

Of you can set your own database configuration in the config/database.yml file. When using temporary databases you'll also have to generate your own schema on the fly rather than migrations creating them automatically.

test:
  adapter: duckdb
  database: :memory

production:
  adapter: duckdb
  database: :memory

Sample App setup

The following steps are required to setup a sample application using the activerecord-duckdb gem:

  1. Create a new Rails application:
rails new sample_app --database=sqlite3
  1. Add the activerecord-duckdb gem to your Gemfile:
gem 'activerecord-duckdb'
  1. Run bundle install to install the gem.

  2. Update the config/database.yml file to use the duckdb adapter:

development:
  adapter: duckdb
  database: db/development.db

test:
  adapter: duckdb
  database: :memory

production:
  adapter: duckdb
  database: :memory
  1. Generate a model for the sample application:
rails g model User name:string email:string
  1. Run some migrations to ensure the database is ready:
rails db:create; rails db:migrate
  1. Create some sample data:
User.create(name: 'John Doe', email: 'john@example.com')
User.create(name: 'Jane Doe', email: 'jane@example.com')
  1. Run some queries:
User.all
User.find_by(email: 'john@example.com')
User.last.delete

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/tarellel/activerecord-duckdb.

License

The gem is available as open source under the terms of the MIT License.

About

A DuckDB database adapter for ActiveRecord

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages