Skip to content
This repository has been archived by the owner on Dec 31, 2022. It is now read-only.

Commit

Permalink
Added Sqlite Driver support
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Rzegocki committed Apr 24, 2016
1 parent 197b7de commit 3ecb321
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 5 deletions.
5 changes: 3 additions & 2 deletions README.md
Expand Up @@ -19,6 +19,7 @@ to do is create a layer and application with assigned RDS data source, then
* MariaDB
* MySQL
* PostgreSQL
* Sqlite3
* SCM
* git
* Framework
Expand Down Expand Up @@ -75,8 +76,8 @@ you don't need to use them. The chef will do all the job, and determine them
for you.

* `app['database']['adapter']`
* **Supported values:** `mariadb`, `mysql`, `postgresql`
* **Default:** `postgresql`
* **Supported values:** `mariadb`, `mysql`, `postgresql`, `sqlite3`
* **Default:** `sqlite3` (if no RDBMS is detected)
* ActiveRecord adapter which will be used for database connection.
* `app['database']['username']`
* Username used to authenticate to the DB
Expand Down
3 changes: 2 additions & 1 deletion libraries/drivers_db_base.rb
Expand Up @@ -3,6 +3,7 @@ module Drivers
module Db
class Base < Drivers::Base
include Drivers::Dsl::Defaults
include Drivers::Dsl::Output
include Drivers::Dsl::Packages

defaults encoding: 'utf8', host: 'localhost', reconnect: true
Expand Down Expand Up @@ -39,7 +40,7 @@ def out

out_defaults.merge(
adapter: adapter, username: options[:rds]['db_user'], password: options[:rds]['db_password'],
host: options[:rds]['address'], database: app['data_sources'].first['database_name']
host: options[:rds]['address'], database: app['data_sources'].first.try(:[], 'database_name')
)
end
# rubocop:enable Metrics/AbcSize
Expand Down
17 changes: 17 additions & 0 deletions libraries/drivers_db_sqlite.rb
@@ -0,0 +1,17 @@
# frozen_string_literal: true
module Drivers
module Db
class Sqlite < Base
adapter :sqlite3
allowed_engines :sqlite, :sqlite3
packages debian: 'libsqlite3-dev', rhel: 'sqlite-devel'
output filter: [:adapter, :database, :pool, :timeout]

def out
output = super
output[:database] ||= 'db/data.sqlite3'
handle_output(output)
end
end
end
end
8 changes: 6 additions & 2 deletions libraries/helpers.rb
Expand Up @@ -44,8 +44,12 @@ def every_enabled_application
end

def every_enabled_rds
rdses.each do |rds|
yield rds
if rdses.blank?
yield('engine' => 'sqlite')
else
rdses.each do |rds|
yield rds
end
end
end

Expand Down
28 changes: 28 additions & 0 deletions spec/unit/recipes/configure_spec.rb
Expand Up @@ -233,6 +233,34 @@
end
end

context 'Sqlite3' do
let(:dummy_node) do
node(deploy: { dummy_project: { database: { adapter: 'sqlite3' } } })
end
let(:chef_run) do
ChefSpec::SoloRunner.new(platform: 'ubuntu', version: '14.04') do |solo_node|
solo_node.set['deploy'] = dummy_node['deploy']
end.converge(described_recipe)
end

before do
stub_search(:aws_opsworks_app, '*:*').and_return([aws_opsworks_app(data_sources: [])])
stub_search(:aws_opsworks_rds_db_instance, '*:*').and_return([])
end

it 'creates proper database.yml template' do
db_config = Drivers::Db::Sqlite.new(
aws_opsworks_app(data_sources: []), dummy_node, rds: aws_opsworks_rds_db_instance(engine: 'sqlite3')
).out
expect(db_config[:adapter]).to eq 'sqlite3'
expect(db_config[:database]).to eq 'db/data.sqlite3'
expect(chef_run)
.to render_file("/srv/www/#{aws_opsworks_app['shortname']}/shared/config/database.yml").with_content(
JSON.parse({ development: db_config, production: db_config }.to_json).to_yaml
)
end
end

it 'empty node[\'deploy\']' do
chef_run = ChefSpec::SoloRunner.new(platform: 'ubuntu', version: '14.04') do |solo_node|
solo_node.set['lsb'] = node['lsb']
Expand Down
14 changes: 14 additions & 0 deletions spec/unit/recipes/setup_spec.rb
Expand Up @@ -163,6 +163,20 @@
end
end

context 'Sqlite' do
before do
stub_search(:aws_opsworks_rds_db_instance, '*:*').and_return([])
end

it 'installs required packages for debian' do
expect(chef_run).to install_package('libsqlite3-dev')
end

it 'installs required packages for rhel' do
expect(chef_run_rhel).to install_package('sqlite-devel')
end
end

it 'empty node[\'deploy\']' do
chef_run = ChefSpec::SoloRunner.new(platform: 'ubuntu', version: '14.04') do |solo_node|
solo_node.set['lsb'] = node['lsb']
Expand Down

0 comments on commit 3ecb321

Please sign in to comment.