|
| 1 | +import os |
| 2 | +import importlib.util |
| 3 | +from qdrant_client import QdrantClient, models |
| 4 | +import logging |
| 5 | + |
| 6 | +logging.basicConfig(level=logging.INFO) |
| 7 | +logger = logging.getLogger(__name__) |
| 8 | +logging.getLogger("httpx").setLevel(logging.WARNING) |
| 9 | + |
| 10 | + |
| 11 | +def initialize_qdrant_client(url, api_key): |
| 12 | + client = QdrantClient(url=url, api_key=api_key) |
| 13 | + return client |
| 14 | + |
| 15 | + |
| 16 | +def check_and_create_migrations_collection(client): |
| 17 | + collections_response = client.get_collections() |
| 18 | + collections = collections_response.collections |
| 19 | + collection_names = [collection.name for collection in collections] |
| 20 | + if "migrations" not in collection_names: |
| 21 | + client.create_collection( |
| 22 | + collection_name="migrations", |
| 23 | + vectors_config=models.VectorParams(size=2, distance=models.Distance.COSINE), |
| 24 | + ) |
| 25 | + client.upsert( |
| 26 | + collection_name="migrations", |
| 27 | + points=[ |
| 28 | + models.PointStruct( |
| 29 | + id="5c56c793-69f3-4fbf-87e6-c4bf54c28c26", |
| 30 | + payload={ |
| 31 | + "version": 0, |
| 32 | + }, |
| 33 | + vector=[0.0, 0.1], |
| 34 | + ), |
| 35 | + ], |
| 36 | + ) |
| 37 | + |
| 38 | + |
| 39 | +def get_current_version(client): |
| 40 | + points = client.scroll( |
| 41 | + "migrations", |
| 42 | + with_payload=True, |
| 43 | + ) |
| 44 | + if points: |
| 45 | + version = points[0][0].payload["version"] |
| 46 | + return version |
| 47 | + return 0 |
| 48 | + |
| 49 | + |
| 50 | +def set_current_version(client, version): |
| 51 | + client.upsert( |
| 52 | + collection_name="migrations", |
| 53 | + points=[ |
| 54 | + models.PointStruct( |
| 55 | + id="5c56c793-69f3-4fbf-87e6-c4bf54c28c26", |
| 56 | + payload={ |
| 57 | + "version": version, |
| 58 | + }, |
| 59 | + vector=[0.0, 0.1], |
| 60 | + ), |
| 61 | + ], |
| 62 | + ) |
| 63 | + |
| 64 | + |
| 65 | +def get_migration_files(migration_folder): |
| 66 | + files = [] |
| 67 | + for file in os.listdir(migration_folder): |
| 68 | + if file.endswith(".py"): |
| 69 | + index = int(file.split("_")[0]) |
| 70 | + files.append((index, file)) |
| 71 | + files.sort(key=lambda x: x[0]) |
| 72 | + return files |
| 73 | + |
| 74 | + |
| 75 | +def run_migrations(client, migration_folder, current_version, target_version=None): |
| 76 | + migration_files = get_migration_files(migration_folder) |
| 77 | + for index, file in migration_files: |
| 78 | + if index > current_version and ( |
| 79 | + target_version is None or index <= target_version |
| 80 | + ): |
| 81 | + module_name = file.replace(".py", "") |
| 82 | + spec = importlib.util.spec_from_file_location( |
| 83 | + module_name, os.path.join(migration_folder, file) |
| 84 | + ) |
| 85 | + module = importlib.util.module_from_spec(spec) |
| 86 | + spec.loader.exec_module(module) |
| 87 | + module.forward(client) |
| 88 | + set_current_version(client, index) |
| 89 | + logger.info( |
| 90 | + f"Migration completed successfully for {file}! Enjoy your migration :D" |
| 91 | + ) |
| 92 | + else: |
| 93 | + logger.info(f"Skipping migration {file}") |
| 94 | + |
| 95 | + |
| 96 | +def rollback_migrations(client, migration_folder, current_version, target_version): |
| 97 | + migration_files = get_migration_files(migration_folder) |
| 98 | + for index, file in reversed(migration_files): |
| 99 | + if index <= current_version and index > target_version: |
| 100 | + module_name = file.replace(".py", "") |
| 101 | + spec = importlib.util.spec_from_file_location( |
| 102 | + module_name, os.path.join(migration_folder, file) |
| 103 | + ) |
| 104 | + module = importlib.util.module_from_spec(spec) |
| 105 | + spec.loader.exec_module(module) |
| 106 | + module.backward(client) |
| 107 | + set_current_version(client, index - 1) |
| 108 | + logger.info( |
| 109 | + f"Rollback completed successfully for {file}! Enjoy your migration :D" |
| 110 | + ) |
| 111 | + else: |
| 112 | + logger.info(f"Skipping rollback {file}") |
| 113 | + |
| 114 | + |
| 115 | +def migrate(url, api_key, migration_folder): |
| 116 | + client = initialize_qdrant_client(url, api_key) |
| 117 | + check_and_create_migrations_collection(client) |
| 118 | + current_version = get_current_version(client) |
| 119 | + run_migrations(client, migration_folder, current_version) |
| 120 | + |
| 121 | + |
| 122 | +def rollback(url, api_key, migration_folder, target_version): |
| 123 | + client = initialize_qdrant_client(url, api_key) |
| 124 | + check_and_create_migrations_collection(client) |
| 125 | + current_version = get_current_version(client) |
| 126 | + rollback_migrations(client, migration_folder, current_version, target_version) |
0 commit comments