first commit
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Database\Migrations;
|
||||
|
||||
use CodeIgniter\Database\Migration;
|
||||
|
||||
class CreateCategories extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
$this->forge->addField([
|
||||
'id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
|
||||
'name' => ['type' => 'VARCHAR', 'constraint' => 100],
|
||||
'type' => ['type' => 'ENUM', 'constraint' => ['income', 'expense']],
|
||||
'parent_id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'null' => true],
|
||||
'created_at' => ['type' => 'DATETIME', 'null' => true],
|
||||
'updated_at' => ['type' => 'DATETIME', 'null' => true],
|
||||
]);
|
||||
$this->forge->addKey('id', true);
|
||||
$this->forge->addKey('parent_id');
|
||||
$this->forge->addKey('type');
|
||||
$this->forge->createTable('categories', true, ['ENGINE' => 'InnoDB']);
|
||||
|
||||
// FK self-referencyjny: usuniecie rodzica -> dzieci dostaja parent_id = NULL
|
||||
$this->db->query(
|
||||
'ALTER TABLE `categories` ADD CONSTRAINT `fk_categories_parent` '
|
||||
. 'FOREIGN KEY (`parent_id`) REFERENCES `categories`(`id`) '
|
||||
. 'ON DELETE SET NULL ON UPDATE CASCADE'
|
||||
);
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
$this->forge->dropTable('categories', true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Database\Migrations;
|
||||
|
||||
use CodeIgniter\Database\Migration;
|
||||
|
||||
class CreateOperations extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
$this->forge->addField([
|
||||
'id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
|
||||
'date' => ['type' => 'DATE'],
|
||||
'amount' => ['type' => 'DECIMAL', 'constraint' => '12,2'],
|
||||
'type' => ['type' => 'ENUM', 'constraint' => ['income', 'expense']],
|
||||
'category_id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'null' => true],
|
||||
'description' => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
|
||||
'created_at' => ['type' => 'DATETIME', 'null' => true],
|
||||
'updated_at' => ['type' => 'DATETIME', 'null' => true],
|
||||
]);
|
||||
$this->forge->addKey('id', true);
|
||||
$this->forge->addKey('date');
|
||||
$this->forge->addKey('category_id');
|
||||
$this->forge->addKey('type');
|
||||
// Usuniecie kategorii -> operacje dostaja category_id = NULL
|
||||
$this->forge->addForeignKey('category_id', 'categories', 'id', 'CASCADE', 'SET NULL');
|
||||
$this->forge->createTable('operations', true, ['ENGINE' => 'InnoDB']);
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
$this->forge->dropTable('operations', true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Database\Migrations;
|
||||
|
||||
use CodeIgniter\Database\Migration;
|
||||
|
||||
class CreateInvInstruments extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
$this->forge->addField([
|
||||
'id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
|
||||
'name' => ['type' => 'VARCHAR', 'constraint' => 120],
|
||||
'created_at' => ['type' => 'DATETIME', 'null' => true],
|
||||
'updated_at' => ['type' => 'DATETIME', 'null' => true],
|
||||
]);
|
||||
$this->forge->addKey('id', true);
|
||||
$this->forge->addUniqueKey('name');
|
||||
$this->forge->createTable('inv_instruments', true, ['ENGINE' => 'InnoDB']);
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
$this->forge->dropTable('inv_instruments', true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Database\Migrations;
|
||||
|
||||
use CodeIgniter\Database\Migration;
|
||||
|
||||
class CreateInvOperations extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
$this->forge->addField([
|
||||
'id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
|
||||
'instrument_id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true],
|
||||
'date' => ['type' => 'DATE'],
|
||||
'amount' => ['type' => 'DECIMAL', 'constraint' => '12,2'],
|
||||
'type' => ['type' => 'ENUM', 'constraint' => ['deposit', 'withdraw']],
|
||||
'description' => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
|
||||
'created_at' => ['type' => 'DATETIME', 'null' => true],
|
||||
'updated_at' => ['type' => 'DATETIME', 'null' => true],
|
||||
]);
|
||||
$this->forge->addKey('id', true);
|
||||
$this->forge->addKey('date');
|
||||
$this->forge->addKey('instrument_id');
|
||||
$this->forge->addKey('type');
|
||||
// RESTRICT: nie kasujemy instrumentu, ktory ma operacje (blokada tez w kontrolerze)
|
||||
$this->forge->addForeignKey('instrument_id', 'inv_instruments', 'id', 'RESTRICT', 'RESTRICT');
|
||||
$this->forge->createTable('inv_operations', true, ['ENGINE' => 'InnoDB']);
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
$this->forge->dropTable('inv_operations', true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Database\Migrations;
|
||||
|
||||
use CodeIgniter\Database\Migration;
|
||||
|
||||
class CreateInvValuations extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
$this->forge->addField([
|
||||
'id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
|
||||
'instrument_id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true],
|
||||
'date' => ['type' => 'DATE'],
|
||||
'value' => ['type' => 'DECIMAL', 'constraint' => '12,2'],
|
||||
'created_at' => ['type' => 'DATETIME', 'null' => true],
|
||||
'updated_at' => ['type' => 'DATETIME', 'null' => true],
|
||||
]);
|
||||
$this->forge->addKey('id', true);
|
||||
$this->forge->addKey('date');
|
||||
$this->forge->addKey('instrument_id');
|
||||
// RESTRICT: nie kasujemy instrumentu, ktory ma wyceny (blokada tez w kontrolerze)
|
||||
$this->forge->addForeignKey('instrument_id', 'inv_instruments', 'id', 'RESTRICT', 'RESTRICT');
|
||||
$this->forge->createTable('inv_valuations', true, ['ENGINE' => 'InnoDB']);
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
$this->forge->dropTable('inv_valuations', true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Database\Seeds;
|
||||
|
||||
use CodeIgniter\Database\Seeder;
|
||||
|
||||
class DefaultCategoriesSeeder extends Seeder
|
||||
{
|
||||
public function run(): void
|
||||
{
|
||||
$now = date('Y-m-d H:i:s');
|
||||
|
||||
$rows = [
|
||||
// Przychody
|
||||
['name' => 'Wynagrodzenie', 'type' => 'income', 'parent_id' => null],
|
||||
['name' => 'Premia', 'type' => 'income', 'parent_id' => null],
|
||||
['name' => 'Inne przychody', 'type' => 'income', 'parent_id' => null],
|
||||
// Wydatki
|
||||
['name' => 'Mieszkanie', 'type' => 'expense', 'parent_id' => null],
|
||||
['name' => 'Jedzenie', 'type' => 'expense', 'parent_id' => null],
|
||||
['name' => 'Transport', 'type' => 'expense', 'parent_id' => null],
|
||||
['name' => 'Rozrywka', 'type' => 'expense', 'parent_id' => null],
|
||||
['name' => 'Zdrowie', 'type' => 'expense', 'parent_id' => null],
|
||||
['name' => 'Inne wydatki', 'type' => 'expense', 'parent_id' => null],
|
||||
];
|
||||
|
||||
foreach ($rows as &$r) {
|
||||
$r['created_at'] = $now;
|
||||
$r['updated_at'] = $now;
|
||||
}
|
||||
|
||||
// Nie duplikuj, jesli seeder juz raz przeszedl
|
||||
if ((int) $this->db->table('categories')->countAllResults() === 0) {
|
||||
$this->db->table('categories')->insertBatch($rows);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Database\Seeds;
|
||||
|
||||
use CodeIgniter\Database\Seeder;
|
||||
|
||||
class DefaultInstrumentsSeeder extends Seeder
|
||||
{
|
||||
public function run(): void
|
||||
{
|
||||
$now = date('Y-m-d H:i:s');
|
||||
$names = ['Obligacje', 'ETF'];
|
||||
|
||||
foreach ($names as $name) {
|
||||
// Idempotentnie: pomin instrument, ktory juz istnieje
|
||||
$exists = (int) $this->db->table('inv_instruments')->where('name', $name)->countAllResults();
|
||||
if ($exists === 0) {
|
||||
$this->db->table('inv_instruments')->insert([
|
||||
'name' => $name,
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user