Module: Rubee::CLI::Db

Defined in:
lib/rubee/cli/db.rb

Class Method Summary collapse

Class Method Details

.call(command, argv) ⇒ Object



5
6
7
8
9
# File 'lib/rubee/cli/db.rb', line 5

def call(command, argv)
  command = argv[1].split(':').first
  ENV['RACK_ENV'] ||= 'development'
  send(command, argv)
end

.drop_tables(_argv) ⇒ Object



40
41
42
43
44
# File 'lib/rubee/cli/db.rb', line 40

def drop_tables(_argv)
  out = Rubee::SequelObject::DB.tables.each { |table| Rubee::SequelObject::DB.drop_table(table, cascade: true) }
  color_puts("These tables have been dropped for the #{ENV['RACK_ENV']} env:", color: :cyan)
  color_puts(out, color: :gray)
end

.init(_argv) ⇒ Object



32
33
34
# File 'lib/rubee/cli/db.rb', line 32

def init(_argv)
  ensure_database_exists(Rubee::Configuration.get_database_url)
end

.run(argv) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rubee/cli/db.rb', line 11

def run(argv)
  _, file_name = argv[1]&.split(':')
  file_names = if file_name == 'all'
    lib = Rubee::PROJECT_NAME == 'rubee' ? '/lib' : ''
    Dir.glob(".#{lib}/db/*.rb").sort.map do |file|
      File.basename(file, '.rb')
    end.reject { |file| file == 'structure' }
  else
    [file_name]
  end
  file_names.each do |file|
    color_puts("Run #{file} file for #{ENV['RACK_ENV']} env", color: :cyan)
    Object.const_get(file.gsub(/^\d+_/, '').split('_').map(&:capitalize).join).new.call
  end
  color_puts("Migration for #{file_name} completed", color: :green)
  unless Rubee::PROJECT_NAME == 'rubee'
    color_puts('Regenerate schema file', color: :cyan)
    generate_structure
  end
end

.schema(argv) ⇒ Object



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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/rubee/cli/db.rb', line 52

def schema(argv)
  target_table_hash = argv[2] ? { argv[2].to_sym => STRUCTURE[argv[2].to_sym] } : nil
  (target_table_hash || STRUCTURE).each do |table_name, table_def|
    unless Rubee::SequelObject::DB.table_exists?(table_name.to_sym)
      color_puts("Table #{table_name} not found", color: :red)
      next
    end
    columns = table_def[:columns]
    foreign_keys = table_def[:foreign_keys] || []

    # Table header
    color_puts(
      "--- #{table_name}",
      color: :cyan,
      style: :bold
    )

    # Columns
    columns.each do |column_name, meta|
      parts = []

      # column name
      parts << "- #{column_name}"

      # PK
      parts << "(PK)" if meta[:primary_key]

      # type
      parts << "type (#{meta[:db_type]})" if meta[:db_type]

      # nullable
      parts << "nullable" if meta[:allow_null]

      line = parts.join(", ")

      color_puts(
        line,
        color: meta[:primary_key] ? :yellow : :gray
      )
    end

    # Foreign keys
    if foreign_keys.any?
      puts
      color_puts("  Foreign keys:", color: :magenta, style: :bold)

      foreign_keys.each do |fk|
        cols = Array(fk[:columns]).join(", ")

        ref_table =
          fk.dig(:references, :table) ||
          fk[:table]

        ref_cols =
          Array(fk.dig(:references, :columns)) || fk["id"]

        fk_line = "  - #{cols}#{ref_table}(#{ref_cols.join(', ')})"

        fk_line += " on delete #{fk[:on_delete]}" if fk[:on_delete]
        fk_line += " on update #{fk[:on_update]}" if fk[:on_update]

        color_puts(fk_line, color: :gray)
      end
    end

    puts
  end
end

.structure(_argv) ⇒ Object



36
37
38
# File 'lib/rubee/cli/db.rb', line 36

def structure(_argv)
  generate_structure
end

.truncate_tables(_argv) ⇒ Object



46
47
48
49
50
# File 'lib/rubee/cli/db.rb', line 46

def truncate_tables(_argv)
  out = Rubee::SequelObject::DB.tables.each { |table| Rubee::SequelObject::DB[table].truncate }
  color_puts("These tables have been truncated for the #{ENV['RACK_ENV']} env:", color: :cyan)
  color_puts(out, color: :gray)
end