db_leftovers 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +4 -2
- data/Gemfile.lock +14 -0
- data/README.md +10 -2
- data/VERSION +1 -1
- data/db_leftovers.gemspec +2 -2
- data/lib/db_leftovers/dsl.rb +1 -1
- data/lib/db_leftovers/postgres_database_interface.rb +4 -4
- data/spec/config/database.yml.sample +20 -0
- data/spec/mysql_spec.rb +6 -2
- data/spec/postgres_spec.rb +6 -2
- metadata +3 -3
data/Gemfile
CHANGED
@@ -18,6 +18,8 @@ group :development do
|
|
18
18
|
end
|
19
19
|
|
20
20
|
group :test do
|
21
|
-
gem 'activerecord-postgresql-adapter'
|
22
|
-
gem 'activerecord-mysql2-adapter'
|
21
|
+
gem 'activerecord-postgresql-adapter', :platforms => :ruby
|
22
|
+
gem 'activerecord-mysql2-adapter', :platforms => :ruby
|
23
|
+
gem 'activerecord-jdbcpostgresql-adapter', :platforms => :jruby
|
24
|
+
gem 'activerecord-jdbcmysql-adapter', :platforms => :jruby
|
23
25
|
end
|
data/Gemfile.lock
CHANGED
@@ -24,6 +24,13 @@ GEM
|
|
24
24
|
activesupport (= 3.1.3)
|
25
25
|
arel (~> 2.2.1)
|
26
26
|
tzinfo (~> 0.3.29)
|
27
|
+
activerecord-jdbc-adapter (1.2.2)
|
28
|
+
activerecord-jdbcmysql-adapter (1.2.2)
|
29
|
+
activerecord-jdbc-adapter (~> 1.2.2)
|
30
|
+
jdbc-mysql (~> 5.1.0)
|
31
|
+
activerecord-jdbcpostgresql-adapter (1.2.2)
|
32
|
+
activerecord-jdbc-adapter (~> 1.2.2)
|
33
|
+
jdbc-postgres (>= 9.0, < 9.2)
|
27
34
|
activerecord-mysql2-adapter (0.0.3)
|
28
35
|
mysql2
|
29
36
|
activerecord-postgresql-adapter (0.0.1)
|
@@ -40,11 +47,14 @@ GEM
|
|
40
47
|
git (1.2.5)
|
41
48
|
hike (1.2.1)
|
42
49
|
i18n (0.6.0)
|
50
|
+
jdbc-mysql (5.1.13)
|
51
|
+
jdbc-postgres (9.1.901)
|
43
52
|
jeweler (1.6.4)
|
44
53
|
bundler (~> 1.0)
|
45
54
|
git (>= 1.2.5)
|
46
55
|
rake
|
47
56
|
json (1.6.5)
|
57
|
+
json (1.6.5-java)
|
48
58
|
mail (2.3.0)
|
49
59
|
i18n (>= 0.4.0)
|
50
60
|
mime-types (~> 1.16)
|
@@ -80,6 +90,7 @@ GEM
|
|
80
90
|
thor (~> 0.14.6)
|
81
91
|
rake (0.9.2.2)
|
82
92
|
rcov (0.9.11)
|
93
|
+
rcov (0.9.11-java)
|
83
94
|
rdoc (3.12)
|
84
95
|
json (~> 1.4)
|
85
96
|
rspec (2.4.0)
|
@@ -102,9 +113,12 @@ GEM
|
|
102
113
|
tzinfo (0.3.31)
|
103
114
|
|
104
115
|
PLATFORMS
|
116
|
+
java
|
105
117
|
ruby
|
106
118
|
|
107
119
|
DEPENDENCIES
|
120
|
+
activerecord-jdbcmysql-adapter
|
121
|
+
activerecord-jdbcpostgresql-adapter
|
108
122
|
activerecord-mysql2-adapter
|
109
123
|
activerecord-postgresql-adapter
|
110
124
|
bundler
|
data/README.md
CHANGED
@@ -15,7 +15,7 @@ This is useful because of the following limitations in vanilla Rails:
|
|
15
15
|
|
16
16
|
That last point deserves some elaboration. First, using `add_index` in your migrations is bug-prone because without rare developer discipline, you wind up missing indexes in some environments. (My rule is "never change a migration after a `git push`," but I haven't seen this followed elsewhere, and there is nothing that automatically enforces it.) Also, since missing indexes don't cause errors, it's easy to be missing one and not notice until users start complaining about performance.
|
17
17
|
|
18
|
-
Second, scattering `add_index` methods throughout migrations
|
18
|
+
Second, scattering `add_index` methods throughout migrations doesn't match the workflow of optimizing database queries. Hopefully you create appropriate indexes when you set up your tables originally, but in practice you often need to go back later and add/remove indexes according to your database usage patterns. Or you just forget the indexes, because you're thinking about modeling the data, not optimizing the queries.
|
19
19
|
It's easier to vet and analyze database indexes if you can see them all in one place,
|
20
20
|
and db\_leftovers lets you do that easily.
|
21
21
|
And since you can rest assured that each environment conforms to the same definition, you don't need to second-guess yourself about indexes that are present in development but missing in production.
|
@@ -121,7 +121,7 @@ and db\_leftovers will notice the changed expression. It will drop and re-add th
|
|
121
121
|
|
122
122
|
One caveat, however: we pull the current expression from the database, and sometimes Postgres does things like
|
123
123
|
add type conversions and extra parentheses. If instance, suppose you said `check :users, :email_length, 'LENGTH(email) > 2'`.
|
124
|
-
The second time you run db\_leftovers, it will read the expression from Postgres and get `
|
124
|
+
The second time you run db\_leftovers, it will read the expression from Postgres and get `length((email)::text) > 2`,
|
125
125
|
and so it will drop and re-create the constraint.
|
126
126
|
It will drop and re-create it every time you run the rake task.
|
127
127
|
To get around this, make sure your config file uses the same expression as printed by db\_leftovers in the rake output.
|
@@ -168,6 +168,14 @@ Known Issues
|
|
168
168
|
* It'd be nice to have another Rake task that will read your database and generate a new `db_leftovers.rb` file, to help people migrating existing projects that already have lots of tables.
|
169
169
|
|
170
170
|
|
171
|
+
|
172
|
+
Tests
|
173
|
+
-----
|
174
|
+
|
175
|
+
Db\_leftovers has three kinds of RSpec tests: tests that run against a mock database, a Postgres database, and a MySQL database. The tests look at `spec/config/database.yml` to see if you've created a test database for Postgres and/or MySQL, and only run those tests if an entry is found there. You can look at `spec/config/database.yml.sample` to get a sense of what is expected and for instructions on setting up your own test databases. If you're contributing a patch, please make sure you've run these tests!
|
176
|
+
|
177
|
+
|
178
|
+
|
171
179
|
Contributing to db\_leftovers
|
172
180
|
-----------------------------
|
173
181
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0
|
1
|
+
1.1.0
|
data/db_leftovers.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "db_leftovers"
|
8
|
-
s.version = "1.0
|
8
|
+
s.version = "1.1.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Paul A. Jungwirth"]
|
12
|
-
s.date = "2012-10-
|
12
|
+
s.date = "2012-10-06"
|
13
13
|
s.description = " Define indexes and foreign keys for your Rails app\n in one place using an easy-to-read DSL,\n then run a rake task to bring your database up-to-date.\n"
|
14
14
|
s.email = "pj@illuminatedcomputing.com"
|
15
15
|
s.extra_rdoc_files = [
|
data/lib/db_leftovers/dsl.rb
CHANGED
@@ -229,7 +229,7 @@ module DBLeftovers
|
|
229
229
|
def get_database_interface
|
230
230
|
db = ActiveRecord::Base.configurations[Rails.env]['adapter']
|
231
231
|
case db
|
232
|
-
when 'postgresql'
|
232
|
+
when 'postgresql', 'jdbcpostgresql'
|
233
233
|
DBLeftovers::PostgresDatabaseInterface.new
|
234
234
|
when 'mysql2'
|
235
235
|
DBLeftovers::MySQLInterface.new
|
@@ -58,10 +58,10 @@ module DBLeftovers
|
|
58
58
|
# TODO: Support multi-column foreign keys:
|
59
59
|
sql = <<-EOQ
|
60
60
|
SELECT c.conname,
|
61
|
-
t1.relname,
|
62
|
-
a1.attname,
|
63
|
-
t2.relname,
|
64
|
-
a2.attname,
|
61
|
+
t1.relname AS from_table,
|
62
|
+
a1.attname AS from_column,
|
63
|
+
t2.relname AS to_table,
|
64
|
+
a2.attname AS to_column,
|
65
65
|
c.confdeltype
|
66
66
|
FROM pg_catalog.pg_constraint c,
|
67
67
|
pg_catalog.pg_class t1,
|
@@ -25,3 +25,23 @@ mysql:
|
|
25
25
|
password: testdb
|
26
26
|
encoding: utf8
|
27
27
|
|
28
|
+
# These are used instead of the above if you're running under JRuby:
|
29
|
+
|
30
|
+
jdbcpostgres:
|
31
|
+
adapter: jdbcpostgresql
|
32
|
+
host: localhost
|
33
|
+
database: db_leftovers_test
|
34
|
+
username: db_leftovers_test
|
35
|
+
password: testdb
|
36
|
+
encoding: utf8
|
37
|
+
template: template0 # Required for UTF-8 encoding
|
38
|
+
|
39
|
+
jdbcmysql:
|
40
|
+
adapter: jdbcmysql
|
41
|
+
host: localhost
|
42
|
+
database: db_leftovers_test
|
43
|
+
username: db_leftovers
|
44
|
+
password: testdb
|
45
|
+
encoding: utf8
|
46
|
+
|
47
|
+
|
data/spec/mysql_spec.rb
CHANGED
@@ -12,13 +12,17 @@ def drop_all_mysql_tables(conn, database_name)
|
|
12
12
|
conn.execute("SET FOREIGN_KEY_CHECKS = 1")
|
13
13
|
end
|
14
14
|
|
15
|
+
def mysql_config
|
16
|
+
test_database_yml(RUBY_PLATFORM == 'java' ? 'jdbcmysql' : 'mysql')
|
17
|
+
end
|
18
|
+
|
15
19
|
describe DBLeftovers::MysqlDatabaseInterface do
|
16
20
|
|
17
|
-
if not
|
21
|
+
if not mysql_config
|
18
22
|
it "WARN: Skipping MySQL tests because no database found. Use spec/config/database.yml to configure one."
|
19
23
|
else
|
20
24
|
before do
|
21
|
-
y =
|
25
|
+
y = mysql_config
|
22
26
|
@conn = test_db_connection(nil, y)
|
23
27
|
@db = DBLeftovers::MysqlDatabaseInterface.new(@conn, y['database'])
|
24
28
|
drop_all_mysql_tables(@conn, y['database'])
|
data/spec/postgres_spec.rb
CHANGED
@@ -10,13 +10,17 @@ def drop_all_postgres_tables(conn, database_name)
|
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
+
def postgres_config
|
14
|
+
test_database_yml(RUBY_PLATFORM == 'java' ? 'jdbcpostgres' : 'postgres')
|
15
|
+
end
|
16
|
+
|
13
17
|
describe DBLeftovers::PostgresDatabaseInterface do
|
14
18
|
|
15
|
-
if not
|
19
|
+
if not postgres_config
|
16
20
|
it "WARN: Skipping Postgres tests because no database found. Use spec/config/database.yml to configure one."
|
17
21
|
else
|
18
22
|
before do
|
19
|
-
y =
|
23
|
+
y = postgres_config
|
20
24
|
@conn = test_db_connection(nil, y)
|
21
25
|
@db = DBLeftovers::PostgresDatabaseInterface.new(@conn)
|
22
26
|
drop_all_postgres_tables(@conn, y['database'])
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: db_leftovers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-10-
|
12
|
+
date: 2012-10-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -147,7 +147,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
147
147
|
version: '0'
|
148
148
|
segments:
|
149
149
|
- 0
|
150
|
-
hash:
|
150
|
+
hash: 3981883002414035216
|
151
151
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
152
152
|
none: false
|
153
153
|
requirements:
|