Skip to content
This repository was archived by the owner on Dec 23, 2024. It is now read-only.

Commit

Permalink
Upgrade (lots of Changes)
Browse files Browse the repository at this point in the history
  • Loading branch information
PixoVoid committed Dec 3, 2024
1 parent c94fde9 commit af39ba6
Show file tree
Hide file tree
Showing 3 changed files with 810 additions and 239 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ MARIADB_PASSWORD=
# Other Configurations
TABLE_ENGINE=InnoDB
BATCH_SIZE=100
LOG_LEVEL=INFO
TIMEZONE=UTC
179 changes: 118 additions & 61 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,96 +1,153 @@
# PostgreSQL to MariaDB Migration Script
# PostgreSQL to MariaDB Migration Tool

This PHP script helps migrate data from a PostgreSQL database to a MariaDB database. It handles the creation of tables in MariaDB based on PostgreSQL schemas and efficiently transfers data using batch processing to optimize performance.
A robust PHP-based tool designed to automate the migration of databases from PostgreSQL to MariaDB, ensuring data integrity, preservation of relationships, and structure.

---

## ⚠️ Warning

**This script is currently under development. Use at your own risk.**
This script is currently under development. Use it at your own risk.

The author assumes no liability for any damages or data loss caused by the use of this script. Ensure that you create proper backups of your databases before running the migration.

Please read this entire `README.md` carefully before proceeding.

---

## 🚀 Features

- Automated schema and data migration.
- Foreign key and index preservation.
- Progress tracking and detailed logging.
- Batch processing for optimal performance.

---

The author **assumes no liability for any damages or data loss** caused by the use of this script. Before running this migration, make sure to create proper backups of your databases.
## 👨‍💻 Author

**Please read this entire README.md carefully before proceeding with the migration.**
- **PixoVoid**
- Email: [[email protected]](mailto:[email protected])
- Website: [pixovoid.net](https://pixovoid.net)
- GitHub: [PixoVoid-net/pgsql-mariadb-migrate](https://github.com/PixoVoid-net/pgsql-mariadb-migrate)

## 🛠️ Requirements
---

- PHP 8.3 or higher
- PostgreSQL and MariaDB databases
- Database credentials with sufficient access
- A `.env` file to store configuration (see example below)
## 📜 License

This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.

---

## 🆚 Version

## 📝 Features
- Version: `1.0.0`

- Migrates PostgreSQL tables to MariaDB.
- Automatically converts data types from PostgreSQL to MariaDB.
- Uses batch processing to improve performance during data transfer.
- Supports configurable table engines (e.g., InnoDB).
- Logs progress and errors to a migration log file (`migration.log`).
---

## Data Type Mapping
## 🧩 Requirements

The script converts PostgreSQL data types to MariaDB data types using the following mapping:
- PHP >= 8.3
- Required PHP Extensions:
- `ext-pdo`
- `ext-pdo_pgsql`
- `ext-pdo_mysql`

| PostgreSQL Type | MariaDB Type |
|-------------------------------------|--------------------|
| `smallint` | `INT` |
| `integer` | `INT` |
| `bigint` | `BIGINT` |
| `boolean` | `TINYINT(1)` |
| `character varying` | `VARCHAR(255)` |
| `text` | `TEXT` |
| `timestamp without time zone` | `DATETIME` |
| `date` | `DATE` |
| `numeric` | `DECIMAL(20,6)` |
---

## 🔧 Installation & Setup
## 🔧 Prerequisites

1. **Clone the repository:**
- PostgreSQL 9.4+
- MariaDB 10.3+

---

## 📥 Installation

1. Clone the repository:
```bash
git clone https://github.com/PixoVoid-net/pgsql-mariadb-migrate
git clone https://github.com/PixoVoid-net/pgsql-mariadb-migrate.git
cd pgsql-mariadb-migrate
```

2. **Configure environment variables:**

Create a `.env` file in the root directory with the following content, adjusting for your environment:

```plaintext
# PostgreSQL Configuration
PGSQL_HOST=your_pgsql_host
2. Create a `.env` file in the project root directory and configure it with your database credentials:
```env
PGSQL_HOST=localhost
PGSQL_PORT=5432
PGSQL_DBNAME=your_pgsql_dbname
PGSQL_USER=your_pgsql_user
PGSQL_PASSWORD=your_pgsql_password
PGSQL_DBNAME=your_database
PGSQL_USER=your_username
PGSQL_PASSWORD=your_password
# MariaDB Configuration
MARIADB_HOST=your_mariadb_host
MARIADB_HOST=localhost
MARIADB_PORT=3306
MARIADB_DBNAME=your_mariadb_dbname
MARIADB_USER=your_mariadb_user
MARIADB_PASSWORD=your_mariadb_password
# Optional: Table Engine (e.g., InnoDB, MyISAM)
TABLE_ENGINE=InnoDB
MARIADB_DBNAME=your_database
MARIADB_USER=your_username
MARIADB_PASSWORD=your_password
```

3. **Run the migration script:**
---

## ⚙️ Configuration

### Database Settings

The tool uses environment variables to manage database connections. All configurations are stored in the `.env` file.

### Migration Constants

Key configuration constants in the `DatabaseConfig` class:
- `BATCH_SIZE`: The number of records to process per batch (default: 100).
- `DEFAULT_ENGINE`: Default storage engine for tables (default: InnoDB).
- `CHARSET`: Default character set (default: utf8mb4).
- `COLLATION`: Default collation (default: utf8mb4_unicode_ci).

Execute the following command to start the migration process:
---

## 🏁 Usage

1. Ensure your `.env` file is configured correctly.
2. Run the migration script:
```bash
php migration.php
```

The script will display a warning message about usage at your own risk, and you will need to confirm by pressing Enter to proceed.
The tool will:
1. Verify the required PHP extensions.
2. Connect to both PostgreSQL and MariaDB databases.
3. Create tables in the correct dependency order.
4. Migrate data with proper type conversion.
5. Establish foreign key constraints.
6. Add indexes.
7. Set up audit triggers.

## 🏗️ How it Works
---

- **Step 1:** The script connects to both PostgreSQL and MariaDB using the credentials provided in the `.env` file.
- **Step 2:** It checks for all available tables in the PostgreSQL database.
- **Step 3:** For each table, it creates an equivalent table in MariaDB with the appropriate data types.
- **Step 4:** The script transfers the data from PostgreSQL to MariaDB in batches for efficiency.
- **Step 5:** Logs are generated for each operation, including success and failure messages.
## 📊 Data Type Mappings

## 📜 License
| PostgreSQL Type | MariaDB Type | Notes |
|------------------------|---------------------|-------------------------------------|
| `smallint` | `INT` | |
| `integer` | `INT` | |
| `bigint` | `BIGINT` | |
| `boolean` | `TINYINT(1)` | `0` = false, `1` = true |
| `character varying` | `VARCHAR(255)` | |
| `text` | `TEXT` | |
| `timestamp` | `DATETIME` | |
| `date` | `DATE` | |
| `numeric` | `DECIMAL(20,6)` | |

---

## 🔍 Logging and Monitoring

- Migration progress is displayed in real-time with a visual progress bar.
- All operations are logged to `migration.log`.
- Console output uses color coding for different message types:
- 🟢 **Success** messages
- 🔴 **Error** messages
- 🟡 **Warning** messages

---

## 📝 License

This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for more details.
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
Loading

0 comments on commit af39ba6

Please sign in to comment.