duck_duck_duck 3.1.1 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e96a0a5d2eea0741dc957719b4ca89504145caad
4
- data.tar.gz: b7f15f5d7eb81dbcf02288a6ca7391190d4db9ca
3
+ metadata.gz: eb33bc9a033282d066dd12a980c0a41ecfcfeca5
4
+ data.tar.gz: 95a6f37f30a099bafcf45db109d14a06d7b91a5e
5
5
  SHA512:
6
- metadata.gz: 480a90259c5b62964b52ef0a0230d8d5783c98c8401fb5da21fbb33cc5dbdad800189075a84cd2227cbbd740f21f9ab26e23f06ec5be01d47461964d94adda30
7
- data.tar.gz: 9343ed12b6b96549e2da6ce57818e7c070a15bcee006c4497cae57a530d9112036703f10af275f8656cb2daf48c73c2575548da102abb5f9ccb4a00abee79a0b
6
+ metadata.gz: eeedc977caa49085b38ef18f7d40a07ae2024e526187d40cbe64b033facc7b9c2d7ca0393355fc8020e30de3b30cb3258f20774e4da0af5e170637a1e7b37063
7
+ data.tar.gz: 05893d31a983d6130e8aacdb311ae88a603ad14a42550532520242f479ca9ca4e2c8ed4758105cf7f61822ecf331d50cdb39bbb10f6ce8cc8762145cbf795d6f
data/README.md CHANGED
@@ -1,12 +1,12 @@
1
1
 
2
- duck_duck_duck
2
+ duck\_duck\_duck
3
3
  ==============
4
4
  You won't find this useful.
5
5
 
6
6
  However, if you are still curious:
7
7
 
8
8
  * If you like to break up apps into smaller apps,
9
- and you want them to use the smae db, but
9
+ and you want them to use the same db, but
10
10
  different tables, duck\_duck\_duck
11
11
  lets you migrate those mini-apps
12
12
  to the same db.
@@ -17,3 +17,28 @@ Previously...
17
17
  Originally, this was a node module.
18
18
  The node module is no longer maintained. It is now
19
19
  a Ruby gem.
20
+
21
+ Commands
22
+ =========
23
+
24
+ ```bash
25
+ cd /my/model/dir
26
+ duck_duck_duck up MODEL_NAME
27
+ duck_duck_duck down MODEL_NAME
28
+ duck_duck_duck create MODEL_NAME postfix
29
+ ```
30
+
31
+ Sample .sql file:
32
+
33
+ ```sql
34
+ SELECT 1;
35
+ -- DOWN
36
+ SELECT 2
37
+
38
+ -- UP:
39
+ -- colons ":' are optional
40
+ SELECT 3;
41
+
42
+ -- DOWN:
43
+ SELECT 4;
44
+ ```
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.1.1
1
+ 4.0.0
@@ -19,8 +19,8 @@ class Duck_Duck_Duck
19
19
  def migrate_schema
20
20
  DB << <<-EOF
21
21
  CREATE TABLE IF NOT EXISTS #{SCHEMA_TABLE} (
22
- name varchar(255) NOT NULL PRIMARY KEY ,
23
- version smallint NOT NULL DEFAULT 0
22
+ name varchar(255) NOT NULL PRIMARY KEY,
23
+ version smallint NOT NULL DEFAULT 0
24
24
  )
25
25
  EOF
26
26
  end
@@ -69,7 +69,12 @@ class Duck_Duck_Duck
69
69
  if !@name
70
70
  fail ArgumentError, "Name required."
71
71
  end
72
- @files = Dir.glob("#{name}/migrates/*.sql")
72
+
73
+ @files = `find . -iregex ".+/#{name}/migrates/.+\.sql"`
74
+ .strip
75
+ .split("\n")
76
+ .grep(/\/\d+\-/)
77
+ .sort
73
78
  end
74
79
 
75
80
  def file_to_ver str
@@ -81,15 +86,25 @@ class Duck_Duck_Duck
81
86
  up
82
87
  end
83
88
 
84
- def up
85
- rec = DB.fetch("SELECT version FROM #{SCHEMA_TABLE} WHERE name = :name", :name=>name).all.first
89
+ def init_model_in_schema
90
+ rec = DB.fetch(
91
+ "SELECT version FROM #{SCHEMA_TABLE} WHERE name = upper( :name )",
92
+ :name=>name
93
+ ).all.first
86
94
 
87
95
  if !rec
88
- ds = DB["INSERT INTO #{SCHEMA_TABLE} (name, version) VALUES (?, ?)", name, 0]
89
- ds.insert
90
- rec = {:version=>0}
96
+ rec = DB.fetch(
97
+ "INSERT INTO #{SCHEMA_TABLE} (name, version) VALUES (upper(:name), :version) RETURNING *",
98
+ :name=>name, :version=>0
99
+ ).all.first
91
100
  end
92
101
 
102
+ {:version=>rec[:version]}
103
+ end
104
+
105
+ def up
106
+ rec = init_model_in_schema
107
+
93
108
  if rec[:version] < 0
94
109
  puts "#{name} has an invalid version: #{rec[:version]}\n"
95
110
  exit 1
@@ -106,7 +121,7 @@ class Duck_Duck_Duck
106
121
  ver = pair.first
107
122
  sql = pair[1]
108
123
  DB << sql
109
- DB[" UPDATE #{SCHEMA_TABLE.inspect} SET version = ? WHERE name = ? ", ver, name].update
124
+ DB[" UPDATE #{SCHEMA_TABLE.inspect} SET version = ? WHERE name = upper( ? ); ", ver, name].update
110
125
  puts "#{name} schema is now : #{ver}"
111
126
  }
112
127
 
@@ -116,13 +131,7 @@ class Duck_Duck_Duck
116
131
  end # === def up
117
132
 
118
133
  def down
119
- rec = DB.fetch("SELECT version FROM #{SCHEMA_TABLE} WHERE name = :name", :name=>name).all.first
120
-
121
- if !rec
122
- ds = DB["INSERT INTO #{SCHEMA_TABLE} (name, version) VALUES (?, ?)", name, 0]
123
- ds.insert
124
- rec = {:version=>0}
125
- end
134
+ rec = init_model_in_schema
126
135
 
127
136
  if rec[:version] == 0
128
137
  puts "#{name} is already the latest: #{rec[:version]}\n"
@@ -151,7 +160,7 @@ class Duck_Duck_Duck
151
160
  ver = prev_pair.first.to_i
152
161
  sql = pair[1]
153
162
  DB << sql
154
- DB[" UPDATE #{SCHEMA_TABLE} SET version = ? WHERE name = ? ", ver, name].update
163
+ DB[" UPDATE #{SCHEMA_TABLE} SET version = ? WHERE name = upper( ? )", ver, name].update
155
164
  puts "#{name} schema is now : #{ver}"
156
165
  }
157
166
 
@@ -160,14 +169,12 @@ class Duck_Duck_Duck
160
169
  def create
161
170
  `mkdir -p #{name}/migrates`
162
171
 
163
- files = Dir.glob("#{name}/migrates/*.sql").grep(/\/\d+\-/).sort
164
-
165
172
  size = 3
166
173
  next_ver = begin
167
- (files.last || '')[/\/(\d+)[^\/]+\z/]
174
+ (@files.last || '')[/\/(\d+)[^\/]+\z/]
168
175
  v = if $1
169
176
  size = $1.size
170
- $1
177
+ $1
171
178
  else
172
179
  '0'
173
180
  end
@@ -112,12 +112,18 @@ describe 'up model' do
112
112
  it 'does not run migrations from previous versions' do
113
113
  Exit_0("duck_duck_duck migrate_schema")
114
114
  DB << File.read("0010_model/migrates/0010-table.sql").split('-- DOWN').first
115
- DB << "INSERT INTO #{schema.inspect} VALUES ('0010_model', '20');"
115
+ DB << "INSERT INTO #{schema.inspect} VALUES (upper('0010_model'), '20');"
116
116
  Exit_0("duck_duck_duck up 0010_model")
117
117
  get('SELECT * FROM "0010_model"', :title).
118
118
  should == ['record 30', 'record 40', 'record 50']
119
119
  end
120
120
 
121
+ it "ignores case of model" do
122
+ Exit_0("duck_duck_duck up 0010_mOdEl")
123
+ get('SELECT * FROM _test_schema').
124
+ first[:version].should == versions('0010_model').last
125
+ end # === it
126
+
121
127
  end # === describe up model
122
128
 
123
129
  describe 'down model' do
@@ -127,7 +133,7 @@ describe 'down model' do
127
133
  it 'leaves version to 0' do
128
134
  Exit_0("duck_duck_duck up 0010_model")
129
135
  Exit_0("duck_duck_duck down 0010_model")
130
- get(%^SELECT * FROM #{schema.inspect} WHERE name = '0010_model'^, :version).last.
136
+ get(%^SELECT * FROM #{schema.inspect} WHERE name = upper('0010_model');^, :version).last.
131
137
  should == 0
132
138
  end
133
139
 
@@ -141,13 +147,20 @@ describe 'down model' do
141
147
  it 'does not run down migrates from later versions' do
142
148
  Exit_0("duck_duck_duck migrate_schema")
143
149
  DB << File.read("0020_model/migrates/0010-table.sql").split('-- DOWN').first
144
- DB << "INSERT INTO #{schema.inspect} VALUES ('0020_model', '20');"
150
+ DB << "INSERT INTO #{schema.inspect} VALUES (upper('0020_model'), '20');"
145
151
  DB << "UPDATE #{schema} SET version = '20' WHERE name = '0020_model';"
146
152
  Exit_0("duck_duck_duck down 0020_model")
147
- get('SELECT * FROM "0020_model"', :title).
153
+ get('SELECT title FROM "0020_model"', :title).
148
154
  should == ['DROP record 20', 'DROP 0020_model']
149
155
  end
150
156
 
157
+ it 'ignores case of model' do
158
+ Exit_0("duck_duck_duck up 0010_model")
159
+ Exit_0("duck_duck_duck down 0010_MoDeL")
160
+ get(%^SELECT * FROM #{schema.inspect} WHERE name = upper('0010_model');^, :version).last.
161
+ should == 0
162
+ end
163
+
151
164
  end # === describe down model
152
165
 
153
166
  describe '-- UP/-- DOWN model' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: duck_duck_duck
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.1
4
+ version: 4.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - da99
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-27 00:00:00.000000000 Z
11
+ date: 2015-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry