Permalink
Browse files

even more consistent formatting

  • Loading branch information...
1 parent 4439e26 commit 57d96dd9be00e728b2ee65a4285e34ae83990b55 @rcarver committed Feb 18, 2012
View
@@ -50,7 +50,7 @@ abort @opts.to_s if ARGV.empty?
# Print table details
def format_items(label, items, &formatter)
pad = " " * 4
- formatter ||= proc { |item | item }
+ formatter ||= proc { |item | item.to_sql }
items.each.with_index { |item, i|
if i == 0
puts [label, pad, formatter.call(item)] * ""
@@ -123,10 +123,14 @@ when "diff"
end
end
+ def prefix_for_item(item, added, removed)
+ added.include?(item) ? "+" : "-"
+ end
+
def format_diff_items(label, added, removed)
format_items(label, (added + removed).sort) { |item|
- prefix = added.include?(item) ? "+" : "-"
- "#{prefix} #{item}"
+ prefix = prefix_for_item(item, added, removed)
+ "#{prefix} #{item.to_sql}"
}
end
@@ -3,9 +3,9 @@ class Column < Struct.new(:name, :sql_type, :nullable, :default, :auto_increment
include MysqlInspector::TablePart
- def to_s
+ def to_sql
parts = []
- parts << "`#{name}`"
+ parts << quote(name)
parts << sql_type
parts << (nullable ? "NULL" : "NOT NULL")
parts << "DEFAULT #{default}" if default
@@ -3,6 +3,20 @@ class Constraint < Struct.new(:name, :column_names, :foreign_table, :foreign_col
include MysqlInspector::TablePart
+ def to_sql
+ parts = []
+ parts << "CONSTRAINT"
+ parts << quote(name)
+ parts << "FOREIGN KEY"
+ parts << paren(column_names.map { |c| quote(c) })
+ parts << "REFERENCES"
+ parts << quote(foreign_table)
+ parts << paren(foreign_column_names.map { |c| quote(c) })
+ parts << "ON DELETE #{on_delete}"
+ parts << "ON UPDATE #{on_update}"
+ parts * " "
+ end
+
def =~(matcher)
name =~ matcher ||
column_names.any? { |c| c =~ matcher } ||
@@ -3,6 +3,15 @@ class Index < Struct.new(:name, :column_names, :unique)
include MysqlInspector::TablePart
+ def to_sql
+ parts = []
+ parts << "UNIQUE" if unique
+ parts << "KEY"
+ parts << quote(name)
+ parts << paren(column_names.map { |c| quote(c) })
+ parts * " "
+ end
+
def =~(matcher)
name =~ matcher || column_names.any? { |c| c =~ matcher }
end
@@ -104,11 +104,11 @@ def to_s
lines << "CREATE TABLE `#{table_name}`"
lines << nil
- columns.each { |x| lines << x.to_s }
+ columns.each { |x| lines << x.to_sql }
lines << nil
- indices.each { |x| lines << x.to_s }
+ indices.each { |x| lines << x.to_sql }
lines << nil
- constraints.each { |x| lines << x.to_s }
+ constraints.each { |x| lines << x.to_sql }
lines << nil
lines << options
@@ -119,7 +119,7 @@ def to_sql
lines = []
lines << "CREATE TABLE `#{table_name}` ("
- lines << (columns + indices + constraints).map { |x| " #{x}" }.join(",\n")
+ lines << (columns + indices + constraints).map { |x| " #{x.to_sql}" }.join(",\n")
lines << ") #{options}"
lines.join("\n")
@@ -8,8 +8,14 @@ def <=>(other)
name <=> other.name
end
- def to_s
- sql_line.strip.chomp(',')
+ protected
+
+ def quote(word)
+ "`#{word}`"
+ end
+
+ def paren(words)
+ "(#{words * ","})"
end
end
@@ -52,7 +52,7 @@
IDX - KEY `color` (`color`)
+ KEY `name` (`first_name`,`last_name`)
CST - CONSTRAINT `belongs_to_color` FOREIGN KEY (`color`) REFERENCES `colors` (`name`) ON DELETE NO ACTION ON UPDATE CASCADE
- + CONSTRAINT `belongs_to_user` FOREIGN KEY (`first_name`, `last_name`) REFERENCES `users` (`first_name`, `last_name`) ON DELETE NO ACTION ON UPDATE CASCADE
+ + CONSTRAINT `belongs_to_user` FOREIGN KEY (`first_name`,`last_name`) REFERENCES `users` (`first_name`,`last_name`) ON DELETE NO ACTION ON UPDATE CASCADE
+ users
EOL
@@ -40,7 +40,7 @@
`last_name` varchar(255) NOT NULL
`name` varchar(255) NOT NULL DEFAULT 'toy'
IDX KEY `name` (`first_name`,`last_name`)
- CST CONSTRAINT `belongs_to_user` FOREIGN KEY (`first_name`, `last_name`) REFERENCES `users` (`first_name`, `last_name`) ON DELETE NO ACTION ON UPDATE CASCADE
+ CST CONSTRAINT `belongs_to_user` FOREIGN KEY (`first_name`,`last_name`) REFERENCES `users` (`first_name`,`last_name`) ON DELETE NO ACTION ON UPDATE CASCADE
users
COL `first_name` varchar(255) NOT NULL
@@ -89,7 +89,7 @@
things
COL `first_name` varchar(255) NOT NULL
IDX KEY `name` (`first_name`,`last_name`)
- CST CONSTRAINT `belongs_to_user` FOREIGN KEY (`first_name`, `last_name`) REFERENCES `users` (`first_name`, `last_name`) ON DELETE NO ACTION ON UPDATE CASCADE
+ CST CONSTRAINT `belongs_to_user` FOREIGN KEY (`first_name`,`last_name`) REFERENCES `users` (`first_name`,`last_name`) ON DELETE NO ACTION ON UPDATE CASCADE
users
COL `first_name` varchar(255) NOT NULL
@@ -61,7 +61,7 @@
KEY `name` (`first_name`,`last_name`)
UNIQUE KEY `things_primary` (`id`)
- CONSTRAINT `belongs_to_user` FOREIGN KEY (`first_name`, `last_name`) REFERENCES `users` (`first_name`, `last_name`) ON DELETE NO ACTION ON UPDATE CASCADE
+ CONSTRAINT `belongs_to_user` FOREIGN KEY (`first_name`,`last_name`) REFERENCES `users` (`first_name`,`last_name`) ON DELETE NO ACTION ON UPDATE CASCADE
ENGINE=InnoDB DEFAULT CHARSET=utf8
EOL
@@ -81,7 +81,7 @@
`weight` int(11) NULL,
KEY `name` (`first_name`,`last_name`),
UNIQUE KEY `things_primary` (`id`),
- CONSTRAINT `belongs_to_user` FOREIGN KEY (`first_name`, `last_name`) REFERENCES `users` (`first_name`, `last_name`) ON DELETE NO ACTION ON UPDATE CASCADE
+ CONSTRAINT `belongs_to_user` FOREIGN KEY (`first_name`,`last_name`) REFERENCES `users` (`first_name`,`last_name`) ON DELETE NO ACTION ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8
EOL
end

0 comments on commit 57d96dd

Please sign in to comment.