Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Latest migration changes broke Postgres support #635

Closed
justjanne opened this issue Jan 8, 2020 · 10 comments · Fixed by #1452
Closed

Latest migration changes broke Postgres support #635

justjanne opened this issue Jan 8, 2020 · 10 comments · Fixed by #1452
Labels
bug / broken-feature Existing feature malfunctioning or broken mod / stale This request has gone stale skill / database Requires a database skill-set

Comments

@justjanne
Copy link

Roughly 1.5 months ago, the project ran just fine with postgres as database backend. Due to new migrations which have been added, and relevant changes, this is now broken.

This would be a minor change, being a huge improvement nonetheless.

@keesbos
Copy link
Contributor

keesbos commented Jan 10, 2020

diff --git a/migrations/versions/3f76448bb6de_add_user_confirmed_column.py b/migrations/versions/3f76448bb6de_add_user_confirmed_column.py
index d1b6d29..490aebe 100644
--- a/migrations/versions/3f76448bb6de_add_user_confirmed_column.py
+++ b/migrations/versions/3f76448bb6de_add_user_confirmed_column.py
@@ -18,8 +18,12 @@ depends_on = None
 def upgrade():
     with op.batch_alter_table('user') as batch_op:
         batch_op.add_column(
-            sa.Column('confirmed', sa.Boolean(), nullable=False,
+            sa.Column('confirmed', sa.Boolean(), nullable=True,
                       default=False))
+    with op.batch_alter_table('user') as batch_op:
+        user = sa.sql.table('user', sa.sql.column('confirmed'))
+        batch_op.execute(user.update().values(confirmed=False))
+        batch_op.alter_column('confirmed', nullable=False)

@ngoduykhanh
Copy link
Contributor

Hi,

Can you show your setup and the error message during the migration?
I've tried with PostgreSQL 12 and it looks good to me.

@keesbos
Copy link
Contributor

keesbos commented Jan 11, 2020

Error:

# flask db upgrade
INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO  [alembic.runtime.migration] Will assume transactional DDL.
INFO  [alembic.runtime.migration] Running upgrade 856bb94b7040 -> b0fea72a3f20, Update domain serial columns type
INFO  [alembic.runtime.migration] Running upgrade b0fea72a3f20 -> 3f76448bb6de, Add user.confirmed column
Traceback (most recent call last):
  File "/opt/powerdns-admin/py-pdns-adm/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1246, in _execute_context
    cursor, statement, parameters, context
  File "/opt/powerdns-admin/py-pdns-adm/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 581, in do_execute
    cursor.execute(statement, parameters)
psycopg2.errors.NotNullViolation: column "confirmed" contains null values

Ubuntu (in lxd container):

# lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 18.04.3 LTS
Release:	18.04
Codename:	bionic

Postgresql:

# dpkg -l | grep postg
ii  postgresql                     10+190ubuntu0.1                    all          object-relational SQL database (supported version)
ii  postgresql-10                  10.10-0ubuntu0.18.04.1             amd64        object-relational SQL database, version 10 server
ii  postgresql-client-10           10.10-0ubuntu0.18.04.1             amd64        front-end programs for PostgreSQL 10
ii  postgresql-client-common       190ubuntu0.1                       all          manager for multiple PostgreSQL client versions
ii  postgresql-common              190ubuntu0.1                       all          PostgreSQL database-cluster manager

(Note, the git diff fixes it for me)

It looks like the default value isn't applied to the table with sqlalchemy. With pg_dump:

CREATE TABLE public."user" (
    id integer NOT NULL,
    username character varying(64),
    password character varying(64),
    firstname character varying(64),
    lastname character varying(64),
    email character varying(128),
    otp_secret character varying(16),
    role_id integer,
    confirmed boolean NOT NULL
);

And no alter table set default for the confirmed column.

The only alter table set default statements I see in then dump are for nextval() (i.e. the id columns).

@bjo81
Copy link

bjo81 commented Aug 7, 2020

Any update on this? Same issue here with postgresql-11.

@halkeye
Copy link

halkeye commented Apr 26, 2021

sqlalchemy.exc.IntegrityError: (psycopg2.errors.NotNullViolation) column "confirmed" contains null values [SQL: ALTER TABLE "user" ADD COLUMN confirmed BOOLEAN NOT NULL]
(Background on this error at: http://sqlalche.me/e/13/gkpj)

So I applied the update manually

ALTER TABLE "user" ADD COLUMN confirmed BOOLEAN NOT NULL default false;
update "user" set confirmed=true;
update "alembic_version" set "version_num"='3f76448bb6de';

Proper fix:
7739bf7#diff-5b6d721374eb2724c78efcc59227e40954dc25bff97a4b6d72123ac5d16cbea2R21 should get a default value to match https://github.com/ngoduykhanh/PowerDNS-Admin/blob/5f10f739ea95f51d539ebd7927d90bba5c5dc7ae/powerdnsadmin/models/user.py#L31

Since i've already applied the migration, I don't have lots of motivation to make a PR.

@joachimtingvold
Copy link
Contributor

joachimtingvold commented Dec 17, 2021

Same happens with PostgreSQL 13. The patch from @keesbos works, so that can essentially be used as the PR for fixing it (unless someone wants to refine it more).

(venv) pdnsadmin@dnsadmin1:/srv/vhosts/dnsadmin.foo.bar$ flask db upgrade
INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO  [alembic.runtime.migration] Will assume transactional DDL.
INFO  [alembic.runtime.migration] Running upgrade 654298797277 -> 0fb6d23a4863, Remove user avatar
INFO  [alembic.runtime.migration] Running upgrade 0fb6d23a4863 -> 856bb94b7040, Add comment column in domain template record table
INFO  [alembic.runtime.migration] Running upgrade 856bb94b7040 -> b0fea72a3f20, Update domain serial columns type
INFO  [alembic.runtime.migration] Running upgrade b0fea72a3f20 -> 3f76448bb6de, Add user.confirmed column
Traceback (most recent call last):
  File "/srv/vhosts/dnsadmin.foo.bar/venv/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1276, in _execute_context
    self.dialect.do_execute(
  File "/srv/vhosts/dnsadmin.foo.bar/venv/lib/python3.9/site-packages/sqlalchemy/engine/default.py", line 593, in do_execute
    cursor.execute(statement, parameters)
psycopg2.errors.NotNullViolation: column "confirmed" of relation "user" contains null values

[...]

  File "/srv/vhosts/dnsadmin.foo.bar/venv/lib/python3.9/site-packages/sqlalchemy/engine/default.py", line 593, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.IntegrityError: (psycopg2.errors.NotNullViolation) column "confirmed" of relation "user" contains null values

[SQL: ALTER TABLE "user" ADD COLUMN confirmed BOOLEAN NOT NULL]

@jbe-dw
Copy link
Contributor

jbe-dw commented May 6, 2022

@keesbos I've not checked the migration scripts but it looks to me that it hasn't been fixed. Can you confirm ?

@vmarkop vmarkop added the bug / broken-feature Existing feature malfunctioning or broken label May 8, 2022
@jbe-dw jbe-dw added the skill / database Requires a database skill-set label May 12, 2022
@jbe-dw
Copy link
Contributor

jbe-dw commented May 12, 2022

Related #787

subbink added a commit to subbink/PowerDNS-Admin that referenced this issue Mar 5, 2023
nkukard added a commit to nkukard/PowerDNS-Admin that referenced this issue Mar 14, 2023
… using MySQL databases

Fixes PowerDNS-Admin#635 which introduced a breaking change for MySQL databases and resolves PowerDNS-Admin#1446.
nkukard added a commit to nkukard/PowerDNS-Admin that referenced this issue Mar 14, 2023
… using MySQL databases

Fixes PowerDNS-Admin#635 which introduced a breaking change for MySQL databases and resolves PowerDNS-Admin#1446.
@github-actions
Copy link

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. PDA is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. Do not attempt to circumvent this process by "bumping" the issue; doing so will result in its immediate closure and you may be barred from participating in any future discussions. Please see our contributing guide.

@github-actions github-actions bot added the mod / stale This request has gone stale label Mar 17, 2023
@justjanne
Copy link
Author

Awesome, thanks! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug / broken-feature Existing feature malfunctioning or broken mod / stale This request has gone stale skill / database Requires a database skill-set
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants