我使用PostgreSQL和Alembic进行迁移。当我向User表添加新列时,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,并在&#39; @&#39;签署之后抛出所有内容(在这种情况下&# 39; @ example.com&#39;)并设置用户名的值(在这种情况下&#39; test&#39;)之后使nullable = false。
我该怎么做?必须是脚本而不是username = <email address with no '@' and everything comes after '@' sign should be removed>
并设置nullable=false
或者,如果有其他方式将username
默认值设置为不带@ sing的电子邮件地址以及之后的所有内容?
答案 0 :(得分:5)
这是问题是如何解决的。
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')
答案 1 :(得分:0)
可以使用子查询编写基于同一表的另一列中的值更新新列的脚本。唯一的技巧是,因为您从同一个表中查询,您需要为表名提供别名以确保从相应的行中进行选择:
update 'user' as target set username = (
select substring(email from '.+?(?=@)')
from 'user' as source where source.id = target.id
);