Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

postgresql db configuration #5

Merged
merged 3 commits into from
Nov 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
natra (0.0.4)
natra (0.0.5)
activesupport (~> 5.0)
thor (~> 0.18)

Expand Down Expand Up @@ -164,4 +164,4 @@ DEPENDENCIES
rspec

BUNDLED WITH
1.16.6
1.17.1
26 changes: 17 additions & 9 deletions lib/natra/generators/app/app_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class AppGenerator < Thor::Group
def setup
@app_path = name.directory_name
@name = name.file_name
options.each {|key, value| instance_variable_set "@#{key}".to_sym, value}
options.each { |key, value| instance_variable_set "@#{key}".to_sym, value }
end

def self.source_root
Expand Down Expand Up @@ -73,11 +73,7 @@ def create_readme
end

def create_db_config
template('config/db.yml', File.join(@app_path, 'config/db.yml'))
end

def create_database_initializer
template('config/initializers/database.rb', File.join(@app_path, 'config/initializers/database.rb'))
template('config/database.yml', File.join(@app_path, 'config/database.yml'))
end

def create_redis_config
Expand Down Expand Up @@ -121,7 +117,7 @@ def create_secrets
end

def create_capistrano_config
inside(@app_path) {run('cap install')} if @capistrano
inside(@app_path) { run('cap install') } if @capistrano
end

def create_rvm_gemset
Expand All @@ -135,11 +131,23 @@ def create_rvm_gemset
end

def initialize_git_repo
inside(@app_path) {run('git init .') if @git}
inside(@app_path) { run('git init .') if @git }
end

def install_dependencies
inside(@app_path) {run('bundle') if @bundle}
inside(@app_path) { run('bundle') if @bundle }
end

def initialize_app
system <<~SCRIPT
cd #{@app_path}
#{'chmod +x bin/setup'}
git init
git add .
docker-compose build --pull
docker-compose run --rm web bundle
#{'nib setup web'}
SCRIPT
end
end
end
Expand Down
12 changes: 2 additions & 10 deletions lib/natra/generators/app/templates/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
FROM ruby AS base

WORKDIR /usr/src/app

RUN gem install bundler

COPY Gemfile /usr/src/app/

FROM base AS development

RUN bundle install -j5 --without staging production

COPY . /usr/src/app

FROM base AS release

RUN bundle install -j5 --without development test

RUN chmod 755 ./usr/src/app
RUN chmod 755 ./usr/src/app/bin/setup
COPY . /usr/src/app

CMD puma
21 changes: 10 additions & 11 deletions lib/natra/generators/app/templates/Gemfile
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
source 'http://rubygems.org'
ruby '2.5.3'
gem 'bcrypt'
gem 'coveralls'
gem 'natra'
gem 'seed-fu'
gem 'scout_apm'
gem 'sinatra'
gem 'activerecord', '~> 4.2', '>= 4.2.6', :require => 'active_record'
gem 'sinatra-activerecord', :require => 'sinatra/activerecord'
gem 'rake'
gem 'rack-timeout'
gem 'require_all'
gem 'thin'
gem 'shotgun'
gem 'scout_apm'
gem 'seed-fu'
gem 'pry'
gem 'bcrypt'
gem "tux"
gem 'pg', '~> 0.21.0'
gem 'puma'
gem 'oj'
group :development, :test do
gem 'pry-byebug'
gem 'shotgun'
gem 'pry'
end
group :test do
gem 'awesome_print'
gem 'capybara'
gem 'database_cleaner'
gem 'factory_bot'
gem 'faker'
gem 'guard-rspec'
gem 'guard-rubocop'
gem 'rack-test'
gem 'simplecov'
gem 'rspec'
gem 'capybara'
gem 'rack-test'
gem 'rspec'
end
2 changes: 1 addition & 1 deletion lib/natra/generators/app/templates/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# <%= @name.camel_case %> service
# <%= @name %> service

## Commands
```
Expand Down
3 changes: 1 addition & 2 deletions lib/natra/generators/app/templates/Rakefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
ENV["SINATRA_ENV"] ||= "development"

require_relative './config/environment'
require 'sinatra/activerecord/rake'
require 'sinatra/activerecord'
6 changes: 0 additions & 6 deletions lib/natra/generators/app/templates/bin/ci

This file was deleted.

6 changes: 3 additions & 3 deletions lib/natra/generators/app/templates/bin/setup
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

set -e

SINATRA_ENV=development rake db:create db:migrate --trace
SINATRA_ENV=development rake db:seed_fu --trace
SINATRA_ENV=test DATABASE_URL=$TEST_DATABASE_URL rake db:create db:migrate --trace
RACK_ENV=development rake db:create db:migrate --trace
RACK_ENV=development rake db:seed --trace
RACK_ENV=test DATABASE_URL=$TEST_DATABASE_URL rake db:create db:migrate --trace
7 changes: 0 additions & 7 deletions lib/natra/generators/app/templates/config.ru
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
require './config/environment'
# Call as early as possible so rack-timeout runs before all other middleware.
use Rack::Timeout, service_timeout: 5

ScoutApm::Rack.install!

if ActiveRecord::Migrator.needs_migration?
raise 'Migrations are pending. Run `rake db:migrate` to resolve the issue.'
end

run ApplicationController
15 changes: 15 additions & 0 deletions lib/natra/generators/app/templates/config/database.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
development:
adapter: postgresql
encoding: unicode
database: ENV['DEV_DATABASE']
pool: 2
production:
adapter: postgresql
encoding: unicode
database: ENV['PROD_DATABASE']
pool: 2
test:
adapter: postgresql
encoding: unicode
database: ENV['TEST_DATABASE']
pool: 2
4 changes: 0 additions & 4 deletions lib/natra/generators/app/templates/config/db.yml

This file was deleted.

8 changes: 2 additions & 6 deletions lib/natra/generators/app/templates/config/environment.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
ENV['SINATRA_ENV'] ||= "development"
ENV['RACK_ENV'] ||= 'development'
require 'rubygems'
require 'bundler/setup'
Bundler.require(:default, ENV['SINATRA_ENV'])
Bundler.require(:default, ENV['RACK_ENV'])
require 'active_support'
require 'active_support/core_ext/hash/indifferent_access'
require 'active_support/core_ext/string'

require 'rack-timeout'
require 'scout_apm'

require 'oj'
ActiveRecord::Base.establish_connection(ENV['DEV_DATABASE_URL'])

require './app/controllers/application_controller'
require_all 'app'

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "yaml"

# Redis Configuration
unless ENV['SINATRA_ENV'] == 'test'
unless ENV['RACK_ENV'] == 'test'
redis_settings = YAML::load_file("config/redis.yml")
REDIS = Redis.new(redis_settings[ENV['SINATRA_ENV']])
REDIS = Redis.new(redis_settings[ENV['RACK_ENV']])
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class AddExtensions < ActiveRecord::Migration
class AddExtensions < ActiveRecord::Migration[5.2]
def change
enable_extension 'hstore'
enable_extension 'uuid-ossp'
Expand Down
19 changes: 14 additions & 5 deletions lib/natra/generators/app/templates/secrets.env
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
SINATRA_ENV=development
RACK_ENV=development
LOG_LEVEL=Logger::INFO
DEV_DATABASE_URL=postgresql://docker:docker@db:5432/<%= @name.camel_case %>_dev?pool=5
PROD_DATABASE_URL=postgresql://docker:docker@db:5432/<%= @name.camel_case %>_prod?pool=5
TEST_DATABASE_URL=postgresql://docker:docker@db:5432/<%= @name.camel_case %>_test?pool=5

DEV_DATABASE=development_<%=@name.directory_name%>

PROD_DATABASE=production_<%=@name.directory_name%>


TEST_DATABASE=test_<%=@name.directory_name%>


POSTGRES_USER=docker
POSTGRES_PASSWORD=docker
POSTGRES_DB=<%= @name.camel_case %>_dev
POSTGRES_DB=development_<%=@name.directory_name%>

DATABASE_URL=postgresql://docker:docker@db:5432/development_<%=@name.directory_name%>?pool=5
TEST_DATABASE_URL=postgresql://docker:docker@db:5432/test_<%=@name.directory_name%>?pool=5
8 changes: 4 additions & 4 deletions lib/natra/generators/app/templates/spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
ENV["SINATRA_ENV"] = "test"

ENV["RACK_ENV"] = "test"
require_relative '../config/environment'
require 'rack/test'
require 'capybara/rspec'
require 'capybara/dsl'

require 'coveralls'
Coveralls.wear!
if ActiveRecord::Migrator.needs_migration?
raise 'Migrations are pending. Run `rake db:migrate SINATRA_ENV=test` to resolve the issue.'
raise 'Migrations are pending. Run `rake db:migrate RACK_ENV=test` to resolve the issue.'
end

ActiveRecord::Base.logger = nil
Expand Down
2 changes: 1 addition & 1 deletion lib/natra/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Natra
VERSION = '0.0.4'.freeze
VERSION = '0.0.5'.freeze
end
2 changes: 1 addition & 1 deletion natra.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
spec.email = ['thirunjuguna@outlook.com']

spec.summary = 'Natra generate a light weight sinatra application'
spec.description = 'Natra generate a light weight sinatra application.It\'s ideal for building micro-services'
spec.description = 'Natra generate a light weight sinatra application.It\'s ideal for building containerized micro-services with postgres database'
spec.homepage = 'https://github.com/thirunjuguna/natra'
spec.license = 'MIT'
if spec.respond_to?(:metadata)
Expand Down