|
| 1 | +import logging |
| 2 | +from dataclasses import dataclass |
| 3 | + |
| 4 | +from pydantic import Field |
| 5 | +from sqlalchemy import text |
| 6 | + |
| 7 | +import src.adapters.db as db |
| 8 | +import src.adapters.db.flask_db as flask_db |
| 9 | +from src.data_migration.data_migration_blueprint import data_migration_blueprint |
| 10 | +from src.util.env_config import PydanticBaseEnvConfig |
| 11 | + |
| 12 | +logger = logging.getLogger(__name__) |
| 13 | + |
| 14 | + |
| 15 | +class ForeignTableConfig(PydanticBaseEnvConfig): |
| 16 | + is_local_foreign_table: bool = Field(False) |
| 17 | + |
| 18 | + |
| 19 | +@dataclass |
| 20 | +class Column: |
| 21 | + column_name: str |
| 22 | + postgres_type: str |
| 23 | + |
| 24 | + is_nullable: bool = True |
| 25 | + is_primary_key: bool = False |
| 26 | + |
| 27 | + |
| 28 | +OPPORTUNITY_COLUMNS: list[Column] = [ |
| 29 | + Column("OPPORTUNITY_ID", "numeric(20)", is_nullable=False, is_primary_key=True), |
| 30 | + Column("OPPNUMBER", "character varying (40)"), |
| 31 | + Column("REVISION_NUMBER", "numeric(20)"), |
| 32 | + Column("OPPTITLE", "character varying (255)"), |
| 33 | + Column("OWNINGAGENCY", "character varying (255)"), |
| 34 | + Column("PUBLISHERUID", "character varying (255)"), |
| 35 | + Column("LISTED", "CHAR(1)"), |
| 36 | + Column("OPPCATEGORY", "CHAR(1)"), |
| 37 | + Column("INITIAL_OPPORTUNITY_ID", "numeric(20)"), |
| 38 | + Column("MODIFIED_COMMENTS", "character varying (2000)"), |
| 39 | + Column("CREATED_DATE", "DATE"), |
| 40 | + Column("LAST_UPD_DATE", "DATE"), |
| 41 | + Column("CREATOR_ID", "character varying (50)"), |
| 42 | + Column("LAST_UPD_ID", "character varying (50)"), |
| 43 | + Column("FLAG_2006", "CHAR(1)"), |
| 44 | + Column("CATEGORY_EXPLANATION", "character varying (255)"), |
| 45 | + Column("PUBLISHER_PROFILE_ID", "numeric(20)"), |
| 46 | + Column("IS_DRAFT", "character varying (1)"), |
| 47 | +] |
| 48 | + |
| 49 | + |
| 50 | +@data_migration_blueprint.cli.command( |
| 51 | + "setup-foreign-tables", help="Setup the foreign tables for connecting to the Oracle database" |
| 52 | +) |
| 53 | +@flask_db.with_db_session() |
| 54 | +def setup_foreign_tables(db_session: db.Session) -> None: |
| 55 | + logger.info("Beginning setup of foreign Oracle tables") |
| 56 | + |
| 57 | + config = ForeignTableConfig() |
| 58 | + |
| 59 | + with db_session.begin(): |
| 60 | + _run_create_table_commands(db_session, config) |
| 61 | + |
| 62 | + logger.info("Successfully ran setup-foreign-tables") |
| 63 | + |
| 64 | + |
| 65 | +def build_sql(table_name: str, columns: list[Column], is_local: bool) -> str: |
| 66 | + """ |
| 67 | + Build the SQL for creating a possibly foreign data table. If running |
| 68 | + with is_local, it instead creates a regular table. |
| 69 | +
|
| 70 | + Assume you have a table with two columns, an "ID" primary key column, and a "description" text column, |
| 71 | + you would call this as:: |
| 72 | +
|
| 73 | + build_sql("EXAMPLE_TABLE", [Column("ID", "integer", is_nullable=False, is_primary_key=True), Column("DESCRIPTION", "text")], is_local) |
| 74 | +
|
| 75 | + Depending on whether the is_local bool is true or false would give two different outputs. |
| 76 | +
|
| 77 | + is_local is True:: |
| 78 | +
|
| 79 | + CREATE TABLE IF NOT EXISTS foreign_example_table (ID integer CONSTRAINT EXAMPLE_TABLE_pkey PRIMARY KEY NOT NULL,DESCRIPTION text) |
| 80 | +
|
| 81 | + is_local is False:: |
| 82 | +
|
| 83 | + CREATE FOREIGN TABLE IF NOT EXISTS foreign_example_table (ID integer OPTIONS (key 'true') NOT NULL,DESCRIPTION text) SERVER grants OPTIONS (schema 'EGRANTSADMIN', table 'EXAMPLE_TABLE') |
| 84 | + """ |
| 85 | + |
| 86 | + column_sql_parts = [] |
| 87 | + for column in columns: |
| 88 | + column_sql = f"{column.column_name} {column.postgres_type}" |
| 89 | + |
| 90 | + # Primary keys are defined as constraints in a regular table |
| 91 | + # and as options in a foreign data table |
| 92 | + if column.is_primary_key and is_local: |
| 93 | + column_sql += f" CONSTRAINT {table_name}_pkey PRIMARY KEY" |
| 94 | + elif column.is_primary_key and not is_local: |
| 95 | + column_sql += " OPTIONS (key 'true')" |
| 96 | + |
| 97 | + if not column.is_nullable: |
| 98 | + column_sql += " NOT NULL" |
| 99 | + |
| 100 | + column_sql_parts.append(column_sql) |
| 101 | + |
| 102 | + create_table_command = "CREATE FOREIGN TABLE IF NOT EXISTS" |
| 103 | + if is_local: |
| 104 | + # Don't make a foreign table if running locally |
| 105 | + create_table_command = "CREATE TABLE IF NOT EXISTS" |
| 106 | + |
| 107 | + create_command_suffix = ( |
| 108 | + f" SERVER grants OPTIONS (schema 'EGRANTSADMIN', table '{table_name}')" # noqa: B907 |
| 109 | + ) |
| 110 | + if is_local: |
| 111 | + # We don't want the config at the end if we're running locally so unset it |
| 112 | + create_command_suffix = "" |
| 113 | + |
| 114 | + return f"{create_table_command} foreign_{table_name.lower()} ({','.join(column_sql_parts)}){create_command_suffix}" |
| 115 | + |
| 116 | + |
| 117 | +def _run_create_table_commands(db_session: db.Session, config: ForeignTableConfig) -> None: |
| 118 | + db_session.execute( |
| 119 | + text(build_sql("TOPPORTUNITY", OPPORTUNITY_COLUMNS, config.is_local_foreign_table)) |
| 120 | + ) |
0 commit comments