Skip to content

Commit 412bd39

Browse files
authored
Merge pull request #28 from ttafsir/release/1.4.0
Release/1.4.0
2 parents 912295b + 2b4e99c commit 412bd39

File tree

14 files changed

+12088
-128
lines changed

14 files changed

+12088
-128
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,6 @@ jobs:
5151
- name: Run tests
5252
run: |
5353
cd tests
54+
echo "Running tests on Ansible $(ansible --version)"
5455
ansible-playbook test_lookup.yml
56+
ansible-playbook test_modules.yml

CHANGELOG.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,25 @@ ttafsir.sqlite_utils Release Notes
44

55
.. contents:: Topics
66

7+
v1.4.0:
8+
========
9+
10+
Release Summary
11+
---------------
12+
13+
This release adds new modules to database and table creation and for loading data into tables.
14+
15+
New Modules
16+
-----------
17+
* `ttafsir.sqlite_utils.create`
18+
* `ttafsir.sqlite_utils.insert`
19+
* `ttafsir.sqlite_utils.insert_json`
20+
21+
Minor Changes
22+
--------------
23+
* move common boilerplate to module_utils to simplify current modules and make it easier to add new modules
24+
* adds new parameter to `ttafsir.sqlite_utils.run_sql` to switch between `query` and `execute` methods
25+
726
v1.3.0:
827
========
928

README.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ A collection of Ansible plugins to manage SQLite databases leveraging the `sqlit
1818
### Modules
1919

2020
* `ttafsir.sqlite_utils.run_sql`: Ansible module to query a sqlite database and return list of dictionaries.
21+
* `ttafsir.sqlite_utils.create`: Ansible module create a table. The module will also create a database file if it doesn't exist.
22+
* `ttafsirsqlite_utils.insert`: Ansible module to insert records into a database table. Supports inserting single and multiple records with a single dictionary or list of dictionaries.
23+
* `ttafsirsqlite_utils.insert_json`: Ansible module to insert records into a database table from JSON files.
2124

2225
#### Lookup Plugins
2326

@@ -26,7 +29,7 @@ A collection of Ansible plugins to manage SQLite databases leveraging the `sqlit
2629

2730
## Usage Examples
2831

29-
### `query` module
32+
### `run_sql` module
3033

3134
```yaml
3235
- name: Fetch data from database
@@ -70,6 +73,31 @@ A collection of Ansible plugins to manage SQLite databases leveraging the `sqlit
7073
- debug: var=query_3.rows_affected
7174
```
7275
76+
### Create a table and insert data
77+
78+
```yaml
79+
- name: Create database
80+
ttafsir.sqlite_utils.create:
81+
db_path: database.db
82+
table: emails
83+
columns: {"email_id": "int", "subject": "str", "body": "str"}
84+
pk: email_id
85+
86+
- name: Insert single records into database
87+
ttafsir.sqlite_utils.insert:
88+
db_path: database.db
89+
table: emails
90+
records: {"email_id": 2, "subject": "Hello World 2", "body": "body of the email"}
91+
92+
- name: Insert from json file
93+
ttafsir.sqlite_utils.insert_json:
94+
db_path: network.db
95+
table: interfaces
96+
file_path: interfaces.json
97+
flatten: true
98+
alter: true
99+
```
100+
73101
### Lookup
74102
75103
```yaml

galaxy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
namespace: ttafsir
33
name: sqlite_utils
4-
version: "1.3.0"
4+
version: "1.4.0"
55
readme: README.md
66

77
authors:

plugins/module_utils/common.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Copyright: (c) 2023, Tafsir Thiam (@ttafsir) <ttafsir@gmail.com>
4+
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5+
from __future__ import absolute_import, division, print_function
6+
7+
__metaclass__ = type
8+
9+
10+
import traceback
11+
from ansible.module_utils.basic import missing_required_lib
12+
from pathlib import Path
13+
14+
15+
IMPORT_ERROR = None
16+
try:
17+
from sqlite_utils import Database
18+
from sqlite_utils.cli import insert_upsert_implementation
19+
20+
HAS_SQLITE_UTILS = True
21+
except ImportError:
22+
HAS_SQLITE_UTILS = False
23+
IMPORT_ERROR = traceback.format_exc()
24+
25+
26+
def insert_arg_spec():
27+
return dict(
28+
db_path=dict(type="str", required=True),
29+
table=dict(type="str", required=True),
30+
records=dict(type="raw", required=True),
31+
pk=dict(type="str", default=None),
32+
column_order=dict(type="list", elements="raw", default=None),
33+
not_null=dict(type="list", elements="raw", default=None),
34+
defaults=dict(type="dict", default=None),
35+
hash_id=dict(type="str", default=None),
36+
alter=dict(type="bool", default=False),
37+
ignore=dict(type="bool", default=False),
38+
replace=dict(type="bool", default=False),
39+
extracts=dict(type="raw", default=None),
40+
conversions=dict(type="dict", default=None),
41+
columns=dict(type="dict", default=None),
42+
)
43+
44+
45+
def file_insert_arg_spec():
46+
return dict(
47+
db_path=dict(type="str", required=True),
48+
table=dict(type="str", required=True),
49+
file_path=dict(type="str", required=True),
50+
pk=dict(type="str", default=None),
51+
empty_null=dict(type="bool", default=False),
52+
lines=dict(type="bool", default=False),
53+
text=dict(type="bool", default=False),
54+
convert=dict(type="dict", default=None),
55+
imports=dict(type="list", elements="raw", default=None),
56+
batch_size=dict(type="int", default=100),
57+
stop_after=dict(type="int", default=None),
58+
alter=dict(type="bool", default=False),
59+
upsert=dict(type="bool", default=False),
60+
ignore=dict(type="bool", default=False),
61+
replace=dict(type="bool", default=False),
62+
truncate=dict(type="bool", default=False),
63+
not_null=dict(type="list", elements="raw", default=None),
64+
analyze=dict(type="bool", default=False),
65+
silent=dict(type="bool", default=False),
66+
)
67+
68+
69+
class SqliteUtilsModule:
70+
def __init__(self, module):
71+
self.module = module
72+
self.result = dict(changed=False, message="")
73+
self.params = module.params
74+
75+
if not HAS_SQLITE_UTILS:
76+
module.fail_json(
77+
msg=missing_required_lib("sqlite-utils"), exception=IMPORT_ERROR
78+
)
79+
80+
def exit_json(self, **kwargs):
81+
self.result.update(**kwargs)
82+
self.module.exit_json(**self.result)
83+
84+
def fail_json(self, msg, **kwargs):
85+
self.result.update(**kwargs)
86+
self.module.fail_json(msg=msg, **self.result)
87+
88+
def get_db(self, **kwargs):
89+
return Database(self.params["db_path"], **kwargs)
90+
91+
92+
class SqliteUtilsInsertModule(SqliteUtilsModule):
93+
def __init__(self, module):
94+
super(SqliteUtilsInsertModule, self).__init__(module)
95+
96+
file_path = module.params["file_path"]
97+
if not Path(file_path).exists():
98+
module.fail_json(
99+
msg=f"File path {file_path} does not exist or is not accessible."
100+
)
101+
self.file = Path(file_path)
102+
103+
def insert_upsert(self, *args, **kwargs):
104+
return insert_upsert_implementation(*args, **kwargs)

0 commit comments

Comments
 (0)