Module: ScribeDb

Defined in:
lib/scribelite/models/forward.rb,
lib/scribelite.rb,
lib/scribelite/schema.rb,
lib/scribelite/models/calldata.rb,
lib/scribelite/models/inscribe.rb

Overview

forward references

require first to resolve circular references

Defined Under Namespace

Modules: Model Classes: CreateDb

Constant Summary collapse

Models =

note: convenience alias for Model lets you use include ScribeDb::Models

Model

Class Method Summary collapse

Class Method Details

.auto_migrate!Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/scribelite.rb', line 56

def self.auto_migrate!
  ### todo/fix:
  ##    check props table and versions!!!!!

  # first time? - auto-run db migratation, that is, create db tables
  unless LogDb::Model::Log.table_exists?
    LogDb.create     # add logs table
  end

  unless ConfDb::Model::Prop.table_exists?
    ConfDb.create    # add props table
  end

  unless ScribeDb::Model::Inscribe.table_exists?
    ScribeDb.create
  end
end

.connect(config = {}) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/scribelite.rb', line 85

def self.connect( config={} )

  if config.empty?
    puts "ENV['DATBASE_URL'] - >#{ENV['DATABASE_URL']}<"

    ### change default to ./scribe.db ?? why? why not?
    db = URI.parse( ENV['DATABASE_URL'] || 'sqlite3:///scribe.db' )

    if db.scheme == 'postgres'
      config = {
        adapter: 'postgresql',
        host: db.host,
        port: db.port,
        username: db.user,
        password: db.password,
        database: db.path[1..-1],
        encoding: 'utf8'
      }
    else # assume sqlite3
     config = {
       adapter: db.scheme, # sqlite3
       database: db.path[1..-1] # scribe.db (NB: cut off leading /, thus 1..-1)
    }
    end
  end

  puts "Connecting to db using settings: "
  pp config
  ActiveRecord::Base.establish_connection( config )
  # ActiveRecord::Base.logger = Logger.new( STDOUT )
end

.createObject



44
45
46
47
48
# File 'lib/scribelite.rb', line 44

def self.create
  CreateDb.new.up
  ConfDb::Model::Prop.create!( key: 'db.schema.scribe.version', 
                               value: Scribelite::VERSION )
end

.create_allObject



50
51
52
53
54
# File 'lib/scribelite.rb', line 50

def self.create_all
  LogDb.create    # add logs table
  ConfDb.create   # add props table
  ScribeDb.create
end

.open(database = './scribe.db') ⇒ Object

convenience helper for sqlite only



75
76
77
78
79
80
81
# File 'lib/scribelite.rb', line 75

def self.open( database='./scribe.db' )   ## convenience helper for sqlite only
    connect( adapter:  'sqlite3',
             database: database )

    ## build schema if database new/empty
    auto_migrate!
end

.setup_in_memory_dbObject



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/scribelite.rb', line 118

def self.setup_in_memory_db

  # Database Setup & Config
  ActiveRecord::Base.logger = Logger.new( STDOUT )
  ## ActiveRecord::Base.colorize_logging = false  - no longer exists - check new api/config setting?

  self.connect( adapter:  'sqlite3',
                database: ':memory:' )

  ## build schema
  ScribeDb.create_all
end