Files
grocy/services/DatabaseMigrationService.php

205 lines
6.7 KiB
PHP
Raw Normal View History

2017-04-16 23:11:03 +02:00
<?php
2018-04-11 19:49:35 +02:00
namespace Grocy\Services;
class DatabaseMigrationService extends BaseService
2017-04-16 23:11:03 +02:00
{
2018-04-11 19:49:35 +02:00
public function MigrateDatabase()
2017-04-16 23:11:03 +02:00
{
2018-04-11 19:49:35 +02:00
$this->DatabaseService->ExecuteDbStatement("CREATE TABLE IF NOT EXISTS migrations (migration INTEGER NOT NULL PRIMARY KEY UNIQUE, execution_time_timestamp DATETIME DEFAULT (datetime('now', 'localtime')))");
$this->ExecuteMigrationWhenNeeded(1, "
2017-04-16 23:11:03 +02:00
CREATE TABLE products (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
name TEXT NOT NULL UNIQUE,
description TEXT,
location_id INTEGER NOT NULL,
qu_id_purchase INTEGER NOT NULL,
qu_id_stock INTEGER NOT NULL,
qu_factor_purchase_to_stock REAL NOT NULL,
2017-04-19 21:09:28 +02:00
barcode TEXT,
2017-04-20 17:10:21 +02:00
min_stock_amount INTEGER NOT NULL DEFAULT 0,
default_best_before_days INTEGER NOT NULL DEFAULT 0,
2017-04-19 21:09:28 +02:00
row_created_timestamp DATETIME DEFAULT (datetime('now', 'localtime'))
2017-04-16 23:11:03 +02:00
)"
);
2018-04-11 19:49:35 +02:00
$this->ExecuteMigrationWhenNeeded(2, "
2017-04-16 23:11:03 +02:00
CREATE TABLE locations (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
name TEXT NOT NULL UNIQUE,
description TEXT,
2017-04-19 21:09:28 +02:00
row_created_timestamp DATETIME DEFAULT (datetime('now', 'localtime'))
2017-04-16 23:11:03 +02:00
)"
);
2018-04-11 19:49:35 +02:00
$this->ExecuteMigrationWhenNeeded(3, "
2017-04-16 23:11:03 +02:00
CREATE TABLE quantity_units (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
name TEXT NOT NULL UNIQUE,
description TEXT,
2017-04-19 21:09:28 +02:00
row_created_timestamp DATETIME DEFAULT (datetime('now', 'localtime'))
2017-04-16 23:11:03 +02:00
)"
);
2018-04-11 19:49:35 +02:00
$this->ExecuteMigrationWhenNeeded(4, "
2017-04-16 23:11:03 +02:00
CREATE TABLE stock (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
product_id INTEGER NOT NULL,
amount INTEGER NOT NULL,
best_before_date DATE,
purchased_date DATE DEFAULT (datetime('now', 'localtime')),
2017-04-19 21:09:28 +02:00
stock_id TEXT NOT NULL,
row_created_timestamp DATETIME DEFAULT (datetime('now', 'localtime'))
2017-04-16 23:11:03 +02:00
)"
);
2018-04-11 19:49:35 +02:00
$this->ExecuteMigrationWhenNeeded(5, "
2017-04-19 21:09:28 +02:00
CREATE TABLE stock_log (
2017-04-16 23:11:03 +02:00
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
product_id INTEGER NOT NULL,
amount INTEGER NOT NULL,
best_before_date DATE,
purchased_date DATE,
2017-04-19 21:09:28 +02:00
used_date DATE,
2017-04-16 23:11:03 +02:00
spoiled INTEGER NOT NULL DEFAULT 0,
2017-04-19 21:09:28 +02:00
stock_id TEXT NOT NULL,
transaction_type TEXT NOT NULL,
row_created_timestamp DATETIME DEFAULT (datetime('now', 'localtime'))
2017-04-16 23:11:03 +02:00
)"
);
2018-04-11 19:49:35 +02:00
$this->ExecuteMigrationWhenNeeded(6, "
2017-04-16 23:11:03 +02:00
INSERT INTO locations (name, description) VALUES ('DefaultLocation', 'This is the first default location, edit or delete it');
INSERT INTO quantity_units (name, description) VALUES ('DefaultQuantityUnit', 'This is the first default quantity unit, edit or delete it');
INSERT INTO products (name, description, location_id, qu_id_purchase, qu_id_stock, qu_factor_purchase_to_stock) VALUES ('DefaultProduct1', 'This is the first default product, edit or delete it', 1, 1, 1, 1);
INSERT INTO products (name, description, location_id, qu_id_purchase, qu_id_stock, qu_factor_purchase_to_stock) VALUES ('DefaultProduct2', 'This is the second default product, edit or delete it', 1, 1, 1, 1);"
);
2017-04-21 13:21:09 +02:00
2018-04-11 19:49:35 +02:00
$this->ExecuteMigrationWhenNeeded(7, "
2017-04-21 13:21:09 +02:00
CREATE VIEW stock_missing_products
AS
SELECT p.id, MAX(p.name) AS name, p.min_stock_amount - IFNULL(SUM(s.amount), 0) AS amount_missing
FROM products p
LEFT JOIN stock s
ON p.id = s.product_id
WHERE p.min_stock_amount != 0
GROUP BY p.id
HAVING IFNULL(SUM(s.amount), 0) < p.min_stock_amount;"
2017-04-21 13:21:09 +02:00
);
2018-04-11 19:49:35 +02:00
$this->ExecuteMigrationWhenNeeded(8, "
2017-04-21 13:21:09 +02:00
CREATE VIEW stock_current
AS
SELECT product_id, SUM(amount) AS amount, MIN(best_before_date) AS best_before_date
2017-07-25 20:03:31 +02:00
FROM stock
2017-04-21 13:21:09 +02:00
GROUP BY product_id
ORDER BY MIN(best_before_date) ASC;"
);
2018-04-11 19:49:35 +02:00
$this->ExecuteMigrationWhenNeeded(9, "
CREATE TABLE shopping_list (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
product_id INTEGER NOT NULL UNIQUE,
amount INTEGER NOT NULL DEFAULT 0,
amount_autoadded INTEGER NOT NULL DEFAULT 0,
row_created_timestamp DATETIME DEFAULT (datetime('now', 'localtime'))
)"
);
2017-07-25 20:03:31 +02:00
2018-04-11 19:49:35 +02:00
$this->ExecuteMigrationWhenNeeded(10, "
2017-07-25 20:03:31 +02:00
CREATE TABLE habits (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
name TEXT NOT NULL UNIQUE,
description TEXT,
period_type TEXT NOT NULL,
period_days INTEGER,
row_created_timestamp DATETIME DEFAULT (datetime('now', 'localtime'))
)"
);
2018-04-11 19:49:35 +02:00
$this->ExecuteMigrationWhenNeeded(11, "
2017-07-25 20:03:31 +02:00
CREATE TABLE habits_log (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
habit_id INTEGER NOT NULL,
tracked_time DATETIME,
row_created_timestamp DATETIME DEFAULT (datetime('now', 'localtime'))
)"
);
2018-04-11 19:49:35 +02:00
$this->ExecuteMigrationWhenNeeded(12, "
2017-07-25 20:03:31 +02:00
CREATE VIEW habits_current
AS
SELECT habit_id, MAX(tracked_time) AS last_tracked_time
FROM habits_log
GROUP BY habit_id
ORDER BY MAX(tracked_time) DESC;"
);
2018-04-11 19:49:35 +02:00
$this->ExecuteMigrationWhenNeeded(13, "
CREATE TABLE batteries (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
name TEXT NOT NULL UNIQUE,
description TEXT,
used_in TEXT,
charge_interval_days INTEGER NOT NULL DEFAULT 0,
row_created_timestamp DATETIME DEFAULT (datetime('now', 'localtime'))
)"
);
2018-04-11 19:49:35 +02:00
$this->ExecuteMigrationWhenNeeded(14, "
CREATE TABLE battery_charge_cycles (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
battery_id TEXT NOT NULL,
tracked_time DATETIME,
row_created_timestamp DATETIME DEFAULT (datetime('now', 'localtime'))
)"
);
2018-04-11 19:49:35 +02:00
$this->ExecuteMigrationWhenNeeded(15, "
CREATE VIEW batteries_current
AS
SELECT battery_id, MAX(tracked_time) AS last_tracked_time
FROM battery_charge_cycles
GROUP BY battery_id
ORDER BY MAX(tracked_time) DESC;"
);
2018-01-04 12:51:36 +01:00
2018-04-11 19:49:35 +02:00
$this->ExecuteMigrationWhenNeeded(16, "
2018-01-04 12:51:36 +01:00
ALTER TABLE shopping_list RENAME TO shopping_list_old;"
);
2018-04-11 19:49:35 +02:00
$this->ExecuteMigrationWhenNeeded(17, "
2018-01-04 12:51:36 +01:00
CREATE TABLE shopping_list (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
product_id INTEGER,
note TEXT,
amount INTEGER NOT NULL DEFAULT 0,
amount_autoadded INTEGER NOT NULL DEFAULT 0,
row_created_timestamp DATETIME DEFAULT (datetime('now', 'localtime'))
)"
);
2018-04-11 19:49:35 +02:00
$this->ExecuteMigrationWhenNeeded(18, "
2018-01-04 12:51:36 +01:00
INSERT INTO shopping_list
(product_id, amount, amount_autoadded, row_created_timestamp)
SELECT product_id, amount, amount_autoadded, row_created_timestamp
FROM shopping_list_old"
);
2018-04-11 19:49:35 +02:00
$this->ExecuteMigrationWhenNeeded(19, "
2018-01-04 12:51:36 +01:00
DROP TABLE shopping_list_old;"
);
2017-04-16 23:11:03 +02:00
}
2018-04-11 19:49:35 +02:00
private function ExecuteMigrationWhenNeeded(int $migrationId, string $sql)
2017-04-16 23:11:03 +02:00
{
2018-04-11 19:49:35 +02:00
$rowCount = $this->DatabaseService->ExecuteDbQuery('SELECT COUNT(*) FROM migrations WHERE migration = ' . $migrationId)->fetchColumn();
2017-04-21 11:52:24 +02:00
if (intval($rowCount) === 0)
2017-04-16 23:11:03 +02:00
{
2018-04-11 19:49:35 +02:00
$this->DatabaseService->ExecuteDbStatement($sql);
$this->DatabaseService->ExecuteDbStatement('INSERT INTO migrations (migration) VALUES (' . $migrationId . ')');
2017-04-16 23:11:03 +02:00
}
}
}