Laravel 5.1 migration new table not working - laravel-5.1

I have got some problem with migration.
I create new migrate file
php artisan make:migration create_menu_table --create=menu
then i edit the new Migration file
and when i try migrate it's not working
I tried:
php artisan migrate
php artisan migrate --force
php artisan migrate:refresh
php artisan migrate:refresh --seed
php artisan migrate:rollback
php artisan migrate:reset
but they do not add created table
I do not have any errors
Thanks for help

Run composer dumpautoload and then try php artisan migrate:refresh again.
Hope this helps!

add schema ::defaultStringLength(191) to migrate file(put in every file migrate)
like this
public function up()
{
schema::defaultStringLength(191);
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->string ('Name');
$table->float('price');
$table->timestamps();
});
}

Related

Sheduling start rails server after rebot with .sh script "script.sh: line 2: rails: command not found" error

I tried to automatically run my Rails server after a server reboot. I use cronetab that refers to file with scripts.
I have a problem with file.sh execution.
In this file I
#!/bin/bash
cd MyApp/ && rails s
redirect to app folder and try to run command to run the server.
I try to do a test run by launch script from bash
/directory/to/script/file.sh
but all i got is
/directory/to/script/file.sh: line 2: rails: command not found
When I run cd MyApp/ && rails s in bash directly everything works just fine.
Can you please help me. I need to automate starting rails server after rebooting.
System => Ubuntu Server 18.04.2 LTS
Thank you, guys.
In the end, the answer was already on Stack "Start rails server automatically when ever I start my ubuntu machine" but not the best-score answer was right :)
I dump idea with external script and pack everything to crontab
#reboot /bin/bash -l -c 'cd /full/path/MyApp && rails s'

TypeORM - run specific migration

I know typeorm migration:run to run all migration files, but some time I want run a file like insert file, or a modified file. But i can't find any doc for that. How to do that thanks.
This is not possible. You can see the docs of the migration with
typeorm migration:run -h
During development you can locally change your connection settings, e.g. change the path to the migration files in your ormconfig.json to match only your file.
"migrations": [
"src/db/migration/**/yourmigration.ts"
]
If you want to seed the database with data you shouldn't use migrations.
Migrations' purpose is to create the database's structure, This is why migrations are executed serially, therefore running a single migration non-serially misses the point.
For database seeding with typeorm you can use typeorm cli query, you can create a script that read the SQL from a file.
For example in a node.js app & bash:
package.json:
"scripts": {
...,
"typeorm": "npx ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli --config ormconfig-migrations.ts",
"db:seed": "bash scripts/db-seed.sh"
}
db-seed.sh:
#!/bin/bash
query=""
for filename in sql/seed/*.seed.sql; do
query=${query}"$(< ${filename})";
done
npm run typeorm query "${query}";
Pay attention there is a bug in this solution, there is always an error thrown from the logger typeorm uses - ignore it. it doesn't relate to the db operation, and does not represent the success of the process.
A migration file that was shared with others shouldn't be modified, in this case, create a new migration.
If that file wasn't shared and is in development, you can run migration:revert as many as you need, then modify and run migration:run again.
For running a specific migration my solution is in ormconfig.js add:
module.exports = {
...
migrations: [process.env.DB_MIGRATION_DIR || 'src/database/migrations/*.ts'],
...
}
and now use DB_MIGRATION_DIR=path/to/migration.ts npm run typeorm migration:run run your specific migration
Another tip for force re-running a migration, but might need some customization for connection string variables - add in your package.json:
{
"scripts": {
...
"migration:rerun": "psql postgresql://$DB_USER#localhost:5432/$DB_NAME -c "delete from migrations where name like '%$SEED_NAME%';" && DB_MIGRATION_DIR='$DB_MIGRATION_DIR' npm run typeorm migration:run"
...
}
...
}
I dont think we can run any migration particular with cli, but we can run it through query runner. please refer the following code:
const connection: Connection = getConnection();
const queryRunner: QueryRunner = connection.createQueryRunner();
//createDatabase16413343434 is class/migration generated from cli
const createDatabase = new createDatabase16413343434();
await createDatabase.up(queryRunner);
One can write custom code to run one migration like inserting a record or just creating/ deleting a single table kind of things. typeorm provides "QueryRunner" which basically run queries in the background. So just create a file with the code given following code to write custom single migration
code to run migration

migrate:refresh and migrate:reset gives Class '' not found error in laravel 5.1

I used the command "php artisan migrate:reset" and "php artisan migrate:refresh" both gives the error
[Symfony\Component\Debug\Exception\FatalErrorException]
Class '' not found
I also need to remove column from table using migration in laravel 5.1, please guide me or send me any reference links.
I attached my error screen here .
Thanks
When I get an issue like this I usually dump the autoloader:
composer dump-autoload
To remove a column from the database you can either remove it from you migration and run:
php artisan migrate:refresh
Which is usually fine in a development environment, but you will lose any data in your database, so it's a good idea to set up some seeders if you want to do it this way.
Otherwise, you can create a new migration to simply drop your column using:
php artisan make:migration drop_my_column_from_my_table --table=my_table
You would then do something like:
Schema::table('my_table', function ($table) {
$table->dropColumn('my_column');
});
and run you migration as normal:
php artisan migrate

Load specific environment with Laravel 5.1 Artisan CLI

I'm currently trying to develop my own Artisan Scripts. And for that, I need to load another environment than only "local".
I've created 3 files : ".env.local", ".env.development", ".env.production" and during the deployment, I create a Symlink from .env.XXX to .env
That's working.
Now, with my Artisan in CLI-mode; I want to just call my method with that :
php artisan --env=development myowncommand:exec
... where "development" is my .env.**** file.
I should call other environments by :
php artisan --env=local myowncommand:exec
php artisan --env=development myowncommand:exec
php artisan --env=production myowncommand:exec
That doesn't work.
What am I doing wrong ? Any idea ?
Thanks,
This is somewhat of a hack, but you can leverage loadEnvironmentFrom(). Copy the "artisan" file in the root directory to "artisan-local", "artisan-development", etc.
In the file, under the line:
$kernel = $app->make('Illuminate\Contracts\Console\Kernel');
Add:
$app->loadEnvironmentFrom('.env.local');
Change '.env.local' as appropriate.

How to dump data from mysql database to postgresql database?

I have done the depot application using mysql... Now i am in need to use postgres... So i need to dump data from mysql database "depot_development" to postgres database "depot_develop"...
Here you can find some interesting links http://wiki.postgresql.org/wiki/Converting_from_other_Databases_to_PostgreSQL#MySQL
Have you tried to copy the tables from one database to the other:
a) export the data from MySQL as a CSV file like:
$> mysql -e "SELECT * FROM table" -h HOST -u USER -p PWD -D DB > /file/path.csv'
and then,
b) import it into Postgres like:
COPY table FROM '/file/path.csv' WITH CSV;
This question is a little old but a few days ago i was dealing with this situation and found pgloader.io.
This is by far the easiest way of doing it, you need to install it, and then run a simple lisp script (script.lips) with the following 3 lines:
/* content of the script.lisp */
LOAD DATABASE
FROM mysql://dbuser#localhost/dbname
INTO postgresql://dbuser#localhost/dbname;
/*run this in the terminal*/
pgload sctipt.lisp
And after that your postgresql DB will have all of the information that you had in your MySQL SB
On a side note, make you you compile pgloader since at the time of this post, the installer has a bug. (version 3.2.0)

Resources