keon94 commented on code in PR #5286: URL: https://github.com/apache/incubator-devlake/pull/5286#discussion_r1205925745
########## backend/python/pydevlake/pydevlake/migration.py: ########## @@ -0,0 +1,113 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +from typing import List, Literal, Optional, Union, Annotated +from enum import Enum +from datetime import datetime + +from pydantic import BaseModel, Field + + +MIGRATION_SCRIPTS = [] + +class Dialect(Enum): + MYSQL = "mysql" + POSTGRESQL = "postgres" + + +class Execute(BaseModel): + type: Literal["execute"] = "execute" + sql: str + dialect: Optional[Dialect] = None + + +class DropColumn(BaseModel): + type: Literal["drop_column"] = "drop_column" + table: str + column: str + + +class DropTable(BaseModel): + type: Literal["drop_table"] = "drop_table" + table: str + + +Operation = Annotated[ + Union[Execute, DropColumn, DropTable], + Field(discriminator="type") +] + + +class MigrationScript(BaseModel): + operations: List[Operation] + version: int + name: str + + +class MigrationScriptBuilder: + def __init__(self): + self.operations = [] + + def execute(self, sql: str, dialect: Optional[Dialect] = None): + """ + Executes a raw SQL statement. + If dialect is specified the statement will be executed only if the db dialect matches. + """ + self.operations.append(Execute(sql=sql, dialect=dialect)) + + def drop_column(self, table: str, column: str): + """ + Drops a column from a table. + """ + self.operations.append(DropColumn(table=table, column=column)) + + def drop_table(self, table: str): + """ + Drops a table. + """ + self.operations.append(DropTable(table=table)) + + +def migration(version: int): Review Comment: could we add a second optional param for the script name? We can default it to fn.__name__ like in L99 if not provided. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
