Class: ActiveRecord::Tasks::TursoDatabaseTasks

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

Instance Method Summary collapse

Constructor Details

#initialize(db_config, root = nil) ⇒ TursoDatabaseTasks

Returns a new instance of TursoDatabaseTasks.



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

def initialize(db_config, root = nil)
  @configuration = db_config
end

Instance Method Details

#charsetObject



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

def charset
  "UTF-8"
end

#charset_collationObject



40
41
42
# File 'lib/active_record/tasks/turso_database_tasks.rb', line 40

def charset_collation
  nil
end

#createObject



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

def create
  path = configuration.database
  return if path == ":memory:"

  FileUtils.mkdir_p(File.dirname(path))
  establish_connection(configuration)
  connection.execute("SELECT 1")
end

#dropObject



21
22
23
24
25
26
27
28
29
# File 'lib/active_record/tasks/turso_database_tasks.rb', line 21

def drop
  path = configuration.database
  return if path == ":memory:"

  disconnect!
  FileUtils.rm_f(path)
  FileUtils.rm_f("#{path}-wal")
  FileUtils.rm_f("#{path}-shm")
end

#purgeObject



31
32
33
34
# File 'lib/active_record/tasks/turso_database_tasks.rb', line 31

def purge
  drop
  create
end

#structure_dump(filename, extra_flags) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/active_record/tasks/turso_database_tasks.rb', line 44

def structure_dump(filename, extra_flags)
  establish_connection(configuration)
  io = File.open(filename, "w")
  io.puts("PRAGMA foreign_keys = OFF;")

  tables = connection.query_values(<<~SQL, "SCHEMA")
    SELECT name FROM sqlite_master
    WHERE type = 'table'
      AND name NOT LIKE 'sqlite_%'
      AND name NOT LIKE '__turso_internal_%'
    ORDER BY name
  SQL

  tables.each do |table|
    sql = connection.query_value(<<~SQL, "SCHEMA")
      SELECT sql FROM sqlite_master WHERE name = #{connection.quote(table)}
    SQL
    io.puts("#{sql};") if sql
  end

  indexes = connection.query_values(<<~SQL, "SCHEMA")
    SELECT name FROM sqlite_master
    WHERE type = 'index'
      AND name NOT LIKE 'sqlite_%'
      AND name NOT LIKE '__turso_internal_%'
      AND sql IS NOT NULL
    ORDER BY name
  SQL

  indexes.each do |index|
    sql = connection.query_value(<<~SQL, "SCHEMA")
      SELECT sql FROM sqlite_master WHERE name = #{connection.quote(index)}
    SQL
    io.puts("#{sql};") if sql
  end

  views = connection.query_values(<<~SQL, "SCHEMA")
    SELECT name FROM sqlite_master
    WHERE type = 'view'
      AND name NOT LIKE 'sqlite_%'
    ORDER BY name
  SQL

  views.each do |view|
    sql = connection.query_value(<<~SQL, "SCHEMA")
      SELECT sql FROM sqlite_master WHERE name = #{connection.quote(view)}
    SQL
    io.puts("#{sql};") if sql
  end

  io.puts("PRAGMA foreign_keys = ON;")
  io.close
end

#structure_load(filename, extra_flags) ⇒ Object



98
99
100
101
102
# File 'lib/active_record/tasks/turso_database_tasks.rb', line 98

def structure_load(filename, extra_flags)
  establish_connection(configuration)
  sql = File.read(filename)
  ActiveRecord::Base.connection.raw_connection.execute_batch(sql)
end