More info about columns add runtime, add migration api

This commit is contained in:
Simon Binder 2019-02-07 20:18:13 +01:00
parent d984f93c3a
commit b52906ad31
7 changed files with 73 additions and 17 deletions

View File

@ -23,4 +23,9 @@ class ShopDb extends _$ShopDb {
Future<List<User>> allUsers() => select(users).get();
Future<List<User>> userByName(String name) => (select(users)..where((u) => u.name.equalsVal(name))).get();
@override
MigrationStrategy get migration => MigrationStrategy();
@override
int get schemaVersion => 1;
}

View File

@ -12,3 +12,4 @@ export 'package:sally/src/runtime/statements/select.dart';
export 'package:sally/src/runtime/structure/columns.dart';
export 'package:sally/src/runtime/structure/table_info.dart';
export 'package:sally/src/runtime/sql_types.dart';
export 'package:sally/src/runtime/migration.dart';

View File

@ -1,5 +1,6 @@
import 'package:sally/sally.dart';
import 'package:sally/src/runtime/executor/type_system.dart';
import 'package:sally/src/runtime/migration.dart';
import 'package:sally/src/runtime/statements/delete.dart';
import 'package:sally/src/runtime/statements/select.dart';
@ -8,6 +9,9 @@ abstract class GeneratedDatabase {
final SqlTypeSystem typeSystem;
final QueryExecutor executor;
int get schemaVersion;
MigrationStrategy get migration;
GeneratedDatabase(this.typeSystem, this.executor);
SelectStatement<Table, ReturnType> select<Table, ReturnType>(
@ -23,7 +27,7 @@ abstract class QueryExecutor {
Future<bool> ensureOpen();
Future<List<Map<String, dynamic>>> runSelect(
String statement, List<dynamic> args);
List<int> runCreate(String statement, List<dynamic> args);
List<int> runInsert(String statement, List<dynamic> args);
Future<int> runUpdate(String statement, List<dynamic> args);
Future<int> runDelete(String statement, List<dynamic> args);
}

View File

@ -0,0 +1,35 @@
import 'package:sally/src/runtime/structure/columns.dart';
import 'package:sally/src/runtime/structure/table_info.dart';
typedef Future<void> OnCreate(Migrator m);
typedef Future<void> OnUpgrade(Migrator m, int from, int to);
Future<void> _defaultOnCreate(Migrator m) => m.createAllTables();
Future<void> _defaultOnUpdate(Migrator m, int from, int to) async =>
throw Exception("You've bumped the schema version for your sally database "
"but didn't provide a strategy for schema updates. Please do that by "
'adapting the migrations getter in your database class.');
class MigrationStrategy {
final OnCreate onCreate;
final OnUpgrade onUpgrade;
MigrationStrategy({
this.onCreate = _defaultOnCreate,
this.onUpgrade = _defaultOnUpdate,
});
}
class Migrator {
Future<void> createAllTables() async {}
Future<void> createTable(TableInfo table) async {}
Future<void> deleteTable(String name) async {}
Future<void> addColumn(TableInfo table, GeneratedColumn column) async {}
Future<void> deleteColumn(TableInfo table, String columnName) async {}
Future<void> issueCustomQuery(String sql) async {}
}

View File

@ -6,7 +6,10 @@ import 'package:sally/src/runtime/expressions/variables.dart';
import 'package:sally/src/runtime/sql_types.dart';
abstract class GeneratedColumn<T, S extends SqlType<T>> extends Column<T, S> {
String get $name;
final String $name;
final bool $nullable;
GeneratedColumn(this.$name, this.$nullable);
@override
Expression<BoolType> equals(Expression<S> compare) =>
@ -23,10 +26,7 @@ abstract class GeneratedColumn<T, S extends SqlType<T>> extends Column<T, S> {
class GeneratedTextColumn extends GeneratedColumn<String, StringType>
implements TextColumn {
@override
final String $name;
GeneratedTextColumn(this.$name);
GeneratedTextColumn(String name, bool nullable) : super(name, nullable);
@override
Expression<BoolType> like(String regex) =>
@ -35,10 +35,7 @@ class GeneratedTextColumn extends GeneratedColumn<String, StringType>
class GeneratedBoolColumn extends GeneratedColumn<bool, BoolType>
implements BoolColumn {
@override
final String $name;
GeneratedBoolColumn(this.$name);
GeneratedBoolColumn(String name, bool nullable) : super(name, nullable);
@override
void writeInto(GenerationContext context) {
@ -50,10 +47,11 @@ class GeneratedBoolColumn extends GeneratedColumn<bool, BoolType>
class GeneratedIntColumn extends GeneratedColumn<int, IntType>
implements IntColumn {
@override
final String $name;
final bool hasAutoIncrement;
GeneratedIntColumn(this.$name);
GeneratedIntColumn(String name, bool nullable,
{this.hasAutoIncrement = false})
: super(name, nullable);
@override
Expression<BoolType> isBiggerThan(int i) =>

View File

@ -4,9 +4,13 @@ import 'package:sally/sally.dart';
abstract class TableInfo<TableDsl, DataClass> {
TableDsl get asDslTable;
/// The primary key of this table. Can be null if no custom primary key has
/// been specified
Set<Column> get $primaryKey => null;
/// The table name in the sql table
String get $tableName;
List<Column> get $columns;
DataClass map(Map<String, dynamic> data);
}
}

View File

@ -1,4 +1,5 @@
import 'package:sally/sally.dart';
import 'package:sally/src/runtime/migration.dart';
class Users extends Table {
IntColumn get id => integer().autoIncrement()();
@ -20,11 +21,13 @@ class GeneratedUsersTable extends Users with TableInfo<Users, UserDataObject> {
GeneratedUsersTable(this.db);
@override
IntColumn id = GeneratedIntColumn('id');
Set<Column> get $primaryKey => Set()..add(id);
@override
TextColumn name = GeneratedTextColumn('name');
IntColumn id = GeneratedIntColumn('id', false);
@override
BoolColumn isAwesome = GeneratedBoolColumn('is_awesome');
TextColumn name = GeneratedTextColumn('name', false);
@override
BoolColumn isAwesome = GeneratedBoolColumn('is_awesome', true);
@override
List<Column<dynamic, SqlType>> get $columns => [id, name, isAwesome];
@override
@ -42,4 +45,10 @@ class TestDatabase extends GeneratedDatabase {
: super(const SqlTypeSystem.withDefaults(), executor);
GeneratedUsersTable get users => GeneratedUsersTable(this);
@override
MigrationStrategy get migration => MigrationStrategy();
@override
int get schemaVersion => 1;
}