lib/ezframe/database.rb in ezframe-0.0.1 vs lib/ezframe/database.rb in ezframe-0.0.3
- old
+ new
@@ -3,17 +3,19 @@
module Ezframe
class Database
attr_accessor :sequel
- def initialize(dbfile = "db/devel.sqlite")
+ def initialize(dbfile = nil)
@dbfile = dbfile
connect
end
def connect
- @sequel = Sequel.connect("sqlite://#{@dbfile}", loggers: [Logger.new($stdout)])
+ @dbfile ||= ENV["EZFRAME_DB"] || Config[:database] || "sqlite://db/devel.sqlite"
+ puts "Database.connect: dbfile=#{@dbfile}"
+ @sequel = Sequel.connect(@dbfile, loggers: [Logger.new($stdout)])
end
def exec(sql)
@sequel.run(sql)
end
@@ -24,11 +26,16 @@
def create_table(table_name, dbtype_h)
%w[id created_at updated_at].each do |key|
dbtype_h.delete(key.to_sym)
end
+ puts "create_table: #{table_name}"
@sequel.create_table(table_name) do
- primary_key :id, auto_increment: true
+# if @dbfile.index("postgresql")
+ serial :id
+# else
+ #primary_key :id, auto_increment: true
+ #end
dbtype_h.each do |key, dbtype|
column(key, dbtype)
end
column(:created_at, :timestamp, default: Sequel::CURRENT_TIMESTAMP)
column(:updated_at, :timestamp, default: Sequel::CURRENT_TIMESTAMP)