From b52906ad31f961b3f3aa8da51289be474df8fd91 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Thu, 7 Feb 2019 20:18:13 +0100 Subject: [PATCH] More info about columns add runtime, add migration api --- example/lib/example.dart | 5 +++ sally/lib/sally.dart | 1 + sally/lib/src/runtime/executor/executor.dart | 6 +++- sally/lib/src/runtime/migration.dart | 35 +++++++++++++++++++ sally/lib/src/runtime/structure/columns.dart | 22 ++++++------ .../lib/src/runtime/structure/table_info.dart | 6 +++- sally/test/generated_tables.dart | 15 ++++++-- 7 files changed, 73 insertions(+), 17 deletions(-) create mode 100644 sally/lib/src/runtime/migration.dart diff --git a/example/lib/example.dart b/example/lib/example.dart index d2ef7f52..a544d0a0 100644 --- a/example/lib/example.dart +++ b/example/lib/example.dart @@ -23,4 +23,9 @@ class ShopDb extends _$ShopDb { Future> allUsers() => select(users).get(); Future> userByName(String name) => (select(users)..where((u) => u.name.equalsVal(name))).get(); + @override + MigrationStrategy get migration => MigrationStrategy(); + @override + int get schemaVersion => 1; + } \ No newline at end of file diff --git a/sally/lib/sally.dart b/sally/lib/sally.dart index 0632ee70..26008486 100644 --- a/sally/lib/sally.dart +++ b/sally/lib/sally.dart @@ -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'; diff --git a/sally/lib/src/runtime/executor/executor.dart b/sally/lib/src/runtime/executor/executor.dart index f4a31f1c..b0925da3 100644 --- a/sally/lib/src/runtime/executor/executor.dart +++ b/sally/lib/src/runtime/executor/executor.dart @@ -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 select( @@ -23,7 +27,7 @@ abstract class QueryExecutor { Future ensureOpen(); Future>> runSelect( String statement, List args); - List runCreate(String statement, List args); + List runInsert(String statement, List args); Future runUpdate(String statement, List args); Future runDelete(String statement, List args); } diff --git a/sally/lib/src/runtime/migration.dart b/sally/lib/src/runtime/migration.dart new file mode 100644 index 00000000..7b3775dc --- /dev/null +++ b/sally/lib/src/runtime/migration.dart @@ -0,0 +1,35 @@ +import 'package:sally/src/runtime/structure/columns.dart'; +import 'package:sally/src/runtime/structure/table_info.dart'; + +typedef Future OnCreate(Migrator m); +typedef Future OnUpgrade(Migrator m, int from, int to); + +Future _defaultOnCreate(Migrator m) => m.createAllTables(); +Future _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 createAllTables() async {} + + Future createTable(TableInfo table) async {} + + Future deleteTable(String name) async {} + + Future addColumn(TableInfo table, GeneratedColumn column) async {} + + Future deleteColumn(TableInfo table, String columnName) async {} + + Future issueCustomQuery(String sql) async {} +} diff --git a/sally/lib/src/runtime/structure/columns.dart b/sally/lib/src/runtime/structure/columns.dart index aa20e223..a2915c7a 100644 --- a/sally/lib/src/runtime/structure/columns.dart +++ b/sally/lib/src/runtime/structure/columns.dart @@ -6,7 +6,10 @@ import 'package:sally/src/runtime/expressions/variables.dart'; import 'package:sally/src/runtime/sql_types.dart'; abstract class GeneratedColumn> extends Column { - String get $name; + final String $name; + final bool $nullable; + + GeneratedColumn(this.$name, this.$nullable); @override Expression equals(Expression compare) => @@ -23,10 +26,7 @@ abstract class GeneratedColumn> extends Column { class GeneratedTextColumn extends GeneratedColumn implements TextColumn { - @override - final String $name; - - GeneratedTextColumn(this.$name); + GeneratedTextColumn(String name, bool nullable) : super(name, nullable); @override Expression like(String regex) => @@ -35,10 +35,7 @@ class GeneratedTextColumn extends GeneratedColumn class GeneratedBoolColumn extends GeneratedColumn 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 class GeneratedIntColumn extends GeneratedColumn 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 isBiggerThan(int i) => diff --git a/sally/lib/src/runtime/structure/table_info.dart b/sally/lib/src/runtime/structure/table_info.dart index 1086a85b..82014a6d 100644 --- a/sally/lib/src/runtime/structure/table_info.dart +++ b/sally/lib/src/runtime/structure/table_info.dart @@ -4,9 +4,13 @@ import 'package:sally/sally.dart'; abstract class TableInfo { TableDsl get asDslTable; + /// The primary key of this table. Can be null if no custom primary key has + /// been specified + Set get $primaryKey => null; + /// The table name in the sql table String get $tableName; List get $columns; DataClass map(Map data); -} +} \ No newline at end of file diff --git a/sally/test/generated_tables.dart b/sally/test/generated_tables.dart index 3e510e4c..ae5beee0 100644 --- a/sally/test/generated_tables.dart +++ b/sally/test/generated_tables.dart @@ -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 { GeneratedUsersTable(this.db); @override - IntColumn id = GeneratedIntColumn('id'); + Set 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> 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; }