Module: Tapsoob::Schema

Extended by:
Schema
Included in:
Schema
Defined in:
lib/tapsoob/schema.rb

Instance Method Summary collapse

Instance Method Details

#dump(database_url, options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/tapsoob/schema.rb', line 13

def dump(database_url, options = {})
  db = Sequel.connect(database_url)
  db.extension :schema_dumper
  template = ERB.new <<-END_MIG
Class.new(Sequel::Migration) do
  def up
  <% db.send(:sort_dumped_tables, db.tables, {}).each do |table| %>
<%= db.dump_table_schema(table, options) %>
  <% end %>
  end

  def down
  <% db.send(:sort_dumped_tables, db.tables, {}).reverse.each do |table| %>
drop_table("<%= table %>", if_exists: true, cascade: true)
  <% end %>
  end
end
END_MIG

  template.result(binding)
end

#dump_table(database_url_or_db, table, options) ⇒ Object



35
36
37
38
39
40
41
42
43
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
# File 'lib/tapsoob/schema.rb', line 35

def dump_table(database_url_or_db, table, options)
  table = table.to_sym
  # Accept either a database URL or an existing connection object
  if database_url_or_db.is_a?(Sequel::Database)
    db = database_url_or_db
    db.extension :schema_dumper
    <<END_MIG
Class.new(Sequel::Migration) do
  def up
#{db.dump_table_schema(table, options)}
  end

  def down
drop_table("#{table}", if_exists: true)
  end
end
END_MIG
  else
    Sequel.connect(database_url_or_db) do |db|
      db.extension :schema_dumper
      <<END_MIG
Class.new(Sequel::Migration) do
  def up
#{db.dump_table_schema(table, options)}
  end

  def down
drop_table("#{table}", if_exists: true)
  end
end
END_MIG
    end
  end
end

#foreign_keys(database_url) ⇒ Object



70
71
72
73
74
# File 'lib/tapsoob/schema.rb', line 70

def foreign_keys(database_url)
  db = Sequel.connect(database_url)
  db.extension :schema_dumper
  db.dump_foreign_key_migration
end

#indexes(database_url) ⇒ Object



76
77
78
79
80
# File 'lib/tapsoob/schema.rb', line 76

def indexes(database_url)
  db = Sequel.connect(database_url)
  db.extension :schema_dumper
  db.dump_indexes_migration
end

#indexes_individual(database_url) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/tapsoob/schema.rb', line 82

def indexes_individual(database_url)
  idxs = {}
  Sequel.connect(database_url) do |db|
    db.extension :schema_dumper

    tables = db.tables
    tables.each do |table|
      idxs[table] = db.send(:dump_table_indexes, table, :add_index, {}).split("\n")
    end
  end

  idxs.each do |table, indexes|
    idxs[table] = indexes.map do |idx|
      <<END_MIG
Class.new(Sequel::Migration) do
  def up
#{idx}
  end
end
END_MIG
    end
  end
  JSON.generate(idxs)
end

#load(database_url_or_db, schema, options = { drop: false }) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/tapsoob/schema.rb', line 107

def load(database_url_or_db, schema, options = { drop: false })
  # Accept either a database URL or an existing connection object
  if database_url_or_db.is_a?(Sequel::Database)
    db = database_url_or_db
    db.extension :schema_dumper
    klass = eval(schema)
    if options[:drop]
      # Start special hack for MySQL
      db.run("SET foreign_key_checks = 0") if [:mysql, :mysql2].include?(db.adapter_scheme)
      db.run("PRAGMA foreign_keys = OFF") if db.adapter_scheme == :sqlite

      # Run down migration
      klass.apply(db, :down)

      # End special hack for MySQL
      db.run("SET foreign_key_checks = 1") if [:mysql, :mysql2].include?(db.adapter_scheme)
      db.run("PRAGMA foreign_keys = ON") if db.adapter_scheme == :sqlite
    end
    klass.apply(db, :up)
  else
    Sequel.connect(database_url_or_db) do |db|
      db.extension :schema_dumper
      klass = eval(schema)
      if options[:drop]
        # Start special hack for MySQL
        db.run("SET foreign_key_checks = 0") if [:mysql, :mysql2].include?(db.adapter_scheme)
        db.run("PRAGMA foreign_keys = OFF") if db.adapter_scheme == :sqlite

        # Run down migration
        klass.apply(db, :down)

        # End special hack for MySQL
        db.run("SET foreign_key_checks = 1") if [:mysql, :mysql2].include?(db.adapter_scheme)
        db.run("PRAGMA foreign_keys = ON") if db.adapter_scheme == :sqlite
      end
      klass.apply(db, :up)
    end
  end
end

#load_foreign_keys(database_url, foreign_keys) ⇒ Object



147
148
149
150
151
152
# File 'lib/tapsoob/schema.rb', line 147

def load_foreign_keys(database_url, foreign_keys)
  Sequel.connect(database_url) do |db|
    db.extension :schema_dumper
    eval(foreign_keys).apply(db, :up)
  end
end

#load_indexes(database_url, indexes) ⇒ Object



154
155
156
157
158
159
# File 'lib/tapsoob/schema.rb', line 154

def load_indexes(database_url, indexes)
  Sequel.connect(database_url) do |db|
    db.extension :schema_dumper
    eval(indexes).apply(db, :up)
  end
end

#reset_db_sequences(database_url) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/tapsoob/schema.rb', line 161

def reset_db_sequences(database_url)
  Sequel.connect(database_url) do |db|
    db.extension :schema_dumper
    next unless db.respond_to?(:reset_primary_key_sequence)
    db.tables.each do |table|
      pk = db.primary_key(table)
      next unless pk
      pk_type = db.schema(table).find { |col, _| col.to_s == pk.to_s }&.last&.dig(:db_type)
      next unless pk_type&.match?(/int|serial/i)
      db.reset_primary_key_sequence(table)
    rescue Sequel::DatabaseError => e
      Tapsoob.log.warn "Could not reset sequence for table '#{table}': #{e.message.lines.first.chomp}"
    end
  end
end