Skip to content

Commit 451d9d6

Browse files
authored
Merge pull request #7 from CrispenGari/table-alters
Table alters
2 parents 091f091 + e18982f commit 451d9d6

25 files changed

+1229
-295
lines changed

Changelog.md

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,51 @@
11
===
2-
Dataloom **`2.0.0`**
2+
Dataloom **`2.1.0`**
33
===
44

55
### Release Notes - `dataloom`
66

7-
We have release the new `dataloom` Version `2.0.0` (`2024-02-12`)
7+
We have release the new `dataloom` Version `2.1.0` (`2024-02-24`)
8+
9+
##### Features
10+
11+
- Connecting to databases using connection `uri` for all the supported dialects
12+
13+
```py
14+
# postgress
15+
pg_loom = Loom(
16+
dialect="postgres",
17+
connection_uri = "postgressql://root:root@localhost:5432/hi",
18+
# ...
19+
)
20+
# mysql
21+
mysql_loom = Loom(
22+
dialect="mysql",
23+
connection_uri = "mysql://root:root@localhost:3306/hi",
24+
# ...
25+
)
26+
27+
# sqlite
28+
sqlite_loom = Loom(
29+
dialect="sqlite",
30+
connection_uri = "sqlite:///hi.db",
31+
# ...
32+
)
33+
```
34+
35+
- updated documentation.
36+
- enable table alterations as an option of `sync` and `connect_and_sync` function.
37+
```py
38+
conn, tables = pg_loom.connect_and_sync([Profile, User], alter=True)
39+
```
40+
> 🥇 **We recommend you to use `drop` or `force` if you are going to change or modify `foreign` and `primary` keys. This is because setting the option `alter` doe not have an effect on `primary` key columns.**
41+
42+
===
43+
44+
# Dataloom **`2.0.0`**
45+
46+
### Release Notes - `dataloom`
47+
48+
We have release the new `dataloom` Version `2.0.0` (`2024-02-21`)
849

950
##### Features
1051

README.md

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
- [Guidelines for Safe Usage](#guidelines-for-safe-usage)
8181
- [Ordering](#ordering)
8282
- [Filters](#filters)
83-
- [Operators](#operators)
83+
- [Operators](#operators)
8484
- [Data Aggregation](#data-aggregation)
8585
- [Aggregation Functions](#aggregation-functions)
8686
- [Utilities](#utilities)
@@ -170,6 +170,18 @@ if __name__ == "__main__":
170170
conn.close()
171171
```
172172

173+
In dataloom you can use connection uris to establish a connection to the database in `postgres` as follows:
174+
175+
```py
176+
pg_loom = Loom(
177+
dialect="postgres",
178+
connection_uri = "postgressql://root:root@localhost:5432/hi",
179+
# ...
180+
)
181+
```
182+
183+
This will establish a connection with `postgres` with the database `hi`.
184+
173185
#### `MySQL`
174186

175187
To establish a connection with a `MySQL` database using `Loom`, you can use the following example:
@@ -198,6 +210,18 @@ if __name__ == "__main__":
198210

199211
```
200212

213+
In dataloom you can use connection uris to establish a connection to the database in `mysql` as follows:
214+
215+
```py
216+
mysql_loom = Loom(
217+
dialect="mysql",
218+
connection_uri = "mysql://root:root@localhost:3306/hi",
219+
# ...
220+
)
221+
```
222+
223+
This will establish a connection with `mysql` with the database `hi`.
224+
201225
#### `SQLite`
202226

203227
To establish a connection with an `SQLite` database using `Loom`, you can use the following example:
@@ -222,6 +246,18 @@ if __name__ == "__main__":
222246
conn.close()
223247
```
224248

249+
In dataloom you can use connection uris to establish a connection to the database in `sqlite` as follows:
250+
251+
```py
252+
sqlite_loom = Loom(
253+
dialect="sqlite",
254+
connection_uri = "sqlite:///hi.db",
255+
# ...
256+
)
257+
```
258+
259+
This will establish a connection with `sqlite` with the database `hi`.
260+
225261
### Dataloom Classes
226262

227263
The following are the list of classes that are available in `dataloom`.
@@ -242,13 +278,21 @@ loom = Loom(
242278
logs_filename="logs.sql",
243279
port=5432,
244280
)
281+
282+
# OR with connection_uri
283+
loom = Loom(
284+
dialect="mysql",
285+
connection_uri = "mysql://root:root@localhost:3306/hi",
286+
# ...
287+
)
245288
```
246289

247290
The `Loom` class takes in the following options:
248291
| Parameter | Description | Value Type | Default Value | Required |
249292
| --------------- | --------------------------------------------------------------------------------- | --------------- | -------------- | -------- |
293+
| `connection_uri` | The connection `uri` for the specified dialect. | `str` or `None` | `None` | `No` |
250294
| `dialect` | Dialect for the database connection. Options are `mysql`, `postgres`, or `sqlite` | `str` or `None` | `None` | `Yes` |
251-
| `database` | Name of the database for `mysql` and `postgres`, filename for `sqlite` | `str` or `None` | `None` | `Yes` |
295+
| `database` | Name of the database for `mysql` and `postgres`, filename for `sqlite` | `str` or `None` | `None` | `No` |
252296
| `password` | Password for the database user (only for `mysql` and `postgres`) | `str` or `None` | `None` | `No` |
253297
| `user` | Database user (only for `mysql` and `postgres`) | `str` or `None` | `None` | `No` |
254298
| `host` | Database host (only for `mysql` and `postgres`) | `str` or `None` | `localhost` | `No` |
@@ -620,6 +664,8 @@ The method returns a list of table names that have been created or that exist in
620664
| `force` | Forcefully drop tables during syncing or not. | `bool` | `False` |
621665
| `alter` | Alter tables instead of dropping them during syncing or not. | `bool` | `False` |
622666

667+
> 🥇 **We recommend you to use `drop` or `force` if you are going to change or modify `foreign` and `primary` keys. This is because setting the option `alter` doe not have an effect on `primary` key columns.**
668+
623669
> We've noticed two steps involved in starting to work with our `orm`. Initially, you need to create a connection and then synchronize the tables in another step.
624670
625671
#### 2. The `connect_and_sync` method.

dataloom/conn/__init__.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from os import PathLike
55
from typing_extensions import TypeAlias
66
from typing import Optional
7-
7+
from urllib.parse import urlparse, parse_qs
88
from dataloom.constants import instances
99

1010

@@ -87,3 +87,21 @@ def get_connection_options(**kwargs):
8787
raise UnsupportedDialectException(
8888
"The dialect passed is not supported the supported dialects are: {'postgres', 'mysql', 'sqlite'}"
8989
)
90+
91+
@staticmethod
92+
def get_mysql_uri_connection_options(uri: str) -> dict:
93+
components = urlparse(uri)
94+
user = components.username
95+
password = components.password
96+
hostname = components.hostname
97+
port = components.port
98+
db = components.path.lstrip("/")
99+
100+
return {
101+
"user": user,
102+
"password": password,
103+
"host": hostname,
104+
"port": port,
105+
"database": db,
106+
**parse_qs(components.query),
107+
}

dataloom/exceptions/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ class InvalidPropertyException(Exception):
1010
pass
1111

1212

13+
class InvalidDropOperationException(Exception):
14+
pass
15+
16+
17+
class InvalidConnectionURI(Exception):
18+
pass
19+
20+
1321
class TooManyPkException(Exception):
1422
pass
1523

dataloom/keys.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Configuration file for unit testing.
2+
3+
14
push = True
25

36

@@ -6,18 +9,37 @@ class PgConfig:
69
password = "postgres"
710
database = "postgres"
811
user = "postgres"
12+
host = "localhost"
13+
port = 5432
14+
connection_uri = f"postgresql://{user}:{password}@{host}:{port}/{database}"
915
else:
1016
database = "postgres"
1117
user = "postgres"
1218
password = "root"
19+
host = "localhost"
20+
port = 5432
21+
connection_uri = f"postgresql://{user}:{password}@{host}:{port}/{database}"
1322

1423

1524
class MySQLConfig:
1625
if push:
1726
password = "testrootpass"
1827
database = "testdb"
1928
user = "root"
29+
host = "0.0.0.0"
30+
port = 3306
31+
connection_uri = f"mysql://{user}:{password}@{host}:{port}/{database}"
2032
else:
2133
database = "hi"
2234
user = "root"
2335
password = "root"
36+
host = "localhost"
37+
port = 3306
38+
connection_uri = f"mysql://{user}:{password}@{host}:{port}/{database}"
39+
40+
41+
class SQLiteConfig:
42+
if push:
43+
connection_uri = "sqlite:///hi.db"
44+
else:
45+
connection_uri = "sqlite:///hi.db"

0 commit comments

Comments
 (0)