class Rubiclifier::DB
Constants
- SETTINGS_TABLE_MIGRATION
Public Class Methods
conn()
click to toggle source
# File lib/database.rb, line 14 def self.conn Feature.fail_unless_enabled(Feature::DATABASE) @conn end
execute(sql)
click to toggle source
# File lib/database.rb, line 19 def self.execute(sql) conn.execute(sql) end
get_setting(key)
click to toggle source
# File lib/database.rb, line 30 def self.get_setting(key) row = query_single_row("SELECT value, salt FROM settings WHERE key = '#{key}'") if row && row[1] Cipher.decrypt(row[0], row[1]) elsif row row[0] end end
hydrate(data_directory, migrations_location)
click to toggle source
# File lib/database.rb, line 8 def self.hydrate(data_directory, migrations_location) system("mkdir -p \"#{data_directory.sub("~", "$HOME")}\"") @conn = SQLite3::Database.new(File.expand_path("#{data_directory}/data.sqlite3")) migrate_if_needed(migrations_location) end
migrate_if_needed(migrations_location)
click to toggle source
# File lib/database.rb, line 53 def self.migrate_if_needed(migrations_location) all_migrations = [SETTINGS_TABLE_MIGRATION] all_migrations.concat(eval(File.read(migrations_location))) if migrations_location conn.execute("CREATE TABLE IF NOT EXISTS migrations (id INT PRIMARY KEY);") last_migration = query_single_row("SELECT id FROM migrations ORDER BY id DESC LIMIT 1;")&.to_a&.fetch(0) || -1 if all_migrations.length - 1 > last_migration all_migrations[(last_migration + 1)..-1].each_with_index do |sql, i| conn.execute(sql) conn.execute("INSERT INTO migrations (id) VALUES (#{i + last_migration + 1});") end end end
query_single_row(sql)
click to toggle source
# File lib/database.rb, line 23 def self.query_single_row(sql) conn.execute(sql) do |row| return row end return nil end
save_setting(key, value, is_secret:)
click to toggle source
# File lib/database.rb, line 39 def self.save_setting(key, value, is_secret:) salt = "NULL" output_value = "NULL" if is_secret && value salt, encrypted = Cipher.encrypt(value) salt = "'#{salt}'" output_value = "'#{encrypted}'" elsif value output_value = "'#{value}'" end conn.execute("DELETE FROM settings WHERE key = '#{key}';") conn.execute("INSERT INTO settings (key, value, salt) VALUES('#{key}', #{output_value}, #{salt});") end