H
H
hitakiri2015-10-28 14:24:14
Laravel
hitakiri, 2015-10-28 14:24:14

How to connect an already working database with migrations?

Framework - Lumen (same as Laravel, only less functionality).
DB - MySQL 5.5
Essence of a question: there is a ready DB with the filled tables. I can't find in the documentation how to work with existing tables in the database through migrations.
Description everywhere: install migrations -> create a table -> work with a file in the "migrations" folder.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Evgrafovich, 2015-10-28
@Tantacula

If you really want to, you can write migrations that would create similar tables with all the fields and keys, then create a migrations table and populate it with your migrations. Here is an example from my database.

--
-- Структура таблицы `migrations`
--

CREATE TABLE IF NOT EXISTS `migrations` (
  `migration` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `batch` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- Дамп данных таблицы `migrations`
--

INSERT INTO `migrations` (`migration`, `batch`) VALUES
('2015_10_20_154943_create_sessions_table', 1),
('2015_10_21_063052_create_visits_table', 1),
('2015_10_21_165314_create_claims_table', 1),
('2015_10_21_171026_create_notifications_table', 1);

Of course, you will enter your own migrations. batch - rolls them back one by one, in this example all migrations will be rolled back in one step. But I would recommend creating migrations from scratch, because if you forget some foreign key, or you create tables in the wrong order, running a migration with a foreign key before the table it is associated with is created, you will get a headache.

V
Vyacheslav Plisko, 2015-10-28
@AmdY

You can work with new ones without any problems,
create a migration and work with an existing table

Schema::table('users', function ($table) {
    $table->string('email');
});

php artisan migrate
If you want to make migrations over an existing database so that you can sit and refresh again, then look for the package you need, I don’t remember the names, but they are there.

E
Evgeny Elchev, 2015-10-29
@rsi

Migrations are a way to change a database. All examples show how to write migrations to create tables, but this is a more powerful tool. It allows you to make any changes to the base structure. This begs the question, what do you mean by "work with already existing tables through migrations"?
Just like with a non-existing one:
- If you want to have migrations to create your database, write them, if you don’t want, don’t write, just work with the database without migration
- If you want to modify the structure (once again I emphasize that migrations change the structure of the database, add, remove tables, fields in base, change their type and td) already available tables? Write migrations for these changes.
If you have a database and want to connect Laravel to them, then you don’t need migrations at all, since models, not migrations, are responsible for working with data in the database

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question