Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding schema only mode to import tables #2

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions lib/mosql/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ def parse_args
@options[:skip_import] = true
end

opts.on("--schema-only", "Only import schema without data") do
@options[:schema_only] = true
end

opts.on("--reimport", "Force a data re-import") do
@options[:reimport] = true
end
Expand Down Expand Up @@ -169,12 +173,16 @@ def run
:sql => @sql,
:schema => @schema)

unless options[:skip_import]
@streamer.import
end
unless options[:schema_only]
unless options[:skip_import]
@streamer.import
end

unless options[:skip_tail]
@streamer.optail
unless options[:skip_tail]
@streamer.optail
end
else
@streamer.import_schema
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion lib/mosql/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,17 @@ def initialize(map)
end

def create_schema(db, clobber=false)
log.info("Creating schema and tables...")
unless clobber
log.info("Not dropping existing tables given no-drop-tables option...")
end
@map.values.each do |dbspec|
dbspec.each do |n, collection|
next unless n.is_a?(String)
meta = collection[:meta]
composite_key = meta[:composite_key]
keys = []
log.info("Creating table '#{meta[:table]}'...")
log.info("Creating table '#{meta[:table]}' if needed...")
db.send(clobber ? :create_table! : :create_table?, meta[:table]) do
collection[:columns].each do |col|
opts = {}
Expand Down
4 changes: 4 additions & 0 deletions lib/mosql/streamer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def import
end
end

def import_schema
@schema.create_schema(@sql.db, !options[:no_drop_tables])
end

def collection_for_ns(ns)
dbname, collection = ns.split(".", 2)
@mongo.db(dbname).collection(collection)
Expand Down