So as title pretty much describes, I'm trying to utilize phinx for database migration, but I have AUTO-INCREMENT column, which is not used as the primary key. As far as I see, this can't be done with phinx.
I realize, that I could probably do without that column altogether, but it's the part of the huge legacy code and, at the moment at least, I don't have the time to refactor entire app to ensure that column is not used anywhere. If my conclusion is correct and phinx is not able to achieve this, I'd appreciate some alternatives, possessing described functionality
You can use the up and down phinx method to create this table using execute fontion with pure sql.
See this
Related
I'm trying to import an existing table in my Database using Doctrine's reverse engineering, but this table I'm importing doesn't have a primary key.
I know Doctrine doesn't allow to import tables without any primary keys, but I would like to know if there's a possibility to do it. I've been trying to edit Doctrine's code to allow it, but without any results for now.
Does anyone know a method? Or, if it's not possible, what are my options? I can't edit the database because it's not mine, so I need to manage this to import the table without "touching" the database.
So after trying a lot, I'd found a couple methods that can solve this. I will explain them to everyone who finds themselves in the same situations as me:
1st method: The one #mulquin said. This method solves the problem by exporting the table and then adding, manually, a primary key using code. Once this is done, it can be imported using Doctrine.
2nd method: Found by me. Creating the table using code directly and connecting it manually instead of using make:entity. You should create the two files, in Entity and Repository, and then write the code to make it match the table in the database, using #ORM\Table, #ORM\Column, etc. and writing everything just like Doctrine would generate it. You can take an existing entity generated by Doctrine to make sure you're not missing anything.
I'm trying to create a table, which contains an "email_address" field and an "activation hash" field.
The default value of "activation hash" field should be
sha1(microtime().email_address))
Is it possible to set this up using laravel migrations and how can I do this?
Surely, i should say, that i'm using postgres as DB engine
Most database engines are limited as to what values can be set as a default to columns. Depending on your database engine, you might be able to do it via an event/trigger but it's easier and arguably better to solve this at an application level with Eloquent events.
When I need to do something like this, I split the code in 2 pieces:
Create the column (using Schema Builder)
Set the value (over PHP, loading all models and updating one by one in a foreach)
Note: Both steps can be done at migration file.
You can't do it entirely at MySQL (if you are using MySQL) query since it doesn't have a microtime() function.
But I found this UDF implementation for microtime() on MySql:
https://github.com/JohannesMP/MySQL-udf-microtime
If you are not updating a huge dataset, I would stick with the first solution. But if you are updating a huge dataset, so the MySQL implementation will be faster.
I am trying to figure out how best to modify a MySQL Table's existing Column using the CakePHP Migrations plugin. I do not need to add or drop the column, I simply want to modify a string column's length.
Currently the column is defined as a varchar(50); I am repurposing the column and wish to define it as varchar(2000).
The goal of the migration is to be part of an automated deployment taking place on a standard CakePHP web app installation on a typical web server.
Near as I can tell, it looks like the only way (other than an ALTER statement) to accomplish this using the Migrations Plugin would be to:
rename the column
add the new column
Move/copy the existing data to the new column
drop the old column
Perhaps I have missed the discussion in the documents and countless tutorials and how to's out there on a better way to accomplish this, but this seems like a cumbersome and self defeating method.
I have been through both the CakePHP Migration Plugin's documentation and the Phinx's documentation but am failing to see the recommended method for this change. I appreciate any input for this.
Unfortunately the Phinx docs aren't that complete, there seem to be various undocumented methods, like the one you are looking for: \Phinx\Db\Table::changeColumn()
https://github.com/robmorgan/phinx/blob/v0.4.6/src/Phinx/Db/Table.php#L392
https://github.com/robmorgan/phinx/issues/682
The following should work
$table = $this->table('table_name');
$table->changeColumn('column_name', 'string', [
'limit' => 2000
]);
$table->update();
I have a migration called CreateItemsTable; I ran that, I have items in that table, now I need to add a new field to the table. I can't just add a field to the migration file and migrate:refresh because I need the data that's in it.
Am I supposed to make another migration for adding a field? That's seems like mess while I'm testing things in development, I might change fields a lot. I'm not sure if migrations are cleaner than just PhpMyAdmin... or maybe I don't understand them?
Yes, each time you need to change a table in some way you'd create a new migration for it. That's the whole point of migrations. When you're developing in a collaborative environment and you pull down some changes from a remote repository, one of the things you should do (if working with a database) is run any migrations that other developers might have created. This keeps your databases in sync.
Sure you might drop and add columns occasionally but it's no big deal.
When you create a table for the first time you are probably using Schema::create(). All subsequent migrations for that table should use Scheme::table(). It accepts the same parameters except it doesn't attempt to create the table first.
This isn't much of an issue with MySQL per-se.
The Full Story
I'm writing a very small PHP framework. It isn't like existing frameworks where they force you to use a certain methodology. It isn't either like a CMS framework. Trust me, I've seen Zend framework and I've used CMSes like Joomla and WordPress extensively, none of them come close to what I'm doing.
Introducing The Issues
I'm writing the Database abstraction part. You get class methods like ::table_exists() etc.
It is designed in a way that people can easily add different database classes and use them instead (eg; mysql, mssql, oracle, flatfile...).
They simply need to write a class which satisfies a base abstract classes'.
The Real Issue
I'm writing the functionality for ::table_create(), but have one main problem: MySQL doesn't like empty tables (ie, without a column).
I have several proposed fixes:
For each new table, create a commonly used column, such as 'id' (type=INT)
For each new table, create a temp column which doesn't use any space as much as possible (perhaps a boolean column?)
Somehow delay table creation until at least one column can be created
This approach is most certainly new, and I'd like to here some unbiased comments about it (anything on the lines of "but no one does it that way" won't do).
Well I would either go with option 1), Adding a generic ID column, which you might find you need anyway, or with option 3) Delaying the table creation. I'm assume after they call ::table_create() they will be calling table_add_col(), etc. So just delay creation until there is at least one column, OR until they actually try and use the table for the first time.
Your proposed fixes look quite good. But I would recommen them in a diffrent order. If you are able to delay the creation, tht's probably the best. My second favorite would be to have a table with only an ID, although you might be delete this column, if you want to create a many-to-many relations table with two foreign keys only.
last of your points.
its really very strange what you are doing here. creating tables on the fly? dynamically or something?
well... whatever you are trying to accomplish. you should have a look at document/object oriented databases like couchdb http://couchdb.apache.org/ ! you can create a document and dynamically add whatever fields you want. those are the closest thing to your "columns"
but as you like it...
your first attempt is ugly because it might lead to conflicts.
the second attempt is clumsy. but if you do so create a col with uniqueprefix_random so you can delete it afterwards.
but its well... i dunno what to say about that.
theird approach seems the only senseful!