halvardssm / deno-nessie

A modular Deno library for PostgreSQL, MySQL, MariaDB and SQLite migrations
MIT License
527 stars 31 forks source link

[IDEA] Add priority for seed files (Discussion wanted) #107

Open halvardssm opened 3 years ago

halvardssm commented 3 years ago

Is your feature request related to a problem? Please describe.

The seed files are currently run without any set order (if any it is alphabetical). We should add a property to the migration files which tells Nessie in which order the files should be run and if there are any dependencies/priorities.

As far as my imagination goes, we could do it the following ways, but feel free to chime in if you have another idea (not ordered, but numbered for easy reference):

  1. Have a property in the abstract seed class to set priority
    • Seed files with higher priority (larger number) are done first.
    • When two files have the same priority, the order doesn't matter and it will default to alphabetical.
    • Might be slower (and heavier) if the seed files are large as it requires each seed file to be imported before sorting.
    • My preference, but I am not too fond of the cons
  2. Have the priority in the file name
    • Will be faster as sorting can be done prior to module import.
    • I think this will be ugly and I would prefer not to go for this option (my opinionated opinion).
  3. Use a comment on the first line of the file which can be read before importing
    • Will probably be faster 1, but slower than 2
    • Could be an antipattern
  4. Have a separate priority const export similar to 1, but for the file instead of the class
    • Not sure if this is also an antipattern
    • Could be as slow as 1

Update: I will leave this open until I get some feedback or I figure out a solid way of doing this. It will not be included in the v2 release, but might be a part of a v2 minor release further down the line

kluzzebass commented 3 years ago

I've just been prefixing the seed files with a number (like 10_my_seed_file.ts), which seems to work fine.

ebebbington commented 3 years ago

@halvardssm why not take the laravel approach, and the user create/define a base seeder class that runs the other seeders? for example from a php project:

<?php

// database/seeders/DatabaseSeeder.php

namespace Database\Seeds;

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        // Run in this order
        $this->call(A::class);
        $this->call(B::class);

        $this->call(C::class);
        $this->call(D::class);
    }
}
halvardssm commented 2 years ago

I am inclined to the approach from @ebebbington but the question is then the implementation. I would say that the default behaviour should be the seed name in ascending order, and then an optional property in the config object (or a CLI flag) can give a path to a seed runner file. The only thing on my end which I am not too sure about is whether this should be a class or a function, but seeing as Nessie is fully class-based, we might as well continue on this path. I won't have time to implement this in the forseable future, so anyone are free to pick this up and send a PR :)