mysql-inspector 0.0.6 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. data/.gitignore +5 -3
  2. data/CHANGELOG.md +8 -0
  3. data/Gemfile +6 -0
  4. data/LICENSE +20 -0
  5. data/README.md +82 -0
  6. data/Rakefile +36 -14
  7. data/bin/mysql-inspector +9 -85
  8. data/lib/mysql-inspector.rb +1 -226
  9. data/lib/mysql_inspector.rb +30 -0
  10. data/lib/mysql_inspector/access.rb +64 -0
  11. data/lib/mysql_inspector/ar/access.rb +55 -0
  12. data/lib/mysql_inspector/cli.rb +291 -0
  13. data/lib/mysql_inspector/column.rb +21 -0
  14. data/lib/mysql_inspector/config.rb +82 -0
  15. data/lib/mysql_inspector/constraint.rb +28 -0
  16. data/lib/mysql_inspector/diff.rb +82 -0
  17. data/lib/mysql_inspector/dump.rb +70 -0
  18. data/lib/mysql_inspector/grep.rb +65 -0
  19. data/lib/mysql_inspector/index.rb +25 -0
  20. data/lib/mysql_inspector/migrations.rb +37 -0
  21. data/lib/mysql_inspector/railtie.rb +17 -0
  22. data/lib/mysql_inspector/railties/databases.rake +92 -0
  23. data/lib/mysql_inspector/table.rb +147 -0
  24. data/lib/mysql_inspector/table_part.rb +21 -0
  25. data/lib/mysql_inspector/version.rb +3 -0
  26. data/mysql-inspector.gemspec +17 -36
  27. data/test/fixtures/migrate/111_create_users.rb +7 -0
  28. data/test/fixtures/migrate/222_create_things.rb +9 -0
  29. data/test/helper.rb +125 -0
  30. data/test/helper_ar.rb +37 -0
  31. data/test/helpers/mysql_schemas.rb +82 -0
  32. data/test/helpers/mysql_utils.rb +35 -0
  33. data/test/helpers/string_unindented.rb +13 -0
  34. data/test/mysql_inspector/cli_basics_test.rb +77 -0
  35. data/test/mysql_inspector/cli_diff_test.rb +60 -0
  36. data/test/mysql_inspector/cli_grep_test.rb +74 -0
  37. data/test/mysql_inspector/cli_load_test.rb +43 -0
  38. data/test/mysql_inspector/cli_write_test.rb +58 -0
  39. data/test/mysql_inspector/config_test.rb +14 -0
  40. data/test/mysql_inspector/diff_test.rb +82 -0
  41. data/test/mysql_inspector/dump_test.rb +81 -0
  42. data/test/mysql_inspector/grep_test.rb +61 -0
  43. data/test/mysql_inspector/table_test.rb +123 -0
  44. data/test/mysql_inspector_ar/ar_dump_test.rb +29 -0
  45. data/test/mysql_inspector_ar/ar_migrations_test.rb +47 -0
  46. metadata +123 -49
  47. data/README +0 -48
  48. data/VERSION +0 -1
@@ -0,0 +1,81 @@
1
+ require 'helper'
2
+
3
+ describe MysqlInspector::Dump do
4
+
5
+ let(:extras) { [] }
6
+
7
+ subject do
8
+ MysqlInspector::Dump.new(tmpdir, *extras)
9
+ end
10
+
11
+ describe "before written" do
12
+ it "does not exist" do
13
+ subject.exists?.must_equal false
14
+ end
15
+ it "has no tables" do
16
+ subject.tables.size.must_equal 0
17
+ end
18
+ end
19
+
20
+ describe "when written" do
21
+ before do
22
+ create_mysql_database(schema_b)
23
+ subject.write!(access)
24
+ end
25
+ it "does exist" do
26
+ subject.must_be :exists?
27
+ end
28
+ it "has tables" do
29
+ subject.tables.size.must_equal 3
30
+ end
31
+ it "writes simple schemas to disk" do
32
+ file = File.join(tmpdir, "things.table")
33
+ File.exist?(file).must_equal true
34
+ schema = File.read(file)
35
+ schema.must_equal MysqlInspector::Table.new(things_schema).to_simple_schema
36
+ end
37
+ end
38
+
39
+ describe "when loaded" do
40
+ before do
41
+ create_mysql_database(schema_b)
42
+ subject.write!(access)
43
+ create_mysql_database(ideas_schema)
44
+ end
45
+ it "recreates all of the tables, even ones that already exist" do
46
+ access.table_names.must_equal ["ideas"]
47
+ subject.load!(access)
48
+ access.table_names.sort.must_equal ["ideas", "things", "users"]
49
+ end
50
+ end
51
+
52
+ describe "when written but a database does not exist" do
53
+ it "fails" do
54
+ proc { subject.write!(access) }.must_raise MysqlInspector::Access::Error
55
+ end
56
+ end
57
+
58
+ describe "extras" do
59
+
60
+ let(:extra) { MiniTest::Mock.new }
61
+
62
+ before do
63
+ create_mysql_database
64
+ end
65
+ after do
66
+ extra.verify
67
+ end
68
+
69
+ it "writes extras" do
70
+ extra.expect :write!, nil, [access]
71
+ extras << extra
72
+ subject.write!(access)
73
+ end
74
+ it "loads extras" do
75
+ extra = MiniTest::Mock.new
76
+ extra.expect :load!, nil, [access]
77
+ extras << extra
78
+ subject.load!(access)
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,61 @@
1
+ require 'helper'
2
+
3
+ describe MysqlInspector::Grep do
4
+
5
+ let(:dump) {
6
+ dump = MiniTest::Mock.new
7
+ dump.expect(:tables, [table])
8
+ dump
9
+ }
10
+
11
+ let(:table) { MysqlInspector::Table.new(things_schema) }
12
+
13
+ let(:matchers) { [] }
14
+
15
+ subject do
16
+ MysqlInspector::Grep.new(dump, matchers)
17
+ end
18
+
19
+ before do
20
+ subject.execute
21
+ end
22
+
23
+ describe "with one matcher" do
24
+
25
+ let(:matchers) { [/^first_name$/] }
26
+
27
+ it "finds the column" do
28
+ subject.columns.size.must_equal 1
29
+ subject.columns.first.name.must_equal "first_name"
30
+ end
31
+
32
+ it "finds the index" do
33
+ subject.indices.size.must_equal 1
34
+ subject.indices.first.name.must_equal "name"
35
+ end
36
+
37
+ it "finds the constraint" do
38
+ subject.constraints.size.must_equal 1
39
+ subject.constraints.first.name.must_equal "belongs_to_user"
40
+ end
41
+ end
42
+
43
+ describe "with multiple matchers (matching with AND)" do
44
+
45
+ let(:matchers) { [/^first_name$/, /^last_name$/] }
46
+
47
+ it "finds no column" do
48
+ subject.columns.size.must_equal 0
49
+ end
50
+
51
+ it "finds the index" do
52
+ subject.indices.size.must_equal 1
53
+ subject.indices.first.name.must_equal "name"
54
+ end
55
+
56
+ it "finds the constraint" do
57
+ subject.constraints.size.must_equal 1
58
+ subject.constraints.first.name.must_equal "belongs_to_user"
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,123 @@
1
+ require 'helper'
2
+
3
+ describe MysqlInspector::Table do
4
+
5
+ subject do
6
+ MysqlInspector::Table.new(things_schema)
7
+ end
8
+
9
+ it "knows the table name" do
10
+ subject.table_name.must_equal "things"
11
+ end
12
+
13
+ it "extracts all of the columns" do
14
+ subject.columns.size.must_equal 5
15
+ end
16
+
17
+ it "describes each column" do
18
+ subject.columns[0].must_equal MysqlInspector::Column.new("first_name", "varchar(255)", false, nil, false)
19
+ subject.columns[1].must_equal MysqlInspector::Column.new("id", "int(11)", false, nil, true)
20
+ subject.columns[2].must_equal MysqlInspector::Column.new("last_name", "varchar(255)", false, nil, false)
21
+ subject.columns[3].must_equal MysqlInspector::Column.new("name", "varchar(255)", false, "'toy'", false)
22
+ subject.columns[4].must_equal MysqlInspector::Column.new("weight", "int(11)", true, nil, false)
23
+ end
24
+
25
+ it "extracts all of the indices" do
26
+ subject.indices.size.must_equal 2
27
+ end
28
+
29
+ it "describes each index" do
30
+ subject.indices[0].must_equal MysqlInspector::Index.new("name", ["first_name", "last_name"], false)
31
+ subject.indices[1].must_equal MysqlInspector::Index.new("things_primary", ["id"], true)
32
+ end
33
+
34
+ it "extracts all of the constraints" do
35
+ subject.constraints.size.must_equal 1
36
+ end
37
+
38
+ it "describes each constraint" do
39
+ subject.constraints[0].must_equal MysqlInspector::Constraint.new("belongs_to_user", ["first_name", "last_name"], "users", ["first_name", "last_name"], "CASCADE", "NO ACTION")
40
+ end
41
+
42
+ it "describes the options" do
43
+ subject.options.must_equal "ENGINE=InnoDB DEFAULT CHARSET=utf8"
44
+ end
45
+
46
+ it "excludes the AUTO_INCREMENT option" do
47
+ table = MysqlInspector::Table.new(") ENGINE=InnoDB AUTO_INCREMENT=122 CHARSET=utf8;")
48
+ table.options.must_equal "ENGINE=InnoDB CHARSET=utf8"
49
+ end
50
+
51
+ it "generates a simplified schema" do
52
+ subject.to_simple_schema.must_equal <<-EOL.unindented.chomp
53
+ CREATE TABLE `things`
54
+
55
+ `first_name` varchar(255) NOT NULL
56
+ `id` int(11) NOT NULL AUTO_INCREMENT
57
+ `last_name` varchar(255) NOT NULL
58
+ `name` varchar(255) NOT NULL DEFAULT 'toy'
59
+ `weight` int(11) NULL
60
+
61
+ KEY `name` (`first_name`,`last_name`)
62
+ UNIQUE KEY `things_primary` (`id`)
63
+
64
+ CONSTRAINT `belongs_to_user` FOREIGN KEY (`first_name`,`last_name`) REFERENCES `users` (`first_name`,`last_name`) ON DELETE NO ACTION ON UPDATE CASCADE
65
+
66
+ ENGINE=InnoDB DEFAULT CHARSET=utf8
67
+ EOL
68
+ end
69
+
70
+ it "may be instantiated with a simplified schema" do
71
+ MysqlInspector::Table.new(subject.to_simple_schema).must_equal subject
72
+ end
73
+
74
+ it "formats a simplified schema well even if the table has nothing" do
75
+ schema = <<-EOL.unindented
76
+ CREATE TABLE `things` (
77
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8
78
+ EOL
79
+ MysqlInspector::Table.new(schema).to_simple_schema.must_equal <<-EOL.unindented.chomp
80
+ CREATE TABLE `things`
81
+
82
+ ENGINE=InnoDB DEFAULT CHARSET=utf8
83
+ EOL
84
+ end
85
+
86
+ it "generates a sql schema" do
87
+ subject.to_sql.must_equal <<-EOL.unindented.chomp
88
+ CREATE TABLE `things` (
89
+ `first_name` varchar(255) NOT NULL,
90
+ `id` int(11) NOT NULL AUTO_INCREMENT,
91
+ `last_name` varchar(255) NOT NULL,
92
+ `name` varchar(255) NOT NULL DEFAULT 'toy',
93
+ `weight` int(11) NULL,
94
+ KEY `name` (`first_name`,`last_name`),
95
+ UNIQUE KEY `things_primary` (`id`),
96
+ CONSTRAINT `belongs_to_user` FOREIGN KEY (`first_name`,`last_name`) REFERENCES `users` (`first_name`,`last_name`) ON DELETE NO ACTION ON UPDATE CASCADE
97
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8
98
+ EOL
99
+ end
100
+ end
101
+
102
+ describe MysqlInspector::Table, "with PRIMARY KEY" do
103
+
104
+ subject do
105
+ MysqlInspector::Table.new(ideas_schema)
106
+ end
107
+
108
+ it "describes the index" do
109
+ subject.indices[0].must_equal MysqlInspector::Index.new("PRIMARY KEY", ["id"], true)
110
+ end
111
+
112
+ it "generates a sql schema" do
113
+ subject.to_sql.must_equal <<-EOL.unindented.chomp
114
+ CREATE TABLE `ideas` (
115
+ `description` text NOT NULL,
116
+ `id` int(11) NOT NULL AUTO_INCREMENT,
117
+ `name` varchar(255) NOT NULL,
118
+ PRIMARY KEY (`id`),
119
+ KEY `name` (`name`)
120
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8
121
+ EOL
122
+ end
123
+ end
@@ -0,0 +1,29 @@
1
+ require 'helper_ar'
2
+
3
+ describe "dump and load activerecord" do
4
+
5
+ subject { MysqlInspector::Dump.new(tmpdir) }
6
+
7
+ describe "when written" do
8
+ before do
9
+ create_mysql_database(schema_b)
10
+ subject.write!(access)
11
+ end
12
+ it "has tables" do
13
+ subject.tables.size.must_equal 3
14
+ end
15
+ end
16
+
17
+ describe "when loaded" do
18
+ before do
19
+ create_mysql_database(schema_b)
20
+ subject.write!(access)
21
+ create_mysql_database(ideas_schema)
22
+ end
23
+ it "recreates all of the tables, even ones that already exist" do
24
+ access.table_names.must_equal ["ideas"]
25
+ subject.load!(access)
26
+ access.table_names.sort.must_equal ["ideas", "things", "users"]
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,47 @@
1
+ require 'helper_ar'
2
+
3
+ describe "activerecord migrations" do
4
+
5
+ let(:dump) { MysqlInspector::Dump.new(tmpdir) }
6
+
7
+ subject do
8
+ MysqlInspector::Migrations.new(tmpdir)
9
+ end
10
+
11
+ before do
12
+ run_active_record_migrations!
13
+ end
14
+
15
+ describe "when written" do
16
+ before do
17
+ subject.write!(access)
18
+ end
19
+ it "has migrations" do
20
+ subject.migrations.size.must_equal 2
21
+ end
22
+ it "writes migrations to disk" do
23
+ file = File.join(tmpdir, "schema_migrations.tsv")
24
+ File.exist?(file).must_equal true
25
+ migrations = File.read(file)
26
+ migrations.must_equal <<-EOL.unindented
27
+ 111
28
+ 222
29
+ EOL
30
+ end
31
+ end
32
+
33
+ describe "when loaded" do
34
+ before do
35
+ dump.write!(access)
36
+ subject.write!(access)
37
+ create_mysql_database
38
+ dump.load!(access)
39
+ subject.load!(access)
40
+ end
41
+ it "loads migrations" do
42
+ values = ActiveRecord::Base.connection.select_values("select * from schema_migrations")
43
+ values.sort.must_equal ["111", "222"]
44
+ end
45
+ end
46
+ end
47
+
metadata CHANGED
@@ -1,68 +1,142 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: mysql-inspector
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 0
8
- - 6
9
- version: 0.0.6
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Ryan Carver
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2010-03-23 00:00:00 -07:00
18
- default_executable: mysql-inspector
19
- dependencies: []
20
-
21
- description:
22
- email: ryan@fivesevensix.com
23
- executables:
12
+ date: 2013-05-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: minitest
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Store and understand your MySQL schema
47
+ email:
48
+ - ryan@ryancarver.com
49
+ executables:
24
50
  - mysql-inspector
25
51
  extensions: []
26
-
27
- extra_rdoc_files:
28
- - README
29
- files:
52
+ extra_rdoc_files: []
53
+ files:
30
54
  - .gitignore
31
- - README
55
+ - CHANGELOG.md
56
+ - Gemfile
57
+ - LICENSE
58
+ - README.md
32
59
  - Rakefile
33
- - VERSION
34
60
  - bin/mysql-inspector
35
61
  - lib/mysql-inspector.rb
62
+ - lib/mysql_inspector.rb
63
+ - lib/mysql_inspector/access.rb
64
+ - lib/mysql_inspector/ar/access.rb
65
+ - lib/mysql_inspector/cli.rb
66
+ - lib/mysql_inspector/column.rb
67
+ - lib/mysql_inspector/config.rb
68
+ - lib/mysql_inspector/constraint.rb
69
+ - lib/mysql_inspector/diff.rb
70
+ - lib/mysql_inspector/dump.rb
71
+ - lib/mysql_inspector/grep.rb
72
+ - lib/mysql_inspector/index.rb
73
+ - lib/mysql_inspector/migrations.rb
74
+ - lib/mysql_inspector/railtie.rb
75
+ - lib/mysql_inspector/railties/databases.rake
76
+ - lib/mysql_inspector/table.rb
77
+ - lib/mysql_inspector/table_part.rb
78
+ - lib/mysql_inspector/version.rb
36
79
  - mysql-inspector.gemspec
37
- has_rdoc: true
38
- homepage: http://github.com/rcarver/mysql-inspector
80
+ - test/fixtures/migrate/111_create_users.rb
81
+ - test/fixtures/migrate/222_create_things.rb
82
+ - test/helper.rb
83
+ - test/helper_ar.rb
84
+ - test/helpers/mysql_schemas.rb
85
+ - test/helpers/mysql_utils.rb
86
+ - test/helpers/string_unindented.rb
87
+ - test/mysql_inspector/cli_basics_test.rb
88
+ - test/mysql_inspector/cli_diff_test.rb
89
+ - test/mysql_inspector/cli_grep_test.rb
90
+ - test/mysql_inspector/cli_load_test.rb
91
+ - test/mysql_inspector/cli_write_test.rb
92
+ - test/mysql_inspector/config_test.rb
93
+ - test/mysql_inspector/diff_test.rb
94
+ - test/mysql_inspector/dump_test.rb
95
+ - test/mysql_inspector/grep_test.rb
96
+ - test/mysql_inspector/table_test.rb
97
+ - test/mysql_inspector_ar/ar_dump_test.rb
98
+ - test/mysql_inspector_ar/ar_migrations_test.rb
99
+ homepage: ''
39
100
  licenses: []
40
-
41
101
  post_install_message:
42
- rdoc_options:
43
- - --charset=UTF-8
44
- require_paths:
102
+ rdoc_options: []
103
+ require_paths:
45
104
  - lib
46
- required_ruby_version: !ruby/object:Gem::Requirement
47
- requirements:
48
- - - ">="
49
- - !ruby/object:Gem::Version
50
- segments:
51
- - 0
52
- version: "0"
53
- required_rubygems_version: !ruby/object:Gem::Requirement
54
- requirements:
55
- - - ">="
56
- - !ruby/object:Gem::Version
57
- segments:
58
- - 0
59
- version: "0"
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ! '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
60
117
  requirements: []
61
-
62
- rubyforge_project:
63
- rubygems_version: 1.3.6
118
+ rubyforge_project: mysql-inspector
119
+ rubygems_version: 1.8.23
64
120
  signing_key:
65
121
  specification_version: 3
66
- summary: Tools for identifying changes to a MySQL schema
67
- test_files: []
68
-
122
+ summary: Store and understand your MySQL schema
123
+ test_files:
124
+ - test/fixtures/migrate/111_create_users.rb
125
+ - test/fixtures/migrate/222_create_things.rb
126
+ - test/helper.rb
127
+ - test/helper_ar.rb
128
+ - test/helpers/mysql_schemas.rb
129
+ - test/helpers/mysql_utils.rb
130
+ - test/helpers/string_unindented.rb
131
+ - test/mysql_inspector/cli_basics_test.rb
132
+ - test/mysql_inspector/cli_diff_test.rb
133
+ - test/mysql_inspector/cli_grep_test.rb
134
+ - test/mysql_inspector/cli_load_test.rb
135
+ - test/mysql_inspector/cli_write_test.rb
136
+ - test/mysql_inspector/config_test.rb
137
+ - test/mysql_inspector/diff_test.rb
138
+ - test/mysql_inspector/dump_test.rb
139
+ - test/mysql_inspector/grep_test.rb
140
+ - test/mysql_inspector/table_test.rb
141
+ - test/mysql_inspector_ar/ar_dump_test.rb
142
+ - test/mysql_inspector_ar/ar_migrations_test.rb