List table columns during migrations

Hello!

We are using ecto with sqlite.

Because sqlite doesn’t support column drop, we create a new table with all the columns but the one we want to delete. We delete the old table and rename the new one.

To do this, I would need to have a list of all the available columns. Is there any way to do that?

Thanks

sqlite3 Elixir library doesn’t have Ecto 3 support yet so you likely use Ecto 2.

As for listing table columns, this SO thread seems to do it.

1 Like

We are using ecto 3 and we started implementing the connection adapter.

I am not sure how listing the columns would work inside migrations.

eg: i have a table tbl with columns a, b, c. I want to drop b

I need execute_ddl return a list like:

[
create tbl_bkp as select a, c from tbl;
drop table tbl;
alter table tbl_bkp rename to tbl;
]

as a single migration operation. To build that, I would need to know the list of columns in advance I believe.

Inside execute_ddl I only get {:alter, %Table{} = table, changes}. The table metadata has no info about columns and changes is only a ist of columns to delete.

Ecto.Adapters.SQL.query!("your_ddl_here") should work. You can also wrap the three expressions in 3 calls in a single transaction.

1 Like