schema_designer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Rakefile +34 -0
  4. data/app/assets/javascripts/schema_designer/_potential_schema.js.coffee +31 -0
  5. data/app/assets/javascripts/schema_designer/application.js +16 -0
  6. data/app/assets/javascripts/schema_designer/backbone.js +1581 -0
  7. data/app/assets/javascripts/schema_designer/jquery.autocomplete.js +645 -0
  8. data/app/assets/javascripts/schema_designer/schema.js +181 -0
  9. data/app/assets/javascripts/schema_designer/springy.js +697 -0
  10. data/app/assets/javascripts/schema_designer/underscore.js +1276 -0
  11. data/app/assets/stylesheets/schema_designer/application.css +13 -0
  12. data/app/assets/stylesheets/schema_designer/schema.css +35 -0
  13. data/app/controllers/schema_designer/application_controller.rb +6 -0
  14. data/app/controllers/schema_designer/schema_controller.rb +137 -0
  15. data/app/views/layouts/schema_designer/application.html.erb +14 -0
  16. data/app/views/schema_designer/schema/index.html.erb +19 -0
  17. data/config/routes.rb +10 -0
  18. data/lib/schema_designer.rb +4 -0
  19. data/lib/schema_designer/engine.rb +5 -0
  20. data/lib/schema_designer/version.rb +3 -0
  21. data/test/controllers/schema_designer/schema_controller_test.rb +11 -0
  22. data/test/dummy/README.rdoc +28 -0
  23. data/test/dummy/Rakefile +6 -0
  24. data/test/dummy/app/assets/javascripts/application.js +13 -0
  25. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  26. data/test/dummy/app/controllers/application_controller.rb +5 -0
  27. data/test/dummy/app/helpers/application_helper.rb +2 -0
  28. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  29. data/test/dummy/bin/bundle +3 -0
  30. data/test/dummy/bin/rails +4 -0
  31. data/test/dummy/bin/rake +4 -0
  32. data/test/dummy/config.ru +4 -0
  33. data/test/dummy/config/application.rb +23 -0
  34. data/test/dummy/config/boot.rb +5 -0
  35. data/test/dummy/config/database.yml +25 -0
  36. data/test/dummy/config/environment.rb +5 -0
  37. data/test/dummy/config/environments/development.rb +29 -0
  38. data/test/dummy/config/environments/production.rb +80 -0
  39. data/test/dummy/config/environments/test.rb +36 -0
  40. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  41. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  42. data/test/dummy/config/initializers/inflections.rb +16 -0
  43. data/test/dummy/config/initializers/mime_types.rb +5 -0
  44. data/test/dummy/config/initializers/secret_token.rb +12 -0
  45. data/test/dummy/config/initializers/session_store.rb +3 -0
  46. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  47. data/test/dummy/config/locales/en.yml +23 -0
  48. data/test/dummy/config/routes.rb +4 -0
  49. data/test/dummy/public/404.html +58 -0
  50. data/test/dummy/public/422.html +58 -0
  51. data/test/dummy/public/500.html +57 -0
  52. data/test/dummy/public/favicon.ico +0 -0
  53. data/test/helpers/schema_designer/schema_helper_test.rb +6 -0
  54. data/test/integration/navigation_test.rb +10 -0
  55. data/test/schema_designer_test.rb +7 -0
  56. data/test/test_helper.rb +15 -0
  57. metadata +163 -0
@@ -0,0 +1,13 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */
@@ -0,0 +1,35 @@
1
+ body {
2
+ margin: 0;
3
+ padding: 0;
4
+ }
5
+ table {
6
+ position: absolute;
7
+ top: 0;
8
+ left: 0;
9
+ border: 1px solid black;
10
+ border-collapse: collapse;
11
+ background: white;
12
+ }
13
+ td {
14
+ border-bottom: 1px solid gray;
15
+ }
16
+ th {
17
+ text-shadow: 0px 0px 1px white;
18
+ }
19
+ #bar {
20
+ background: #ccc;
21
+ box-shadow: 0 -1px 5px rgba(0, 0, 0, 0.3);
22
+ padding: 10px;
23
+ margin: 0;
24
+
25
+ }
26
+ #bar input {
27
+ font-size: 14px;
28
+ font-family: Monaco, fixed;
29
+ padding: 4px;
30
+ width: 95%
31
+ }
32
+ .autocomplete-suggestions { border: 1px solid #999; background: #FFF; overflow: auto; }
33
+ .autocomplete-suggestion { padding: 2px 5px; white-space: nowrap; overflow: hidden; }
34
+ .autocomplete-selected { background: #F0F0F0; }
35
+ .autocomplete-suggestions strong { font-weight: normal; color: #3399FF; }
@@ -0,0 +1,6 @@
1
+ module SchemaDesigner
2
+ class ApplicationController < ActionController::Base
3
+
4
+
5
+ end
6
+ end
@@ -0,0 +1,137 @@
1
+ require_dependency "schema_designer/application_controller"
2
+
3
+ module SchemaDesigner
4
+ class SchemaController < ApplicationController
5
+ helper_method :parse_schema
6
+ def index
7
+ end
8
+
9
+ def save
10
+ new_schema = JSON.parse params[:tables]
11
+ old_schema = parse_schema
12
+ @change = []
13
+ @up = []
14
+ @down = []
15
+ new_schema.each do |table|
16
+ id = table["id"]
17
+ corresponding_table = old_schema.find(&by_id(id))
18
+ if corresponding_table.nil?
19
+ new_table(table)
20
+ else
21
+ old_ids = corresponding_table[:columns].map{|t| t[:id]}
22
+ table["columns"].each do |col|
23
+ if old_ids.include? col["id"]
24
+ unless (old_col = corresponding_table[:columns].find(&by_id(col["id"]))) == col.symbolize_keys
25
+ column_change(table, old_col, col)
26
+ end
27
+ old_ids.delete col["id"]
28
+ else
29
+ new_column(table, col)
30
+ end
31
+ end
32
+ old_ids.each do |id|
33
+ delete_column table, corresponding_table[:columns].find(&by_id(id))
34
+ end
35
+ end
36
+ end
37
+ migration = "class #{params[:name].camelize} < ActiveRecord::Migration\n"
38
+ migration << "\n def up\n #{@up.join("\n ")}\n end\n" if @up.length > 0
39
+ migration << "\n def down\n #{@down.join("\n ")}\n end\n" if @down.length > 0
40
+ migration << "\n def change\n #{@change.join("\n ")}\n end\n" if @change.length > 0
41
+ migration << "\nend"
42
+
43
+ File.write(File.join(Rails.root, 'db', 'migrate', "#{Time.now.to_i}_#{params[:name].underscore}.rb"), migration, mode: 'w')
44
+
45
+ head :ok
46
+ end
47
+
48
+ protected
49
+ def by_id(id)
50
+ Proc.new{|t| t[:id] == id || t["id"] == id}
51
+ end
52
+
53
+ def column_change(table, old, new)
54
+ if old[:type] == new["type"]
55
+ @change << "rename_column :#{table["name"]}, :#{old[:name]}, :#{new["name"]}"
56
+ elsif old[:name] == new["name"]
57
+ @up << "change_column :#{table["name"]}, :#{old[:name]}, :#{new["type"]}"
58
+ @down << "change_column :#{table["name"]}, :#{old[:name]}, :#{old[:type]}"
59
+ else
60
+ raise "not implemented"
61
+ end
62
+ end
63
+
64
+ def new_column(table, col)
65
+ @change << "add_column :#{table["name"]}, :#{col["name"]}, :#{col["type"]}"
66
+ end
67
+
68
+ def delete_column(table, col)
69
+ @up << "remove_column :#{table["name"]}, :#{col[:name]}"
70
+ @down << "add_column :#{table["name"]}, :#{col[:name]}, :#{col[:type]}"
71
+ end
72
+
73
+ def new_table(table)
74
+ opts = table["hasId"] ? "" : ", :id => false"
75
+ code = "create_table :#{table["name"]}#{opts} do |t|\n"
76
+ table["columns"].each do |col|
77
+ if ['primary_key', 'string', 'text', 'integer', 'float', 'decimal', 'datetime', 'timestamp', 'time', 'date', 'binary', 'boolean', 'references'].include? col["type"]
78
+ code << " t.#{col["type"]} :#{col["name"]}\n"
79
+ else
80
+ code << " t.column :#{col["name"]}, :#{col["type"]}\n"
81
+ end
82
+ end
83
+ if table["hasTimestamps"]
84
+ code << " t.timestamps\n"
85
+ end
86
+ code << " end"
87
+ @change << code
88
+ end
89
+
90
+ def parse_schema
91
+ path = File.join(Rails.root, 'db', 'schema.rb')
92
+ if File.exist? path
93
+ lines = File.read(path).split(/\n/)
94
+ last_table = nil
95
+ tables = {}
96
+ table_id = 0
97
+ column_id = 0
98
+ lines.each do |line|
99
+ if match = line.match(/^\s*create_table "(.+?)"(.+)/)
100
+ last_table = match[1]
101
+ hasId = !(match[2] =~ /id:\s*false/)
102
+ table_id += 1
103
+ tables[last_table] = {
104
+ id: table_id,
105
+ name: last_table,
106
+ hasId: hasId,
107
+ hasTimestamps: false,
108
+ columns: []
109
+ }
110
+ elsif last_table && (match = line.match(/^\s*t.(\w+?)\s+"(.+?)"(\s*,\s*.+)?/))
111
+ options = match[3]
112
+ if match[2] == 'created_at' || match[2] == 'updated_at'
113
+ tables[last_table][:hasTimestamps] = true
114
+ elsif match[1] == 'integer' && match[2] =~ /^(.+)_id$/
115
+ column_id += 1
116
+ tables[last_table][:columns] << {
117
+ id: column_id,
118
+ name: $1,
119
+ type: 'references'
120
+ }
121
+ else
122
+ column_id += 1
123
+ tables[last_table][:columns] << {
124
+ id: column_id,
125
+ name: match[2],
126
+ type: match[1]
127
+ }
128
+ end
129
+ end
130
+ end
131
+ tables.values
132
+ else
133
+ []
134
+ end
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>SchemaDesigner</title>
5
+ <%= stylesheet_link_tag "schema_designer/application", media: "all" %>
6
+ <%= javascript_include_tag "schema_designer/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,19 @@
1
+
2
+ <div id="chart">
3
+ <svg width=13000 height=1000>
4
+ </svg>
5
+
6
+ </div>
7
+
8
+ <div id="bar" style="position: fixed; bottom: 0; width: 100%">
9
+ <form action="">
10
+ <!--<label><input type="radio" name="create" checked /> Create</label>
11
+ <label><input type="radio" /> Modify</label>-->
12
+ <input type="text" placeholder='Type in migration command here (Format: "table_name column_name:type column_name:type")' />
13
+ </form>
14
+ <button id="output">Save Migration</button>
15
+ </div>
16
+
17
+ <script>
18
+ var INITIAL_DATA = <%= raw parse_schema.to_json %>;
19
+ </script>
@@ -0,0 +1,10 @@
1
+ # SchemaDesigner::Engine.routes.draw do
2
+ #get "schema/index"
3
+ # end
4
+
5
+ Rails.application.routes.draw do
6
+ if Rails.env.development?
7
+ get '/_schema' => "schema_designer/schema#index"
8
+ post '/_schema' => "schema_designer/schema#save"
9
+ end
10
+ end
@@ -0,0 +1,4 @@
1
+ require "schema_designer/engine"
2
+
3
+ module SchemaDesigner
4
+ end
@@ -0,0 +1,5 @@
1
+ module SchemaDesigner
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace SchemaDesigner
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module SchemaDesigner
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,11 @@
1
+ require 'test_helper'
2
+
3
+ module SchemaDesigner
4
+ class SchemaControllerTest < ActionController::TestCase
5
+ test "should get index" do
6
+ get :index
7
+ assert_response :success
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Dummy::Application.load_tasks
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,13 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */
@@ -0,0 +1,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
6
+ <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3
+ load Gem.bin_path('bundler', 'bundle')
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
3
+ require_relative '../config/boot'
4
+ require 'rails/commands'
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../config/boot'
3
+ require 'rake'
4
+ Rake.application.run
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Rails.application
@@ -0,0 +1,23 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require 'rails/all'
4
+
5
+ Bundler.require(*Rails.groups)
6
+ require "schema_designer"
7
+
8
+ module Dummy
9
+ class Application < Rails::Application
10
+ # Settings in config/environments/* take precedence over those specified here.
11
+ # Application configuration should go into files in config/initializers
12
+ # -- all .rb files in that directory are automatically loaded.
13
+
14
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
15
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
16
+ # config.time_zone = 'Central Time (US & Canada)'
17
+
18
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
19
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
20
+ # config.i18n.default_locale = :de
21
+ end
22
+ end
23
+
@@ -0,0 +1,5 @@
1
+ # Set up gems listed in the Gemfile.
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../../Gemfile', __FILE__)
3
+
4
+ require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
5
+ $LOAD_PATH.unshift File.expand_path('../../../../lib', __FILE__)
@@ -0,0 +1,25 @@
1
+ # SQLite version 3.x
2
+ # gem install sqlite3
3
+ #
4
+ # Ensure the SQLite 3 gem is defined in your Gemfile
5
+ # gem 'sqlite3'
6
+ development:
7
+ adapter: sqlite3
8
+ database: db/development.sqlite3
9
+ pool: 5
10
+ timeout: 5000
11
+
12
+ # Warning: The database defined as "test" will be erased and
13
+ # re-generated from your development database when you run "rake".
14
+ # Do not set this db to the same as development or production.
15
+ test:
16
+ adapter: sqlite3
17
+ database: db/test.sqlite3
18
+ pool: 5
19
+ timeout: 5000
20
+
21
+ production:
22
+ adapter: sqlite3
23
+ database: db/production.sqlite3
24
+ pool: 5
25
+ timeout: 5000