Class: JunglePath::DBModel::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/jungle_path/db_model/schema.rb

Instance Method Summary collapse

Constructor Details

#initialize(db) ⇒ Schema

Returns a new instance of Schema.



4
5
6
7
# File 'lib/jungle_path/db_model/schema.rb', line 4

def initialize db
  # db should be a "Sequel" db object that is connected to some database.
  @db = db
end

Instance Method Details

#create_table(table_class) ⇒ Object



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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/jungle_path/db_model/schema.rb', line 39

def create_table table_class
  # translate column type keys into Sequel methods used by Sequel (DB.create_table).

  # call the Sequel DB.create_table method with a block:
  #puts "\ttable_name: #{table_class.table_name}"
  schema = self
  @db.create_table? table_class.table_name do
    made_pk = false
    table_class.columns.each_value do |column|
      puts "\tcolumn: #{column.name} #{schema.get_sequel_method(column.type)} #{column.foreign_key_table_name}."
      method = schema.get_sequel_method(column.type, (table_class.primary_key_columns.count > 1))
      args_hash = {}
      if column.foreign_key? and column.not_null?
        #args = [column.name, column.foreign_key_table_name, :null=>false]
        args = [column.name, column.foreign_key_table_name]
        args_hash[:null] = false
      elsif column.foreign_key?
        args = [column.name, column.foreign_key_table_name]
      elsif column.primary_key?
        args = [column.name]
        #made_pk = true
      else
        args = [column.name]
      end
      args_hash[:unique] = true if column.unique?
      args_hash[:null] = false if column.not_null?
      args_hash[:type] = schema.get_postgresql_override_type(column.override_type) unless column.override_type == nil
      args_hash[:default] = column.default_value unless column.default_value == nil
      args << args_hash
      puts "\t\tmethod: #{method}. args:#{args}.\n"
      made_pk = true if column.type == :primary_key and table_class.primary_key_columns.count == 1 # should only happen when pk is single column.
      puts "column: #{column.name}, override_type: #{column.override_type}, override_type.class: #{column.override_type.class}, method: #{method}, args: #{args}."
      send(method, *args)

      #index:
      if column.index
        send('index', column.index)
      end

      #unique index:
      if column.unique_index
        send('unique', column.unique_index)
      end
    end

    if not made_pk
      #puts "creating primary_key for table #{table_class.name}. keys: #{table_class.primary_key_columns.keys}."
      send('primary_key', table_class.primary_key_columns.keys, :name=>"#{table_class.table_name}_pk".to_sym)
    end
    #puts "end of block"
  end
end

#create_table!(table_class) ⇒ Object



92
93
94
# File 'lib/jungle_path/db_model/schema.rb', line 92

def create_table! table_class
  nil
end

#drop_table(table_class) ⇒ Object



96
97
98
# File 'lib/jungle_path/db_model/schema.rb', line 96

def drop_table table_class
  @db.drop_table? table_class.table_name
end

#get_postgresql_override_type(type) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/jungle_path/db_model/schema.rb', line 25

def get_postgresql_override_type type
  return 'text' if type == :string
  return 'integer' if type == :integer
  return 'bigint' if type == :bigint
  return 'timestamp' if type == :timestamp
  return 'timestamp' if type == :timestamp_local
  return 'boolean' if type == :boolean
  return 'date' if type == :date
  return 'json' if type == :json
  return 'jsonb' if type == :jsonb
  return 'double' if type == :float
  return type.to_s
end

#get_sequel_method(type, multi_column_pk = false) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/jungle_path/db_model/schema.rb', line 9

def get_sequel_method type, multi_column_pk=false
  return 'foreign_key' if type == :foreign_key
  return 'primary_key' if type == :primary_key and multi_column_pk == false
  return 'Integer' if type == :primary_key and multi_column_pk == true
  return 'String' if type == :string
  return 'Integer' if type == :integer
  return 'DateTime' if type == :timestamp
  return 'DateTime' if type == :timestamp_local
  return 'TrueClass' if type == :boolean
  return 'Date' if type == :date
  return 'json' if type == :json
  return 'jsonb' if type == :jsonb
  return 'Float' if type == :float
  return type
end