Module: Taps::Schema

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

Instance Method Summary collapse

Instance Method Details

#dump(database_url) ⇒ Object



10
11
12
13
# File 'lib/taps/schema.rb', line 10

def dump(database_url)
  db = Sequel.connect(database_url)
  db.dump_schema_migration(:indexes => false)
end

#dump_table(database_url, table) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/taps/schema.rb', line 15

def dump_table(database_url, table)
  table = table.to_sym
  Sequel.connect(database_url) do |db|
    <<END_MIG
Class.new(Sequel::Migration) do
def up
  #{db.dump_table_schema(table.identifier, :indexes => false)}
end

def down
  drop_table("#{table}") if @db.table_exists?("#{table}")
end
end
END_MIG
  end
end

#indexes(database_url) ⇒ Object



32
33
34
35
# File 'lib/taps/schema.rb', line 32

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

#indexes_individual(database_url) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/taps/schema.rb', line 37

def indexes_individual(database_url)
  idxs = {}
  Sequel.connect(database_url) do |db|
    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
  OkJson.encode(idxs)
end

#load(database_url, schema) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/taps/schema.rb', line 60

def load(database_url, schema)
  Sequel.connect(database_url) do |db|
    klass = eval(schema)
    klass.apply(db, :down)
    klass.apply(db, :up)
  end
end

#load_indexes(database_url, indexes) ⇒ Object



68
69
70
71
72
# File 'lib/taps/schema.rb', line 68

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

#reset_db_sequences(database_url) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/taps/schema.rb', line 74

def reset_db_sequences(database_url)
  db = Sequel.connect(database_url)
  return unless db.respond_to?(:reset_primary_key_sequence)
  db.tables.each do |table|
    db.reset_primary_key_sequence(table)
  end
end