-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseed_users.py
More file actions
133 lines (120 loc) · 4.16 KB
/
seed_users.py
File metadata and controls
133 lines (120 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import sys
import os
from sqlalchemy import text
from sqlalchemy.orm import Session
import hashlib
# Ensure project root is on path so app.* imports work
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
PROJECT_ROOT = CURRENT_DIR
if PROJECT_ROOT not in sys.path:
sys.path.append(PROJECT_ROOT)
from app.models.user import User
from app.db.postgres.database import sync_session
def hash_password(password):
"""Simple password hashing for demo purposes"""
return hashlib.sha256(password.encode()).hexdigest()
def main():
try:
print("Seeding users table...")
db: Session = sync_session()
# Inspect actual column names to decide identifiers
cols = {row[0] for row in db.execute(text("""
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'users'
"""))}
def ident(logical_name: str) -> str:
# Prefer exact match (quoted if mixed case), else lowercase
if logical_name in cols:
return f'"{logical_name}"'
lower = logical_name.lower()
if lower in cols:
return lower
raise RuntimeError(f"Column {logical_name} not found in users table. Available: {cols}")
# Create sample users
users_data = [
# Admin users
{
"userid": 1,
"name": "Admin User",
"password": hash_password("admin123"),
"emailid": "admin@company.com",
"isadmin": True,
"islogin": False
},
{
"userid": 2,
"name": "HR Manager",
"password": hash_password("hr123"),
"emailid": "hr@company.com",
"isadmin": True,
"islogin": False
},
# Regular users
{
"userid": 3,
"name": "John Doe",
"password": hash_password("user123"),
"emailid": "john.doe@email.com",
"isadmin": False,
"islogin": False
},
{
"userid": 4,
"name": "Jane Smith",
"password": hash_password("user123"),
"emailid": "jane.smith@email.com",
"isadmin": False,
"islogin": False
},
{
"userid": 5,
"name": "Mike Johnson",
"password": hash_password("user123"),
"emailid": "mike.johnson@email.com",
"isadmin": False,
"islogin": False
}
]
# Clear existing rows and insert using detected identifiers
col_id = ident('UserId')
col_name = ident('Name')
col_pass = ident('Password')
col_email = ident('EmailId')
col_isadmin = ident('IsAdmin')
col_islogin = ident('IsLogin')
insert_sql = text(
f"INSERT INTO users ({col_id}, {col_name}, {col_pass}, {col_email}, {col_isadmin}, {col_islogin})\n"
"VALUES (:userid, :name, :password, :emailid, :isadmin, :islogin)"
)
try:
db.execute(text("DELETE FROM users"))
for u in users_data:
db.execute(insert_sql, u)
db.commit()
except Exception:
db.rollback()
raise
print("Users seeded successfully!")
print("\nCreated users:")
print("Admin users:")
print("- admin@company.com / admin123 (Admin User)")
print("- hr@company.com / hr123 (HR Manager)")
print("\nRegular users:")
print("- john.doe@email.com / user123 (John Doe)")
print("- jane.smith@email.com / user123 (Jane Smith)")
print("- mike.johnson@email.com / user123 (Mike Johnson)")
except Exception as e:
print(f"Error seeding users: {e}")
try:
db.rollback()
except Exception:
pass
raise
finally:
try:
db.close()
except Exception:
pass
if __name__ == "__main__":
main()