support setting for writing supabase migration files (#427)
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import { useState } from "react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Switch } from "@/components/ui/switch";
|
||||
import { Label } from "@/components/ui/label";
|
||||
// We might need a Supabase icon here, but for now, let's use a generic one or text.
|
||||
// import { Supabase } from "lucide-react"; // Placeholder
|
||||
import { DatabaseZap } from "lucide-react"; // Using DatabaseZap as a placeholder
|
||||
@@ -16,6 +18,8 @@ export function SupabaseIntegration() {
|
||||
// Clear the entire supabase object in settings
|
||||
const result = await updateSettings({
|
||||
supabase: undefined,
|
||||
// Also disable the migration setting on disconnect
|
||||
enableSupabaseWriteSqlMigration: false,
|
||||
});
|
||||
if (result) {
|
||||
showSuccess("Successfully disconnected from Supabase");
|
||||
@@ -31,6 +35,17 @@ export function SupabaseIntegration() {
|
||||
}
|
||||
};
|
||||
|
||||
const handleMigrationSettingChange = async (enabled: boolean) => {
|
||||
try {
|
||||
await updateSettings({
|
||||
enableSupabaseWriteSqlMigration: enabled,
|
||||
});
|
||||
showSuccess("Setting updated");
|
||||
} catch (err: any) {
|
||||
showError(err.message || "Failed to update setting");
|
||||
}
|
||||
};
|
||||
|
||||
// Check if there's any Supabase accessToken to determine connection status
|
||||
const isConnected = !!settings?.supabase?.accessToken;
|
||||
|
||||
@@ -39,26 +54,50 @@ export function SupabaseIntegration() {
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h3 className="text-sm font-medium text-gray-700 dark:text-gray-300">
|
||||
Supabase Integration
|
||||
</h3>
|
||||
<p className="text-xs text-gray-500 dark:text-gray-400 mt-1">
|
||||
Your account is connected to Supabase.
|
||||
</p>
|
||||
<div>
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h3 className="text-sm font-medium text-gray-700 dark:text-gray-300">
|
||||
Supabase Integration
|
||||
</h3>
|
||||
<p className="text-xs text-gray-500 dark:text-gray-400 mt-1">
|
||||
Your account is connected to Supabase.
|
||||
</p>
|
||||
</div>
|
||||
<Button
|
||||
onClick={handleDisconnectFromSupabase}
|
||||
variant="destructive"
|
||||
size="sm"
|
||||
disabled={isDisconnecting}
|
||||
className="flex items-center gap-2"
|
||||
>
|
||||
{isDisconnecting ? "Disconnecting..." : "Disconnect from Supabase"}
|
||||
<DatabaseZap className="h-4 w-4" />
|
||||
</Button>
|
||||
</div>
|
||||
<div className="mt-4">
|
||||
<div className="flex items-center space-x-3">
|
||||
<Switch
|
||||
id="supabase-migrations"
|
||||
checked={!!settings?.enableSupabaseWriteSqlMigration}
|
||||
onCheckedChange={handleMigrationSettingChange}
|
||||
/>
|
||||
<div className="space-y-1">
|
||||
<Label
|
||||
htmlFor="supabase-migrations"
|
||||
className="text-sm font-medium"
|
||||
>
|
||||
Write SQL migration files
|
||||
</Label>
|
||||
<p className="text-xs text-gray-500 dark:text-gray-400">
|
||||
Generate SQL migration files when modifying your Supabase schema.
|
||||
This helps you track database changes in version control, though
|
||||
these files aren't used for chat context, which uses the live
|
||||
schema.
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<Button
|
||||
onClick={handleDisconnectFromSupabase}
|
||||
variant="destructive"
|
||||
size="sm"
|
||||
disabled={isDisconnecting}
|
||||
className="flex items-center gap-2"
|
||||
>
|
||||
{isDisconnecting ? "Disconnecting..." : "Disconnect from Supabase"}
|
||||
<DatabaseZap className="h-4 w-4" /> {/* Placeholder icon */}
|
||||
</Button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -14,8 +14,10 @@ import {
|
||||
executeSupabaseSql,
|
||||
} from "../../supabase_admin/supabase_management_client";
|
||||
import { isServerFunction } from "../../supabase_admin/supabase_utils";
|
||||
import { SqlQuery } from "../../lib/schemas";
|
||||
import { SqlQuery, UserSettings } from "../../lib/schemas";
|
||||
import { gitCommit } from "../utils/git_utils";
|
||||
import { readSettings } from "@/main/settings";
|
||||
import { writeMigrationFile } from "../utils/file_utils";
|
||||
|
||||
const readFile = fs.promises.readFile;
|
||||
const logger = log.scope("response_processor");
|
||||
@@ -207,6 +209,7 @@ export async function processFullResponseActions(
|
||||
return {};
|
||||
}
|
||||
|
||||
const settings: UserSettings = readSettings();
|
||||
const appPath = getDyadAppPath(chatWithApp.app.path);
|
||||
const writtenFiles: string[] = [];
|
||||
const renamedFiles: string[] = [];
|
||||
@@ -247,6 +250,22 @@ export async function processFullResponseActions(
|
||||
supabaseProjectId: chatWithApp.app.supabaseProjectId!,
|
||||
query: query.content,
|
||||
});
|
||||
|
||||
// Only write migration file if SQL execution succeeded
|
||||
if (settings.enableSupabaseWriteSqlMigration) {
|
||||
try {
|
||||
await writeMigrationFile(
|
||||
appPath,
|
||||
query.content,
|
||||
query.description,
|
||||
);
|
||||
} catch (error) {
|
||||
errors.push({
|
||||
message: `Failed to write SQL migration file for: ${query.description}`,
|
||||
error: error,
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
errors.push({
|
||||
message: `Failed to execute SQL query: ${query.content}`,
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import fs from "node:fs";
|
||||
import { promises as fsPromises } from "node:fs";
|
||||
import path from "node:path";
|
||||
import fsExtra from "fs-extra";
|
||||
import { generateCuteAppName } from "../../lib/utils";
|
||||
|
||||
/**
|
||||
* Recursively gets all files in a directory, excluding node_modules and .git
|
||||
@@ -57,3 +59,36 @@ export async function copyDirectoryRecursive(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function writeMigrationFile(
|
||||
appPath: string,
|
||||
queryContent: string,
|
||||
queryDescription?: string,
|
||||
) {
|
||||
const migrationsDir = path.join(appPath, "supabase", "migrations");
|
||||
await fsExtra.ensureDir(migrationsDir);
|
||||
|
||||
const files = await fsExtra.readdir(migrationsDir);
|
||||
const migrationNumbers = files
|
||||
.map((file) => {
|
||||
const match = file.match(/^(\d{4})_/);
|
||||
return match ? parseInt(match[1], 10) : -1;
|
||||
})
|
||||
.filter((num) => num !== -1);
|
||||
|
||||
const nextMigrationNumber =
|
||||
migrationNumbers.length > 0 ? Math.max(...migrationNumbers) + 1 : 0;
|
||||
const paddedNumber = String(nextMigrationNumber).padStart(4, "0");
|
||||
|
||||
let description = "migration";
|
||||
if (queryDescription) {
|
||||
description = queryDescription.toLowerCase().replace(/[\s\W-]+/g, "_");
|
||||
} else {
|
||||
description = generateCuteAppName().replace(/-/g, "_");
|
||||
}
|
||||
|
||||
const migrationFileName = `${paddedNumber}_${description}.sql`;
|
||||
const migrationFilePath = path.join(migrationsDir, migrationFileName);
|
||||
|
||||
await fsExtra.writeFile(migrationFilePath, queryContent);
|
||||
}
|
||||
|
||||
@@ -142,6 +142,7 @@ export const UserSettingsSchema = z.object({
|
||||
enableProLazyEditsMode: z.boolean().optional(),
|
||||
enableProSmartFilesContextMode: z.boolean().optional(),
|
||||
selectedTemplateId: z.string().optional(),
|
||||
enableSupabaseWriteSqlMigration: z.boolean().optional(),
|
||||
|
||||
enableNativeGit: z.boolean().optional(),
|
||||
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
import { IS_TEST_BUILD } from "@/ipc/utils/test_utils";
|
||||
import { getSupabaseClient } from "./supabase_management_client";
|
||||
import { SUPABASE_SCHEMA_QUERY } from "./supabase_schema_query";
|
||||
|
||||
async function getPublishableKey({ projectId }: { projectId: string }) {
|
||||
if (IS_TEST_BUILD) {
|
||||
return "test-publishable-key";
|
||||
}
|
||||
|
||||
const supabase = await getSupabaseClient();
|
||||
let keys;
|
||||
try {
|
||||
@@ -50,6 +55,10 @@ export async function getSupabaseContext({
|
||||
}: {
|
||||
supabaseProjectId: string;
|
||||
}) {
|
||||
if (IS_TEST_BUILD) {
|
||||
return "[[TEST_BUILD_SUPABASE_CONTEXT]]";
|
||||
}
|
||||
|
||||
const supabase = await getSupabaseClient();
|
||||
const publishableKey = await getPublishableKey({
|
||||
projectId: supabaseProjectId,
|
||||
|
||||
@@ -140,6 +140,10 @@ export async function executeSupabaseSql({
|
||||
supabaseProjectId: string;
|
||||
query: string;
|
||||
}): Promise<string> {
|
||||
if (IS_TEST_BUILD) {
|
||||
return "{}";
|
||||
}
|
||||
|
||||
const supabase = await getSupabaseClient();
|
||||
const result = await supabase.runQuery(supabaseProjectId, query);
|
||||
return JSON.stringify(result);
|
||||
|
||||
Reference in New Issue
Block a user