gpt4 book ai didi

database - 使用 alembic 升级表时设置列的值

转载 作者:太空狗 更新时间:2023-10-30 01:42:55 25 4
gpt4 key购买 nike

我正在使用 PostgreSQL 和 Alembic 进行迁移。当我将新列添加到我的用户表时,Alembic 使用以下脚本生成了迁移:

revision = '4824acf75bf3'
down_revision = '2f0fbdd56de1'

from alembic import op
import sqlalchemy as sa

def upgrade():
op.add_column(
'user',
sa.Column(
'username',
sa.Unicode(length=255),
nullable=False
)
)

def downgrade():
op.drop_column('user', 'username')

我真正想做的是在升级生产版本时自动生成用户名的值。换句话说,我的生产版本里面有很多用户,如果我在上面运行上面的升级,会出现用户名不能为 NULL 的错误,所以我必须删除所有用户,升级用户表和再次添加用户后,这很痛苦。因此,我想将上面的脚本更改为:

revision = '4824acf75bf3'
down_revision = '2f0fbdd56de1'

from alembic import op
import sqlalchemy as sa

def upgrade():
op.add_column(
'user',
sa.Column(
'username',
sa.Unicode(length=255)
)
)
op.execute(
'UPDATE "user" set username = <email address with no '@'
and everything comes after '@' sign should be removed>
WHERE email is not null'
)
<only after the above code is executed 'nullable=False' must be set up>

def downgrade():
op.drop_column('user', 'username')

正如上面代码中所述,我想执行一个 SQL 代码来检查像 test@example.com 这样的电子邮件地址,并在“@”符号(在本例中为“@example.com”)之后抛出所有内容,并且在使 nullable=false 之后设置用户名的值(在本例中为“test”)。

我该怎么做?什么必须是脚本而不是 username = <email address with no '@' and everything comes after '@' sign should be removed>和设置 nullable=false

或者如果有任何其他方式设置username默认值为不带@sing 的电子邮件地址及其后的所有内容?

最佳答案

问题是这样解决的。

def upgrade():
op.add_column(
'user',
sa.Column(
'username',
sa.Unicode(length=255)
)
)
op.create_index('ix_user_username', 'user', ['username'], unique=True)
op.execute(
'''
DO
$do$
DECLARE uid INTEGER;
DECLARE username_candidate TEXT;
BEGIN
FOR uid, username_candidate IN (
SELECT
id,
lower(
substring(email for position('@' in email) - 1)
)
FROM "user" WHERE username is null
) LOOP
UPDATE "user"
SET username = username_candidate
WHERE
id = uid AND
NOT EXISTS (
SELECT id FROM "user" WHERE username = username_candidate
);
END LOOP;
END
$do$
'''
)
# Fix name colissions
op.execute(
'''
DO
$do$
DECLARE uniqufier INTEGER := 0;
DECLARE uid INTEGER;
DECLARE username_candidate TEXT;
BEGIN
WHILE EXISTS (SELECT id FROM "user" WHERE username is null) LOOP
uniqufier := uniqufier + 1;
FOR uid, username_candidate IN (
SELECT
id,
lower(
substring(email for position('@' in email) - 1)
|| uniqufier
)
FROM "user" WHERE username is null
) LOOP
UPDATE "user"
SET username = username_candidate
WHERE
id = uid AND
NOT EXISTS (
SELECT id FROM "user" WHERE username = username_candidate
);
END LOOP;
END LOOP;
END;
$do$
'''
)
op.alter_column(
'user',
'username',
nullable=False,
)


def downgrade():
op.drop_index('ix_user_username', table_name='user')
op.drop_column('user', 'username')

关于database - 使用 alembic 升级表时设置列的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25403549/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com