202 lines
5.5 KiB
Python
202 lines
5.5 KiB
Python
"""update notification settings fields
|
|
|
|
Revision ID: 20260407_0001
|
|
Revises: 20260403_0004
|
|
Create Date: 2026-04-07 00:00:00
|
|
"""
|
|
|
|
from typing import Sequence, Union
|
|
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
|
|
revision: str = "20260407_0001"
|
|
down_revision: Union[str, Sequence[str], None] = "202604030004"
|
|
branch_labels: Union[str, Sequence[str], None] = None
|
|
depends_on: Union[str, Sequence[str], None] = None
|
|
|
|
|
|
def upgrade() -> None:
|
|
op.execute(
|
|
"""
|
|
create or replace function public.initialize_profile_and_points_on_signup()
|
|
returns trigger
|
|
language plpgsql
|
|
security definer
|
|
set search_path = public
|
|
as $$
|
|
declare
|
|
v_username text;
|
|
v_ledger_id uuid;
|
|
v_event_id text;
|
|
begin
|
|
v_username := 'user_' || substring(md5(new.id::text || clock_timestamp()::text || random()::text) from 1 for 6);
|
|
v_ledger_id := md5(new.id::text || 'ledger' || clock_timestamp()::text || random()::text)::uuid;
|
|
v_event_id := 'register:' || new.id::text;
|
|
|
|
insert into public.profiles (id, username, avatar_url, bio, settings)
|
|
values (
|
|
new.id,
|
|
v_username,
|
|
null,
|
|
null,
|
|
jsonb_build_object(
|
|
'version', 1,
|
|
'preferences', jsonb_build_object(
|
|
'interface_language', 'zh-CN',
|
|
'ai_language', 'zh-CN',
|
|
'timezone', 'Asia/Shanghai',
|
|
'country', 'CN'
|
|
),
|
|
'privacy', jsonb_build_object('profile_visibility', 'public'),
|
|
'notification', jsonb_build_object(
|
|
'allow_notifications', true,
|
|
'allow_vibration', true
|
|
)
|
|
)
|
|
)
|
|
on conflict (id) do nothing;
|
|
|
|
insert into public.user_points (
|
|
user_id,
|
|
balance,
|
|
frozen_balance,
|
|
lifetime_earned,
|
|
lifetime_spent,
|
|
version
|
|
)
|
|
values (new.id, 100, 0, 100, 0, 0)
|
|
on conflict (user_id) do nothing;
|
|
|
|
insert into public.points_ledger (
|
|
id,
|
|
user_id,
|
|
direction,
|
|
amount,
|
|
balance_after,
|
|
change_type,
|
|
biz_type,
|
|
biz_id,
|
|
event_id,
|
|
operator_id,
|
|
metadata
|
|
)
|
|
values (
|
|
v_ledger_id,
|
|
new.id,
|
|
1,
|
|
100,
|
|
100,
|
|
'register',
|
|
null,
|
|
null,
|
|
v_event_id,
|
|
null,
|
|
jsonb_build_object(
|
|
'schema_version', 1,
|
|
'reason_code', 'REGISTER_WELCOME',
|
|
'operator_type', 'system',
|
|
'run_id', v_event_id,
|
|
'request_id', null,
|
|
'ext', jsonb_build_object('source', 'auth_signup')
|
|
)
|
|
)
|
|
on conflict (user_id, event_id) do nothing;
|
|
|
|
return new;
|
|
end;
|
|
$$;
|
|
"""
|
|
)
|
|
|
|
|
|
def downgrade() -> None:
|
|
op.execute(
|
|
"""
|
|
create or replace function public.initialize_profile_and_points_on_signup()
|
|
returns trigger
|
|
language plpgsql
|
|
security definer
|
|
set search_path = public
|
|
as $$
|
|
declare
|
|
v_username text;
|
|
v_ledger_id uuid;
|
|
v_event_id text;
|
|
begin
|
|
v_username := 'user_' || substring(md5(new.id::text || clock_timestamp()::text || random()::text) from 1 for 6);
|
|
v_ledger_id := md5(new.id::text || 'ledger' || clock_timestamp()::text || random()::text)::uuid;
|
|
v_event_id := 'register:' || new.id::text;
|
|
|
|
insert into public.profiles (id, username, avatar_url, bio, settings)
|
|
values (
|
|
new.id,
|
|
v_username,
|
|
null,
|
|
null,
|
|
jsonb_build_object(
|
|
'version', 1,
|
|
'preferences', jsonb_build_object(
|
|
'interface_language', 'zh-CN',
|
|
'ai_language', 'zh-CN',
|
|
'timezone', 'Asia/Shanghai',
|
|
'country', 'CN'
|
|
),
|
|
'privacy', jsonb_build_object('profile_visibility', 'public'),
|
|
'notification', jsonb_build_object('push_enabled', true)
|
|
)
|
|
)
|
|
on conflict (id) do nothing;
|
|
|
|
insert into public.user_points (
|
|
user_id,
|
|
balance,
|
|
frozen_balance,
|
|
lifetime_earned,
|
|
lifetime_spent,
|
|
version
|
|
)
|
|
values (new.id, 100, 0, 100, 0, 0)
|
|
on conflict (user_id) do nothing;
|
|
|
|
insert into public.points_ledger (
|
|
id,
|
|
user_id,
|
|
direction,
|
|
amount,
|
|
balance_after,
|
|
change_type,
|
|
biz_type,
|
|
biz_id,
|
|
event_id,
|
|
operator_id,
|
|
metadata
|
|
)
|
|
values (
|
|
v_ledger_id,
|
|
new.id,
|
|
1,
|
|
100,
|
|
100,
|
|
'register',
|
|
null,
|
|
null,
|
|
v_event_id,
|
|
null,
|
|
jsonb_build_object(
|
|
'schema_version', 1,
|
|
'reason_code', 'REGISTER_WELCOME',
|
|
'operator_type', 'system',
|
|
'run_id', v_event_id,
|
|
'request_id', null,
|
|
'ext', jsonb_build_object('source', 'auth_signup')
|
|
)
|
|
)
|
|
on conflict (user_id, event_id) do nothing;
|
|
|
|
return new;
|
|
end;
|
|
$$;
|
|
"""
|
|
)
|