1919from typing import Any , Optional
2020
2121# pylint: disable=no-self-argument
22- from pydantic import BaseSettings , PostgresDsn , validator
22+ from pydantic import BaseSettings , validator
2323
2424
2525class Settings (BaseSettings ):
@@ -31,96 +31,40 @@ class Settings(BaseSettings):
3131 and synthetic values inserted.
3232
3333 Attributes:
34- src_host_name (str):
35- An element (host-name) of connection parameter
36- src_port (int):
37- Connection port eg. 5432
38- src_user_name (str) :
39- Connection username e.g. `postgres` or `myuser@mydb`
40- src_password (str) :
41- Connection password
42- src_db_name (str) :
43- Connection database e.g. "postgres"
44- src_ssl_required (bool) :
45- Flag `True` if db requires SSL
46-
47- dst_host_name (str):
48- Connection host-name to destination db
49- dst_port (int) :
50- Connection port eg. 5432
51- dst_user_name (str) :
52- Connection username e.g. `postgres` or `myuser@mydb`
53- dst_password (str) :
54- Connection password
55- dst_db_name (str) :
56- Connection database e.g. `postgres`
57- dst_ssl_required (bool) :
58- Flag `True` if db requires SSL
34+ src_dsn (str) :
35+ A DSN for connecting to the source database.
36+
37+ src_schema (str) :
38+ The source database schema to use, if applicable.
39+
40+ dst_dsn (str) :
41+ A DSN for connecting to the destination database.
42+
43+ dst_schema (str) :
44+ The destination database schema to use, if applicable.
5945 """
6046
61- # Connection parameters for the source PostgreSQL database. See also
47+ # Connection parameters for the source database. See also
6248 # https://www.postgresql.org/docs/11/libpq-connect.html#LIBPQ-PARAMKEYWORDS
63- src_host_name : Optional [str ] # e.g. "mydb.mydomain.com" or "0.0.0.0"
64- src_port : int = 5432
65- src_user_name : Optional [str ] # e.g. "postgres" or "myuser@mydb"
66- src_password : Optional [str ]
67- src_db_name : Optional [str ]
68- src_ssl_required : bool = False # whether the db requires SSL
69- src_schema : Optional [str ]
49+ src_dsn : Optional [str ]
50+ dst_dsn : Optional [str ]
7051
71- # Connection parameters for the destination PostgreSQL database.
72- dst_host_name : Optional [
73- str
74- ] # Connection parameter e.g. "mydb.mydomain.com" or "0.0.0.0"
75- dst_port : int = 5432
76- dst_user_name : Optional [str ] # e.g. "postgres" or "myuser@mydb"
77- dst_password : Optional [str ]
78- dst_db_name : Optional [str ]
52+ src_schema : Optional [str ]
7953 dst_schema : Optional [str ]
80- dst_ssl_required : bool = False # whether the db requires SSL
81-
82- # These are calculated so do not provide them explicitly
83- src_postgres_dsn : Optional [PostgresDsn ]
84- dst_postgres_dsn : Optional [PostgresDsn ]
85-
86- @validator ("src_postgres_dsn" , pre = True )
87- def validate_src_postgres_dsn (
88- cls , _ : Optional [PostgresDsn ], values : Any
89- ) -> Optional [str ]:
90- """Create and validate the source db data source name."""
91- return cls .check_postgres_dsn (_ , values , "src" )
92-
93- @validator ("dst_postgres_dsn" , pre = True )
94- def validate_dst_postgres_dsn (
95- cls , _ : Optional [PostgresDsn ], values : Any
96- ) -> Optional [str ]:
97- """Create and validate the destination db data source name."""
98- return cls .check_postgres_dsn (_ , values , "dst" )
99-
100- @staticmethod
101- def check_postgres_dsn (
102- _ : Optional [PostgresDsn ], values : Any , prefix : str
103- ) -> Optional [str ]:
104- """Build a DSN string from the host, db name, port, username and password."""
105- # We want to build the Data Source Name ourselves so none should be provided
106- if _ :
107- raise ValueError ("postgres_dsn should not be provided" )
108-
109- user = values [f"{ prefix } _user_name" ]
110- password = values [f"{ prefix } _password" ]
111- host = values [f"{ prefix } _host_name" ]
112- port = values [f"{ prefix } _port" ]
113- db_name = values [f"{ prefix } _db_name" ]
114-
115- if user and password and host and port and db_name :
116- dsn = f"postgresql://{ user } :{ password } @{ host } :{ port } /{ db_name } "
117-
118- if values [f"{ prefix } _ssl_required" ]:
119- return dsn + "?sslmode=require"
120-
121- return dsn
122-
123- return None
54+
55+ @validator ("src_dsn" )
56+ def validate_src_dsn (cls , dsn : Optional [str ], values : Any ) -> Optional [str ]:
57+ """Create and validate the source DB DSN."""
58+ if dsn and dsn .startswith ("mariadb" ):
59+ assert values .get ("src_schema" ) is None
60+ return dsn
61+
62+ @validator ("dst_dsn" )
63+ def validate_dst_dsn (cls , dsn : Optional [str ], values : Any ) -> Optional [str ]:
64+ """Create and validate the destination DB DSN."""
65+ if dsn and dsn .startswith ("mariadb" ):
66+ assert values .get ("dst_schema" ) is None
67+ return dsn
12468
12569 @dataclass
12670 class Config :
0 commit comments