Class: Yescode::Database
- Inherits:
-
Object
- Object
- Yescode::Database
- Defined in:
- lib/yescode/database.rb
Class Attribute Summary collapse
- .connection_string ⇒ Object
-
.logger ⇒ Object
Returns the value of attribute logger.
Class Method Summary collapse
- .connection ⇒ Object
- .execute(sql, params = nil) ⇒ Object
- .get_first_row(sql, params = nil) ⇒ Object
- .get_first_value(sql, params = nil) ⇒ Object
- .migrate(filenames) ⇒ Object
- .rollback ⇒ Object
- .rollback_schema(filenames, step:) ⇒ Object
- .schema ⇒ Object
- .transaction(mode = :deferred) ⇒ Object
- .version(filename) ⇒ Object
Class Attribute Details
.connection_string ⇒ Object
9 10 11 |
# File 'lib/yescode/database.rb', line 9 def connection_string @connection_string || ENV["DATABASE_URL"] end |
.logger ⇒ Object
Returns the value of attribute logger.
7 8 9 |
# File 'lib/yescode/database.rb', line 7 def logger @logger end |
Class Method Details
.connection ⇒ Object
13 14 15 |
# File 'lib/yescode/database.rb', line 13 def connection @connection ||= SQLite3::Database.new(connection_string, { results_as_hash: true }) end |
.execute(sql, params = nil) ⇒ Object
86 87 88 89 90 |
# File 'lib/yescode/database.rb', line 86 def execute(sql, params = nil) logger&.debug(sql:, params:) connection.execute sql, params || [] end |
.get_first_row(sql, params = nil) ⇒ Object
98 99 100 101 102 |
# File 'lib/yescode/database.rb', line 98 def get_first_row(sql, params = nil) logger&.debug(sql:, params:) connection.get_first_row sql, params || [] end |
.get_first_value(sql, params = nil) ⇒ Object
92 93 94 95 96 |
# File 'lib/yescode/database.rb', line 92 def get_first_value(sql, params = nil) logger&.debug(sql:, params:) connection.get_first_value sql, params || [] end |
.migrate(filenames) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/yescode/database.rb', line 21 def migrate(filenames) return unless connection_string execute "create table if not exists yescode_migrations ( version integer primary key )" rows = execute("select version from yescode_migrations").map { |r| r["version"] } file_versions = filenames.map { |f| version(f) } return if (file_versions - rows).empty? transaction do filenames.each do |filename| version = version(filename) next if rows.include?(version) queries = Queries.queries(filename) execute(queries.find { |name, _| name == :up }[1]) execute "insert into yescode_migrations (version) values (?)", version end end end |
.rollback ⇒ Object
82 83 84 |
# File 'lib/yescode/database.rb', line 82 def rollback @abort = true end |
.rollback_schema(filenames, step:) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/yescode/database.rb', line 42 def rollback_schema(filenames, step:) return unless connection_string execute "create table if not exists yescode_migrations ( version integer primary key )" rows = execute("select version from yescode_migrations order by version desc limit ?", step).map { |r| r["version"] } return if rows.empty? transaction do rows.each do |row| filename = filenames.find { |f| version(f) == row } version = version(filename) next unless rows.include?(version) queries = Queries.queries(filename) execute(queries.find { |name, _| name == :down }[1]) execute "delete from yescode_migrations where version = ?", version end end end |
.schema ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/yescode/database.rb', line 104 def schema return @schema if @schema sql = <<-SQL select m.name as tableName, pti.name as columnName, pti.type as columnType, m.sql as sql, pti.pk as pk from sqlite_master m left outer join pragma_table_info(m.name) pti on pti.name <> m.name where m.type == 'table' order by tableName, columnName SQL rows = Database.execute sql @schema = rows.group_by do |r| r["tableName"] end end |
.transaction(mode = :deferred) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/yescode/database.rb', line 63 def transaction(mode = :deferred) return yield(connection) if connection.transaction_active? execute "begin #{mode} transaction" @abort = false yield self true rescue StandardError @abort = true raise ensure if @abort execute "rollback transaction" else execute "commit transaction" end end |
.version(filename) ⇒ Object
17 18 19 |
# File 'lib/yescode/database.rb', line 17 def version(filename) File.basename(filename).split("_").first.to_i end |