def change_column_nullable(table_oid, column_index, nullable, engine): table = tables.reflect_table_from_oid(table_oid, engine) column = table.columns[column_index] with engine.begin() as conn: ctx = MigrationContext.configure(conn) op = Operations(ctx) op.alter_column(table.name, column.name, nullable=nullable, schema=table.schema) return tables.reflect_table_from_oid(table_oid, engine).columns[column_index]
def retype_column(table_oid, column_index, new_type, engine): table = tables.reflect_table_from_oid(table_oid, engine) alteration.alter_column_type( table.schema, table.name, table.columns[column_index].name, new_type, engine, friendly_names=False, ) return tables.reflect_table_from_oid(table_oid, engine).columns[column_index]
def drop_column( engine, table_oid, column_index, ): column_index = int(column_index) table = tables.reflect_table_from_oid(table_oid, engine) column = table.columns[column_index] with engine.begin() as conn: ctx = MigrationContext.configure(conn) op = Operations(ctx) op.drop_column(table.name, column.name, schema=table.schema)
def _sa_table(self): try: table = tables.reflect_table_from_oid( self.oid, self.schema._sa_engine, ) # We catch these errors, since it lets us decouple the cadence of # overall DB reflection from the cadence of cache expiration for # table names. Also, it makes it obvious when the DB layer has # been altered, as opposed to other reasons for a 404 when # requesting a table. except (TypeError, IndexError): table = tables.create_empty_table("MISSING") return table
def create_column(engine, table_oid, column_data): column_type = column_data[TYPE] column_nullable = column_data.get(NULLABLE, True) supported_types = alteration.get_supported_alter_column_types( engine, friendly_names=False, ) sa_type = supported_types.get(column_type) if sa_type is None: logger.warning("Requested type not supported. falling back to VARCHAR") sa_type = supported_types["VARCHAR"] table = tables.reflect_table_from_oid(table_oid, engine) column = MathesarColumn( column_data[NAME], sa_type, nullable=column_nullable, ) with engine.begin() as conn: ctx = MigrationContext.configure(conn) op = Operations(ctx) op.add_column(table.name, column, schema=table.schema) return tables.reflect_table_from_oid(table_oid, engine).columns[column_data[NAME]]
def test_drop_column_correct_column(engine_with_schema): engine, schema = engine_with_schema table_name = "atable" target_column_name = "thecolumntodrop" nontarget_column_name = "notthecolumntodrop" table = Table( table_name, MetaData(bind=engine, schema=schema), Column(target_column_name, Integer), Column(nontarget_column_name, String), ) table.create() table_oid = tables.get_oid_from_table(table_name, schema, engine) columns.drop_column(engine, table_oid, 0) altered_table = tables.reflect_table_from_oid(table_oid, engine) assert len(altered_table.columns) == 1 assert nontarget_column_name in altered_table.columns assert target_column_name not in altered_table.columns
def test_create_column(engine_with_schema): engine, schema = engine_with_schema table_name = "atableone" target_type = "BOOLEAN" initial_column_name = "original_column" new_column_name = "added_column" table = Table( table_name, MetaData(bind=engine, schema=schema), Column(initial_column_name, Integer), ) table.create() table_oid = tables.get_oid_from_table(table_name, schema, engine) column_data = {"name": new_column_name, "type": target_type} created_col = columns.create_column(engine, table_oid, column_data) altered_table = tables.reflect_table_from_oid(table_oid, engine) assert len(altered_table.columns) == 2 assert created_col.name == new_column_name assert created_col.type.compile(engine.dialect) == "BOOLEAN"