Skip to content

Commit

Permalink
Fix database initialization and deployment
Browse files Browse the repository at this point in the history
- Remove sudo from PHP commands
- Improve database initialization script
- Add schema update support
- Handle existing data properly
  • Loading branch information
screenfluent committed Nov 17, 2024
1 parent 97e238a commit 15a9d1c
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 66 deletions.
123 changes: 68 additions & 55 deletions db/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,71 +12,84 @@
}

$db = new Database();

// Always apply schema (handles both new and existing databases)
$result = $db->db->exec($schema);
if ($result === false) {
throw new Exception("Failed to execute schema: " . $db->db->lastErrorMsg());
}

// Sample implementations
$implementations = [
[
'name' => 'Superwall',
'logo_url' => '/logos/superwall.svg',
'description' => 'Paywall infrastructure for mobile apps',
'llms_txt_url' => 'https://docs.superwall.com/llms.txt',
'has_full' => 1,
'is_featured' => 1,
'is_requested' => 0,
'is_draft' => 0,
'votes' => 15
],
[
'name' => 'Anthropic',
'logo_url' => '/logos/anthropic.svg',
'description' => 'AI research company and creator of Claude',
'llms_txt_url' => 'https://docs.anthropic.com/llms.txt',
'has_full' => 1,
'is_featured' => 1,
'is_requested' => 0,
'is_draft' => 0,
'votes' => 25
]
];
// Only add sample data if no implementations exist
$stmt = $db->db->prepare('SELECT COUNT(*) as count FROM implementations');
$result = $stmt->execute();
$count = $result->fetchArray(SQLITE3_ASSOC)['count'];

// Requested implementations
$requested = [
[
'name' => 'Vercel',
'logo_url' => '/logos/vercel.png',
'description' => 'Frontend cloud platform and framework provider',
'llms_txt_url' => 'https://vercel.com/docs/llms.txt',
'has_full' => 0,
'is_featured' => 0,
'is_requested' => 1,
'is_draft' => 0,
'votes' => 42
],
[
'name' => 'Next.js',
'logo_url' => '/logos/nextjs.png',
'description' => 'React framework for production',
'llms_txt_url' => 'https://nextjs.org/docs/llms.txt',
'has_full' => 0,
'is_featured' => 0,
'is_requested' => 1,
'is_draft' => 0,
'votes' => 38
]
];
if ($count === 0) {
echo "Adding sample data...\n";

// Sample implementations
$implementations = [
[
'name' => 'Superwall',
'logo_url' => '/logos/superwall.svg',
'description' => 'Paywall infrastructure for mobile apps',
'llms_txt_url' => 'https://docs.superwall.com/llms.txt',
'has_full' => 1,
'is_featured' => 1,
'is_requested' => 0,
'is_draft' => 0,
'votes' => 15
],
[
'name' => 'Anthropic',
'logo_url' => '/logos/anthropic.svg',
'description' => 'AI research company and creator of Claude',
'llms_txt_url' => 'https://docs.anthropic.com/llms.txt',
'has_full' => 1,
'is_featured' => 1,
'is_requested' => 0,
'is_draft' => 0,
'votes' => 25
]
];

// Insert all implementations
foreach (array_merge($implementations, $requested) as $impl) {
if (!$db->addImplementation($impl)) {
error_log("Failed to add implementation: {$impl['name']}");
// Requested implementations
$requested = [
[
'name' => 'Vercel',
'logo_url' => '/logos/vercel.png',
'description' => 'Frontend cloud platform and framework provider',
'llms_txt_url' => 'https://vercel.com/docs/llms.txt',
'has_full' => 0,
'is_featured' => 0,
'is_requested' => 1,
'is_draft' => 0,
'votes' => 42
],
[
'name' => 'Next.js',
'logo_url' => '/logos/nextjs.png',
'description' => 'React framework for production',
'llms_txt_url' => 'https://nextjs.org/docs/llms.txt',
'has_full' => 0,
'is_featured' => 0,
'is_requested' => 1,
'is_draft' => 0,
'votes' => 38
]
];

// Insert all implementations
foreach (array_merge($implementations, $requested) as $impl) {
if (!$db->addImplementation($impl)) {
error_log("Failed to add implementation: {$impl['name']}");
}
}
} else {
echo "Database already contains data, skipping sample data.\n";
}

echo "Database initialized successfully.\n";
echo "Database initialization/update completed successfully.\n";
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
exit(1);
Expand Down
17 changes: 6 additions & 11 deletions forge/deploy.production.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,26 @@ chown -R llmstxtdirectory:www-data public/logos
chmod -R 775 public/logos

# Create and initialize database if it doesn't exist
mkdir -p db
chmod 755 db

if [ ! -f "db/votes.db" ] || [ ! -s "db/votes.db" ]; then
echo "Initializing database..."
rm -f db/votes.db
touch db/votes.db
chown llmstxtdirectory:www-data db/votes.db
chmod 664 db/votes.db
sudo -u llmstxtdirectory php db/init.php
php db/init.php
else
echo "Database exists, checking schema..."
# Apply schema updates
sudo -u llmstxtdirectory php -r "
require_once 'db/database.php';
\$db = new Database();
\$schema = file_get_contents('db/schema.sql');
\$db->db->exec(\$schema);
"
# Apply schema updates without sudo
php db/init.php
fi

# Set database permissions
chown llmstxtdirectory:www-data db/votes.db
chmod 664 db/votes.db

# Ensure db directory is accessible
chmod 755 db

# Restart PHP
( flock -w 10 9 || exit 1
echo 'Restarting FPM...'; sudo -S service $FORGE_PHP_FPM reload ) 9>/tmp/fpmlock
Expand Down

0 comments on commit 15a9d1c

Please sign in to comment.