Skip to content

Commit 7b5478c

Browse files
authored
Merge pull request #3 from remoteoss/add-column-update
feat: add column update
2 parents 94f7bc0 + f4dfd30 commit 7b5478c

File tree

6 files changed

+757
-6
lines changed

6 files changed

+757
-6
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
tests:
1515
strategy:
1616
matrix:
17-
python-version: ["3.10", "3.11", "3.12"]
17+
python-version: ["3.10.9", "3.11.5", "3.12.2"]
1818
poetry-version: ["1.8.2"]
1919
runs-on: ubuntu-latest
2020
steps:
@@ -71,4 +71,4 @@ jobs:
7171
- name: Python Semantic Release
7272
uses: python-semantic-release/python-semantic-release@master
7373
with:
74-
github_token: ${{ secrets.GITHUB_TOKEN }}
74+
github_token: ${{ secrets.GITHUB_TOKEN }}

snowflake_utils/__main__.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import typer
22
from typing_extensions import Annotated
33

4-
from .models import FileFormat, InlineFileFormat, Table
4+
from .models import FileFormat, InlineFileFormat, Table, Schema, Column
5+
from .queries import connect
6+
import logging
7+
import os
58

69
app = typer.Typer()
710

@@ -27,5 +30,33 @@ def copy(
2730
)
2831

2932

33+
@app.command()
34+
def mass_single_column_update(
35+
schema: Annotated[str, typer.Argument()],
36+
target_column: Annotated[str, typer.Argument()],
37+
new_column: Annotated[str, typer.Argument()],
38+
data_type: Annotated[str, typer.Argument()],
39+
) -> None:
40+
db_schema = Schema(name=schema)
41+
target_column = Column(name=target_column, data_type=data_type)
42+
new_column = Column(name=new_column, data_type=data_type)
43+
log_level = os.getenv("LOG_LEVEL", "INFO")
44+
logging.getLogger("snowflake-utils").setLevel(log_level)
45+
with connect() as conn, conn.cursor() as cursor:
46+
tables = db_schema.get_tables(cursor=cursor)
47+
for table in tables:
48+
columns = table.get_columns(cursor=cursor)
49+
column_names = [str.upper(column.name) for column in columns]
50+
if (
51+
str.upper(target_column.name) in column_names
52+
and str.upper(new_column.name) in column_names
53+
):
54+
table.single_column_update(
55+
cursor=cursor, target_column=target_column, new_column=new_column
56+
)
57+
else:
58+
logging.debug("One or both of the columns don't exist in the table")
59+
60+
3061
if __name__ == "__main__":
3162
app()

snowflake_utils/models.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,28 @@ class Column(BaseModel):
6464
data_type: str
6565

6666

67+
class Schema(BaseModel):
68+
name: str
69+
database: str | None = None
70+
71+
@property
72+
def fully_qualified_name(self):
73+
if self.database:
74+
return f"{self.database}.{self.name}"
75+
else:
76+
return self.name
77+
78+
def get_tables(self, cursor: SnowflakeCursor):
79+
cursor.execute(f"show tables in schema {self.fully_qualified_name};")
80+
data = cursor.execute(
81+
'select "name", "database_name", "schema_name" FROM TABLE(RESULT_SCAN(LAST_QUERY_ID()));'
82+
).fetchall()
83+
return [
84+
Table(name=name, schema_=schema, database=database)
85+
for (name, database, schema, *_) in data
86+
]
87+
88+
6789
class Table(BaseModel):
6890
name: str
6991
schema_: str
@@ -300,6 +322,17 @@ def drop(self, cursor: SnowflakeCursor) -> None:
300322
logging.debug(f"Dropping table:{self.schema_}.{self.name}")
301323
cursor.execute(f"drop table {self.schema_}.{self.name}")
302324

325+
def single_column_update(
326+
self, cursor: SnowflakeCursor, target_column: Column, new_column: Column
327+
):
328+
"""Updates the value of one column with the value of another column in the same table."""
329+
logging.debug(
330+
f"Updating the value of {target_column.name} with {new_column.name} in the table {self.name}"
331+
)
332+
cursor.execute(
333+
f"UPDATE {self.schema_}.{self.name} SET {target_column.name} = {new_column.name};"
334+
)
335+
303336

304337
def _possibly_cast(s: str, old_column_type: str, new_column_type: str) -> str:
305338
if old_column_type == "VARIANT" and new_column_type != "VARIANT":

0 commit comments

Comments
 (0)