annotate libervia/backend/memory/migration/versions/2ab01aa1f686_create_table_for_notifications.py @ 4130:02f0adc745c6

core: notifications implementation, first draft: add a new table for notifications, and methods/bridge methods to manipulate them.
author Goffi <goffi@goffi.org>
date Mon, 16 Oct 2023 17:29:31 +0200
parents
children 0d7bb4df2343
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4130
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 """create table for notifications
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
2
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
3 Revision ID: 2ab01aa1f686
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 Revises: 4b002773cf92
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 Create Date: 2023-10-16 12:11:43.507295
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 """
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 from alembic import op
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 import sqlalchemy as sa
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
10
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # revision identifiers, used by Alembic.
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 revision = '2ab01aa1f686'
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 down_revision = '4b002773cf92'
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 branch_labels = None
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 depends_on = None
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
17
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
18
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
19 def upgrade():
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 op.create_table('notifications',
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 sa.Column('timestamp', sa.Float(), nullable=False),
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 sa.Column('expire_at', sa.Float(), nullable=True),
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 sa.Column('profile_id', sa.Integer(), nullable=True),
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 sa.Column('type', sa.Enum('chat', 'blog', 'calendar', 'file', 'call', 'service', 'other', name='notificationtype'), nullable=False),
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 sa.Column('title', sa.Text(), nullable=True),
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 sa.Column('body_plain', sa.Text(), nullable=False),
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 sa.Column('body_rich', sa.Text(), nullable=True),
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 sa.Column('requires_action', sa.Boolean(), nullable=True),
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 sa.Column('priority', sa.Integer(), nullable=True),
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 sa.Column('extra_data', sa.JSON(), nullable=True),
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 sa.Column('status', sa.Enum('new', 'read', name='notificationstatus'), nullable=True),
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 sa.ForeignKeyConstraint(['profile_id'], ['profiles.id'], name=op.f('fk_notifications_profile_id_profiles'), ondelete='CASCADE'),
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 sa.PrimaryKeyConstraint('id', name=op.f('pk_notifications'))
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 )
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 with op.batch_alter_table('notifications', schema=None) as batch_op:
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 batch_op.create_index(batch_op.f('ix_notifications_profile_id'), ['profile_id'], unique=False)
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 batch_op.create_index('notifications_profile_id_status', ['profile_id', 'status'], unique=False)
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
39
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
40
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 def downgrade():
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 with op.batch_alter_table('notifications', schema=None) as batch_op:
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 batch_op.drop_index('notifications_profile_id_status')
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 batch_op.drop_index(batch_op.f('ix_notifications_profile_id'))
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
45
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 op.drop_table('notifications')