-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from schultek/develop
version 0.8.0
- Loading branch information
Showing
45 changed files
with
2,008 additions
and
404 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
.dart_tool/ | ||
.packages | ||
build/ | ||
.vscode/ | ||
|
||
pubspec.lock | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,133 +1,21 @@ | ||
import 'dart:convert'; | ||
import 'dart:io'; | ||
import 'dart:isolate'; | ||
|
||
import 'package:collection/collection.dart'; | ||
import 'package:stormberry/stormberry.dart'; | ||
import 'package:yaml/yaml.dart'; | ||
|
||
import 'src/differentiator.dart'; | ||
import 'src/patcher.dart'; | ||
import 'src/schema.dart'; | ||
import 'package:args/command_runner.dart'; | ||
import 'package:stormberry/src/cli/runner.dart'; | ||
|
||
Future<void> main(List<String> args) async { | ||
bool dryRun = args.contains('--dry-run'); | ||
String? dbName = args.where((a) => a.startsWith('-db=')).map((a) => a.split('=')[1]).firstOrNull; | ||
bool applyChanges = args.contains('--apply-changes'); | ||
|
||
var pubspecYaml = File('pubspec.yaml'); | ||
|
||
if (!pubspecYaml.existsSync()) { | ||
print('Cannot find pubspec.yaml file in current directory.'); | ||
var runner = CommandRunner<void>( | ||
'stormberry', | ||
'Tool for migrating your database to the schema generated from your models.', | ||
)..addCommand(MigrateCommand()); | ||
|
||
try { | ||
await runner.run(args); | ||
exit(0); | ||
} on UsageException catch (e) { | ||
print('${e.message}\n${e.usage}'); | ||
exit(1); | ||
} | ||
|
||
var packageName = loadYaml(await pubspecYaml.readAsString())['name'] as String; | ||
|
||
var buildYaml = File('build.yaml'); | ||
|
||
if (!buildYaml.existsSync()) { | ||
print('Cannot find build.yaml file in current directory.'); | ||
} catch (e, st) { | ||
print('$e\n$st'); | ||
exit(1); | ||
} | ||
|
||
var content = loadYaml(await buildYaml.readAsString()); | ||
|
||
List<String>? generateTargets = (content['targets'] as YamlMap?) | ||
?.values | ||
.map((t) => t['builders']?['stormberry']) | ||
.where((b) => b != null) | ||
.expand((b) => b['generate_for'] as List) | ||
.map((d) => d as String) | ||
.toList(); | ||
|
||
if (generateTargets == null || generateTargets.isEmpty) { | ||
print('Cannot find stormberry generation targets in build.yaml. ' | ||
'Make sure you have the stormberry builder configured with at least one generation target.'); | ||
exit(1); | ||
} | ||
|
||
var schema = DatabaseSchema.empty(); | ||
|
||
for (var target in generateTargets) { | ||
var schemaPath = target.replaceFirst('.dart', '.runner.g.dart'); | ||
var file = File('.dart_tool/build/generated/$packageName/$schemaPath'); | ||
if (!file.existsSync()) { | ||
print('Could not run migration for target $target. Did you run the build script?'); | ||
exit(1); | ||
} | ||
|
||
var port = ReceivePort(); | ||
await Isolate.spawnUri( | ||
file.absolute.uri, | ||
[], | ||
port.sendPort, | ||
packageConfig: Uri.parse('.packages'), | ||
); | ||
|
||
var schemaMap = jsonDecode(await port.first as String); | ||
var targetSchema = DatabaseSchema.fromMap(schemaMap as Map<String, dynamic>); | ||
|
||
schema = schema.mergeWith(targetSchema); | ||
} | ||
|
||
if (dbName == null && Platform.environment['DB_NAME'] == null) { | ||
stdout.write('Select a database to update: '); | ||
dbName = stdin.readLineSync(encoding: Encoding.getByName('utf-8')!); | ||
} | ||
|
||
var db = Database(database: dbName); | ||
|
||
await db.open(); | ||
|
||
print('Getting schema changes of ${db.name}'); | ||
print('========================='); | ||
|
||
var diff = await getSchemaDiff(db, schema); | ||
|
||
if (diff.hasChanges) { | ||
printDiff(diff); | ||
print('========================='); | ||
|
||
if (dryRun) { | ||
print('DATABASE SCHEME HAS CHANGES, EXITING'); | ||
await db.close(); | ||
exit(1); | ||
} else { | ||
await db.startTransaction(); | ||
|
||
String? answerApplyChanges; | ||
if (!applyChanges) { | ||
stdout.write('Do you want to apply these changes? (yes/no): '); | ||
answerApplyChanges = stdin.readLineSync(encoding: Encoding.getByName('utf-8')!); | ||
} | ||
|
||
if (applyChanges || answerApplyChanges == 'yes') { | ||
print('Database schema changed, applying updates now:'); | ||
|
||
try { | ||
db.debugPrint = true; | ||
await patchSchema(db, diff); | ||
} catch (_) { | ||
db.cancelTransaction(); | ||
} | ||
} else { | ||
db.cancelTransaction(); | ||
} | ||
|
||
var updateWasSuccessFull = await db.finishTransaction(); | ||
|
||
print('========================'); | ||
if (updateWasSuccessFull) { | ||
print('---\nDATABASE UPDATE SUCCESSFUL'); | ||
} else { | ||
print('---\nALL CHANGES REVERTED, EXITING'); | ||
} | ||
|
||
await db.close(); | ||
exit(updateWasSuccessFull ? 0 : 1); | ||
} | ||
} else { | ||
print('NO CHANGES, ALL DONE'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
CREATE TABLE IF NOT EXISTS "accounts" ( | ||
"id" serial NOT NULL, | ||
"first_name" text NOT NULL, | ||
"last_name" text NOT NULL, | ||
"location" point NOT NULL, | ||
"data" serial NOT NULL, | ||
"company_id" text NULL | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS "billing_addresses" ( | ||
"account_id" int8 NULL, | ||
"company_id" text NULL, | ||
"city" text NOT NULL, | ||
"postcode" text NOT NULL, | ||
"name" text NOT NULL, | ||
"street" text NOT NULL | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS "companies" ( | ||
"id" text NOT NULL, | ||
"name" text NOT NULL | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS "invoices" ( | ||
"account_id" int8 NULL, | ||
"company_id" text NULL, | ||
"id" text NOT NULL, | ||
"title" text NOT NULL, | ||
"invoice_id" text NOT NULL | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS "parties" ( | ||
"sponsor_id" text NULL, | ||
"id" text NOT NULL, | ||
"name" text NOT NULL, | ||
"date" int8 NOT NULL | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS "accounts_parties" ( | ||
"account_id" int8 NOT NULL, | ||
"party_id" text NOT NULL | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
ALTER TABLE "accounts" | ||
ADD PRIMARY KEY ( "id" ); | ||
|
||
ALTER TABLE "billing_addresses" | ||
ADD UNIQUE ( "account_id" ); | ||
|
||
ALTER TABLE "companies" | ||
ADD PRIMARY KEY ( "id" ); | ||
|
||
ALTER TABLE "invoices" | ||
ADD PRIMARY KEY ( "id" ); | ||
|
||
ALTER TABLE "parties" | ||
ADD PRIMARY KEY ( "id" ); | ||
|
||
ALTER TABLE "accounts_parties" | ||
ADD PRIMARY KEY ( "account_id", "party_id" ); | ||
|
||
ALTER TABLE "accounts" | ||
ADD FOREIGN KEY ( "company_id" ) REFERENCES companies ( "id" ) ON DELETE SET NULL ON UPDATE CASCADE; | ||
|
||
ALTER TABLE "billing_addresses" | ||
ADD FOREIGN KEY ( "account_id" ) REFERENCES accounts ( "id" ) ON DELETE CASCADE ON UPDATE CASCADE, | ||
ADD FOREIGN KEY ( "company_id" ) REFERENCES companies ( "id" ) ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
||
ALTER TABLE "invoices" | ||
ADD FOREIGN KEY ( "account_id" ) REFERENCES accounts ( "id" ) ON DELETE SET NULL ON UPDATE CASCADE, | ||
ADD FOREIGN KEY ( "company_id" ) REFERENCES companies ( "id" ) ON DELETE SET NULL ON UPDATE CASCADE; | ||
|
||
ALTER TABLE "parties" | ||
ADD FOREIGN KEY ( "sponsor_id" ) REFERENCES companies ( "id" ) ON DELETE SET NULL ON UPDATE CASCADE; | ||
|
||
ALTER TABLE "accounts_parties" | ||
ADD FOREIGN KEY ( "account_id" ) REFERENCES accounts ( "id" ) ON DELETE CASCADE ON UPDATE CASCADE, | ||
ADD FOREIGN KEY ( "party_id" ) REFERENCES parties ( "id" ) ON DELETE CASCADE ON UPDATE CASCADE; |
Oops, something went wrong.