2016-03-25 01:18:05 -07:00
< ? php
namespace App\Console\Commands ;
2016-06-05 08:24:42 -07:00
use App\Models\Asset ;
2021-09-20 18:03:13 -07:00
use App\Models\CustomField ;
use Schema ;
2024-05-29 04:38:15 -07:00
use Illuminate\Support\Facades\DB ;
Importer mapping - v1 (#3677)
* Move importer to an inline-template, allows for translations and easier passing of data from laravel to vue.
* Pull the modal out into a dedicated partial, move importer to views/importer.
* Add document of CSV->importer mappings. Reorganize some code.
Progress.
* Add header_row and first_row to imports table, and process upon uploading a file
* Use an expandable table row instead of a modal for import processing. This should allow for field mapping interaction easier.
* Fix import processing after moving method.
* Frontend importer mapping improvements.
Invert display so we show found columns and allow users to select an
importer field to map to. Also implement sample data based on first row
of csv.
* Update select2. Maintain selected items properly.
* Backend support for importing. Only works on the web importer currently. Definitely needs testing and polish.
* We no longer use vue-modal plugin.
* Add a column to track field mappings to the imports table.
* Cleanup/rename methods+refactor
* Save field mappings and import type when attempting an import, and repopulate these values when returning to the page.
* Update debugbar to fix a bug in the debugbar code.
* Fix asset tag detection.
Also rename findMatch to be a bit clearer as to what it does.
Remove logging to file of imports for http imports because
it eats an incredible amouint of memory.
This commit also moves imports out of the hardware namespace and into
their own webcontroller and route prefix, remove dead code from
AssetController as a result.
* Dynamically limit options for select2 based on import type selected, and group them by item type.
* Add user importer.
Still need to implement emailing of passwords to new users, and probably
test a bit more.
This also bumps the memory limit for web imports up as well, I need to
profile memory usage here before too long.
* Query the db to find user matches rather than search the array. Performance is much much better.
* Speed/memory improvements in importers.
Move to querying the db rather than maintaining an array for all
importers. Also only store the id of items when we import, rather than
the full model. It saves a decent amount of memory.
* Remove grouping of items in select2
With the values being set dynamically, the grouping is redundant. It
also caused a regression with automatically guessing/matching field
names. This is starting to get close.
* Remove debug line on every create.
* Switch migration to be text field instead of json field for compatibility with older mysql/mariadb
* Fix asset import regression matching email address.
* Rearrange travis order in attempt to fix null settings.
* Use auth::id instead of fetching it off the user. Fixes a null object reference during seeding.
2017-06-21 16:37:37 -07:00
use Illuminate\Console\Command ;
2016-06-05 08:24:42 -07:00
2016-03-25 01:18:05 -07:00
class PaveIt extends Command
{
/**
* The name and signature of the console command .
*
* @ var string
*/
2021-09-20 18:22:06 -07:00
protected $signature = 'snipeit:pave {--force : Skip the interactive yes/no prompt for confirmation}' ;
2016-03-25 01:18:05 -07:00
/**
* The console command description .
*
* @ var string
*/
2021-09-20 17:19:41 -07:00
protected $description = 'Clear the database tables, leaving all migrations, table structure, and the first user in place. (It is primarily a quick tool for developers.) If you want to destroy all tables as well, use php artisan db:wipe.' ;
2016-03-25 01:18:05 -07:00
/**
* Create a new command instance .
*
* @ return void
*/
public function __construct ()
{
parent :: __construct ();
}
/**
* Execute the console command .
*
* @ return mixed
*/
public function handle ()
{
2021-09-20 18:22:06 -07:00
if ( ! $this -> option ( 'force' )) {
2021-09-20 18:49:04 -07:00
$confirmation = $this -> confirm ( " \n **************************************************** \n THIS WILL DELETE ALL OF THE DATA IN YOUR DATABASE. \n There is NO undo. This WILL destroy ALL of your data, \n INCLUDING ANY non-Snipe-IT tables you have in this database. \n **************************************************** \n \n Do you wish to continue? No backsies! " );
2021-09-20 18:22:06 -07:00
if ( ! $confirmation ) {
$this -> error ( 'ABORTING' );
exit ( - 1 );
}
}
2021-09-20 17:19:41 -07:00
2021-09-20 18:22:06 -07:00
// List all the tables in the database so we don't have to worry about missing some as the app grows
$tables = DB :: connection () -> getDoctrineSchemaManager () -> listTableNames ();
2021-09-20 17:19:41 -07:00
2021-09-20 18:22:06 -07:00
$except_tables = [
'oauth_access_tokens' ,
'oauth_clients' ,
'oauth_personal_access_clients' ,
'migrations' ,
'settings' ,
'users' ,
];
2021-09-20 17:19:41 -07:00
2021-09-20 18:22:06 -07:00
// We only need to find out what these are so we can nuke these columns on the assets table.
$custom_fields = CustomField :: get ();
foreach ( $custom_fields as $custom_field ) {
$this -> info ( 'DROP the ' . $custom_field -> db_column . ' column from assets as well.' );
2021-09-20 18:03:13 -07:00
2021-09-20 18:22:06 -07:00
if ( \Schema :: hasColumn ( 'assets' , $custom_field -> db_column )) {
\Schema :: table ( 'assets' , function ( $table ) use ( $custom_field ) {
$table -> dropColumn ( $custom_field -> db_column );
});
2021-09-20 18:03:13 -07:00
}
2021-09-20 18:22:06 -07:00
}
2021-09-20 18:03:13 -07:00
2021-09-20 18:22:06 -07:00
foreach ( $tables as $table ) {
if ( in_array ( $table , $except_tables )) {
$this -> info ( $table . ' is SKIPPED.' );
} else {
\DB :: statement ( 'truncate ' . $table );
$this -> info ( $table . ' is TRUNCATED.' );
}
2016-03-25 01:18:05 -07:00
}
2021-09-20 18:22:06 -07:00
// Leave in the demo oauth keys so we don't have to reset them every day in the demos
\DB :: statement ( 'delete from oauth_clients WHERE id > 2' );
\DB :: statement ( 'delete from oauth_access_tokens WHERE id > 2' );
2016-03-25 01:18:05 -07:00
}
2021-09-20 18:03:13 -07:00
}