conduit db
command line tool creates and executes migration files. A migration file contains Dart code that executes SQL commands to create and modify database tables.ManagedObject<T>
subclasses and their table definition. Migration files describe a series of database commands that will create or modify a database schema to match an application's data model. Migration files are executed on a database when an application is first deployed and when changes to the data model occur - like adding new ManagedObject<T>
subclasses or adding an database index to a property.Author
type and generated a migration file, the migration file would create the author table. If you then added a Book
type and generated a new migration file, the new file would only create the book table. When a migration is used to upgrade a database schema, every migration file that has not yet been run will be run.conduit db generate
command generates a new migration file. This tool finds all ManagedObject<T>
subclasses - your data model - in your application and compares them to the data model the last time the tool was run. Any differences between the data models are represented as a command in the generated migration file. If the new migration file were to be used to upgrade a database, the database would match the current data model in your application.migrations/
directory. Migration files are prefixed with a version number, a "0" padded eight digit number, ad suffixed with .migration.dart
. For example, 00000001_initial.migration.dart
is a migration filename. The version number portion of the filename is required, as is the .migration.dart
suffix. The underscore and remainder of the filename are optional and have no effect, they are just a way to name the file. Here is an example of two migration file names:conduit db validate
tool validates that the database schema after running all migration files matches the application's data model. The validate tool will display differences found between the schema in code and the schema created by migration files.conduit db list
to list all database migration files and their resolved version number.conduit db get-version
. This command takes --connect
or a database.yaml
file as described in the next section to get connection info for the database.conduit db upgrade
will apply the commands of migration files to a running database. This tool is run in an application's directory and database connection info is provided with the --connect
option. For example, the following would execute the current project directory's migration files on a PostgreSQL database:database.yaml
in the application directory. If this file exists with the following format, --connect
can be omitted and connection information will be read from this file:conduit db generate
is run again, it will replay only the existing migration files before determining which commands to add to the new migration file. For example, if you have 10 migration files over time and delete them all - the next generated migration file will contain commands to recreate the entire database schema.seed
method. You must run SQL queries instead of using Query<T>
when seeding data. The Migration
base class that all of your migrations extends have a property for a PersistentStore
connected to the database the migration is being run on.ugprade
method has completed. Seeding data also occurs in the same transaction as upgrade
.