Permalink
Browse files

Database configuration and models

  • Loading branch information...
1 parent 26a73a7 commit 9b356afef2b0fd6abf400f3c874129242e75840e @dmac committed May 23, 2012
Showing with 64 additions and 0 deletions.
  1. +2 −0 Gemfile
  2. +2 −0 Rakefile
  3. +4 −0 environment.rb
  4. +8 −0 lib/models.rb
  5. +9 −0 script/console.rb
  6. +30 −0 script/create_migration.rb
  7. +9 −0 script/run_migrations.rb
View
2 Gemfile
@@ -6,6 +6,8 @@ gem "unicorn"
gem "pathological"
gem "pinion"
gem "sass"
+gem "sequel"
+gem "pg"
group :development do
gem "foreman"
View
2 Rakefile
@@ -0,0 +1,2 @@
+require "bundler/setup"
+require "pathological"
View
4 environment.rb
@@ -0,0 +1,4 @@
+DB_HOST = "localhost"
+DB_NAME = "app"
+DB_USER = "postgres"
+DB_PASS = ""
View
8 lib/models.rb
@@ -0,0 +1,8 @@
+require "sequel"
+require "pathological"
+require "environment"
+
+DB = Sequel.postgres( :host => DB_HOST, :user => DB_USER, :password => DB_PASS, :database => DB_NAME)
@philc
philc Jun 29, 2012

I see you've built sinatra apps like this 10 times, but on which have you used postgres?

@dmac
dmac Jun 29, 2012

I started using it on the side project that was the catalyst for writing this skeleton, and I haven't looked back. It's nice to use a database that's not stuck in 1990. (utf-8 by default??)

@philc
philc Jun 29, 2012

You can keep your wretched utf8. If latin1 ain't broke, don't fix it.

+
+model_files = Dir[File.join(File.dirname(File.dirname(__FILE__)), "models") + "/*.rb"]
+model_files.each { |file| require file }
View
9 script/console.rb
@@ -0,0 +1,9 @@
+#!/usr/bin/env ruby-local-exec
+
+require "bundler/setup"
+require "pathological"
+require "irb"
+require "lib/models"
+
+IRB.start
+
View
30 script/create_migration.rb
@@ -0,0 +1,30 @@
+#!/usr/bin/env ruby-local-exec
+
+require "fileutils"
+
+name = ARGV[0]
+
+if name.nil?
+ puts "Usage: ruby create_migration name_of_migration"
+ exit 1
+end
+
+migrations_directory = File.join(File.dirname(File.dirname(__FILE__)), "migrations/")
+FileUtils.mkdir_p migrations_directory
+
+timestamp = Time.now.strftime("%Y%m%d%H%M%S") # yyyymmddhhmmss
+filename = File.join(migrations_directory, "#{timestamp}_#{name}.rb")
+
+File.open(filename, "w") do |f|
+ f.puts <<EOF
+Sequel.migration do
+ up do
+ end
+
+ down do
+ end
+end
+EOF
+end
+
+puts filename
View
9 script/run_migrations.rb
@@ -0,0 +1,9 @@
+#!/usr/bin/env ruby-local-exec
+
+require "bundler/setup"
+require "pathological"
+require "environment"
+
+command = "bundle exec sequel -m migrations/"
+db_url = "postgres://#{DB_HOST}/#{DB_NAME}"
+puts `#{command} #{db_url}`

0 comments on commit 9b356af

Please sign in to comment.