Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set a different owner for all that alembic creates?

So we have a restricted user that should update, insert and delete on the tables and one who may create, alter and drop tables.

We use alembic to migrate the database so of course the second user has to run the migration but then the first user has no rights to use the tables.

Of course I could run some postgres specific code to individually change the owner on whatever alembic creates but that can't be right. How am I supposed to solve this? Or is this a postgres issue? I don't see how I can grant user1 stuff on non-existing tables of one database.

like image 607
Giszmo Avatar asked Oct 09 '13 03:10

Giszmo


People also ask

How do you change heads in alembic?

Show activity on this post. Delete (or move to another folder) the specific migration file (in migrations/versions folder). The head will automatically revert to the most recent remaining migration. Using stamp will set the db version value to the specified revision; not alter the head revision number.

What is op in alembic?

op is a real Python module, populated with individual proxies for each method on Operations , so symbols can be imported safely from the alembic. op namespace. The Operations system is also fully extensible.

How do you run all alembic migrations?

Alembic is keeping track of the migrations in the alembic_version table on your database. Simple drop the table to start from scratch using the following command: DROP TABLE alembic_version; And then try to run your migration again!

What does alembic upgrade head do?

Then I ran the alembic upgrade head command. This command makes sure that the database is up to date with the most recent revision. As you can see alembic correctly ran the baseline revision, created the bug table according to the upgrade instruction and also added its own table alembic_version .


1 Answers

I know it's an old question but I met the same issue and found two nice solutions for that:

1. the quick and easy - add a general grant command to the upgrade template (script.py.mako): mine looks hence:

def upgrade():
    ${upgrades if upgrades else "pass"}
    sql = "grant all on all tables in schema public to simpleuser"
    op.execute(sql)

2. if you're not happy with a too-general grant statement, you can try to alter the create_table directive in alembic to add a specific grant command after the table creation. I found this attitude too unnecessarily too complicated but if this your desired direction please read about alembic api here

P.S. when granting access to tables, never forget their sequences if they have serials

like image 189
akiva Avatar answered Oct 14 '22 06:10

akiva