|
4 | 4 | from typing import Any, Optional |
5 | 5 |
|
6 | 6 | from sqlalchemy import Table, Index, Column |
7 | | -from sqlalchemy import Integer, String, DateTime, Boolean, Text, Float, Enum |
| 7 | +from sqlalchemy import Integer, String, DateTime, Boolean, Text, Float, Enum, Text |
8 | 8 | from sqlalchemy import MetaData, DDL |
9 | 9 | from sqlalchemy import create_engine, Engine |
10 | 10 | from sqlalchemy import select, insert, update, func, asc |
@@ -46,7 +46,6 @@ def _define_db_tables(self) -> None: |
46 | 46 | Index("idx_log_username", "username"), |
47 | 47 | Index("idx_log_created", "created"), |
48 | 48 | ) |
49 | | - |
50 | 49 | self.user_table = Table( |
51 | 50 | "users", |
52 | 51 | self.metadata_obj, |
@@ -78,6 +77,7 @@ def _define_db_tables(self) -> None: |
78 | 77 | Column("book_id", Integer), |
79 | 78 | Column("category_type", Enum(CategoryType), default=CategoryType.EXPENSE), |
80 | 79 | Column("title", String(255)), |
| 80 | + Column("options", Text, default=''), |
81 | 81 | Column("deleted", Boolean, default=False), |
82 | 82 | Index("idx_categories_parent_id", "parent_id"), |
83 | 83 | Index("idx_categories_book_title", "book_id", "title"), |
@@ -116,6 +116,18 @@ def _define_db_tables(self) -> None: |
116 | 116 |
|
117 | 117 | def _alter_schema(self) -> None: |
118 | 118 | """Alter database schema, if necessary.""" |
| 119 | + column = Column("options", Text, default='') |
| 120 | + column_name = column.compile(dialect=self.engine.dialect) |
| 121 | + column_type = column.type.compile(self.engine.dialect) |
| 122 | + try: |
| 123 | + with self.engine.connect() as connection: |
| 124 | + connection.execute(DDL( |
| 125 | + f"ALTER TABLE categories ADD COLUMN {column_name} " |
| 126 | + f"{column_type} DEFAULT('')" |
| 127 | + )) |
| 128 | + connection.commit() |
| 129 | + except OperationalError: |
| 130 | + pass |
119 | 131 | column = Column("category_type", Enum(CategoryType), default=CategoryType.EXPENSE) |
120 | 132 | column_name = column.compile(dialect=self.engine.dialect) |
121 | 133 | column_type = column.type.compile(self.engine.dialect) |
|
0 commit comments