Skip to content

Commit c8323c9

Browse files
authored
Merge pull request #28 from openaustralia/feature/2-upload-sqlite-database
Make data.sqlite upload explicit and optional
2 parents 2ab4660 + 04cabde commit c8323c9

6 files changed

Lines changed: 69 additions & 20 deletions

File tree

‎AGENTS.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ command-line familiarity.
3434
included.** `all_paths` prunes directories whose name starts with `.`, so
3535
`.git` and `.bundle` are skipped, but a dot-*file* is not a directory and so
3636
gets packed: a top-level `.env` is uploaded, as is `data.sqlite` and anything
37-
else the person happens to have sitting there. Keep that distinction in mind
38-
before changing `all_paths`, and don't describe the behaviour as "skips
39-
hidden files", because it doesn't.
37+
else the person happens to have sitting there. `data.sqlite` is deliberate —
38+
it's how a run continues from existing data — and is announced in the
39+
"Uploading" line, with `--skip-data` to leave it out. Keep that distinction
40+
in mind before changing `all_paths`, and don't describe the behaviour as
41+
"skips hidden files", because it doesn't.
4042
- **The server streams newline-delimited JSON, not plain text.** Each line has
4143
a `stream` (`stdout`, `internalout` or `stderr`) and `text`, and `log` raises
4244
on any other stream value. Partial chunks are buffered on the newline, so

‎README.md‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ Yup, that's it.
2626
It runs the code that's there right now. It doesn't need to be checked into git
2727
or anything.
2828

29+
If there's a `data.sqlite` in the directory it gets uploaded along with the
30+
code, so the scraper run picks up from the data it already has. If it's big and
31+
you'd rather not wait for the upload, leave it out with
32+
33+
morph --skip-data
34+
2935
The first time you run it, it will ask for your morph.io API key, which it
3036
saves in `~/.morph`.
3137

@@ -38,8 +44,7 @@ For help
3844
It uploads your code every time. So if it's big it might take a little while.
3945
Scrapers are not usually so I'm hoping this won't really be an issue.
4046

41-
It doesn't yet return you the resulting sqlite database (or use the one you
42-
might have locally).
47+
It doesn't yet return you the resulting sqlite database.
4348

4449
## Development
4550

‎lib/morph-cli.rb‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,25 @@
1111
require 'minitar'
1212

1313
module MorphCLI
14-
def self.execute(directory, _development, env_config)
14+
def self.execute(directory, _development, env_config, skip_data: false)
1515
all_paths = MorphCLI.all_paths(directory)
1616

1717
unless all_paths.find { |file| /scraper\.[\S]+$/ =~ file }
1818
warn "Can't find scraper to upload. Expected to find a file called scraper.rb, scraper.php, scraper.py, scraper.pl, scraper.js, etc to upload"
1919
exit(1)
2020
end
2121

22+
database_path = MorphCLI.database_path(directory)
23+
if skip_data
24+
all_paths.delete(database_path)
25+
database_path = nil
26+
end
27+
2228
size = MorphCLI.get_dir_size(directory, all_paths)
23-
puts "Uploading #{size}..."
29+
puts "Uploading #{size}#{" (including #{database_path})" if database_path}..."
2430

2531
file = MorphCLI.create_tar(directory, all_paths)
2632

27-
timeout = if env_config.key?(:timeout)
28-
env_config[:timeout]
29-
else
30-
600 # 10 minutes should be "enough for everyone", right?
31-
# Setting to nil will disable the timeout entirely.
32-
# Default is 60 seconds.
33-
end
34-
3533
connection = Faraday.new(url: env_config[:base_url]) do |f|
3634
f.request :multipart
3735
f.response :raise_error
@@ -44,7 +42,10 @@ def self.execute(directory, _development, env_config)
4442
api_key: env_config[:api_key],
4543
code: Faraday::Multipart::FilePart.new(file, "application/octet-stream")
4644
}
47-
req.options.timeout = timeout
45+
# 10 minutes should be "enough for everyone", right?
46+
# Setting :timeout to nil in the config will disable the timeout
47+
# entirely. The Faraday default is 60 seconds.
48+
req.options.timeout = env_config.fetch(:timeout, 600)
4849
req.options.on_data = proc do |chunk, _overall_received_bytes, env|
4950
next unless env.status == 200
5051

‎lib/morph-cli/cli.rb‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ def self.exit_on_failure?
1414

1515
desc '[execute]', 'execute morph scraper'
1616
option :directory, default: Dir.getwd
17+
option :skip_data, default: false, type: :boolean,
18+
desc: "Don't upload the local data.sqlite database with the scraper"
1719

1820
def execute
1921
config = MorphCLI.load_config
@@ -28,7 +30,7 @@ def execute
2830
api_key_is_valid = false
2931
until api_key_is_valid
3032
begin
31-
MorphCLI.execute(options[:directory], options[:dev], env_config)
33+
MorphCLI.execute(options[:directory], options[:dev], env_config, skip_data: options[:skip_data])
3234
api_key_is_valid = true
3335
rescue Faraday::UnauthorizedError
3436
puts "Your key isn't working. Let's try again."

‎spec/morph_cli/cli_spec.rb‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,16 @@
3030
described_class.start(['execute', '--directory', '/somewhere'])
3131

3232
expect(MorphCLI).to have_received(:execute)
33-
.with('/somewhere', false, config[:production])
33+
.with('/somewhere', false, config[:production], skip_data: false)
34+
end
35+
36+
it 'leaves the database out when --skip-data is given' do
37+
allow(MorphCLI).to receive(:execute)
38+
39+
described_class.start(['execute', '--skip-data'])
40+
41+
expect(MorphCLI).to have_received(:execute)
42+
.with(anything, false, config[:production], skip_data: true)
3443
end
3544

3645
it 'runs the scraper with the development config when --dev is given' do
@@ -39,7 +48,7 @@
3948
described_class.start(['execute', '--dev'])
4049

4150
expect(MorphCLI).to have_received(:execute)
42-
.with(anything, true, config[:development])
51+
.with(anything, true, config[:development], skip_data: false)
4352
end
4453

4554
it 'asks for an API key and saves the config when none is set' do
@@ -54,7 +63,7 @@
5463
expect(config[:production][:api_key]).to eq('shiny-new-key')
5564
expect(MorphCLI).to have_received(:save_config).with(config)
5665
expect(MorphCLI).to have_received(:execute)
57-
.with(anything, false, config[:production])
66+
.with(anything, false, config[:production], skip_data: false)
5867
end
5968

6069
it 'asks for a new API key and retries when the server rejects it' do

‎spec/morph_cli_spec.rb‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,36 @@ def with_scraper_directory
4545
end)
4646
end
4747

48+
it "uploads the local database and says so" do
49+
stub_request(:post, "https://morph.io/run").to_return(status: 200, body: "")
50+
51+
with_scraper_directory do |dir|
52+
File.write(File.join(dir, "data.sqlite"), "sqlite data")
53+
54+
expect { described_class.execute(dir, false, env_config) }
55+
.to output(/\AUploading 21\.00 B \(including data\.sqlite\)\.\.\.\n\z/).to_stdout
56+
end
57+
58+
expect(WebMock).to(have_requested(:post, "https://morph.io/run").with do |req|
59+
req.body.include?("data.sqlite")
60+
end)
61+
end
62+
63+
it "leaves the database out of the upload when skip_data is true" do
64+
stub_request(:post, "https://morph.io/run").to_return(status: 200, body: "")
65+
66+
with_scraper_directory do |dir|
67+
File.write(File.join(dir, "data.sqlite"), "sqlite data")
68+
69+
expect { described_class.execute(dir, false, env_config, skip_data: true) }
70+
.to output(/\AUploading 10\.00 B\.\.\.\n\z/).to_stdout
71+
end
72+
73+
expect(WebMock).to(have_requested(:post, "https://morph.io/run").with do |req|
74+
!req.body.include?("data.sqlite")
75+
end)
76+
end
77+
4878
it "raises Faraday::UnauthorizedError when the API key is rejected" do
4979
stub_request(:post, "https://morph.io/run").to_return(status: 401, body: "")
5080

0 commit comments

Comments
 (0)