Skip to content

Commit

Permalink
Make sql test connection work with saved Database instance (#694)
Browse files Browse the repository at this point in the history
Fix #596
  • Loading branch information
xrmx authored and mistercrunch committed Sep 17, 2016
1 parent 69d37d8 commit afa1f09
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
6 changes: 5 additions & 1 deletion caravel/templates/caravel/models/database/macros.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@

var data = {};
try{
data = JSON.stringify({ uri: $("#sqlalchemy_uri").val(), extras: JSON.parse($("#extra").val()) })
data = JSON.stringify({
uri: $("#sqlalchemy_uri").val(),
name: $('#database_name').val(),
extras: JSON.parse($("#extra").val())
})
} catch(parse_error){
alert("Malformed JSON in the extras field: " + parse_error);
return false
Expand Down
10 changes: 10 additions & 0 deletions caravel/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1251,6 +1251,16 @@ def testconn(self):
"""Tests a sqla connection"""
try:
uri = request.json.get('uri')
db_name = request.json.get('name')
if db_name and ':XXXXXXXXXX@' in uri:
database = (
db.session()
.query(models.Database)
.filter_by(database_name=db_name).first()
)
if database is not None:
uri = database.sqlalchemy_uri_decrypted

connect_args = (
request.json
.get('extras', {})
Expand Down
12 changes: 12 additions & 0 deletions tests/core_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,18 @@ def test_misc(self):
assert self.client.get('/health').data.decode('utf-8') == "OK"
assert self.client.get('/ping').data.decode('utf-8') == "OK"

def test_testconn(self):
data = json.dumps({'uri': 'sqlite:////tmp/caravel_unittests.db'})
response = self.client.post('/caravel/testconn', data=data, content_type='application/json')
assert response.status_code == 200

data = json.dumps({
'uri': 'postgresql+psycopg2://foo:[email protected]/bar',
'name': 'main'
})
response = self.client.post('/caravel/testconn', data=data, content_type='application/json')
assert response.status_code == 200

def test_warm_up_cache(self):
slice = db.session.query(models.Slice).first()
resp = self.client.get(
Expand Down

0 comments on commit afa1f09

Please sign in to comment.