• Stars
    star
    1,447
  • Rank 32,513 (Top 0.7 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 9 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Rails Database Viewer and SQL Query Runner

Rails DB

Please share this gem with your friends to get more feedback. I would really appreciate for the gem review if you have a blog. If you have ideas - please create an issue or contact me by email: igorkasyanchuk[@]gmail.com.

Build Status Gem Version RailsJazz https://www.patreon.com/igorkasyanchuk Listed on OpenSource-Heroes.com

Quick way to inspect your Rails database http://localhost:12345/rails/db, see content of tables, export them to CSV, EXPLAIN SQL and run SQL queries.

Demo available on: https://rails-db.herokuapp.com/rails/db/

Video Demos: https://youtu.be/TYsRxXRFp1g, https://youtu.be/dfViQPZw9zE

Requirements

Version >2.0 100% works with Ruby 2.3+, and Rails 5 and Rails 6 (2.3.1 requires >= Rails 6, for Rails 5 use 2.2.1). As for other versions of RoR use 1.6 version of gem. Also there are automated tests running on Travis CI. Supports: PostgreSQL, MySQL, SQLite. Not sure about other adapters. Since gem is using ActiveRecord it will work with others DBs.

For older versions of Rails (Rails 3.2, 4.X) please user gem "rails_db", "1.6".

Note (!)

Gem works with ruby 1.9.3 but you need to use kaminari < 1.0.0 gem and rails_db <= 1.6. Please add to gemfile gem 'kaminari', '< 1.0.0'.

Main Features

  • command line tools railsdb and runsql to run app and sql for your apps.
  • view content and schema of DB tables (including indexes)
  • create, edit content
  • sort content of DB tables
  • filtering data (multiple filters supported too)
  • execute SQL queries
  • autocomplete for SQL queries
  • filter tables for easier navigation
  • show/hide columns in tables
  • use history of you SQL queries
  • see EXPLAIN results for your queries
  • export data to CSV and Excel
  • import SQL files
  • expand/collapse sidebar
  • hide/show columns
  • allow access for current user for example with appropriate role
  • added HTTP_BASIC auth for Rails DB
  • ability to turn on/off using initializer
  • white/black list of tables
  • data-tables inside your app
  • ajax-ify Rails DB
  • delete records, truncate tables
  • visualize SQL schema (future)
  • CRUD for table records (future)

Install

Add this to the development group in your Gemfile

For all environments (be careful with production environment):

  # Rails >= 6
  gem 'rails_db', '>= 2.3.1'

  # Rails < 6
  gem 'rails_db', '2.2.1'

Or only for development:

  group :development do
    # Rails >= 6
    # and there are is a big change it will work with Rails 5.2 too
    gem 'rails_db', '>= 2.3.1'

    # Rails < 6
    gem 'rails_db', '2.2.1'
  end

Run bundle install

Visit http://localhost:3000/rails/db and you will see your database tables and SQL editors.

Use port 12345 if running in 'standalone' mode

Optionally, add gem 'caxlsx_rails' to your application's Gemfile to be able to export to excel .xlsx files.

Customization

If you want to customize gem (create initializer) run in console:

rails g rails_db initializer

If will create file config/initializers/rails_db.rb.

Options

  • enabled - enable or disable gem (default: true).
  • automatic_routes_mount - automatically mount engine routes in your app (default: true).
  • http_basic_authentication_enabled - Enable HTTP_BASIC authentication (default: false).
  • http_basic_authentication_user_name - HTTP_BASIC authentication user name.
  • http_basic_authentication_password - HTTP_BASIC authentication password.
  • black_list_tables - black list for tables (hide tables from sidebar).
  • white_list_tables - black list for tables (show only these tables in sidebar).
  • verify_access_proc - allow access by specific conditions, for example by role for current_user, or you can use for example Pundit here (default: proc { |controller| true })
  • sandbox - allow only read-only queries (#122)

If you want to add routes manually (or customize the mounted location) you need to first turn off automatic_routes_mount and then add to your routes.rb

  mount RailsDb::Engine => '/rails/db', :as => 'rails_db'

If you wanted to only allow access to admins and, for example, you are using Devise you could do following (in config/initializers/rails_db.rb)

  config.verify_access_proc = proc { |controller| controller.current_user.admin? }

Data Tables

You can easily embed data-tables into your App with content from DB.

You can build data tables for the whole Table, Table with specific columns or based on SQL query.

Below you can see samples on how you can embed data-tables directly in your app.

<h3>Table</h3>

<%= rails_db_data_table 'accounts',
                        footer: true,
                        columns: ['id', 'name', 'users_count'],
                        header: true,
                        style: :bootstrap %>

<h3>SQL</h3>

<%= rails_db_data_table_sql 'select id, name, age from users order by age desc limit 10',
                            footer: false,
                            header: true %>

Samples & Screenshots

Schema

SQL Editor

Rails App SQL editor with syntax highlight and code complete.

Install & visit http://localhost:3000/rails/db to see it in action.

RailsDB Standalone

Run RailsDB Standalone commands locally withing configuration of your config/database.yml.

RailsDB Standalone and runsql doesn't require to be added to Gemfile or you application.

RunSQL Util

Run SQL commands locally withing configuration of your config/database.yml

Edit Content in Rails DB

===

++There is a dependency on MySQL. You may need to gem install mysql2 -v '0.3.20'

Contributing

  • Fork it
  • Create your feature branch (git checkout -b my-new-feature)
  • Commit your changes (git commit -am 'Added some feature')
  • Push to the branch (git push origin my-new-feature)
  • Create new Pull Request

Local Development

Local Build

  • in gem root folder run gem build rails_db.gemspec
  • then try to install local version of the gem gem install rails_db-0.X.X.gem
  • now you can use this gem locally
  • don't forget to uninstall it

Run Tests

Locally works with Ruby 2.7.2 (dev env, tests) and goal is to upgrade dummy app and Ruby to newer versions.

To run tests locally you need to execute rake test withing root folder of project.

After your push to git, all tests will be executed on Travis CI environment.

You can also change locally DB to verify gem accross multiple Databases (mysql, postgres, sqlite). For more details see test/dummy/config/database.yml (you need to set DB ENV variable).

Sample:

DB=sqlite BUNDLE_GEMFILE=./gemfiles/rails_6_0.gemfile rails test
DB=postgresql BUNDLE_GEMFILE=./gemfiles/rails_6_0.gemfile rails test
DB=mysql BUNDLE_GEMFILE=./gemfiles/rails_6_0.gemfile rails test

On CI tests are executed with BUNDLE_GEMFILE=./gemfiles/rails_6_0.gemfile rails test.

Common Issues and Solutions

  • "Invalid css error" - #11
  • "Automatic routes mounting" - #4
  • gems(mysql, pg) are not found - RailsDB doesn't have any strict dependency inside. So you need to install gems manually gem install pg or gem install mysql -v=0.3.18.
  • "ActionView::Template::Error (No input found for jsonb):" - #39
  • issue with Ransack gem (in rails_db search method is removed), try to use method ransack instead because search is deprectated.

Plans

  • Add more tests
  • Verify all environments
  • Add DB schema visualization
  • Create WIKI pages, improve documentation
  • Add favicon for standalone and dummy apps
  • Add ERD schema

Other stuff

As a good addition to this gem (or as separate utility) you can use this gem: https://github.com/igorkasyanchuk/execute_sql which allows to execute SQL in rails console or app.

Dev Environment

Might be helpful, if you have issues with mysql2 gem installation on macOS:

gem install mysql2 -- \
 --with-mysql-lib=/usr/local/Cellar/mysql/8.0.33/lib \
 --with-mysql-dir=/usr/local/Cellar/mysql/8.0.33 \
 --with-mysql-config=/usr/local/Cellar/mysql/8.0.33/bin/mysql_config \
 --with-mysql-include=/usr/local/Cellar/mysql/8.0.33/include

License

This project uses MIT-LICENSE.

More Repositories

1

active_storage_validations

Do it like => validates :photos, attached: true, content_type: ['image/png', 'image/jpg', 'image/jpeg'], size: { less_than: 500.kilobytes }, limit: { min: 1, max: 3 }, aspect_ratio: :landscape, dimension: { width: { in: 800..1600 }
Ruby
959
star
2

rails_performance

Monitor performance of you Rails applications (self-hosted and free)
Ruby
793
star
3

any_login

Easy way to login as any user in system
Ruby
376
star
4

log_analyzer

Rails logs analyzer (see how fast your views are rendering)
Ruby
349
star
5

rails_pdf

A reliable way to generate PDF of any complexity in Ruby on Rails apps
HTML
175
star
6

fake_api

The fastest way to prototype API in your Rails application
Ruby
142
star
7

execution_time

How fast is your code? See it directly in Rails console.
Ruby
111
star
8

benchmark_methods

Benchmark and measure execution time your Ruby methods without an additional code changes
Ruby
89
star
9

new_google_recaptcha

reCAPTCHA v3 Ruby on Rails gem
Ruby
82
star
10

transactify

Wrap your methods in DB Transactions
Ruby
55
star
11

sql_view

Rails SQL Views made easy ;)
Ruby
49
star
12

sweet_staging

Access your Rails console, see logs, execute rake commands directly from the browser. Great addition to your Staging ENV.
JavaScript
46
star
13

execute_sql

Execute SQL inside Rails console, or app itself
Ruby
41
star
14

cache_with_locale

Easy wait to do view caching with automatically added "locale" value to the cached key.
Ruby
37
star
15

avatarro

Generate google-style avatars in your application
Ruby
37
star
16

awesome_back_url

Redirect the user to the proper "back" page
Ruby
33
star
17

records_count

See in development logs how many records your queries returns. It can help with solving performance issues.
Ruby
31
star
18

new_ckeditor

Ruby on Rails + CKEditor 5
Ruby
31
star
19

amazon_static_site

Static website using https with your own domain name using Amazon S3 and Cloudflare for FREE
Ruby
29
star
20

omg_image

Generate PNG previews for HTML snippets (html/css/js). Any complexity.
Ruby
28
star
21

wrapped_print

Easy print debug information to your console in Ruby/Rails app.
Ruby
23
star
22

calculate_in_group

Group Active Record by ranges or set of values with a single SQL query.
Ruby
22
star
23

embed_view

Embed ERB files inside another ERB files for faster performance (5-20% BOOST!!!)
HTML
21
star
24

rails_time_travel

HTML
19
star
25

sabotage

Coding & debugging must be fun. Make life a bit harder for your colleagues :)
Ruby
18
star
26

mechanical

All models in a single table, new attributes without migrations. Works like regular AR model
Ruby
17
star
27

secrett11tto

Simple way to protect your content from copy-pasting
Ruby
15
star
28

rails_live

Ruby
15
star
29

mini-guard

Ruby
14
star
30

railsjazz.com

Rails Jazz (personal web site)
JavaScript
14
star
31

sidekiq_log_analyzer

SidekiqLogAnalyser gem allows to see summary of your sidekiq workers (based on log file).
Ruby
13
star
32

hasharay_ext

Painless work with complex Ruby hashes/arrays.
Ruby
13
star
33

active_storage_silent_logs

The idea of this gem is to hide as much as possible Active Storage logs from console so you can see only important information and requests
Ruby
13
star
34

rails_cached_method

Simple way to cache results of methods.
Ruby
11
star
35

with_record

Returns relations/association for soft deleted records in DB
Ruby
10
star
36

rrr

Run recent rspec files only (the only recently modified).
Ruby
9
star
37

font_awesome_file_icons

Ruby
4
star
38

unwhere

Ruby
4
star
39

travel_and_talk

JavaScript
3
star
40

lazy_mobile_tester

Rails Lazy Mobile Tester
Ruby
3
star
41

serpjazz

SERP keywords tracking
JavaScript
3
star
42

jeanappv2

JavaScript
2
star
43

slim_erb_backport

Slim 4+ and ERB friends again :)
Ruby
2
star
44

mega-simple-authorization

mega simple authorization plugin for RoR
Ruby
2
star
45

tv

eb5 tv
JavaScript
2
star
46

spring_rspec_commands_addon

rails+spring+rspec = friends :)
Ruby
2
star
47

layouts_from_db_sample

Allow store layouts to DB (Sample)
2
star
48

CheaperDrinker

CheaperDrinker web site
JavaScript
2
star
49

tell_my_env

Ruby
2
star
50

any_login_test

AnyLogin gem test application
Ruby
2
star
51

VerySimple

1
star
52

ShareT

online translations
JavaScript
1
star
53

test-ec2

test-ec2
1
star
54

sa1

1
star
55

better_tempfile

Ruby
1
star
56

ar_enumerations_test_application

ActiveRecord enumeration field type - test application
Ruby
1
star
57

tophouse.com.ua

JavaScript
1
star
58

capistranotest

1
star
59

zip_and_phone

Zips & Phones
Ruby
1
star
60

portfolio

1
star
61

tdemo

tdemo
Ruby
1
star
62

cisarska_and_frankivska

Cisarska & Frankivska
JavaScript
1
star
63

seminars

JavaScript
1
star
64

deprecations_collector

Save all Rails deprecation in log file for future investigation
Ruby
1
star
65

just_for_fun

Do you want to call 42.to_user, [42, 43, 44].to_users? Try this gem :)
Ruby
1
star
66

rails_logs

Ruby
1
star