Skip to content

Instantly share code, notes, and snippets.

@osyoyu
Created July 24, 2025 10:16
Show Gist options
  • Select an option

  • Save osyoyu/446a00e1e6d65b9adba3ce46dcf7dc20 to your computer and use it in GitHub Desktop.

Select an option

Save osyoyu/446a00e1e6d65b9adba3ce46dcf7dc20 to your computer and use it in GitHub Desktop.
splitter for ridgepole --export
c = File.read(ARGV[0])
f = nil
table_name = nil
c.each_line do |line|
if line =~ /^create_table/
# Table definition
table_name = line.match(/^create_table "([^"]+)",/)[1]
f = File.open(File.join(File.dirname(ARGV[0]), "schemata", "#{table_name}.schema.rb"), "w")
comment = line.match(/comment: "([^"]+?)", force/)
f.write("# frozen_string_literal: true\n\n")
if comment
before_comment, after_comment = line.split(comment[0])
f.write(before_comment)
f.write("comment: <<~__EOS__, force")
f.write(after_comment)
f.write(" #{comment[1]}\n")
f.write("__EOS__\n")
else
f.write(line)
end
elsif line =~ /^end/
# end
f.write(line)
f.close
puts "Created #{table_name}.schema.rb"
elsif line =~ /^add_foreign_key/
# add_foreign_key
table_name = line.match(/^add_foreign_key "([^"]+)",/)[1]
f = File.open(File.join(File.dirname(ARGV[0]), "schemata", "#{table_name}.schema.rb"), "a")
f.write(line)
f.close
else
if f != nil && !f.closed?
# column definition
f.write(line)
else
# print skipped lines
if line.chomp != ''
STDERR.puts line
end
end
end
end
schema_files = Dir.glob("#{File.dirname(ARGV[0])}/schemata/*.schema.rb")
schema_files.each do |file|
c = ""
lines = File.read(file).each_line
begin
while line = lines.next
c << line
# Insert newlines for readability
if !line.match(/^ t.index/) && lines.peek =~ /^ t.index/
c << "\n"
end
if !line.match(/^add_foreign_key/) && lines.peek =~ /^add_foreign_key/
c << "\n"
end
end
rescue StopIteration => e
# do nothing
end
# # Insert modeline
# c << "\n"
# c << "# -*- mode: ruby -*-\n"
# c << "# vi: set ft=ruby :\n"
File.write(file, c)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment