createSchema function
Creates a new schema. Applications should execute the resulting SQL through reviewed migrations; this does not inspect or mutate an existing database.
Implementation
List<SqlCommand> createSchema(List<TableSchema> tables, SqlDialect dialect) {
if (isMysqlFamily(dialect)) return mysqlCreateSchema(tables, dialect);
final commands = <SqlCommand>[];
validateSchema(tables, dialect);
for (final table in tables) {
commands.add(SqlCommand(createTable(table, dialect)));
}
if (dialect == SqlDialect.postgres) {
// Creating constraints after all tables also supports cycles and self links.
for (final table in tables) {
for (final key in table.foreignKeys) {
commands.add(
SqlCommand(
'ALTER TABLE ${quoteIdentifier(table.name)} ADD ${foreignKey(key)}',
),
);
}
}
}
for (final table in tables) {
for (final index in table.indexes) {
commands.add(
SqlCommand(
'CREATE ${index.unique ? 'UNIQUE ' : ''}INDEX ${quoteIdentifier(index.name)} '
'ON ${quoteIdentifier(table.name)} (${index.columns.map(quoteIdentifier).join(', ')})',
),
);
}
}
return List.unmodifiable(commands);
}