├── .gitignore ├── CHANGES.md ├── Gemfile ├── README.md ├── Rakefile ├── config.ru ├── example.env.test ├── lib ├── tamarama.rb └── tamarama │ ├── db │ ├── sequel.rb │ └── sequel │ │ └── database_validator.rb │ ├── env.rb │ ├── rake_task.rb │ └── version.rb ├── tamarama.gemspec └── test └── test_helper.rb /.gitignore: -------------------------------------------------------------------------------- 1 | .env* 2 | -------------------------------------------------------------------------------- /CHANGES.md: -------------------------------------------------------------------------------- 1 | # 0.1.1 2 | 3 | * Fix `rake_task.rb` so it uses `Sequel.()` for DB connection. 4 | 5 | # 0.1.0 6 | 7 | * First release into an unsuspecting world. 8 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # gem 'puma' 4 | gem "rake" 5 | gem 'sinatra' 6 | gem 'pg' 7 | gem "reform" 8 | gem "trailblazer" 9 | gem "trailblazer-loader" 10 | gem "sequel" 11 | gem "dry-validation" 12 | 13 | group :development, :test do 14 | gem 'dotenv' 15 | gem "rack-test" 16 | # gem "match_json" 17 | gem 'database_cleaner' 18 | 19 | gem "minitest" 20 | end 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Configuration 2 | 3 | Tamarama uses the `dotenv` gem for managing environment variables such as database connection strings. 4 | 5 | For you, this boils down to providing `.env.development` and `.env.test` files in the project root. Check out `example.env.test` for an example of what such a config file might look like. 6 | 7 | ```ruby 8 | # .env.test 9 | DATABASE_URL="postgres://user:password@localhost/myblog_test" 10 | ``` 11 | 12 | ## Migrations 13 | 14 | ``` 15 | RACK_ENV=test rake db:migrate 16 | RACK_ENV=test rake db:migrate[0] 17 | ``` 18 | 19 | ## Testing Connection 20 | 21 | ``` 22 | RACK_ENV=test rake db:debug 23 | ``` 24 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/trailblazer/tamarama/2938feb600903d5c2825c3973d72fa783b287f52/Rakefile -------------------------------------------------------------------------------- /config.ru: -------------------------------------------------------------------------------- 1 | require "rack" 2 | 3 | 4 | require_relative "boot" 5 | run Application 6 | -------------------------------------------------------------------------------- /example.env.test: -------------------------------------------------------------------------------- 1 | DATABASE_URL="postgres://nick:stawpmockingmay@localhost/exp_test" 2 | -------------------------------------------------------------------------------- /lib/tamarama.rb: -------------------------------------------------------------------------------- 1 | module Tamarama 2 | end 3 | 4 | # http://blog.andrewvc.com/quickly-constantize/ 5 | # "Net::HTTP".split('::').inject(Object) {|memo,name| memo = memo.const_get(name); memo} 6 | -------------------------------------------------------------------------------- /lib/tamarama/db/sequel.rb: -------------------------------------------------------------------------------- 1 | require "sequel" 2 | 3 | module Tamarama 4 | module DB 5 | module Sequel 6 | def self.call() 7 | ::Sequel::Model.strict_param_setting = false 8 | ::Sequel::Database.extension :pg_json 9 | ::Sequel.extension :pg_json_ops 10 | 11 | environment = ENV["RACK_ENV"] 12 | connection_string = ENV["DATABASE_URL"] 13 | raise "Missing Connection string" if connection_string.nil? 14 | 15 | ::Sequel.connect(connection_string) 16 | end 17 | end 18 | end 19 | end 20 | 21 | # Sequel::Database.extension :"uuid" 22 | # Sequel::Model.plugin :timestamps 23 | -------------------------------------------------------------------------------- /lib/tamarama/db/sequel/database_validator.rb: -------------------------------------------------------------------------------- 1 | module Tamarama 2 | module DB 3 | module Sequel 4 | # Keeps the connection up on a infrequently visited website, but makes it slower. 5 | module DatabaseValidator 6 | def self.call(database_handle) 7 | # via a middleware: https://gist.github.com/jacaetevha/3801154 8 | database_handle.extension(:connection_validator) 9 | database_handle.pool.connection_validation_timeout = -1 # apparently, this is very slow and shouldn't be really done. 10 | end 11 | end 12 | end 13 | end 14 | end 15 | 16 | -------------------------------------------------------------------------------- /lib/tamarama/env.rb: -------------------------------------------------------------------------------- 1 | environment = ENV['RACK_ENV'] ||= 'development' 2 | 3 | require 'dotenv' 4 | Dotenv.load(".env.#{environment}") 5 | 6 | require 'bundler' 7 | Bundler.require :default, environment 8 | -------------------------------------------------------------------------------- /lib/tamarama/rake_task.rb: -------------------------------------------------------------------------------- 1 | # database tasks 2 | require "dotenv/tasks" 3 | 4 | namespace :db do 5 | desc "Run migrations up to specified version or to latest." 6 | task :migrate, [:version] => [:dotenv] do |_, args| 7 | require_relative "env" 8 | require_relative "db/sequel" 9 | # DISCUSS: this should be `require_relative "application"` if you want to load the entire stack for migrations. 10 | 11 | require "sequel/extensions/migration" 12 | 13 | version = args[:version] 14 | migrations_directory = "migrations" 15 | 16 | db = Tamarama::DB::Sequel.() 17 | 18 | message = if args[:version].nil? 19 | Sequel::Migrator.run(db, migrations_directory) 20 | "Migrated to latest" 21 | else 22 | Sequel::Migrator.run(db, migrations_directory, target: version.to_i) 23 | "Migrated to version #{version}" 24 | end 25 | 26 | puts message# if environment != "test" 27 | end 28 | 29 | task :debug => [:dotenv] do 30 | require "application" 31 | 32 | db = Tamarama::DB::Sequel.() 33 | 34 | puts "=== DB: #{db.uri}" 35 | puts db.schema(:users).inspect 36 | # puts User::Persistence.db_schema 37 | # puts User::Persistence.dataset.all.inspect 38 | end 39 | end 40 | 41 | task :default => :test 42 | require "rake/testtask" 43 | 44 | # the test task will require all test files, which in turn require test_helper.rb which in turn 45 | # requires application.rb. 46 | desc 'Default: run tests.' 47 | Rake::TestTask.new(:test) do |test| 48 | test.libs << 'test' 49 | test.pattern = 'test/**/*_test.rb' 50 | test.verbose = true 51 | end 52 | -------------------------------------------------------------------------------- /lib/tamarama/version.rb: -------------------------------------------------------------------------------- 1 | module Tamarama 2 | VERSION = "0.1.1" 3 | end 4 | -------------------------------------------------------------------------------- /tamarama.gemspec: -------------------------------------------------------------------------------- 1 | lib = File.expand_path('../lib', __FILE__) 2 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 3 | require 'tamarama/version' 4 | 5 | Gem::Specification.new do |spec| 6 | spec.name = "tamarama" 7 | spec.version = Tamarama::VERSION 8 | spec.authors = ["Nick Sutterer"] 9 | spec.email = ["apotonick@gmail.com"] 10 | spec.summary = %q{The simplest web stack.} 11 | spec.homepage = "http://trailblazer.to" 12 | spec.license = "MIT" 13 | 14 | spec.files = `git ls-files`.split($/) 15 | spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } 16 | spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) 17 | spec.require_paths = ["lib"] 18 | 19 | spec.add_development_dependency "rake" 20 | spec.add_development_dependency "minitest" 21 | 22 | spec.required_ruby_version = '>= 2.0.0' 23 | end 24 | -------------------------------------------------------------------------------- /test/test_helper.rb: -------------------------------------------------------------------------------- 1 | ENV['RACK_ENV'] = 'test' 2 | require "minitest/autorun" 3 | 4 | 5 | require_relative "../stack/env" 6 | require_relative "../stack/db" 7 | DB.connect 8 | 9 | require_relative "../application" 10 | 11 | require 'rack/test' 12 | Dir['./test/support/*.rb'].each { |file| require file } 13 | 14 | # require "match_json" 15 | --------------------------------------------------------------------------------