Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 43 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ UNAME_S := $(shell uname -s)
SQLITE_PRISMA_DATABASE_URL ?= file:../../db/main.db
# set param statement_cache_size=1 to avoid query error `ERROR: cached plan must not change result type` after alter column type (modify field type)
POSTGES_PRISMA_DATABASE_URL ?= postgresql://teable:teable\@127.0.0.1:5432/teable?schema=public\&statement_cache_size=1
MYSQL_PRISMA_DATABASE_URL ?= mysql://root:your_password\@127.0.0.1:3306/your_database_name

# If the first make argument is "start", "stop"...
ifeq (docker.start,$(firstword $(MAKECMDGOALS)))
Expand Down Expand Up @@ -113,13 +114,15 @@ DOCKER_COMPOSE_ARGS := DOCKER_UID=$(shell id -u) \
define print_db_mode_options
@echo -e "\nSelect a database to start.\n"
@echo -e "1)sqlite Lightweight embedded, ideal for mobile and embedded systems, simple, resource-efficient, easy integration (default database)"
@echo -e "2)postges(pg) Powerful and scalable, suitable for complex enterprise needs, highly customizable, rich community support\n"
@echo -e "2)postges(pg) Powerful and scalable, suitable for complex enterprise needs, highly customizable, rich community support"
@echo -e "3)mysql Popular open-source relational database, widely used in web applications, good performance and reliability\n"
endef

define print_db_push_options
@echo -e "The 'db pull' command connects to your database and adds Prisma models to your Prisma schema that reflect the current database schema.\n"
@echo -e "1) sqlite"
@echo -e "2) postges(pg)\n"
@echo -e "2) postges(pg)"
@echo -e "3) mysql\n"
endef

.PHONY: db-mode sqlite.mode postgres.mode gen-prisma-schema gen-sqlite-prisma-schema gen-postgres-prisma-schema
Expand Down Expand Up @@ -228,7 +231,16 @@ gen-postgres-prisma-schema:
echo '{ "PRISMA_PROVIDER": "postgres" }' | pnpm mustache - ./prisma/template.prisma > ./prisma/postgres/schema.prisma
@echo 'generate【 prisma/postgres/schema.prisma 】success.'

gen-prisma-schema: gen-sqlite-prisma-schema gen-postgres-prisma-schema ## Generate 'schema.prisma' files for all versions of the system
gen-mysql-prisma-schema:
@cd ./packages/db-main-prisma; \
echo '{ "PRISMA_PROVIDER": "mysql" }' | pnpm mustache - ./prisma/template.prisma > ./prisma/mysql/schema.prisma; \
sed -i '' 's/detailDesc String? @map("detail_desc")/detailDesc String? @map("detail_desc") @db.Text/' ./prisma/mysql/schema.prisma || true; \
sed -i '' 's/i18n String?/i18n String? @db.Text/' ./prisma/mysql/schema.prisma || true; \
sed -i '' 's/config String?/config String? @db.Text/' ./prisma/mysql/schema.prisma || true; \
sed -i '' 's/positions String/positions String @db.Text/' ./prisma/mysql/schema.prisma || true
@echo 'generate【 prisma/mysql/schema.prisma 】success.'

gen-prisma-schema: gen-sqlite-prisma-schema gen-postgres-prisma-schema gen-mysql-prisma-schema ## Generate 'schema.prisma' files for all versions of the system

sqlite-db.push: ## db.push by sqlite
@cd ./packages/db-main-prisma; \
Expand All @@ -238,6 +250,12 @@ postgres-db.push: ## db.push by postgres
@cd ./packages/db-main-prisma; \
pnpm prisma-db-push --schema ./prisma/postgres/schema.prisma

mysql-db.push: ## db.push by mysql
@cd ./packages/db-main-prisma; \
echo "Note: If you have existing tables with foreign keys, you may need to disable foreign key checks first."; \
echo "Run: mysql -u root -p your_database_name -e \"SET FOREIGN_KEY_CHECKS=0;\" before this command if needed."; \
pnpm prisma-db-push --schema ./prisma/mysql/schema.prisma --accept-data-loss

db.push: ## connects to your database and adds Prisma models to your Prisma schema that reflect the current database schema.
$(print_db_push_options)
@read -p "Enter a command: " command; \
Expand All @@ -247,6 +265,9 @@ db.push: ## connects to your database and adds Prisma models to your Prisma sch
elif [ "$$command" = "2" ] || [ "$$command" = "postges" ] || [ "$$command" = "pg" ]; then \
make gen-postgres-prisma-schema; \
make postgres-db.push; \
elif [ "$$command" = "3" ] || [ "$$command" = "mysql" ]; then \
make gen-mysql-prisma-schema; \
make mysql-db.push; \
else echo "Unknown command."; fi

sqlite-db-migration:
Expand Down Expand Up @@ -275,6 +296,17 @@ postgres.mode: ## postgres.mode
@cd ./packages/db-main-prisma; \
pnpm prisma-generate --schema ./prisma/postgres/schema.prisma; \
pnpm prisma-migrate deploy --schema ./prisma/postgres/schema.prisma

mysql.mode: ## mysql.mode
@cd ./packages/db-main-prisma; \
pnpm prisma-generate --schema ./prisma/mysql/schema.prisma; \
if [ -d "./prisma/mysql/migrations" ] && [ "$(ls -A ./prisma/mysql/migrations 2>/dev/null)" ]; then \
pnpm prisma-migrate deploy --schema ./prisma/mysql/schema.prisma; \
else \
echo "No migrations found. Using db push instead of migrate deploy."; \
pnpm prisma-db-push --schema ./prisma/mysql/schema.prisma --skip-generate || true; \
fi

# Override environment variable files based on variables
RUN_DB_MODE ?= sqlite
FILE_ENV_PATHS = $(ENV_PATH)/.env.development* $(ENV_PATH)/.env.test*
Expand All @@ -294,6 +326,11 @@ else ifeq ($(CI)-$(RUN_DB_MODE),0-postges)
echo $$file; \
perl -i -pe 's~^PRISMA_DATABASE_URL=.*~PRISMA_DATABASE_URL=$(POSTGES_PRISMA_DATABASE_URL)~' $$file; \
done
else ifeq ($(CI)-$(RUN_DB_MODE),0-mysql)
@for file in $(FILE_ENV_PATHS); do \
echo $$file; \
perl -i -pe 's~^PRISMA_DATABASE_URL=.*~PRISMA_DATABASE_URL=$(MYSQL_PRISMA_DATABASE_URL)~' $$file; \
done
endif

switch-db-mode: ## Switch Database environment
Expand All @@ -307,6 +344,9 @@ switch-db-mode: ## Switch Database environment
make docker.up teable-postgres; \
make docker.await teable-postgres; \
make postgres.mode; \
elif [ "$$command" = "3" ] || [ "$$command" = "mysql" ]; then \
make switch.prisma.env RUN_DB_MODE=mysql; \
make mysql.mode; \
else \
echo "Unknown command."; fi

Expand Down
237 changes: 237 additions & 0 deletions SETUP_MYSQL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
# Setting Up Teable with MySQL Locally

This guide will help you set up and run Teable locally with MySQL as your database.

## Prerequisites

1. **Node.js** (>=22.0.0) and **pnpm** (>=9.13.0)
2. **MySQL** server running locally (or accessible remotely)
3. **Corepack** enabled

## Step 1: Install Dependencies

```bash
# Enable corepack (if not already enabled)
corepack enable

# Install all project dependencies
pnpm install
```

## Step 2: Generate MySQL Prisma Schema

Since MySQL support was just added, you need to generate the MySQL Prisma schema:

```bash
make gen-mysql-prisma-schema
```

This will create `packages/db-main-prisma/prisma/mysql/schema.prisma`.

## Step 3: Set Up Environment Variables

### Option A: Using MySQL-specific environment variables (Recommended)

Create or edit `apps/nextjs-app/.env.development.local`:

```env
# Database Connection
PRISMA_DATABASE_URL=mysql2://root:your_password@localhost:3306/your_database_name

# MySQL Writer Connection (optional, used if provided)
WRITER_DB_SCHEMA=your_database_name
WRITER_DB_USERNAME=root
WRITER_DB_PASSWORD=your_password
WRITER_DB_HOSTNAME=localhost
WRITER_DB_PORT=3306
WRITER_DB_POOL_MIN=1
WRITER_DB_POOL_MAX=10
WRITER_DB_POOL_ACQUIRE=30000
WRITER_DB_POOL_IDLE=10000

# MySQL Reader Connection (optional, for read/write splitting)
READER_DB_SCHEMA=your_database_name
READER_DB_USERNAME=root
READER_DB_PASSWORD=your_password
READER_DB_HOSTNAME=localhost
READER_DB_PORT=3306
READER_DB_POOL_MIN=1
READER_DB_POOL_MAX=10
READER_DB_POOL_ACQUIRE=30000
READER_DB_POOL_IDLE=10000

# Application Settings
PUBLIC_ORIGIN=http://localhost:3000
PORT=3000
NODE_ENV=development

# Optional: Cache Configuration
BACKEND_CACHE_PROVIDER=memory
```

### Option B: Using only PRISMA_DATABASE_URL

If you prefer to use only the connection string:

```env
PRISMA_DATABASE_URL=mysql2://root:your_password@localhost:3306/your_database_name
PUBLIC_ORIGIN=http://localhost:3000
PORT=3000
NODE_ENV=development
```

## Step 4: Prepare MySQL Database

Make sure your MySQL database exists and is accessible:

```bash
# Connect to MySQL and create the database if it doesn't exist
mysql -u root -p -e "CREATE DATABASE IF NOT EXISTS your_database_name;"
```

## Step 5: Run Database Migrations

Push the Prisma schema to your MySQL database:

```bash
cd packages/db-main-prisma
pnpm prisma-db-push --schema ./prisma/mysql/schema.prisma
```

Or generate and run migrations:

```bash
# Generate Prisma client for MySQL
cd packages/db-main-prisma
pnpm prisma-generate --schema ./prisma/mysql/schema.prisma

# Run migrations (if you have migration files)
pnpm prisma-migrate deploy --schema ./prisma/mysql/schema.prisma
```

## Step 6: Build Packages (First Time Only)

Before running the dev server for the first time, build the packages:

```bash
pnpm build:packages
```

## Step 7: Start the Development Server

The backend will automatically start the frontend (Next.js) server:

```bash
cd apps/nestjs-backend
pnpm dev
```

This will:

- Start the NestJS backend server
- Automatically start the Next.js frontend server
- Enable hot reload for both servers

## Step 8: Access the Application

Once the servers are running, you can access:

- **Frontend (UI)**: http://localhost:3000
- **Backend API**: http://localhost:3000/api (or check the console for the exact port)
- **API Documentation**: http://localhost:3000/api-docs (if Swagger is enabled)

## Troubleshooting

### Port Conflicts

If port 3000 is already in use:

```bash
# Check what's using the port
lsof -i:3000

# Kill the process if needed
kill -9 [PID]
```

### Database Connection Issues

1. **Verify MySQL is running:**

```bash
mysql -u root -p -e "SELECT 1;"
```

2. **Check connection string format:**

- Format: `mysql2://username:password@host:port/database`
- Make sure special characters in passwords are URL-encoded

3. **Verify database exists:**
```bash
mysql -u root -p -e "SHOW DATABASES LIKE 'your_database_name';"
```

### Prisma Client Issues

If you see errors about Prisma client:

```bash
cd packages/db-main-prisma
pnpm prisma-generate --schema ./prisma/mysql/schema.prisma
```

### Module Not Found Errors

If you see module resolution errors:

```bash
# Clean and reinstall
pnpm clean:global-cache
pnpm install
pnpm build:packages
```

## Development Workflow

### Making Code Changes

- Backend changes: The NestJS server will auto-reload
- Frontend changes: Next.js will hot-reload automatically
- Package changes: You may need to rebuild packages:
```bash
pnpm build:packages
```

### Database Schema Changes

If you modify the Prisma schema:

1. Update `packages/db-main-prisma/prisma/template.prisma`
2. Regenerate MySQL schema:
```bash
make gen-mysql-prisma-schema
```
3. Push changes to database:
```bash
cd packages/db-main-prisma
pnpm prisma-db-push --schema ./prisma/mysql/schema.prisma
```

## Additional Notes

- **WebSocket Port**: In development, Next.js uses port 3000 for hot reload. The application's WebSocket uses port 3001 to avoid conflicts.
- **Plugin Development**: To develop plugins, start the plugin server separately:
```bash
cd plugins
pnpm dev
```
This runs on port 3002.

## Next Steps

Once everything is running:

1. Open http://localhost:3000 in your browser
2. Create your first account
3. Start building your database!
Loading