File: sqlite.rb

package info (click to toggle)
ruby-test-prof 1.6.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,448 kB
  • sloc: ruby: 13,093; sh: 4; makefile: 4
file content (42 lines) | stat: -rw-r--r-- 987 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# frozen_string_literal: true

require "test_prof/any_fixture/dump/base_adapter"

module TestProf
  module AnyFixture
    class Dump
      class SQLite < BaseAdapter
        def reset_sequence!(table_name, start)
          execute <<~SQL.chomp
            DELETE FROM sqlite_sequence WHERE name=#{table_name}
          SQL

          execute <<~SQL.chomp
            INSERT INTO sqlite_sequence (name, seq)
            VALUES (#{table_name}, #{start})
          SQL
        end

        def compile_sql(sql, binds)
          sql.gsub("?") { binds.shift.gsub("\n", "' || char(10) || '") }
        end

        def import(path)
          db = conn.pool.spec.config[:database]
          return false if %r{:memory:}.match?(db)

          # Check that sqlite3 is installed
          `sqlite3 --version`

          while_disconnected do
            `sqlite3 #{db} < "#{path}"`
          end

          true
        rescue Errno::ENOENT
          false
        end
      end
    end
  end
end