2017-04-16 23:11:03 +02:00
< ? php
class GrocyDbMigrator
{
public static function MigrateDb ( PDO $pdo )
{
self :: ExecuteMigrationWhenNeeded ( $pdo , 1 , "
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
) "
);
self :: ExecuteMigrationWhenNeeded ( $pdo , 2 , "
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
) "
);
self :: ExecuteMigrationWhenNeeded ( $pdo , 3 , "
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
) "
);
self :: ExecuteMigrationWhenNeeded ( $pdo , 4 , "
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
) "
);
self :: ExecuteMigrationWhenNeeded ( $pdo , 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
) "
);
self :: ExecuteMigrationWhenNeeded ( $pdo , 6 , "
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 ); "
);
}
private static function ExecuteMigrationWhenNeeded ( PDO $pdo , int $migrationId , string $sql )
{
2017-04-21 11:52:24 +02:00
$rowCount = Grocy :: ExecuteDbQuery ( $pdo , 'SELECT COUNT(*) FROM migrations WHERE migration = ' . $migrationId ) -> fetchColumn ();
if ( intval ( $rowCount ) === 0 )
2017-04-16 23:11:03 +02:00
{
2017-04-21 11:52:24 +02:00
Grocy :: ExecuteDbStatement ( $pdo , $sql );
Grocy :: ExecuteDbStatement ( $pdo , 'INSERT INTO migrations (migration) VALUES (' . $migrationId . ')' );
2017-04-16 23:11:03 +02:00
}
}
}