This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
I have a plugin that I call locations, which is primed with thousands of cities worldwide. The data takes days to compile from numerous sources, so for obvious reasons, I don't want to have to regenerate the data each time I initiate a plugin:refresh command to update the plugin. However, I wish to be able to make updates to the plugin, including but not limited to adding new fields to existing tables and adding new tables. As an example, I have the following model definition:
namespace Sokser\Locations\Updates;
use Schema;
use October\Rain\Database\Updates\Migration;
class CreatePointsOfInterestsTable extends Migration
{
public function up()
{
Schema::create('sokser_locations_points_of_interests', function($table)
{
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('name',128)->index();
$table->string('name_long',512)->index();
$table->float('latitude')->index();
$table->float('longitude')->index();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('sokser_locations_points_of_interests');
}
}
I would like to add the following field to the table definition:
$table->integer('city_id')->unsigned()->nullable()->default(null)->index();
without deleting any of the current data. How can I do this? Additionally, how can I add a new table to the plugin so that it will install on a plugin:refresh, but not delete any of the existing content? For example, the following is an example of my version.yaml file:
1.0.1: First version of locations
1.0.3:
- Create Regions Table
- create_regions_table.php
- Create Countries Table
- create_countries_table.php
- Create Cities Table
- create_cities_table.php
- Create Ages Mappings Table
- create_ages_mappings_table.php
- Create Ages Table
- create_ages_table.php
- Create Airports Table
- create_airports_table.php
- Create Country Currencies Table
- create_country_currencies_table.php
- Create Currencies Table
- create_currencies_table.php
- Create Hubs Table
- create_hubs_table.php
- Create Languages Table
- create_languages_table.php
- Create Plugs Table
- create_plugs_table.php
- Create Tags Mappings Table
- create_tags_mappings_table.php
- Create Tags Table
- create_tags_table.php
- Create Timezones Table
- create_timezones_table.php
- Create Traveler Types Table
- create_traveler_types_table.php
- Create Types Mappings Table
- create_types_mappings_table.php
- Create Types Table
- create_types_table.php
- Create Continents Table
- create_continents_table.php
- Create Country Plugs Table
- create_country_plugs_table.php
- Create Country Languages Table
- create_country_languages_table.php
- Create City Prices Table
- create_city_prices_table.php
1.0.4:
- Create Points Of Interest Table
- create_points_of_interests_table.php
I want to add the tables specified in version 1.0.4, but not affect anything from prior versions? Any hints?
Last updated
Use php artisan october:up
instead. That will only update, but not refresh the plugins. It is required to create a new version in the versions.yaml
ofcourse, otherwise the system wont know that there are any updates.
Great, thanks for the answer. How can I edit the models so that I can add fields to existing tables?
Migrations can also modify existing tables. You can have a look at the user plugin for an example: https://github.com/rainlab/user-plugin/blob/master/updates/users_add_superuser_flag.php
Don't forget to list these migrations (in correct order) in the versions.yaml as well!
I use this in my create table thingy to discern between varieties. one is for if not exists, the other for if exists.
if (!Schema::connection('exitproducts')->hasTable('exitcontrol_carmanager_region')) {
Schema::connection('exitproducts')->create('exitcontrol_carmanager_region', function($table) use ($runafterupdate)
{
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('name');
$table->text('description');
$table->string('code');
$runafterupdate->update('coderegion',true);
$table->timestamps();
});
}
else {
Schema::connection('exitproducts')->table('exitcontrol_carmanager_region', function($table) use ($runafterupdate) {
if(!Schema::connection('exitproducts')->hasColumn('exitcontrol_carmanager_region', 'code')) {
$table->string('code')->nullable();
$runafterupdate->update('coderegion',true);
}
});
}
Last updated
1-6 of 6