Skip to content

How do I manage multiple migration files? #30

@ralyodio

Description

@ralyodio

The blog example only has one migration file.

I've tried this but the migration doesn't run for posts:

package MojoForum;
use Mojo::Base 'Mojolicious', -signatures;
use Mojo::SQLite;
use MojoForum::Model::Posts;
use MojoForum::Controller::Posts;
use MojoForum::Model::Chats;
use MojoForum::Controller::Chats;

# This method will run once at server start
sub startup ($self) {

  $self->plugin('Config');
  # Configure the application
  $self->secrets($self->config('secrets'));
  #$self->secrets($config->{secrets});
	
	# Model
  $self->helper(sqlite => sub { state $sql = Mojo::SQLite->new->from_filename(shift->config('sqlite')) });
  $self->helper(
    posts => sub { state $posts = MojoForum::Model::Posts->new(sqlite => shift->sqlite) });
 
  # Migrate to latest version if necessary
  my $path = $self->home->child('migrations', 'blog.sql');
  $self->sqlite->auto_migrate(1)->migrations->name('blog')->from_file($path);

  $path = $self->home->child('migrations', 'chat.sql');
  $self->sqlite->auto_migrate(1)->migrations->name('chat')->from_file($path);

  # Router
  my $r = $self->routes;

  # Normal route to controller
  $r->get('/')->to('Example#welcome');

	# /posts
	$r->get('/posts')->to('posts#index');
  $r->get('/posts/create')->to('posts#create')->name('create_post');
  $r->post('/posts')->to('posts#store')->name('store_post');
  $r->get('/posts/:id')->to('posts#show')->name('show_post');
  $r->get('/posts/:id/edit')->to('posts#edit')->name('edit_post');
  $r->put('/posts/:id')->to('posts#update')->name('update_post');

	# /chats
	$r->get('/chats')->to('chats#index');
	$r->websocket('/title')->to('chats#title');
}

1;

Full code is at https://github.com/profullstack/mojo_forum

I feel like migrations should be a standalone script that you run before you deploy. Putting it in the main .pm file seems wrong to me.

I see Pg::Migrations allows you to use a directory:

https://docs.mojolicious.org/Mojo/Pg/Migrations#from_dir

Does Mojo::SQLite support same interface?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions