1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 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 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 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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
module Sequel
module Schema
class Generator
def initialize(db, &block)
@db = db
@columns = []
@indexes = []
@primary_key = nil
instance_eval(&block) if block
end
def method_missing(type, name = nil, opts = {})
name ? column(name, type, opts) : super
end
def primary_key_name
@primary_key ? @primary_key[:name] : nil
end
def primary_key(name, *args)
@primary_key = @db.serial_primary_key_options.merge({:name => name})
if opts = args.pop
opts = {:type => opts} unless opts.is_a?(Hash)
if type = args.pop
opts.merge!(:type => type)
end
@primary_key.merge!(opts)
end
@primary_key
end
def column(name, type, opts = {})
@columns << {:name => name, :type => type}.merge(opts)
index(name) if opts[:index]
end
def foreign_key(name, opts = {})
@columns << {:name => name, :type => :integer}.merge(opts)
index(name) if opts[:index]
end
def check(*args, &block)
@columns << {:name => nil, :type => :check, :check => block || args}
end
def constraint(name, *args, &block)
@columns << {:name => name, :type => :check, :check => block || args}
end
def has_column?(name)
@columns.each {|c| return true if c[:name] == name}
false
end
def index(columns, opts = {})
columns = [columns] unless columns.is_a?(Array)
@indexes << {:columns => columns}.merge(opts)
end
def full_text_index(columns, opts = {})
index(columns, opts.merge(:type => :full_text))
end
def spatial_index(columns, opts = {})
index(columns, opts.merge(:type => :spatial))
end
def unique(columns, opts = {})
index(columns, opts.merge(:unique => true))
end
def create_info
if @primary_key && !has_column?(@primary_key[:name])
@columns.unshift(@primary_key)
end
[@columns, @indexes]
end
end
class AlterTableGenerator
attr_reader :operations
def initialize(db, &block)
@db = db
@operations = []
instance_eval(&block) if block
end
def add_column(name, type, opts = {})
@operations << { \
:op => :add_column, \
:name => name, \
:type => type \
}.merge(opts)
end
def drop_column(name)
@operations << { \
:op => :drop_column, \
:name => name \
}
end
def rename_column(name, new_name, opts = {})
@operations << { \
:op => :rename_column, \
:name => name, \
:new_name => new_name \
}.merge(opts)
end
def set_column_type(name, type)
@operations << { \
:op => :set_column_type, \
:name => name, \
:type => type \
}
end
def set_column_default(name, default)
@operations << { \
:op => :set_column_default, \
:name => name, \
:default => default \
}
end
def add_index(columns, opts = {})
columns = [columns] unless columns.is_a?(Array)
@operations << { \
:op => :add_index, \
:columns => columns \
}.merge(opts)
end
def add_full_text_index(columns, opts = {})
columns = [columns] unless columns.is_a?(Array)
@operations << { \
:op => :add_index, \
:columns => columns, \
:type => :full_text \
}.merge(opts)
end
def add_spatial_index(columns, opts = {})
columns = [columns] unless columns.is_a?(Array)
@operations << { \
:op => :add_index, \
:columns => columns, \
:type => :spatial \
}.merge(opts)
end
def drop_index(columns)
columns = [columns] unless columns.is_a?(Array)
@operations << { \
:op => :drop_index, \
:columns => columns \
}
end
def add_constraint(name, *args, &block)
@operations << { \
:op => :add_constraint, \
:name => name, \
:type => :check, \
:check => block || args \
}
end
def drop_constraint(name)
@operations << { \
:op => :drop_constraint, \
:name => name \
}
end
end
end
end
|