googleapis / python-spanner-sqlalchemy

Apache License 2.0
38 stars 28 forks source link

How to update tables with new columns/rows #300

Closed Joselguti closed 1 year ago

Joselguti commented 1 year ago

Hello! I lost track of an old Ticket I had submited, so I'm creating this specific question

I am trying to add new columns to a table that already exists in Spanner with the following code:

`autocommit_engine = spanner_engine.execution_options(isolation_level="AUTOCOMMIT")
    types_dict = {np.dtype('O'): String(50), np.dtype('int64'): Integer, np.dtype('float64'): Float,
                  'TIMESTAMP': 'TIMESTAMP'}
    metadata = MetaData(bind=autocommit_engine)
    ###Bring table Object
    table = Table(holding_name, metadata, autoload=True)
    ###Get index to insert new data
    with autocommit_engine.begin() as connection:
        rows = list(connection.execute(table.select().order_by(desc('index')).limit(1)))
    ### Compare current df with spanner df, If new columns, attach
    index=rows[0][0]
    data_columns=list(rows[0].keys())
    new_df_columns=list(df.columns)
    if new_df_columns != data_columns:
        new_cols = list(set(new_df_columns) - set(data_columns))
        for each in new_cols:
            table.append_column(Column(each, types_dict[df.dtypes[each]], nullable=True))
    metadata.create_all(autocommit_engine)`

I am using: autocommit_engine = spanner_engine.execution_options(isolation_level="AUTOCOMMIT") But it's not working to force update either, I see no changes in my spanner table.

Basically what I want to achieve is the following

(Code taken from: https://cloud.google.com/spanner/docs/samples/spanner-add-column)

`def add_column(instance_id, database_id): """Adds a new column to the Albums table in the example database.""" spanner_client = spanner.Client() instance = spanner_client.instance(instance_id) database = instance.database(database_id)

operation = database.update_ddl(
    ["ALTER TABLE Albums ADD COLUMN MarketingBudget INT64"]
)

print("Waiting for operation to complete...")
operation.result(OPERATION_TIMEOUT_SECONDS)

print("Added the MarketingBudget column.")`

Is there a way of doing this with python-spanner-sqlalchemy

IlyaFaer commented 1 year ago

Answered in another issue: https://github.com/googleapis/python-spanner-sqlalchemy/issues/285