Versão 0.5 antes do PWA
This commit is contained in:
@@ -8,6 +8,41 @@ if (!env.DATABASE_URL) throw new Error('DATABASE_URL is not set');
|
||||
|
||||
const client = new Database(env.DATABASE_URL);
|
||||
|
||||
// Run schema migration if 'name' column is missing
|
||||
try {
|
||||
const tableInfo = client.prepare("PRAGMA table_info(users)").all() as any[];
|
||||
const hasNameColumn = tableInfo.some((col) => col.name === 'name');
|
||||
if (!hasNameColumn) {
|
||||
console.log("Migrating users table: adding 'name' column...");
|
||||
|
||||
// 1. Add nullable 'name' column
|
||||
client.prepare("ALTER TABLE users ADD COLUMN name TEXT").run();
|
||||
|
||||
// 2. Set 'name' to the current 'username' (which represented the name before)
|
||||
client.prepare("UPDATE users SET name = username").run();
|
||||
|
||||
// 3. Update 'username' to the lowercase first name
|
||||
const allUsers = client.prepare("SELECT id, name FROM users").all() as any[];
|
||||
for (const u of allUsers) {
|
||||
const fullName = u.name || '';
|
||||
const firstName = fullName.trim().split(/\s+/)[0].toLowerCase();
|
||||
|
||||
let uniqueUsername = firstName;
|
||||
let counter = 1;
|
||||
while (true) {
|
||||
const exists = client.prepare("SELECT id FROM users WHERE username = ? AND id != ?").get(uniqueUsername, u.id);
|
||||
if (!exists) break;
|
||||
uniqueUsername = `${firstName}${counter}`;
|
||||
counter++;
|
||||
}
|
||||
client.prepare("UPDATE users SET username = ? WHERE id = ?").run(uniqueUsername, u.id);
|
||||
}
|
||||
console.log("Database migration for users table completed.");
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("Migration failed:", err);
|
||||
}
|
||||
|
||||
export const db = drizzle(client, { schema });
|
||||
|
||||
// Seed admin user if the users table is empty (US00)
|
||||
@@ -17,6 +52,7 @@ try {
|
||||
console.log('Seeding default admin user (refoodpdn)...');
|
||||
const passwordHash = bcrypt.hashSync('rpdn!2512', 10);
|
||||
db.insert(schema.users).values({
|
||||
name: 'Administrador Refood',
|
||||
username: 'refoodpdn',
|
||||
passwordHash: passwordHash,
|
||||
role: 'admin'
|
||||
|
||||
@@ -2,6 +2,7 @@ import { integer, sqliteTable, text } from 'drizzle-orm/sqlite-core';
|
||||
|
||||
export const users = sqliteTable('users', {
|
||||
id: text('id').primaryKey().$defaultFn(() => crypto.randomUUID()),
|
||||
name: text('name').notNull(),
|
||||
username: text('username').notNull().unique(),
|
||||
passwordHash: text('password_hash').notNull(),
|
||||
role: text('role').notNull(), // 'admin' | 'shift_manager' | 'volunteer'
|
||||
|
||||
Reference in New Issue
Block a user