Class: Duck_Duck_Duck
- Inherits:
-
Object
- Object
- Duck_Duck_Duck
- Defined in:
- lib/duck_duck_duck.rb
Constant Summary collapse
- DB =
Sequel.connect(ENV['DATABASE_URL'])
- SCHEMA_TABLE =
ENV['SCHEMA_TABLE'] || '_schema'
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
Instance methods: ===============================================.
-
#name ⇒ Object
readonly
Instance methods: ===============================================.
-
#sub_action ⇒ Object
readonly
Instance methods: ===============================================.
Class Method Summary collapse
- .create(*args) ⇒ Object
- .dev_only ⇒ Object
- .migrate_schema ⇒ Object
- .read_file(f) ⇒ Object
- .stdout(*args) ⇒ Object
Instance Method Summary collapse
-
#create ⇒ Object
def down.
-
#down ⇒ Object
def up.
- #file_to_ver(str) ⇒ Object
- #init_model_in_schema ⇒ Object
-
#initialize(*args) ⇒ Duck_Duck_Duck
constructor
A new instance of Duck_Duck_Duck.
- #reset ⇒ Object
- #stdout(*args) ⇒ Object
- #up ⇒ Object
Constructor Details
#initialize(*args) ⇒ Duck_Duck_Duck
Returns a new instance of Duck_Duck_Duck.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/duck_duck_duck.rb', line 78 def initialize *args @name, @action, @sub_action = args if !@name fail ArgumentError, "Name required." end @__files = `find . -iregex ".+/#{name}/migrates/__.+\.sql"` .strip .split("\n") .sort @files = ( `find . -iregex ".+/#{name}/migrates/.+\.sql"` .strip .split("\n") .grep(/\/\d+\-/) .sort ) end |
Instance Attribute Details
#action ⇒ Object (readonly)
Instance methods:
76 77 78 |
# File 'lib/duck_duck_duck.rb', line 76 def action @action end |
#name ⇒ Object (readonly)
Instance methods:
76 77 78 |
# File 'lib/duck_duck_duck.rb', line 76 def name @name end |
#sub_action ⇒ Object (readonly)
Instance methods:
76 77 78 |
# File 'lib/duck_duck_duck.rb', line 76 def sub_action @sub_action end |
Class Method Details
.create(*args) ⇒ Object
19 20 21 |
# File 'lib/duck_duck_duck.rb', line 19 def create *args new(*args).create end |
.dev_only ⇒ Object
15 16 17 |
# File 'lib/duck_duck_duck.rb', line 15 def dev_only fail "Not allowed on a dev machine." if ENV['IS_DEV'] end |
.migrate_schema ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/duck_duck_duck.rb', line 23 def migrate_schema DB << <<-EOF CREATE TABLE IF NOT EXISTS #{SCHEMA_TABLE} ( name varchar(255) NOT NULL PRIMARY KEY, version smallint NOT NULL DEFAULT 0 ) EOF end |
.read_file(f) ⇒ Object
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 |
# File 'lib/duck_duck_duck.rb', line 32 def read_file f raw = File.read(f).split(/\s*--\s+(UP|BOTH|DOWN):?\s*/). inject([:UP, {:UP=>[], :DOWN=>[]}]) do |memo, val| dir = memo.first = memo.last case val when 'UP', :UP, :DOWN, 'DOWN', 'BOTH', :BOTH dir = val.to_sym when '' # do nothing else case dir when :BOTH [:UP] << val [:DOWN] << val else [dir] << val end end [dir, ] end = raw.last {:UP=>[:UP].join("\n"), :DOWN=>[:DOWN].join("\n")} end |
.stdout(*args) ⇒ Object
11 12 13 |
# File 'lib/duck_duck_duck.rb', line 11 def stdout *args args.each { |a| print "#{a}\n" } end |
Instance Method Details
#create ⇒ Object
def down
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/duck_duck_duck.rb', line 207 def create `mkdir -p #{name}/migrates` size = 3 next_ver = begin (@files.last || '')[/\/(\d+)[^\/]+\z/] v = if $1 size = $1.size $1 else '0' end "%0#{size}d" % (v.to_i + 1) end new_file = "#{name}/migrates/#{next_ver}-#{[action, sub_action].compact.join('-')}.sql" File.open(new_file, 'a') do |f| f.print "\n\n\n\n-- DOWN\n\n\n\n" end stdout new_file end |
#down ⇒ Object
def up
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/duck_duck_duck.rb', line 166 def down rec = init_model_in_schema if rec[:version] < 0 stdout "#{name} is at invalid version: #{rec[:version]}\n" exit 1 end files = if rec[:version] == 0 [] else @files.sort.reverse.map { |f| ver = file_to_ver(f) next unless ver <= rec[:version] [ ver, Duck_Duck_Duck.read_file(f)[:DOWN] ] }.compact end if @__files.empty? && files.empty? stdout "#{name} is already the latest: #{rec[:version]}\n" exit 0 end new_ver = nil files.each_with_index { |pair, i| prev_pair = files[i+1] || [0, nil] ver = prev_pair.first.to_i sql = pair[1] DB << sql DB[" UPDATE #{SCHEMA_TABLE} SET version = ? WHERE name = upper( ? )", ver, name].update stdout "#{name} schema is now : #{ver}" } @__files.reverse.each { |f| DB << Duck_Duck_Duck.read_file(f)[:DOWN] stdout "down: #{f}" } end |
#file_to_ver(str) ⇒ Object
102 103 104 |
# File 'lib/duck_duck_duck.rb', line 102 def file_to_ver str File.basename(str)[/\A\d{1,}/].to_i end |
#init_model_in_schema ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/duck_duck_duck.rb', line 111 def init_model_in_schema rec = DB.fetch( "SELECT version FROM #{SCHEMA_TABLE} WHERE name = upper( :name )", :name=>name ).all.first if !rec dir_name = "#{name}/migrates" dir = `find . -iregex ".+/#{dir_name}"`.strip.split("\n") if dir.empty? fail "Directory #{dir_name} does not exist." end rec = DB.fetch( "INSERT INTO #{SCHEMA_TABLE} (name, version) VALUES (upper(:name), :version) RETURNING *", :name=>name, :version=>0 ).all.first end {:version=>rec[:version]} end |
#reset ⇒ Object
106 107 108 109 |
# File 'lib/duck_duck_duck.rb', line 106 def reset down up end |
#stdout(*args) ⇒ Object
98 99 100 |
# File 'lib/duck_duck_duck.rb', line 98 def stdout *args Duck_Duck_Duck.stdout *args end |
#up ⇒ Object
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 |
# File 'lib/duck_duck_duck.rb', line 132 def up rec = init_model_in_schema if rec[:version] < 0 stdout "#{name} has an invalid version: #{rec[:version]}\n" exit 1 end files = @files.sort.map { |f| ver = file_to_ver(f) if ver > rec[:version] [ ver, Duck_Duck_Duck.read_file(f)[:UP] ] end }.compact if @__files.empty? && files.empty? stdout "#{name} is already the latest: #{rec[:version]}" end @__files.each { |f| DB << Duck_Duck_Duck.read_file(f)[:UP] stdout "ran file: #{f}" } files.each { |pair| ver = pair.first sql = pair[1] DB << sql DB[" UPDATE #{SCHEMA_TABLE.inspect} SET version = ? WHERE name = upper( ? ); ", ver, name].update stdout "#{name} schema is now : #{ver}" } end |