|
| 1 | +import json |
| 2 | + |
1 | 3 | import pytest
|
2 | 4 | from sqlalchemy import text
|
3 | 5 |
|
@@ -123,3 +125,196 @@ def test_fix_capitalization_migration() -> None:
|
123 | 125 | assert len(results) == 2
|
124 | 126 | assert results[0].external_user_group_ids == ["group1", "group2", "group3"]
|
125 | 127 | assert results[1].external_user_group_ids == ["upper1", "upper2", "upper3"]
|
| 128 | + |
| 129 | + |
| 130 | +def test_jira_connector_migration() -> None: |
| 131 | + """Test that the da42808081e3 migration correctly updates Jira connector configurations""" |
| 132 | + # Reset the database and run migrations up to the migration before the Jira connector change |
| 133 | + downgrade_postgres( |
| 134 | + database="postgres", config_name="alembic", revision="base", clear_data=True |
| 135 | + ) |
| 136 | + upgrade_postgres( |
| 137 | + database="postgres", |
| 138 | + config_name="alembic", |
| 139 | + # Upgrade it to the migration before the Jira connector change |
| 140 | + revision="f13db29f3101", |
| 141 | + ) |
| 142 | + |
| 143 | + # Insert test data with various Jira connector configurations |
| 144 | + test_data = [ |
| 145 | + { |
| 146 | + "id": 1, |
| 147 | + "name": "jira_connector_1", |
| 148 | + "source": "JIRA", |
| 149 | + "connector_specific_config": { |
| 150 | + "jira_project_url": "https://example.atlassian.net/projects/PROJ", |
| 151 | + "comment_email_blacklist": ["test@example.com"], |
| 152 | + "batch_size": 100, |
| 153 | + "labels_to_skip": ["skip-me"], |
| 154 | + }, |
| 155 | + }, |
| 156 | + { |
| 157 | + "id": 2, |
| 158 | + "name": "jira_connector_2", |
| 159 | + "source": "JIRA", |
| 160 | + "connector_specific_config": { |
| 161 | + "jira_project_url": "https://other.atlassian.net/projects/OTHER" |
| 162 | + }, |
| 163 | + }, |
| 164 | + { |
| 165 | + "id": 3, |
| 166 | + "name": "jira_connector_3", |
| 167 | + "source": "JIRA", |
| 168 | + "connector_specific_config": { |
| 169 | + "jira_project_url": "https://example.atlassian.net/projects/TEST", |
| 170 | + "batch_size": 50, |
| 171 | + }, |
| 172 | + }, |
| 173 | + ] |
| 174 | + |
| 175 | + # Insert the test data |
| 176 | + with get_session_context_manager() as db_session: |
| 177 | + for connector in test_data: |
| 178 | + db_session.execute( |
| 179 | + text( |
| 180 | + """ |
| 181 | + INSERT INTO connector ( |
| 182 | + id, |
| 183 | + name, |
| 184 | + source, |
| 185 | + connector_specific_config |
| 186 | + ) |
| 187 | + VALUES ( |
| 188 | + :id, |
| 189 | + :name, |
| 190 | + :source, |
| 191 | + :config |
| 192 | + ) |
| 193 | + """ |
| 194 | + ), |
| 195 | + { |
| 196 | + "id": connector["id"], |
| 197 | + "name": connector["name"], |
| 198 | + "source": connector["source"], |
| 199 | + "config": json.dumps(connector["connector_specific_config"]), |
| 200 | + }, |
| 201 | + ) |
| 202 | + db_session.commit() |
| 203 | + |
| 204 | + # Verify the data was inserted correctly |
| 205 | + with get_session_context_manager() as db_session: |
| 206 | + results = db_session.execute( |
| 207 | + text( |
| 208 | + """ |
| 209 | + SELECT id, connector_specific_config |
| 210 | + FROM connector |
| 211 | + WHERE source = 'JIRA' |
| 212 | + ORDER BY id |
| 213 | + """ |
| 214 | + ) |
| 215 | + ).fetchall() |
| 216 | + |
| 217 | + # Verify initial state |
| 218 | + assert len(results) == 3 |
| 219 | + assert ( |
| 220 | + results[0].connector_specific_config |
| 221 | + == test_data[0]["connector_specific_config"] |
| 222 | + ) |
| 223 | + assert ( |
| 224 | + results[1].connector_specific_config |
| 225 | + == test_data[1]["connector_specific_config"] |
| 226 | + ) |
| 227 | + assert ( |
| 228 | + results[2].connector_specific_config |
| 229 | + == test_data[2]["connector_specific_config"] |
| 230 | + ) |
| 231 | + |
| 232 | + # Run migrations again to apply the Jira connector change |
| 233 | + upgrade_postgres( |
| 234 | + database="postgres", config_name="alembic", revision="da42808081e3" |
| 235 | + ) |
| 236 | + |
| 237 | + # Verify the upgrade was applied correctly |
| 238 | + with get_session_context_manager() as db_session: |
| 239 | + results = db_session.execute( |
| 240 | + text( |
| 241 | + """ |
| 242 | + SELECT id, connector_specific_config |
| 243 | + FROM connector |
| 244 | + WHERE source = 'JIRA' |
| 245 | + ORDER BY id |
| 246 | + """ |
| 247 | + ) |
| 248 | + ).fetchall() |
| 249 | + |
| 250 | + # Verify new format |
| 251 | + assert len(results) == 3 |
| 252 | + |
| 253 | + # First connector - full config |
| 254 | + config_0 = results[0].connector_specific_config |
| 255 | + assert config_0["jira_base_url"] == "https://example.atlassian.net" |
| 256 | + assert config_0["project_key"] == "PROJ" |
| 257 | + assert config_0["comment_email_blacklist"] == ["test@example.com"] |
| 258 | + assert config_0["batch_size"] == 100 |
| 259 | + assert config_0["labels_to_skip"] == ["skip-me"] |
| 260 | + |
| 261 | + # Second connector - minimal config |
| 262 | + config_1 = results[1].connector_specific_config |
| 263 | + assert config_1["jira_base_url"] == "https://other.atlassian.net" |
| 264 | + assert config_1["project_key"] == "OTHER" |
| 265 | + assert "comment_email_blacklist" not in config_1 |
| 266 | + assert "batch_size" not in config_1 |
| 267 | + assert "labels_to_skip" not in config_1 |
| 268 | + |
| 269 | + # Third connector - partial config |
| 270 | + config_2 = results[2].connector_specific_config |
| 271 | + assert config_2["jira_base_url"] == "https://example.atlassian.net" |
| 272 | + assert config_2["project_key"] == "TEST" |
| 273 | + assert config_2["batch_size"] == 50 |
| 274 | + assert "comment_email_blacklist" not in config_2 |
| 275 | + assert "labels_to_skip" not in config_2 |
| 276 | + |
| 277 | + # Test downgrade path |
| 278 | + downgrade_postgres( |
| 279 | + database="postgres", config_name="alembic", revision="f13db29f3101" |
| 280 | + ) |
| 281 | + |
| 282 | + # Verify the downgrade was applied correctly |
| 283 | + with get_session_context_manager() as db_session: |
| 284 | + results = db_session.execute( |
| 285 | + text( |
| 286 | + """ |
| 287 | + SELECT id, connector_specific_config |
| 288 | + FROM connector |
| 289 | + WHERE source = 'JIRA' |
| 290 | + ORDER BY id |
| 291 | + """ |
| 292 | + ) |
| 293 | + ).fetchall() |
| 294 | + |
| 295 | + # Verify reverted to old format |
| 296 | + assert len(results) == 3 |
| 297 | + |
| 298 | + # First connector - full config |
| 299 | + config_0 = results[0].connector_specific_config |
| 300 | + assert ( |
| 301 | + config_0["jira_project_url"] |
| 302 | + == "https://example.atlassian.net/projects/PROJ" |
| 303 | + ) |
| 304 | + assert config_0["comment_email_blacklist"] == ["test@example.com"] |
| 305 | + assert config_0["batch_size"] == 100 |
| 306 | + assert config_0["labels_to_skip"] == ["skip-me"] |
| 307 | + |
| 308 | + # Second connector - minimal config |
| 309 | + config_1 = results[1].connector_specific_config |
| 310 | + assert ( |
| 311 | + config_1["jira_project_url"] == "https://other.atlassian.net/projects/OTHER" |
| 312 | + ) |
| 313 | + |
| 314 | + # Third connector - partial config |
| 315 | + config_2 = results[2].connector_specific_config |
| 316 | + assert ( |
| 317 | + config_2["jira_project_url"] |
| 318 | + == "https://example.atlassian.net/projects/TEST" |
| 319 | + ) |
| 320 | + assert config_2["batch_size"] == 50 |
0 commit comments