File tree Expand file tree Collapse file tree 3 files changed +24
-3
lines changed Expand file tree Collapse file tree 3 files changed +24
-3
lines changed Original file line number Diff line number Diff line change @@ -454,13 +454,16 @@ def get_list_database(self):
454
454
"""
455
455
return list (self .query ("SHOW DATABASES" ).get_points ())
456
456
457
- def create_database (self , dbname ):
457
+ def create_database (self , dbname , if_not_exists = False ):
458
458
"""Create a new database in InfluxDB.
459
459
460
460
:param dbname: the name of the database to create
461
461
:type dbname: str
462
462
"""
463
- self .query ("CREATE DATABASE \" %s\" " % dbname )
463
+ if if_not_exists :
464
+ self .query ("CREATE DATABASE IF NOT EXISTS \" %s\" " % dbname )
465
+ else :
466
+ self .query ("CREATE DATABASE \" %s\" " % dbname )
464
467
465
468
def drop_database (self , dbname ):
466
469
"""Drop a database from InfluxDB.
Original file line number Diff line number Diff line change @@ -413,6 +413,19 @@ def test_create_database(self):
413
413
'create database "new_db"'
414
414
)
415
415
416
+ def test_create_database_with_exist_check (self ):
417
+ with requests_mock .Mocker () as m :
418
+ m .register_uri (
419
+ requests_mock .GET ,
420
+ "http://localhost:8086/query" ,
421
+ text = '{"results":[{}]}'
422
+ )
423
+ self .cli .create_database ('new_db' , if_not_exists = True )
424
+ self .assertEqual (
425
+ m .last_request .qs ['q' ][0 ],
426
+ 'create database if not exists "new_db"'
427
+ )
428
+
416
429
def test_create_numeric_named_database (self ):
417
430
with requests_mock .Mocker () as m :
418
431
m .register_uri (
Original file line number Diff line number Diff line change @@ -131,7 +131,12 @@ def test_create_database(self):
131
131
[{'name' : 'new_db_1' }, {'name' : 'new_db_2' }]
132
132
)
133
133
134
- def test_create_database_fails (self ):
134
+ def test_create_database_twice_if_not_exist (self ):
135
+ self .assertIsNone (self .cli .create_database ('new_db' ))
136
+ self .assertIsNone (
137
+ self .cli .create_database ('new_db' , if_not_exists = True ))
138
+
139
+ def test_create_database_twice_fails (self ):
135
140
self .assertIsNone (self .cli .create_database ('new_db' ))
136
141
with self .assertRaises (InfluxDBClientError ) as ctx :
137
142
self .cli .create_database ('new_db' )
You can’t perform that action at this time.
0 commit comments