conduit dbcommand 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.
Authortype and generated a migration file, the migration file would create the author table. If you then added a
Booktype 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 generatecommand 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.dartis a migration filename. The version number portion of the filename is required, as is the
.migration.dartsuffix. 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 validatetool 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 listto list all database migration files and their resolved version number.
conduit db get-version. This command takes
database.yamlfile as described in the next section to get connection info for the database.
conduit db upgradewill 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
--connectoption. For example, the following would execute the current project directory's migration files on a PostgreSQL database:
database.yamlin the application directory. If this file exists with the following format,
--connectcan be omitted and connection information will be read from this file:
conduit db generateis 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.
seedmethod. You must run SQL queries instead of using
Query<T>when seeding data. The
Migrationbase class that all of your migrations extends have a property for a
PersistentStoreconnected to the database the migration is being run on.
ugprademethod has completed. Seeding data also occurs in the same transaction as