Skip to content

Commit

Permalink
Doctrine: multi database support
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Dec 12, 2024
1 parent 1bf3d13 commit dab8bf8
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 39 deletions.
Binary file added .docs/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
103 changes: 78 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,77 @@ Website 🚀 <a href="https://contributte.org">contributte.org</a> | Contact
</p>

<p align=center>
<img src="https://api.microlink.io?url=https%3A%2F%2Fexamples.contributte.org%2Fdoctrine-skeleton%2F&overlay.browser=light&screenshot=true&meta=false&embed=screenshot.url"></img>
<a href="https://examples.contributte.org/doctrine-skeleton/">examples.contributte.org/doctrine-skeleton/</a>
</p>

<p align=center>
<img src="https://api.microlink.io?url=https%3A%2F%2Fexamples.contributte.org%2Fdoctrine-skeleton%2F&overlay.browser=light&screenshot=true&meta=false&embed=screenshot.url"/>
</p>

-----

## Goal
## Introduction

Main goal is to provide easy-to-use starter-kit project for Nette developers.
This is a simple application with basic setup of [Doctrine](https://www.doctrine-project.org/) and [Nette](https://nette.org/).

## Demo
To test this application, follow these steps:

https://examples.contributte.org/doctrine-skeleton/
1. Create a new project.
2. Install dependencies.
3. Setup configuration.
4. Run the application.

## Installation

You will need `PHP 8.3+` and [Composer](https://getcomposer.org/).
You will need `PHP 8.3+` and [Composer](https://getcomposer.org/) and [Git](https://git-scm.com/) installed.

Install this application using **Composer** or **Git**.

**Composer**

Create project using composer.

```bash
composer create-project -s dev contributte/doctrine-skeleton acme
```

Now you have application installed. It's time to run it.
**Git**

```bash
git clone [email protected]:contributte/doctrine-skeleton.git acme
```

Now you have application installed. It's time to install dependencies.

```bash
cd acme
composer install
```

## Configuration

This application uses 2 databases PostgreSQL and MariaDB. You have to configure them in `local.neon`.
This is how default configuration looks like.

```neon
parameters:
postgres:
driver: pdo_pgsql
host: 0.0.0.0
port: 5432
user: contributte
password: contributte
dbname: demopostgres
mariadb:
driver: mysqli
host: 0.0.0.0
port: 3306
user: contributte
password: contributte
dbname: demomariadb
```

## Startup
## Development

The easiest way is to use php built-in web server.

Expand All @@ -55,27 +100,35 @@ php -S 0.0.0.0:8000 -t www

Then visit [http://localhost:8000](http://localhost:8000) in your browser.

## Backend
## Usage

It's based on [Contributte](https://contributte.org/) packages.
To setup this application properly, you have to execute migrations.

- [`contributte/application`](https://github.com/contributte/application)
- [`contributte/bootstrap`](https://github.com/contributte/bootstrap)
- [`contributte/di`](https://github.com/contributte/di)
- [`contributte/forms`](https://github.com/contributte/forms)
- [`contributte/http`](https://github.com/contributte/http)
- [`contributte/utils`](https://github.com/contributte/utils)
- [`contributte/latte`](https://github.com/contributte/latte)
- [`contributte/tracy`](https://github.com/contributte/tracy)
1. For **PostgreSQL** database.

- [`contributte/doctrine-dbal`](https://github.com/contributte/doctrine-dbal)
- [`contributte/doctrine-orm`](https://github.com/contributte/doctrine-orm)
- [`contributte/doctrine-annotations`](https://github.com/contributte/doctrine-annotations)
- [`contributte/doctrine-cache`](https://github.com/contributte/doctrine-cache)
- [`contributte/doctrine-migrations`](https://github.com/contributte/doctrine-migrations)
- [`contributte/doctrine-fixtures`](https://github.com/contributte/doctrine-fixtures)
```bash
# run migrations
NETTE__MIGRATION__DB=postgres NETTE__MIGRATION__MANAGER=default bin/console migrations:migrate

## Development
# or generate new migration
#NETTE__MIGRATION__DB=postgres NETTE__MIGRATION__MANAGER=default bin/console migrations:diff
```

2. For **MariaDB** database.

```bash
# run migrations
NETTE__MIGRATION__DB=mariadb NETTE__MIGRATION__MANAGER=second bin/console migrations:migrate

# or generate new migration
#NETTE__MIGRATION__DB=mariadb NETTE__MIGRATION__MANAGER=second bin/console migrations:diff
```

## Screenshots

![](.docs/screenshot.png)

## Maintenance

See [how to contribute](https://contributte.org/contributing.html) to this package.

Expand Down
18 changes: 9 additions & 9 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions config/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ parameters:
password: contributte
dbname: contributte

migration:
db: postgres
manager: default
# ======================================
# Services =============================
services:
Expand Down
3 changes: 2 additions & 1 deletion config/doctrine.neon
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@ nettrine.fixtures:

nettrine.migrations:
directories:
DB\Migrations: %appDir%/../db/Migrations
DB\Migrations: %appDir%/../db/%migration.db%
manager: %migration.manager%
4 changes: 2 additions & 2 deletions config/local.neon.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
# port: 5432
# user: contributte
# password: contributte
# dbname: contributte
# dbname: demopostgres
# mariadb:
# driver: mysqli
# host: 0.0.0.0
# port: 3306
# user: contributte
# password: contributte
# dbname: contributte
# dbname: demomariadb
31 changes: 31 additions & 0 deletions db/mariadb/Version20241212192036.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace DB\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20241212192036 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE TABLE `user` (id INT AUTO_INCREMENT NOT NULL, username VARCHAR(255) NOT NULL, created_at DATETIME NOT NULL, updated_at DATETIME DEFAULT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET UTF8');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('DROP TABLE `user`');
}
}
31 changes: 31 additions & 0 deletions db/postgres/Version20241212173845.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace DB\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20241212173845 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE TABLE "user" (id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL, username VARCHAR(255) NOT NULL, created_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, updated_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, PRIMARY KEY(id))');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('DROP TABLE "user"');
}
}
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ services:
image: postgres:15-alpine
restart: always
environment:
POSTGRES_DB: contributte
POSTGRES_DB: demopostgres
POSTGRES_USER: contributte
POSTGRES_PASSWORD: contributte
ports:
Expand All @@ -15,7 +15,7 @@ services:
image: mariadb:10.10
restart: always
environment:
MARIADB_DATABASE: contributte
MARIADB_DATABASE: demomariadb
MARIADB_USER: contributte
MARIADB_PASSWORD: contributte
MARIADB_ROOT_PASSWORD: contributte
Expand Down

0 comments on commit dab8bf8

Please sign in to comment.