Skip to content

Commit b7b0d29

Browse files
author
Eli Yarson
committed
feat: add column update
1 parent 94f7bc0 commit b7b0d29

File tree

5 files changed

+753
-4
lines changed

5 files changed

+753
-4
lines changed

snowflake_utils/__main__.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
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
57

68
app = typer.Typer()
79

@@ -27,5 +29,32 @@ def copy(
2729
)
2830

2931

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