115 lines
3.5 KiB
Python
115 lines
3.5 KiB
Python
|
|
"""seed initial data
|
||
|
|
|
||
|
|
Revision ID: 20241204_0000
|
||
|
|
Revises: initial_schema
|
||
|
|
Create Date: 2024-12-04 00:00:00.000000
|
||
|
|
|
||
|
|
"""
|
||
|
|
from typing import Sequence, Union
|
||
|
|
from alembic import op
|
||
|
|
import sqlalchemy as sa
|
||
|
|
from datetime import datetime, timezone
|
||
|
|
from app.core.security import get_password_hash
|
||
|
|
|
||
|
|
# revision identifiers, used by Alembic.
|
||
|
|
revision: str = '20241204_0000'
|
||
|
|
down_revision: Union[str, None] = 'initial_schema'
|
||
|
|
branch_labels: Union[str, Sequence[str], None] = None
|
||
|
|
depends_on: Union[str, Sequence[str], None] = None
|
||
|
|
|
||
|
|
|
||
|
|
def upgrade() -> None:
|
||
|
|
# Seed roles
|
||
|
|
op.execute("""
|
||
|
|
INSERT INTO roles (id, name, description, is_active)
|
||
|
|
VALUES
|
||
|
|
('0196a031-a540-78ca-a0d2-ccfb6fb6823c', 'data_viewer', 'Contoh deskripsi', true),
|
||
|
|
('01968027-048b-7166-9b71-412e178f65ee', 'administrator', 'Contoh deskripsi', true),
|
||
|
|
('0196802b-b5e8-740b-9f0c-b7e423aff947', 'data_validator', 'Contoh deskripsi', true),
|
||
|
|
('01969f62-aae2-7b59-9e23-da29223f70e1', 'data_manager', 'Contoh deskripsi', true)
|
||
|
|
ON CONFLICT (id) DO NOTHING;
|
||
|
|
""")
|
||
|
|
|
||
|
|
# Seed regional
|
||
|
|
op.execute("""
|
||
|
|
INSERT INTO regionals (id, code, name, description, thumbnail, is_active, created_at, updated_at)
|
||
|
|
VALUES (
|
||
|
|
'01968b53-a910-7a67-bd10-975b8923b92e',
|
||
|
|
'35.00',
|
||
|
|
'Jawa Timur',
|
||
|
|
'Provinsi Jawa Timur',
|
||
|
|
NULL,
|
||
|
|
true,
|
||
|
|
'2025-04-29 13:04:51.693000+00',
|
||
|
|
'2025-04-29 13:04:51.693000+00'
|
||
|
|
)
|
||
|
|
ON CONFLICT (id) DO NOTHING;
|
||
|
|
""")
|
||
|
|
|
||
|
|
# Seed organization
|
||
|
|
op.execute("""
|
||
|
|
INSERT INTO organizations (id, name, description, thumbnail, address, phone_number, email, website, is_active, is_deleted, created_at, modified_at)
|
||
|
|
VALUES (
|
||
|
|
'01968b54-0000-7a67-bd10-975b8923b92e',
|
||
|
|
'Kominfo',
|
||
|
|
'Kementerian Komunikasi dan Informatika',
|
||
|
|
NULL,
|
||
|
|
NULL,
|
||
|
|
NULL,
|
||
|
|
'info@kominfo.go.id',
|
||
|
|
'https://www.kominfo.go.id',
|
||
|
|
true,
|
||
|
|
false,
|
||
|
|
NOW(),
|
||
|
|
NOW()
|
||
|
|
)
|
||
|
|
ON CONFLICT (id) DO NOTHING;
|
||
|
|
""")
|
||
|
|
|
||
|
|
# Seed user admin
|
||
|
|
# Password: admin123 (hashed with bcrypt using get_password_hash)
|
||
|
|
hashed_password = get_password_hash("admin123")
|
||
|
|
op.execute(f"""
|
||
|
|
INSERT INTO users (
|
||
|
|
id,
|
||
|
|
name,
|
||
|
|
email,
|
||
|
|
profile_picture,
|
||
|
|
username,
|
||
|
|
password,
|
||
|
|
position,
|
||
|
|
role_id,
|
||
|
|
employee_id,
|
||
|
|
organization_id,
|
||
|
|
is_active,
|
||
|
|
is_deleted,
|
||
|
|
created_at,
|
||
|
|
modified_at
|
||
|
|
)
|
||
|
|
VALUES (
|
||
|
|
'01968b55-0000-7a67-bd10-975b8923b92e',
|
||
|
|
'Administrator Kominfo',
|
||
|
|
'admin@kominfo.go.id',
|
||
|
|
NULL,
|
||
|
|
'admin',
|
||
|
|
'{hashed_password}',
|
||
|
|
'System Administrator',
|
||
|
|
'01968027-048b-7166-9b71-412e178f65ee',
|
||
|
|
NULL,
|
||
|
|
'01968b54-0000-7a67-bd10-975b8923b92e',
|
||
|
|
true,
|
||
|
|
false,
|
||
|
|
NOW(),
|
||
|
|
NOW()
|
||
|
|
)
|
||
|
|
ON CONFLICT (username) DO NOTHING;
|
||
|
|
""")
|
||
|
|
|
||
|
|
|
||
|
|
def downgrade() -> None:
|
||
|
|
# Delete in reverse order due to foreign key constraints
|
||
|
|
op.execute("DELETE FROM users WHERE username = 'admin';")
|
||
|
|
op.execute("DELETE FROM organizations WHERE name = 'Kominfo';")
|
||
|
|
op.execute("DELETE FROM regionals WHERE code = '35.00';")
|
||
|
|
op.execute("DELETE FROM roles WHERE name IN ('data_viewer', 'administrator', 'data_validator', 'data_manager');")
|