Skip to content

Commit d7428f6

Browse files
committed
feat: auto-initialize database on dev start
- Detect if database exists before starting services - Automatically run migrations if database not found - No need to manually run dev:setup anymore - Improved developer experience
1 parent 22023c9 commit d7428f6

1 file changed

Lines changed: 26 additions & 5 deletions

File tree

scripts/dev.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,36 @@ ${colors.yellow}Service Ports:${colors.reset}
150150
function checkDatabase() {
151151
const dbPath = join(ROOT_DIR, ".wrangler/state/v3/d1");
152152
if (!existsSync(dbPath)) {
153-
log(
154-
"\n⚠️ Local database not found. Run setup first:\n pnpm run dev:setup\n",
155-
colors.yellow,
156-
);
157-
return false;
153+
log("\n⚠️ Local database not found.", colors.yellow);
154+
log("Creating and initializing database...\n", colors.blue);
155+
return initializeDatabase();
158156
}
159157
return true;
160158
}
161159

160+
// Initialize database with migrations
161+
function initializeDatabase() {
162+
try {
163+
const { execSync } = require("child_process");
164+
const accountApiPath = join(ROOT_DIR, "services/account-api");
165+
166+
log("Running database migrations...", colors.blue);
167+
execSync("wrangler d1 migrations apply edgeauth-db --local", {
168+
cwd: accountApiPath,
169+
stdio: "inherit",
170+
});
171+
172+
log("\n✓ Database initialized successfully!\n", colors.green);
173+
return true;
174+
} catch (error) {
175+
log(`\n✗ Failed to initialize database: ${error.message}`, colors.red);
176+
log("\nPlease run manually:", colors.yellow);
177+
log(" cd services/account-api", colors.yellow);
178+
log(" wrangler d1 migrations apply edgeauth-db --local\n", colors.yellow);
179+
return false;
180+
}
181+
}
182+
162183
// Start a service
163184
function startService(service, type = "backend") {
164185
return new Promise((resolve, reject) => {

0 commit comments

Comments
 (0)