-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_users_table.py
More file actions
84 lines (70 loc) · 3.07 KB
/
create_users_table.py
File metadata and controls
84 lines (70 loc) · 3.07 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
import sys
import os
from sqlalchemy import create_engine, text
import hashlib
# Add the project root to the Python path
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
# Database connection settings
DATABASE_URL = "postgresql+psycopg2://postgres:root@localhost:5432/interview_db"
# Create database engine
engine = create_engine(DATABASE_URL)
def hash_password(password):
"""Simple password hashing for demo purposes"""
return hashlib.sha256(password.encode()).hexdigest()
def main():
try:
print("Creating USERS table...")
with engine.connect() as conn:
# Drop existing users table if it exists
conn.execute(text("DROP TABLE IF EXISTS users CASCADE"))
# Create users table with exact schema requested
create_table_sql = """
CREATE TABLE users (
UserId INT PRIMARY KEY,
Name VARCHAR(255),
Password VARCHAR(255),
EmailId VARCHAR(255),
IsAdmin BOOLEAN,
CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
UpdatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
IsLogin BOOLEAN
)
"""
conn.execute(text(create_table_sql))
# Insert sample data
users_data = [
# Admin users
(1, 'Admin User', hash_password('admin123'), 'admin@company.com', True, False),
(2, 'HR Manager', hash_password('hr123'), 'hr@company.com', True, False),
# Regular users
(3, 'John Doe', hash_password('user123'), 'john.doe@email.com', False, False),
(4, 'Jane Smith', hash_password('user123'), 'jane.smith@email.com', False, False),
(5, 'Mike Johnson', hash_password('user123'), 'mike.johnson@email.com', False, False)
]
insert_sql = """
INSERT INTO users (UserId, Name, Password, EmailId, IsAdmin, IsLogin)
VALUES (:userid, :name, :password, :emailid, :isadmin, :islogin)
"""
for user in users_data:
conn.execute(text(insert_sql), {
'userid': user[0],
'name': user[1],
'password': user[2],
'emailid': user[3],
'isadmin': user[4],
'islogin': user[5]
})
conn.commit()
print("USERS table created and 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 creating users table: {e}")
if __name__ == "__main__":
main()