erd 0.0.2 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +4 -8
- data/app/assets/stylesheets/erd/erd.css.scss +13 -10
- data/app/controllers/erd/erd_controller.rb +27 -11
- data/app/views/erd/erd/index.html.erb +14 -11
- data/config/routes.rb +1 -0
- data/lib/erd/version.rb +1 -1
- metadata +6 -6
data/README.rdoc
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
= Erd
|
2
2
|
|
3
|
-
A Rails engine for drawing your app's ER diagram
|
3
|
+
A Rails engine for drawing your app's ER diagram and operating migrations
|
4
4
|
|
5
5
|
|
6
6
|
== Requirements
|
@@ -30,15 +30,11 @@ Browse at your http://localhost:3000/erd
|
|
30
30
|
|
31
31
|
* You can drag and arrange the positions of each model
|
32
32
|
|
33
|
-
* You can add column
|
33
|
+
* You can operate DB schema manipulations such as `add column`, `rename column`, `alter column`, and `drop table`
|
34
34
|
|
35
|
-
*
|
35
|
+
* Then, Erd generates migration files on the server
|
36
36
|
|
37
|
-
*
|
38
|
-
|
39
|
-
* You can drop table
|
40
|
-
|
41
|
-
* Finally, Erd generates migration files on the server (you need to run `rake db:migrate` by hand)
|
37
|
+
* And you can run each migration on your browser
|
42
38
|
|
43
39
|
|
44
40
|
== TODO
|
@@ -1,11 +1,11 @@
|
|
1
1
|
* {
|
2
|
-
|
3
|
-
|
2
|
+
margin: 0;
|
3
|
+
padding: 0;
|
4
4
|
}
|
5
5
|
|
6
6
|
body div {
|
7
|
-
|
8
|
-
|
7
|
+
text-align: left;
|
8
|
+
margin: 0 auto;
|
9
9
|
}
|
10
10
|
|
11
11
|
li {
|
@@ -29,6 +29,12 @@ fieldset { padding:0; border:0; margin-top:25px; }
|
|
29
29
|
position: relative;
|
30
30
|
float: right;
|
31
31
|
z-index: 999;
|
32
|
+
|
33
|
+
table {
|
34
|
+
td {
|
35
|
+
font-size: small;
|
36
|
+
}
|
37
|
+
}
|
32
38
|
}
|
33
39
|
|
34
40
|
#erd {
|
@@ -113,12 +119,9 @@ fieldset { padding:0; border:0; margin-top:25px; }
|
|
113
119
|
}
|
114
120
|
|
115
121
|
|
116
|
-
#
|
117
|
-
border:
|
118
|
-
|
119
|
-
|
120
|
-
#changes {
|
121
|
-
display: none;
|
122
|
+
#failed {
|
123
|
+
border: 2px #f00 solid;
|
124
|
+
padding: 2px;
|
122
125
|
}
|
123
126
|
|
124
127
|
#model_name_changes, #column_name_changes {
|
@@ -19,11 +19,24 @@ module Erd
|
|
19
19
|
{}
|
20
20
|
end
|
21
21
|
@erd = render_plain plain, positions
|
22
|
+
|
23
|
+
migrated_versions = ActiveRecord::Base.connection.select_values("SELECT version FROM #{ActiveRecord::Migrator.schema_migrations_table_name}").map {|v| '%.3d' % v}
|
24
|
+
@migrations = []
|
25
|
+
ActiveRecord::Migrator.migrations_paths.each do |path|
|
26
|
+
Dir.foreach(path) do |file|
|
27
|
+
if (version_and_name = /^(\d{3,})_(.+)\.rb$/.match(file))
|
28
|
+
status = migrated_versions.delete(version_and_name[1]) ? 'up' : 'down'
|
29
|
+
@migrations << {status: status, version: version_and_name[1], name: version_and_name[2]}
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
@migrations += migrated_versions.map {|v| {status: 'up', version: v, name: '*** NO FILE ***'}}
|
34
|
+
@migrations.sort_by! {|m| m[:version]}
|
22
35
|
end
|
23
36
|
|
24
37
|
def update
|
25
38
|
changes = ActiveSupport::JSON.decode(params[:changes])
|
26
|
-
|
39
|
+
failed_migrations = []
|
27
40
|
changes.each do |row|
|
28
41
|
begin
|
29
42
|
action, model, column, from, to = row['action'], row['model'].tableize, row['column'], row['from'], row['to']
|
@@ -33,35 +46,29 @@ module Erd
|
|
33
46
|
execute_generate_migration "drop_#{model}"
|
34
47
|
generated_migration_file = (Dir.glob(Rails.root.join('db', 'migrate', '*.rb')) - before_migration_files).first
|
35
48
|
gsub_file generated_migration_file, /def up.* end/m, "def change\n drop_table :#{model}\n end"
|
36
|
-
generated_migrations << File.basename(generated_migration_file)
|
37
49
|
when 'rename_model'
|
38
50
|
from, to = from.tableize, to.tableize
|
39
51
|
execute_generate_migration "rename_#{from}_to_#{to}"
|
40
52
|
generated_migration_file = (Dir.glob(Rails.root.join('db', 'migrate', '*.rb')) - before_migration_files).first
|
41
53
|
gsub_file generated_migration_file, /def up.* end/m, "def change\n rename_table :#{from}, :#{to}\n end"
|
42
|
-
generated_migrations << File.basename(generated_migration_file)
|
43
54
|
when 'add_column'
|
44
55
|
name_and_type = column.scan(/(.*)\((.*?)\)/).first
|
45
56
|
name, type = name_and_type[0], name_and_type[1]
|
46
57
|
execute_generate_migration "add_#{name}_to_#{model}", ["#{name}:#{type}"]
|
47
58
|
generated_migration_file = (Dir.glob(Rails.root.join('db', 'migrate', '*.rb')) - before_migration_files).first
|
48
|
-
generated_migrations << File.basename(generated_migration_file)
|
49
59
|
when 'rename_column'
|
50
60
|
execute_generate_migration "rename_#{model}_#{from}_to_#{to}"
|
51
61
|
generated_migration_file = (Dir.glob(Rails.root.join('db', 'migrate', '*.rb')) - before_migration_files).first
|
52
62
|
gsub_file generated_migration_file, /def up.* end/m, "def change\n rename_column :#{model}, :#{from}, :#{to}\n end"
|
53
|
-
generated_migrations << File.basename(generated_migration_file)
|
54
63
|
when 'alter_column'
|
55
64
|
execute_generate_migration "change_#{model}_#{column}_type_to_#{to}"
|
56
65
|
generated_migration_file = (Dir.glob(Rails.root.join('db', 'migrate', '*.rb')) - before_migration_files).first
|
57
66
|
gsub_file generated_migration_file, /def up.* end/m, "def change\n change_column :#{model}, :#{column}, :#{to}\n end"
|
58
|
-
generated_migrations << File.basename(generated_migration_file)
|
59
67
|
when 'move'
|
60
68
|
json_file = Rails.root.join('tmp', 'erd_positions.json')
|
61
69
|
positions = json_file.exist? ? ActiveSupport::JSON.decode(json_file.read) : {}
|
62
70
|
positions[model] = to
|
63
71
|
json_file.open('w') {|f| f.write positions.to_json}
|
64
|
-
generated_migrations << 'saved entity positions'
|
65
72
|
else
|
66
73
|
raise "unexpected action: #{action}"
|
67
74
|
end
|
@@ -70,7 +77,12 @@ module Erd
|
|
70
77
|
end
|
71
78
|
end
|
72
79
|
|
73
|
-
redirect_to erd.root_path, :flash => {:
|
80
|
+
redirect_to erd.root_path, :flash => {:failed_migrations => failed_migrations}
|
81
|
+
end
|
82
|
+
|
83
|
+
def migrate
|
84
|
+
run_migrations :up => params[:up], :down => params[:down]
|
85
|
+
redirect_to erd.root_path
|
74
86
|
end
|
75
87
|
|
76
88
|
private
|
@@ -103,10 +115,14 @@ module Erd
|
|
103
115
|
end
|
104
116
|
|
105
117
|
# `rake db:migrate`
|
106
|
-
def
|
107
|
-
|
118
|
+
def run_migrations(migrations)
|
119
|
+
migrations.each do |direction, versions|
|
120
|
+
versions.each do |version|
|
121
|
+
ActiveRecord::Migrator.run(direction, ActiveRecord::Migrator.migrations_path, version.to_i)
|
122
|
+
end if versions
|
123
|
+
end
|
108
124
|
if ActiveRecord::Base.schema_format == :ruby
|
109
|
-
File.open(ENV['SCHEMA'] || "#{Rails.root}/db/schema.rb",
|
125
|
+
File.open(ENV['SCHEMA'] || "#{Rails.root}/db/schema.rb", 'w') do |file|
|
110
126
|
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
|
111
127
|
end
|
112
128
|
end
|
@@ -4,18 +4,8 @@
|
|
4
4
|
</div>
|
5
5
|
|
6
6
|
<div id="migration">
|
7
|
-
<%- if flash[:generated_migrations].present? -%>
|
8
|
-
<div id="generated">
|
9
|
-
generated migrations (please run rake db:migrate)
|
10
|
-
<ul>
|
11
|
-
<%- flash[:generated_migrations].each do |m| -%>
|
12
|
-
<li><%= m %></li>
|
13
|
-
<%- end -%>
|
14
|
-
</ul>
|
15
|
-
</div>
|
16
|
-
<%- end -%>
|
17
7
|
<%- if flash[:failed_migrations].present? -%>
|
18
|
-
<div id="
|
8
|
+
<div id="failed">
|
19
9
|
failed migrations
|
20
10
|
<ul>
|
21
11
|
<%- flash[:failed_migrations].each do |m| -%>
|
@@ -25,6 +15,7 @@
|
|
25
15
|
</div>
|
26
16
|
<%- end -%>
|
27
17
|
<table id="changes">
|
18
|
+
<caption>schema changes</caption>
|
28
19
|
<thead><tr><th>action</th><th>model</th><th>column</th><th>from</th><th>to</th></tr></thead>
|
29
20
|
<tbody></tbody>
|
30
21
|
</table>
|
@@ -32,5 +23,17 @@
|
|
32
23
|
<%= hidden_field_tag 'changes' %>
|
33
24
|
<%= submit_tag 'save changes' %>
|
34
25
|
<% end %>
|
26
|
+
<%= form_tag '/erd/migrate', :method => :put do %>
|
27
|
+
<table>
|
28
|
+
<caption>migration status</caption>
|
29
|
+
<thead><tr><th>status</th><th>version</th><th>name</th></tr></thead>
|
30
|
+
<tbody>
|
31
|
+
<%- @migrations.each do |m| -%>
|
32
|
+
<tr><td><label><%= check_box_tag (m[:status] == 'up' ? 'down[]' : 'up[]'), m[:version] %><%= m[:status] %></label></td><td><%= m[:version] %></td><td><%= m[:name] %></td></tr>
|
33
|
+
<%- end -%>
|
34
|
+
</tbody>
|
35
|
+
</table>
|
36
|
+
<%= submit_tag 'run migrations' %>
|
37
|
+
<% end %>
|
35
38
|
</div>
|
36
39
|
</div>
|
data/config/routes.rb
CHANGED
data/lib/erd/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: erd
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-04-
|
12
|
+
date: 2012-04-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails-erd
|
16
|
-
requirement: &
|
16
|
+
requirement: &70264677495520 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 0.4.5
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70264677495520
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: nokogiri
|
27
|
-
requirement: &
|
27
|
+
requirement: &70264677495100 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70264677495100
|
36
36
|
description: erd engine on Rails
|
37
37
|
email:
|
38
38
|
- ronnie@dio.jp
|