Git Product home page Git Product logo

pgtk's Introduction

EO principles respected here Managed by Zerocracy DevOps By Rultor.com We recommend RubyMine

Build Status Build status PDD status Gem Version Maintainability Test Coverage

This small Ruby gem helps you integrate PostgreSQL with your Ruby web app, through Liquibase. It also adds a simple connection pool and query processor, to make SQL manipulation simpler.

First of all, on top of Ruby and Bundler you need to have PostgreSQL, Java 8+, and Maven 3.2+ installed. In Ubuntu 16+ this should be enough:

$ sudo apt-get install -y postgresql-10 postgresql-client-10
$ sudo apt-get install -y default-jre maven

Then, add this to your Gemfile:

gem 'pgtk'

Then, add this to your Rakefile:

require 'pgtk/pgsql_task'
Pgtk::PgsqlTask.new :pgsql do |t|
  t.dir = 'target/pgsql' # Temp directory with PostgreSQL files
  t.fresh_start = true # To delete the directory on every start
  t.user = 'test'
  t.password = 'test'
  t.dbname = 'test'
  t.yaml = 'target/pgsql-config.yml' # YAML file to be created with connection details
end

And this too:

require 'pgtk/liquibase_task'
Pgtk::LiquibaseTask.new liquibase: :pgsql do |t|
  t.master = 'liquibase/master.xml' # Master XML file path
  t.yaml = 'target/pgsql-config.yml' # YAML file with connection details
end

You should create that liquibase/master.xml file in your repository, and a number of other XML files with Liquibase changes. This example will help you understand them.

Now, you can do this:

$ bundle exec rake pgsql liquibase

A temporary PostgreSQL server will be started and the entire set of Liquibase SQL changes will be applied. You will be able to connect to it from your application, using the file target/config.yml.

From inside your app you may find this class useful:

require 'pgtk/pool'
yaml = YAML.load_file('config.yml')
pgsql = Pgtk::Pool.new(
  port: yaml['pgsql']['port'],
  dbname: yaml['pgsql']['dbname'],
  user: yaml['pgsql']['user'],
  password: yaml['pgsql']['password']
)
pgsql.start(5) # Start it with five simultaneous connections
name = pgsql.exec('SELECT name FROM user WHERE id = $1', [id])[0]['name']

You may also use it if you need direct access to the connection, for example in order to run a set of requests in a transaction:

pgsql.connect do |c|
  c.transaction do |conn|
    conn.exec_params('DELETE FROM user WHERE id = $1', [id])
    conn.exec_params('INSERT INTO user (name, phone) VALUES ($1, $2)', [name, phone])
  end
end

To make your PostgreSQL database visible in your unit test, I would recommend you create a method test_pgsql in your test__helper.rb file (which is required in all unit tests) and implement it like this:

require 'yaml'
require 'minitest/autorun'
require 'pgtk/pool'
module Minitest
  class Test
    def test_pgsql
      config = YAML.load_file('target/pgsql-config.yml')
      @test_pgsql ||= Pgtk::Pool.new(
        host: config['pgsql']['host'],
        port: config['pgsql']['port'],
        dbname: config['pgsql']['dbname'],
        user: config['pgsql']['user'],
        password: config['pgsql']['password']
      ).start
    end
  end
end

Should work. Well, it works in zold-io/wts.zold.io and yegor256/mailanes. They both are open source, you can see how they use pgtk.

How to contribute

Read these guidelines. Make sure your build is green before you contribute your pull request. You will need to have Ruby 2.3+ and Bundler installed. Then:

$ bundle update
$ bundle exec rake

If it's clean and you don't see any error messages, submit your pull request.

pgtk's People

Contributors

yegor256 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.