Class: ActiveRecord::Tasks::SQLiteDatabaseTasks

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/tasks/sqlite_database_tasks.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(configuration, root = ActiveRecord::Tasks::DatabaseTasks.root) ⇒ SQLiteDatabaseTasks

Returns a new instance of SQLiteDatabaseTasks.


8
9
10
# File 'lib/active_record/tasks/sqlite_database_tasks.rb', line 8

def initialize(configuration, root = ActiveRecord::Tasks::DatabaseTasks.root)
  @configuration, @root = configuration, root
end

Instance Method Details

#charsetObject


36
37
38
# File 'lib/active_record/tasks/sqlite_database_tasks.rb', line 36

def charset
  connection.encoding
end

#createObject


12
13
14
15
16
17
# File 'lib/active_record/tasks/sqlite_database_tasks.rb', line 12

def create
  raise DatabaseAlreadyExists if File.exist?(configuration["database"])

  establish_connection configuration
  connection
end

#dropObject


19
20
21
22
23
24
25
26
27
# File 'lib/active_record/tasks/sqlite_database_tasks.rb', line 19

def drop
  require "pathname"
  path = Pathname.new configuration["database"]
  file = path.absolute? ? path.to_s : File.join(root, path)

  FileUtils.rm(file)
rescue Errno::ENOENT => error
  raise NoDatabaseError.new(error.message)
end

#purgeObject


29
30
31
32
33
34
# File 'lib/active_record/tasks/sqlite_database_tasks.rb', line 29

def purge
  drop
rescue NoDatabaseError
ensure
  create
end

#structure_dump(filename, extra_flags) ⇒ Object


40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/active_record/tasks/sqlite_database_tasks.rb', line 40

def structure_dump(filename, extra_flags)
  args = []
  args.concat(Array(extra_flags)) if extra_flags
  args << configuration["database"]

  ignore_tables = ActiveRecord::SchemaDumper.ignore_tables
  if ignore_tables.any?
    condition = ignore_tables.map { |table| connection.quote(table) }.join(", ")
    args << "SELECT sql FROM sqlite_master WHERE tbl_name NOT IN (#{condition}) ORDER BY tbl_name, type DESC, name"
  else
    args << ".schema"
  end
  run_cmd("sqlite3", args, filename)
end

#structure_load(filename, extra_flags) ⇒ Object


55
56
57
58
59
# File 'lib/active_record/tasks/sqlite_database_tasks.rb', line 55

def structure_load(filename, extra_flags)
  dbfile = configuration["database"]
  flags = extra_flags.join(" ") if extra_flags
  `sqlite3 #{flags} #{dbfile} < "#{filename}"`
end