taps 0.3.11 → 0.3.12

Sign up to get free protection for your applications and to get access to all the features.
data/lib/taps/schema.rb CHANGED
@@ -6,78 +6,78 @@ require 'json/pure'
6
6
 
7
7
  module Taps
8
8
  module Schema
9
- extend self
9
+ extend self
10
10
 
11
- def dump(database_url)
12
- db = Sequel.connect(database_url)
13
- db.dump_schema_migration(:indexes => false)
14
- end
11
+ def dump(database_url)
12
+ db = Sequel.connect(database_url)
13
+ db.dump_schema_migration(:indexes => false)
14
+ end
15
15
 
16
- def dump_table(database_url, table)
17
- table = table.to_sym
18
- Sequel.connect(database_url) do |db|
19
- <<END_MIG
16
+ def dump_table(database_url, table)
17
+ table = table.to_sym
18
+ Sequel.connect(database_url) do |db|
19
+ <<END_MIG
20
20
  Class.new(Sequel::Migration) do
21
- def up
22
- #{db.dump_table_schema(table.identifier, :indexes => false)}
23
- end
21
+ def up
22
+ #{db.dump_table_schema(table.identifier, :indexes => false)}
23
+ end
24
24
 
25
- def down
26
- drop_table("#{table}") if @db.table_exists?("#{table}")
27
- end
25
+ def down
26
+ drop_table("#{table}") if @db.table_exists?("#{table}")
27
+ end
28
28
  end
29
29
  END_MIG
30
- end
31
- end
30
+ end
31
+ end
32
32
 
33
- def indexes(database_url)
34
- db = Sequel.connect(database_url)
35
- db.dump_indexes_migration
36
- end
33
+ def indexes(database_url)
34
+ db = Sequel.connect(database_url)
35
+ db.dump_indexes_migration
36
+ end
37
37
 
38
- def indexes_individual(database_url)
39
- idxs = {}
40
- Sequel.connect(database_url) do |db|
41
- tables = db.tables
42
- tables.each do |table|
43
- idxs[table] = db.send(:dump_table_indexes, table, :add_index, {}).split("\n")
44
- end
45
- end
38
+ def indexes_individual(database_url)
39
+ idxs = {}
40
+ Sequel.connect(database_url) do |db|
41
+ tables = db.tables
42
+ tables.each do |table|
43
+ idxs[table] = db.send(:dump_table_indexes, table, :add_index, {}).split("\n")
44
+ end
45
+ end
46
46
 
47
- idxs.each do |table, indexes|
48
- idxs[table] = indexes.map do |idx|
49
- <<END_MIG
47
+ idxs.each do |table, indexes|
48
+ idxs[table] = indexes.map do |idx|
49
+ <<END_MIG
50
50
  Class.new(Sequel::Migration) do
51
- def up
52
- #{idx}
53
- end
51
+ def up
52
+ #{idx}
53
+ end
54
54
  end
55
55
  END_MIG
56
- end
57
- end
58
- idxs.to_json
59
- end
56
+ end
57
+ end
58
+ idxs.to_json
59
+ end
60
60
 
61
- def load(database_url, schema)
62
- Sequel.connect(database_url) do |db|
63
- klass = eval(schema)
64
- klass.apply(db, :down)
65
- klass.apply(db, :up)
66
- end
67
- end
61
+ def load(database_url, schema)
62
+ Sequel.connect(database_url) do |db|
63
+ klass = eval(schema)
64
+ klass.apply(db, :down)
65
+ klass.apply(db, :up)
66
+ end
67
+ end
68
68
 
69
- def load_indexes(database_url, indexes)
70
- Sequel.connect(database_url) do |db|
71
- eval(indexes).apply(db, :up)
72
- end
73
- end
69
+ def load_indexes(database_url, indexes)
70
+ Sequel.connect(database_url) do |db|
71
+ eval(indexes).apply(db, :up)
72
+ end
73
+ end
74
74
 
75
- def reset_db_sequences(database_url)
76
- db = Sequel.connect(database_url)
77
- return unless db.respond_to?(:reset_primary_key_sequence)
78
- db.tables.each do |table|
79
- db.reset_primary_key_sequence(table)
80
- end
81
- end
75
+ def reset_db_sequences(database_url)
76
+ db = Sequel.connect(database_url)
77
+ return unless db.respond_to?(:reset_primary_key_sequence)
78
+ db.tables.each do |table|
79
+ db.reset_primary_key_sequence(table)
80
+ end
81
+ end
82
82
  end
83
83
  end
data/lib/taps/server.rb CHANGED
@@ -6,161 +6,161 @@ require 'taps/data_stream'
6
6
 
7
7
  module Taps
8
8
  class Server < Sinatra::Base
9
- use Rack::Auth::Basic do |login, password|
10
- login == Taps::Config.login && password == Taps::Config.password
11
- end
12
-
13
- use Rack::Deflater unless ENV['NO_DEFLATE']
14
-
15
- error do
16
- e = request.env['sinatra.error']
17
- "Taps Server Error: #{e}\n#{e.backtrace}"
18
- end
9
+ use Rack::Auth::Basic do |login, password|
10
+ login == Taps::Config.login && password == Taps::Config.password
11
+ end
12
+
13
+ use Rack::Deflater unless ENV['NO_DEFLATE']
14
+
15
+ error do
16
+ e = request.env['sinatra.error']
17
+ "Taps Server Error: #{e}\n#{e.backtrace}"
18
+ end
19
19
 
20
- before do
21
- major, minor, patch = request.env['HTTP_TAPS_VERSION'].split('.') rescue []
22
- unless "#{major}.#{minor}" == Taps.compatible_version
23
- halt 417, "Taps v#{Taps.compatible_version}.x is required for this server"
24
- end
25
- end
26
-
27
- get '/' do
28
- "hello"
29
- end
30
-
31
- post '/sessions' do
32
- key = rand(9999999999).to_s
33
-
34
- if ENV['NO_DEFAULT_DATABASE_URL']
35
- database_url = request.body.string
36
- else
37
- database_url = Taps::Config.database_url || request.body.string
38
- end
39
-
40
- DbSession.create(:key => key, :database_url => database_url, :started_at => Time.now, :last_access => Time.now)
41
-
42
- "/sessions/#{key}"
43
- end
44
-
45
- post '/sessions/:key/push/table' do
46
- session = DbSession.filter(:key => params[:key]).first
47
- halt 404 unless session
48
-
49
- json = DataStream.parse_json(params[:json])
50
-
51
- size = 0
52
- session.conn do |db|
53
- begin
54
- stream = DataStream.factory(db, json[:state])
55
- size = stream.fetch_remote_in_server(params)
56
- rescue Taps::DataStream::CorruptedData
57
- halt 412
58
- end
59
- end
60
-
61
- # TODO: return the stream's state with the size
62
- size.to_s
63
- end
64
-
65
- post '/sessions/:key/push/reset_sequences' do
66
- session = DbSession.filter(:key => params[:key]).first
67
- halt 404 unless session
68
-
69
- Taps::Utils.schema_bin(:reset_db_sequences, session.database_url)
70
- end
71
-
72
- post '/sessions/:key/push/schema' do
73
- session = DbSession.filter(:key => params[:key]).first
74
- halt 404 unless session
75
-
76
- schema_data = request.body.read
77
- Taps::Utils.load_schema(session.database_url, schema_data)
78
- end
79
-
80
- post '/sessions/:key/push/indexes' do
81
- session = DbSession.filter(:key => params[:key]).first
82
- halt 404 unless session
83
-
84
- index_data = request.body.read
85
- Taps::Utils.load_indexes(session.database_url, index_data)
86
- end
87
-
88
- post '/sessions/:key/pull/schema' do
89
- session = DbSession.filter(:key => params[:key]).first
90
- halt 404 unless session
91
-
92
- Taps::Utils.schema_bin(:dump_table, session.database_url, params[:table_name])
93
- end
94
-
95
- get '/sessions/:key/pull/indexes' do
96
- session = DbSession.filter(:key => params[:key]).first
97
- halt 404 unless session
98
-
99
- content_type 'application/json'
100
- Taps::Utils.schema_bin(:indexes_individual, session.database_url)
101
- end
102
-
103
- get '/sessions/:key/pull/table_names' do
104
- session = DbSession.filter(:key => params[:key]).first
105
- halt 404 unless session
106
-
107
- tables = []
108
- session.conn do |db|
109
- tables = db.tables
110
- end
111
-
112
- content_type 'application/json'
113
- tables.to_json
114
- end
115
-
116
- post '/sessions/:key/pull/table_count' do
117
- session = DbSession.filter(:key => params[:key]).first
118
- halt 404 unless session
119
-
120
- count = 0
121
- session.conn do |db|
122
- count = db[ params[:table].to_sym.identifier ].count
123
- end
124
- count.to_s
125
- end
126
-
127
- post '/sessions/:key/pull/table' do
128
- session = DbSession.filter(:key => params[:key]).first
129
- halt 404 unless session
130
-
131
- encoded_data = nil
132
- stream = nil
133
-
134
- session.conn do |db|
135
- state = JSON.parse(params[:state]).symbolize_keys
136
- stream = Taps::DataStream.factory(db, state)
137
- encoded_data = stream.fetch.first
138
- end
139
-
140
- checksum = Taps::Utils.checksum(encoded_data).to_s
141
- json = { :checksum => checksum, :state => stream.to_hash }.to_json
142
-
143
- content, content_type_value = Taps::Multipart.create do |r|
144
- r.attach :name => :encoded_data,
145
- :payload => encoded_data,
146
- :content_type => 'application/octet-stream'
147
- r.attach :name => :json,
148
- :payload => json,
149
- :content_type => 'application/json'
150
- end
151
-
152
- content_type content_type_value
153
- content
154
- end
155
-
156
- delete '/sessions/:key' do
157
- session = DbSession.filter(:key => params[:key]).first
158
- halt 404 unless session
159
-
160
- session.destroy
161
-
162
- "ok"
163
- end
20
+ before do
21
+ major, minor, patch = request.env['HTTP_TAPS_VERSION'].split('.') rescue []
22
+ unless "#{major}.#{minor}" == Taps.compatible_version
23
+ halt 417, "Taps v#{Taps.compatible_version}.x is required for this server"
24
+ end
25
+ end
26
+
27
+ get '/' do
28
+ "hello"
29
+ end
30
+
31
+ post '/sessions' do
32
+ key = rand(9999999999).to_s
33
+
34
+ if ENV['NO_DEFAULT_DATABASE_URL']
35
+ database_url = request.body.string
36
+ else
37
+ database_url = Taps::Config.database_url || request.body.string
38
+ end
39
+
40
+ DbSession.create(:key => key, :database_url => database_url, :started_at => Time.now, :last_access => Time.now)
41
+
42
+ "/sessions/#{key}"
43
+ end
44
+
45
+ post '/sessions/:key/push/table' do
46
+ session = DbSession.filter(:key => params[:key]).first
47
+ halt 404 unless session
48
+
49
+ json = DataStream.parse_json(params[:json])
50
+
51
+ size = 0
52
+ session.conn do |db|
53
+ begin
54
+ stream = DataStream.factory(db, json[:state])
55
+ size = stream.fetch_remote_in_server(params)
56
+ rescue Taps::DataStream::CorruptedData
57
+ halt 412
58
+ end
59
+ end
60
+
61
+ # TODO: return the stream's state with the size
62
+ size.to_s
63
+ end
64
+
65
+ post '/sessions/:key/push/reset_sequences' do
66
+ session = DbSession.filter(:key => params[:key]).first
67
+ halt 404 unless session
68
+
69
+ Taps::Utils.schema_bin(:reset_db_sequences, session.database_url)
70
+ end
71
+
72
+ post '/sessions/:key/push/schema' do
73
+ session = DbSession.filter(:key => params[:key]).first
74
+ halt 404 unless session
75
+
76
+ schema_data = request.body.read
77
+ Taps::Utils.load_schema(session.database_url, schema_data)
78
+ end
79
+
80
+ post '/sessions/:key/push/indexes' do
81
+ session = DbSession.filter(:key => params[:key]).first
82
+ halt 404 unless session
83
+
84
+ index_data = request.body.read
85
+ Taps::Utils.load_indexes(session.database_url, index_data)
86
+ end
87
+
88
+ post '/sessions/:key/pull/schema' do
89
+ session = DbSession.filter(:key => params[:key]).first
90
+ halt 404 unless session
91
+
92
+ Taps::Utils.schema_bin(:dump_table, session.database_url, params[:table_name])
93
+ end
94
+
95
+ get '/sessions/:key/pull/indexes' do
96
+ session = DbSession.filter(:key => params[:key]).first
97
+ halt 404 unless session
98
+
99
+ content_type 'application/json'
100
+ Taps::Utils.schema_bin(:indexes_individual, session.database_url)
101
+ end
102
+
103
+ get '/sessions/:key/pull/table_names' do
104
+ session = DbSession.filter(:key => params[:key]).first
105
+ halt 404 unless session
106
+
107
+ tables = []
108
+ session.conn do |db|
109
+ tables = db.tables
110
+ end
111
+
112
+ content_type 'application/json'
113
+ tables.to_json
114
+ end
115
+
116
+ post '/sessions/:key/pull/table_count' do
117
+ session = DbSession.filter(:key => params[:key]).first
118
+ halt 404 unless session
119
+
120
+ count = 0
121
+ session.conn do |db|
122
+ count = db[ params[:table].to_sym.identifier ].count
123
+ end
124
+ count.to_s
125
+ end
126
+
127
+ post '/sessions/:key/pull/table' do
128
+ session = DbSession.filter(:key => params[:key]).first
129
+ halt 404 unless session
130
+
131
+ encoded_data = nil
132
+ stream = nil
133
+
134
+ session.conn do |db|
135
+ state = JSON.parse(params[:state]).symbolize_keys
136
+ stream = Taps::DataStream.factory(db, state)
137
+ encoded_data = stream.fetch.first
138
+ end
139
+
140
+ checksum = Taps::Utils.checksum(encoded_data).to_s
141
+ json = { :checksum => checksum, :state => stream.to_hash }.to_json
142
+
143
+ content, content_type_value = Taps::Multipart.create do |r|
144
+ r.attach :name => :encoded_data,
145
+ :payload => encoded_data,
146
+ :content_type => 'application/octet-stream'
147
+ r.attach :name => :json,
148
+ :payload => json,
149
+ :content_type => 'application/json'
150
+ end
151
+
152
+ content_type content_type_value
153
+ content
154
+ end
155
+
156
+ delete '/sessions/:key' do
157
+ session = DbSession.filter(:key => params[:key]).first
158
+ halt 404 unless session
159
+
160
+ session.destroy
161
+
162
+ "ok"
163
+ end
164
164
 
165
165
  end
166
166
  end